Bizio
Bizio

Reputation: 45

How to edit a shader to make it show sprites it's on without any lights?

I found some shader on the Internet that makes radial blur effect for spinning objects. It works and looks pretty cool but when I apply it to a sprite, but it works like Sprite/Diffuse shader: it needs a light source to be seen, otherwise it's black.

How can I edit this shader to make it work like normal Sprite/Default shader that doesn't need a light?

Here's the code:

  Shader "Custom/SpinBlur"{
  Properties{
      _Color("Main Color", Color) = (1,1,1,1)
      _Samples("Samples", Range(0,360)) = 100
      _Angle("Angle", Range(0,360)) = 10
      _MainTex("Color (RGB) Alpha (A)", 2D) = "white"
  }
  SubShader{
  Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }

  LOD 200
  Cull Off

  CGPROGRAM
  #pragma target 3.0
  #pragma surface surf Lambert alpha

  sampler2D _MainTex;
  int _Angle;
  int _Samples;
  float4 _Color;

  struct Input {
      float2 uv_MainTex;
      float4 screenPos;
  };

  float2 rotateUV(float2 uv, float degrees) {
      const float Deg2Rad = (UNITY_PI * 2.0) / 360.0;
      float rotationRadians = degrees * Deg2Rad;
      float s = sin(rotationRadians);
      float c = cos(rotationRadians);
      float2x2 rotationMatrix = float2x2(c, -s, s, c);
      uv -= 0.5;
      uv = mul(rotationMatrix, uv);
      uv += 0.5;
      return uv;
  }

  void surf(Input IN, inout SurfaceOutput o) {
      const float Deg2Rad = (UNITY_PI * 2.0) / 360.0;
      const float Rad2Deg = 180.0 / UNITY_PI;
      float2 vUv = IN.uv_MainTex;
      float2 coord = vUv;
      float4 FragColor = float4(0.0, 0.0, 0.0, 0.0);
      int samp = _Samples;
      if (samp <= 0) samp = 1;
      for (float i = 0; i < samp; i++) {
          float a = (float)_Angle / (float)samp;
          coord = rotateUV(coord, a);
          float4 texel = tex2D(_MainTex, coord);
          texel *= 1.0 / samp;
          FragColor += texel;
      }
      float4 c = FragColor*_Color;
      o.Albedo = c.rgb;
      o.Alpha = c.a;
  }
  ENDCG
  }
      FallBack "Diffuse"
  }

Upvotes: 1

Views: 293

Answers (1)

shingo
shingo

Reputation: 27021

Write your custom lighting model

half4 Lighting<Name> (SurfaceOutput s, UnityGI gi)
{
    return half4(s.Albedo, s.Alpha);
}

void Lighting<Name>_GI (SurfaceOutput s, UnityGIInput data, inout UnityGI gi)
{
}

and change Lambert to <Name>.

Upvotes: 2

Related Questions