diff options
Diffstat (limited to 'Scripts/GrassGridBlit.shader')
| -rw-r--r-- | Scripts/GrassGridBlit.shader | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/Scripts/GrassGridBlit.shader b/Scripts/GrassGridBlit.shader new file mode 100644 index 0000000..f4dc872 --- /dev/null +++ b/Scripts/GrassGridBlit.shader @@ -0,0 +1,77 @@ +Shader "yum_food/GrassGridBlit" +{ + Properties + { + } + SubShader + { + Tags { "RenderType"="Opaque" } + LOD 100 + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + #include "UnityCG.cginc" + + struct appdata + { + float4 vertex : POSITION; + float2 uv : TEXCOORD0; + }; + + struct v2f + { + float2 uv : TEXCOORD0; + float4 vertex : SV_POSITION; + }; + + // Grid parameters + float3 _PlayerGridPos; // Player's grid cell position (x, y, z) + int3 _GridCount; // Number of instances along each axis + int3 _GridHalf; // Half dimensions for centering around player + int2 _TexDimensions; // Texture width and height + + v2f vert (appdata v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.uv = v.uv; + return o; + } + + float4 frag (v2f i) : SV_Target + { + // Convert UV to pixel coordinates + int2 pixelCoord = int2(i.uv.x * _TexDimensions.x, i.uv.y * _TexDimensions.y); + + // Convert pixel coordinate to instance index + int instanceID = pixelCoord.x + pixelCoord.y * _TexDimensions.x; + + // Calculate total valid instances + int totalInstances = _GridCount.x * _GridCount.y * _GridCount.z; + + // If this pixel is beyond valid instances, output invalid marker + if (instanceID >= totalInstances) { + return float4(0, 0, 0, -1); // Alpha = -1 marks invalid + } + + // Convert instance index to local grid coordinates (0 to GridCount-1) + int localX = instanceID % _GridCount.x; + int localY = (instanceID / _GridCount.x) % _GridCount.y; + int localZ = instanceID / (_GridCount.x * _GridCount.y); + + // Convert to world grid coordinates centered around player + int worldX = _PlayerGridPos.x - _GridHalf.x + localX; + int worldY = _PlayerGridPos.y - _GridHalf.y + localY; + int worldZ = _PlayerGridPos.z - _GridHalf.z + localZ; + + // Store world grid coordinates in RGB channels, alpha = 0 marks valid + return float4(worldX, worldY, worldZ, 0); + } + ENDCG + } + } +} |
