Advertisement
Galloman

Unity - Sprite Lamp + Spine shader material

Feb 15th, 2014
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Spine/BumpedSkeleton" {
  2.     Properties
  3.     {
  4.         _MainTex ("Diffuse Texture", 2D) = "white" {}
  5.         _Normal ("Normal", 2D) = "bump" {}
  6.     }
  7.  
  8.     SubShader
  9.     {
  10.         AlphaTest Greater 0.9
  11.         Blend One OneMinusSrcAlpha
  12.  
  13.         Pass
  14.         {
  15.             Tags { "LightMode" = "ForwardBase" }
  16.            
  17.             CGPROGRAM
  18.  
  19.             #pragma vertex vert  
  20.             #pragma fragment frag
  21.  
  22.             #include "UnityCG.cginc"
  23.  
  24.             // User-specified properties
  25.             uniform sampler2D _MainTex;
  26.  
  27.             struct VertexInput
  28.             {
  29.                 float4 vertex : POSITION;
  30.                 float4 color : COLOR;
  31.                 float4 uv : TEXCOORD0;    
  32.             };
  33.  
  34.             struct VertexOutput
  35.             {
  36.                 float4 pos : POSITION;
  37.                 float4 color : COLOR;
  38.                 float2 uv : TEXCOORD0;
  39.             };
  40.  
  41.             VertexOutput vert(VertexInput input)
  42.             {
  43.                 VertexOutput output;
  44.  
  45.                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
  46.                 output.color = input.color;
  47.                 output.uv = float2(input.uv);
  48.                 return output;
  49.             }
  50.  
  51.             float4 frag(VertexOutput input) : COLOR
  52.             {
  53.                 float4 diffuseColor = tex2D(_MainTex, input.uv);
  54.  
  55.                 float3 ambientLighting = float3(UNITY_LIGHTMODEL_AMBIENT) * float3(diffuseColor) * float3(input.color);
  56.                 return float4(ambientLighting, diffuseColor.a);
  57.             }
  58.  
  59.             ENDCG
  60.         }
  61.  
  62.         Pass {
  63.             Tags { "LightMode"="ForwardAdd" }
  64.             Blend One OneMinusSrcAlpha
  65.  
  66.             CGPROGRAM
  67.  
  68.             #pragma vertex vert  
  69.             #pragma fragment frag
  70.  
  71.             #include "UnityCG.cginc"
  72.  
  73.             // User-specified properties
  74.             uniform sampler2D _MainTex;
  75.             uniform sampler2D _Normal;
  76.             uniform float4 _LightColor0;
  77.            
  78.             struct VertexInput
  79.             {
  80.             float4 vertex : POSITION;
  81.             float4 tangent : TANGENT;  
  82.             float3 normal : NORMAL;
  83.             float4 uv : TEXCOORD0;  
  84.             float4 texcoord1 : TEXCOORD1;
  85.             fixed4 color : COLOR;
  86.             };
  87.  
  88.             struct VertexOutput
  89.             {
  90.                 float4 pos : POSITION;
  91.                 float4 color : COLOR;
  92.                 float2 uv : TEXCOORD0;
  93.                 float4 posWorld : TEXCOORD1;
  94.                 float4 info : TEXCOORD2;
  95.             };
  96.  
  97.             VertexOutput vert(VertexInput input)
  98.             {
  99.                 VertexOutput output;
  100.                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
  101.                 output.posWorld = mul(_Object2World, input.vertex);
  102.                 output.color = input.color;
  103.                 output.uv = float2(input.uv);
  104.                 output.info.x = input.tangent.x;
  105.                 output.info.y = input.tangent.y;
  106.                 output.info.z = input.tangent.z;
  107.                 output.info.w = input.tangent.w;
  108.                 return output;
  109.             }
  110.  
  111.             float4 frag(VertexOutput input) : COLOR
  112.             {
  113.                 float4 diffuseColor = tex2D(_MainTex, input.uv);
  114.  
  115.                 //Computing the normal:
  116.                 float3 normalDirection = (tex2D(_Normal, input.uv).xyz * 2.0f) - 1.0f;
  117.                
  118.                 //rotation matrix
  119.                 float angle = -input.info.x;
  120.                 float scale = input.info.y;
  121.                 float4x4 rotMatrix = float4x4(cos(angle) * scale,  -sin(angle) * scale,  0, 0,
  122.                                               sin(angle) * scale,   cos(angle) * scale,      0, 0,
  123.                                               0,            0,           1, 0,
  124.                                               0,            0,           0, 1);
  125.                                                                              
  126.                 normalDirection = float3(mul(float4(normalDirection, 1.0f), mul(rotMatrix, _World2Object)));
  127.                 normalDirection.z *= -1;
  128.                 normalDirection = normalize(normalDirection);
  129.                
  130.                 // Distance
  131.                 float3 vertexToLightSource = float3(_WorldSpaceLightPos0   - input.posWorld);
  132.                 float distance = length(float3(vertexToLightSource));
  133.                
  134.                  // The values for attenuation and lightDirection are assuming point lights
  135.                 float attenuation = 30.0 / distance; // Linear attenuation is good enough for now
  136.                 float3 lightDirection = normalize(vertexToLightSource);
  137.                
  138.                 // Compute diffuse part of lighting
  139.                 float normalDotLight = clamp(dot(normalDirection, lightDirection), 0.0, 1.0);
  140.                 float diffuseLevel = normalDotLight * attenuation;
  141.  
  142.                 float3 diffuseReflection =
  143.                     float3(diffuseColor)
  144.                     * input.color
  145.                     * float3(_LightColor0)
  146.                     * float3(diffuseLevel)
  147.                     ;
  148.                
  149.                 return float4(diffuseReflection, diffuseColor.a);
  150.              }
  151.  
  152.              ENDCG
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement