Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Instance of Texture

Discussion in 'Shaders' started by elpie89, Oct 16, 2017.

  1. elpie89

    elpie89

    Joined:
    Jun 30, 2014
    Posts:
    27
    I'm trying to use the unity instanciation, working with fixed4 type no problem
    but I cant find the way to access a sampler
    I define a sampler
    UNITY_DEFINE_INSTANCED_PROP(sampler2d, _MainTex)

    and then I access it with

    UNITY_ACCESS_INSTANCED_PROP(_MainTex)

    but I have this error
    sampler array index must be a literal expression at line ..

    I can't understand what is the problem.
    Can someone help me

    Thanks
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    6,970
    Because you can't set a sampler as an instanced property, you can only have numerical values be instanced properties.

    The way instancing properties works is behind the scenes that property is made into an array of values. When rendering an instance it has an instance ID, a number, that it can use to access a specific entry from that array.

    The problem is you can't really do an array of sampler2d in shaders. I mean you can, but they don't actually exist in the compiled shader. At compile time _MyTexture[0] becomes _MyTexture0, the index of the "array" needs to be known at compile time. Since the instance isn't known until it's being rendered, it can't be known at compile time.

    The solution is to use a Texture2DArray instead, but that asset has to be managed manually and Unity's instancing is done behind the closed doors of c++. You would have to build and assign your Texture2DArray in script (either at runtime or preferably in editor) and assign an index for the Texture2DArray on the material as an instanced property.
     
    fra3point likes this.