The working texture of the shader field does not work.

I got this shader from somewhere that I forgot, by default only solid color is used for the hit effect. so I'm trying to change it so that it also supports the screen texture.

Shader "TFTM/FX/Shield2" { Properties { _Position ("Collision", Vector) = (-1, -1, -1, -1) _MaxDistance ("Effect Size", float) = 40 _ShieldColor ("Color (RGBA)", Color) = (0.7, 1, 1, 0) _EmissionColor ("Emission color (RGBA)", Color) = (0.7, 1, 1, 0.01) _EffectTime ("Effect Time (ms)", float) = 0 _MainTex ("Texture (RGB)", 2D) = "white" {} } SubShader { Tags { "Queue" = "Transparent" "RenderType" = "Transparent" } LOD 2000 Cull Off CGPROGRAM #pragma surface surf Lambert vertex:vert alpha #pragma target 3.0 struct Input { float customDist; float2 uv_MainTex; }; sampler2D _MainTex; float4 _Position; float _MaxDistance; float4 _ShieldColor; float4 _EmissionColor; float _EffectTime; float _Amount; void vert (inout appdata_full v, out Input o) { o.customDist = distance(_Position.xyz, v.vertex.xyz); o.uv_MainTex = v.texcoord; } void surf (Input IN, inout SurfaceOutput o) { o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;//_ShieldColor.rgb; o.Emission = _EmissionColor; if(_EffectTime > 0) { if(IN.customDist < _MaxDistance){ o.Alpha = _EffectTime/500 - (IN.customDist / _MaxDistance) + _ShieldColor.a; if(o.Alpha < _ShieldColor.a){ o.Alpha = _ShieldColor.a; } } else { o.Alpha = _ShieldColor.a; } } else{ o.Alpha = o.Alpha = _ShieldColor.a; } } ENDCG } Fallback "Transparent/Diffuse" } 

I replace the color with the texture here

o.Albedo = tex2D (_MainTex, IN.uv_MainTex) .rgb; // _ ShieldColor.rgb;

but it doesn’t work, it looks like one color instead of the texture around it.

enter image description here enter image description here

+5
source share
1 answer

The problem is o.Aplha . In a published shader, surf returns Alpha regardless of the texture, so you need to change the surf method to set the Alpha output based on the texture.

This can be done as follows:

1) Replace

 o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;//_ShieldColor.rgb; 

with

 // Read texture and set it to vector4 half4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c.rgb; 

2) Insert

 o.Alpha *= ca; 

after

 else { o.Alpha = o.Alpha = _ShieldColor.a; } 

The result looks like this:

enter image description here

Full shader result code:

 Shader "TFTM/FX/Shield2" { Properties { _Position("Collision", Vector) = (-1, -1, -1, -1) _MaxDistance("Effect Size", float) = 40 _ShieldColor("Color (RGBA)", Color) = (0.7, 1, 1, 0) _EmissionColor("Emission color (RGBA)", Color) = (0.7, 1, 1, 0.01) _EffectTime("Effect Time (ms)", float) = 0 _MainTex("Texture (RGB)", 2D) = "white" {} } SubShader { Tags { "Queue" = "Transparent" "RenderType" = "Transparent" } LOD 2000 Cull Off CGPROGRAM #pragma surface surf Lambert vertex:vert alpha #pragma target 3.0 struct Input { float customDist; float2 uv_MainTex; }; sampler2D _MainTex; float4 _Position; float _MaxDistance; float4 _ShieldColor; float4 _EmissionColor; float _EffectTime; float _Amount; void vert(inout appdata_full v, out Input o) { o.customDist = distance(_Position.xyz, v.vertex.xyz); o.uv_MainTex = v.texcoord; } void surf(Input IN, inout SurfaceOutput o) { half4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c.rgb;//_ShieldColor.rgb; o.Emission = _EmissionColor; if (_EffectTime > 0) { if (IN.customDist < _MaxDistance) { o.Alpha = _EffectTime / 500 - (IN.customDist / _MaxDistance) + _ShieldColor.a; if (o.Alpha < _ShieldColor.a) { o.Alpha = _ShieldColor.a; } } else { o.Alpha = _ShieldColor.a; } } else { o.Alpha = o.Alpha = _ShieldColor.a; } o.Alpha *= ca; } ENDCG } Fallback "Transparent/Diffuse" } 
+3
source

Source: https://habr.com/ru/post/1262259/


All Articles