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
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.
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.