The main texture of my surface shader is a Google Maps image tile similar to this:
.
I want to replace pixels close to the specified color with a separate texture. Now the following works:
Shader "MyShader" { Properties { _MainTex("Base (RGB) Trans (A)", 2D) = "white" {} _GrassTexture("Grass Texture", 2D) = "white" {} _RoadTexture("Road Texture", 2D) = "white" {} _WaterTexture("Water Texture", 2D) = "white" {} } SubShader { Tags{ "Queue" = "Transparent-1" "IgnoreProjector" = "True" "ForceNoShadowCasting" = "True" "RenderType" = "Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert alpha approxview halfasview noforwardadd nometa uniform sampler2D _MainTex; uniform sampler2D _GrassTexture; uniform sampler2D _RoadTexture; uniform sampler2D _WaterTexture; struct Input { float2 uv_MainTex; }; void surf(Input IN, inout SurfaceOutput o) { fixed4 ct = tex2D(_MainTex, IN.uv_MainTex);
This is the first shader I've ever written, and it's probably not very efficient. It seems contraindicated for me to call tex2D for each texture for each pixel, just to throw away this data, but I could not figure out how best to do it if / else (which I read poorly for GPUs).
This is a Unity Surface Shader, not a fragment / vertex shader. I know that there is a step that happens behind the scenes that will generate the fragment / vertex shader for me (adding scene lighting, fog, etc.). This shader applies to 100 256x256px cards (total 2560x2560 pixels). Grass / road / water textures also have 256x256 pixels.
My question is: is there a more efficient way to accomplish what I'm doing here? The game works on Android and iOS.
source share