Search Unity

Instance of Texture

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

  1. elpie89

    elpie89

    Joined:
    Jun 30, 2014
    Posts:
    32
    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:
    12,352
    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.
     
    FM-Productions and fra3point like this.
  3. seandanger

    seandanger

    Joined:
    Jun 13, 2012
    Posts:
    39
    Not to necro an old thread, but I'm working on the same issue and I'm hesitant to use Texture2DArray since the manual states they require OpenGL ES 3.0 or above, and I'd like to target some 2.0 devices. I realized that another possible solution is to arrange my individual textures onto a single texture myself, and then use an instanced index property to offset where in the texture the sampler pulls from. I will post back if that option doesn't work for some reason.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Yep, that's the traditional atlas based fallback.