2017-01-04 5 views
5

Ich habe diesen Shader von irgendwo, was ich vergessen habe, ist die Standardeinstellung nur Solid-Farbe für den Effekt wird getroffen. also versuche ich es so zu modifizieren, dass es auch die Schildtextur unterstützt.Force Feld Shader unwrap Textur funktioniert nicht

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 ersetzen Farbe mit Textur hier

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

aber es funktioniert nicht, es erscheint als eine einzige Farbe statt Textur um ihn herum.

enter image description here enter image description here

+0

Haben Sie sichergestellt, dass die Kugel richtig uv-Koordinaten hat? Sie können sie zum Beispiel mit 'o.Albedo = float3 (IN.uv_MainTex.rg, 0.0)' betrachten. – Gnietschow

Antwort

3

Das Problem ist mit o.Aplha. In dem Shader, den Sie veröffentlicht haben, gibt surf Alpha unabhängig von der Textur zurück. Sie müssen also die surf Methode ändern, um die Ausgabe Alpha basierend auf der Textur festzulegen.

Es kann auf diese Weise geschehen:

1)

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

mit

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

2) Legen Sie

o.Alpha *= c.a; 
ersetzen 0

nach

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

Ergebnis sieht smth wie folgt aus:

enter image description here

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

    ENDCG 
    } 
    Fallback "Transparent/Diffuse" 
}