I may not be explaining this correctly so let me try this.
I have a texture atlas that is 1024 * 1024, there are 64 tiles in a 128*128 tile grid. I’m trying to figure out how to get my Shader’s instanceID to move the UV’s of that mesh instance to a title.
instanceID 1 = Tile 1
instanceID 2 = Tile 2
…
instanceID 64 = Tile 64
instanceID 10456 = (whatever that happens to wrap around to)
No idea. I certainly don’t see anything particularly obviously wrong.
This shader works for me just having multiple quads in the scene with this material, but I’m not implementing a lot of the stuff you have in yours since I’m not drawing them from script.
I tried using your sample almost verbatim, although I think we’re using different versions of unity so I have instanceID coming as a parameter rather than needing to ask for it directly. But still, I’m not getting the correct results. Things are still stretching weirdly.
I’m using Unity 2017.3 I’m using this with DrawMeshIndirect() which is how the instanceID is being passed in. It’s just a slightly modified version of the ‘Custom Shader’ shown on the bottom of the documentation page here: Unity - Scripting API: Graphics.DrawMeshInstancedIndirect
Still, even if I force-ably set the index to 2 (rather than letting it use the instanceID) I’m getting weird stretching. Maybe I’m not setting up my scaling correctly?
Using UNITY_GET_INSTANCE_ID is basically just SV_instanceID on the PC, but will work on consoles as well. Looking at the code I think my shader should actually just use unity_InstanceID instead of UNITY_GET_INSTANCE_ID. But as long as you’re not running on a console or using VR it shouldn’t be substantively different from my shader.
With out knowing more about your own shader, mesh, and texture you’re using I couldn’t tell you why it’s not working properly for you.
These are console games so that has to be taken into account, but still, I’ve found very few shaders that needed to be ‘console-ified’ to work. The logic on your end seems sound but I’m clearly doing something not right.
Firefox doesn’t play well with OneDrive so you might need Chrome or IE
The math seems correct, the UV of the plane should offset 1 title per instanceID with a scale of 0.125 (128/1024) but as you can see, it’s stretching strangely rather than showing the full sold colour title.
The UV layout of the plane is correct because if you set the values in the shader properties to 8,8,1,1 you see the first full tile (sold white, black outline) which is how the UV was set up in the modeller. (blender)
Not entirely sure why your version is showing the issues it does. If I use the built in quad mesh instead of the plane in your example everything works as I would expect. My own shader works on any mesh I apply it to.
Yes, that is absolutely the issue. In that case you don’t want to scale the UVs in the shader, only offset them. In my example shader that would be done by removing the parentheses on line 58.
o.uv = i.uv + uvOffset * _Tiles.zw;