Hey guys, I'm new to writing shaders and finding it a bit difficult to get in. I've read through the Unity tutorials but I'm struggling with adding transparency to my shader. so I'm making a "thermal" shader for a 2d game. The conversion from full colour range to blue - yellow works okay, but i cant for the life of me get it to work properly with transparent areas. I've tried changing thermal.a but those alpha values seem to not be accurate(as if every pixel shares the same alpha value)... Code (csharp): Shader "Custom/ThermalVision" { Properties { _MainTex ("Base (RGB) Trans (A)", 2D) = "" {} _vxOffset ("Offset", Range(0,1)) = 1 } SubShader { Blend SrcAlpha OneMinusSrcAlpha Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} // Pass{ // Color(.389, .1465, .4645, 0.5) // SetTexture[_MainTex] {Combine one - texture * primary alpha} // SetTexture[_] {Combine previous Dot3 primary} // SetTexture[_MainTex] {Combine previous, texture alpha} // } Pass{ CGPROGRAM #pragma vertex vert_img #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" #pragma target 3.0 #pragma only_renderers d3d9 struct v2f { float4 pos:SV_POSITION; float4 uv:TEXCOORD0; }; float3 texCol; float _vxOffset; v2f vert(appdata_base input) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, input.vertex); o.uv = input.texcoord; return o; } uniform sampler2D _MainTex; float4 thermal; float4 frag (v2f_img i) : COLOR { float4 pixcol = tex2D(_MainTex, i.uv); float4 colors[3]; colors[0] = float4(0.0,0.0,1.0,1.0); colors[1] = float4(1.0,1.0,0.0,1.0); colors[2] = float4(1.0,0.0,0.0,1.0); float lum = (pixcol.r+pixcol.g+pixcol.b)/3.; int ix = (lum < 0.5)? 0:1; while( thermal.x < 0.5 ) { thermal += lerp(colors[0], colors[1], (lum-float(ix)*0.5)/0.5); //thermal = pixcol * i.color; } return thermal; } ENDCG } } FallBack "VertexLit" } Thanks for reading this far
Your `colors` variables have a 1.0 in the last parameter, setting alpha to 1.0 for all pixels, and then later you add your `thermal` stuff together but it's simply going to bring in those 1.0's, instead of the alpha from the texture? You need to extract alpha from pixcol.a somewhere e.g. return float4(thermal.rgb,pixcol.a);