2017-01-04 21 views
5

Tôi đã nhận shader này từ một nơi nào đó mà tôi quên, mặc định là chỉ sử dụng màu sắc rắn cho hiệu quả được nhấn. vì vậy tôi cố gắng sửa đổi nó để làm cho nó hỗ trợ kết cấu lá chắn là tốt.Lực lượng trường shader kết cấu unwrap không làm việc

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" 
} 

tôi thay thế màu sắc với kết cấu ở đây

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

nhưng nó không hoạt động, nó xuất hiện dưới dạng một màu duy nhất thay vì kết cấu xung quanh.

enter image description here enter image description here

+0

Bạn có đảm bảo, rằng lĩnh vực có đúng uv-tọa độ? Bạn có thể xem chúng ví dụ với 'o.Albedo = float3 (IN.uv_MainTex.rg, 0.0)'. – Gnietschow

Trả lời

3

Vấn đề là với o.Aplha. Trong bóng đổ bạn đã xuất bản, surf trả về Alpha bất kể kết cấu, vì vậy bạn cần thay đổi phương thức surf để đặt đầu ra Alpha dựa trên kết cấu.

Nó có thể được thực hiện theo cách này:

1) Thay thế

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

với

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

2) Chèn

o.Alpha *= c.a; 

sau

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

quả trông smth như thế này:

enter image description here

Toàn bộ mã kết quả shader:

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 *= c.a; 
    } 

    ENDCG 
    } 
    Fallback "Transparent/Diffuse" 
} 
Các vấn đề liên quan