Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trying to mix two "slices" from a sampler2Darray but I keep getting errors

Discussion in 'Shaders' started by BTCallahan, Feb 19, 2017.

  1. BTCallahan

    BTCallahan

    Joined:
    Jun 6, 2015
    Posts:
    71
    I've been trying to use texture arrays as an alternative to having diffuse textureA, diffuse textureB, diffuse textureC, ect, and mixing them but I haven't had much luck. I've tried to follow the example in the documentation, however 've had to innovate in a few area's, given how sparse it is. What I'm aiming for is to take an array with four slices, and then blend them together based on a vector that I pass in.

    In the properties I have:
    Code (CSharp):
    1. _AO_Array ("AO Array", 2DArray) = "" {}
    2.  _BlendingValues ("Blending Values", vector) = (0.3333333, 0.3333333, 0.3333333, 0)
    Then in the shader block:
    Code (CSharp):
    1. UNITY_DECLARE_TEX2DARRAY(_AO_Array);
    2. half4 _BlendingValues;
    3.  
    4. fixed4 mapArray(float2 uvs, sampler2Darray mapArray, half4 blendValues){
    5.        return lerp(UNITY_SAMPLE_TEX2DARRAY(mapArray, float3(uvs, 0.0)), lerp(lerp(UNITY_SAMPLE_TEX2DARRAY(mapArray, float3(uvs, 0.333)), blendValues.x), UNITY_SAMPLE_TEX2DARRAY(mapArray, float3(uvs, 0.667)), blendValues.y), blendValues.z);
    6. }
    And int the surf function:
    Code (CSharp):
    1. fixed occ = mapArray(IN.uv_MainTex, _AO_Array, _BlendingValues).r;
    The problem is that I get an error telling me that: Unexpected identifier "sampler2Darray". Expected one of: in inout out uniform sampler sampler1D sampler2D sampler3D samplerCUBE sampler_state SamplerState SamplerComparisonState bool int or a user-defined type at line 85
    This refers to the declaration of the mapArray function. I've tried using sampler2D[] instead of sampler2Darray, but that gust gives me:
    Unexpected token '['. Expected: identifier at line 85

    So I'm pretty much at a loss.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    If you haven't already, download the built in shader source. Then search for UNITY_DECLARE_TEX2DARRAY to see how it's defined. You'll see it means many different things depending on what platform / API is being compiled for, and for some it doesn't use a sampler2Darray, in fact for those platforms they don't even exist, hence why it's not always being used.

    What this means for you is you can't just use sampler2Darray in your function definition. Either you need to use a macro that changes what's passed depending on the API, or you can force the shader to only compile for the platform(s) you need that do use sampler2Darray.

    See only_renderers exclude_renderers and target
    https://docs.unity3d.com/Manual/SL-ShaderPrograms.html
    https://docs.unity3d.com/Manual/SL-ShaderCompileTargets.html
     
  3. BTCallahan

    BTCallahan

    Joined:
    Jun 6, 2015
    Posts:
    71
    Ok, I downloaded the sources for 5.5.1 and I decided to replace the UNITY_DECLARE_TEX2DARRAY with UNITY_DECLARE_TEX2DARRAY_NOSAMPLER and new my mapArray function new reads as:

    Code (CSharp):
    1.         fixed4 mapArray(float2 uvs, sampler2D mapArray, half4 blendValues){//newlines made it much easier to read
    2.  
    3.             return lerp(UNITY_SAMPLE_TEX2DARRAY(sampler##mapArray, float3(uvs, 0.0)),
    4.             lerp(lerp(UNITY_SAMPLE_TEX2DARRAY(sampler##mapArray, float3(uvs, 0.333)),
    5.             UNITY_SAMPLE_TEX2DARRAY(sampler##mapArray, float3(uvs, 0.667)) blendValues.x),
    6.             UNITY_SAMPLE_TEX2DARRAY(sampler##mapArray, float3(uvs, 1.0)), blendValues.y), blendValues.z);
    7.         }
    The shader seems to work now, thank you.