Skip to content

Commit

Permalink
[unity] Updated Sprites shader code.
Browse files Browse the repository at this point in the history
  • Loading branch information
pharan committed Dec 13, 2016
1 parent db36661 commit 3ced1e1
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,6 @@ inline half3 calculateNormalFromBumpMap(float2 texUV, half3 tangentWorld, half3

#endif // _NORMALMAP

#if defined(_DIFFUSE_RAMP)

////////////////////////////////////////
// Diffuse ramp functions
//

uniform sampler2D _DiffuseRamp;

inline fixed3 calculateDiffuseRamp(float ramp)
{
return tex2D(_DiffuseRamp, float2(ramp, ramp)).rgb;
}

inline fixed3 calculateRampedDiffuse(fixed3 lightColor, float attenuation, float angleDot)
{
float ramp = clamp(((angleDot * 0.5) + 0.5) * attenuation, 0.0, 1.0);
return lightColor * calculateDiffuseRamp(ramp);
}
#endif // _DIFFUSE_RAMP

////////////////////////////////////////
// Blending functions
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ inline half3 calculateSpriteWorldNormal(VertexInput vertex)
//Rotate fixed normal by inverse camera matrix to convert the fixed normal into world space
float3x3 invView = transpose((float3x3)UNITY_MATRIX_VP);
float3 normal = _FixedNormal.xyz;

#if UNITY_REVERSED_Z
normal.z = -normal.z;
#endif

return normalize(mul(invView, normal));
#endif // !MESH_NORMALS
}
Expand Down Expand Up @@ -84,6 +82,36 @@ inline half3 calculateSpriteWorldBinormal(half3 normalWorld, half3 tangentWorld,

#endif // _NORMALMAP

#if defined(_DIFFUSE_RAMP)


////////////////////////////////////////
// Diffuse ramp functions
//

//Disable for softer, more traditional diffuse ramping
#define HARD_DIFFUSE_RAMP

uniform sampler2D _DiffuseRamp;

inline fixed3 calculateDiffuseRamp(float ramp)
{
return tex2D(_DiffuseRamp, float2(ramp, ramp)).rgb;
}

inline fixed3 calculateRampedDiffuse(fixed3 lightColor, float attenuation, float angleDot)
{
float d = angleDot * 0.5 + 0.5;
#if defined(HARD_DIFFUSE_RAMP)
half3 ramp = calculateDiffuseRamp(d * attenuation * 2);
return lightColor * ramp;
#else
half3 ramp = calculateDiffuseRamp(d);
return lightColor * ramp * (attenuation * 2);
#endif
}
#endif // _DIFFUSE_RAMP

////////////////////////////////////////
// Rim Lighting functions
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ inline fixed3 calculateLightDiffuse(VertexOutput input, float3 normalWorld)
float3 lightWorldDirection = normalize(_WorldSpaceLightPos0.xyz - input.posWorld.xyz * _WorldSpaceLightPos0.w);

float attenuation = LIGHT_ATTENUATION(input);
float angleDot = dotClamped(normalWorld, lightWorldDirection);
float angleDot = max(0, dot(normalWorld, lightWorldDirection));

#if defined(_DIFFUSE_RAMP)
fixed3 lightDiffuse = calculateRampedDiffuse(_LightColor0.rgb, sqrt(attenuation), angleDot);
fixed3 lightDiffuse = calculateRampedDiffuse(_LightColor0.rgb, attenuation, angleDot);
#else
fixed3 lightDiffuse = _LightColor0.rgb * (attenuation * angleDot);
#endif // _DIFFUSE_RAMP
Expand Down Expand Up @@ -147,7 +147,7 @@ fixed4 fragBase(VertexOutput input) : SV_Target
fixed3 diffuse = calculateLightDiffuse(input, normalWorld);

//Combine along with vertex lighting for the base lighting pass
fixed3 lighting = saturate(ambient + diffuse + input.vertexLighting);
fixed3 lighting = ambient + diffuse + input.vertexLighting;

APPLY_EMISSION(lighting, input.texcoord)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ struct VertexLightInfo
fixed3 lightColor;

#if defined(_DIFFUSE_RAMP)
float attenuationSqrt;
float attenuation;
#endif // _DIFFUSE_RAMP
};

inline VertexLightInfo getVertexLightAttenuatedInfo(int index, float3 viewPos)
{
VertexLightInfo lightInfo;

//For directional lights _WorldSpaceLightPos0.w is set to zero
//For directional lights unity_LightPosition.w is set to zero
lightInfo.lightDirection = unity_LightPosition[index].xyz - viewPos.xyz * unity_LightPosition[index].w;
float lengthSq = dot(lightInfo.lightDirection, lightInfo.lightDirection);

Expand All @@ -157,7 +157,7 @@ inline VertexLightInfo getVertexLightAttenuatedInfo(int index, float3 viewPos)
//If using a diffuse ramp texture then need to pass through the lights attenuation, otherwise premultiply the light color with it
#if defined(_DIFFUSE_RAMP)
lightInfo.lightColor = unity_LightColor[index].rgb;
lightInfo.attenuationSqrt = sqrt(attenuation);
lightInfo.attenuation = attenuation;
#else
lightInfo.lightColor = unity_LightColor[index].rgb * attenuation;
#endif // _DIFFUSE_RAMP
Expand Down Expand Up @@ -205,42 +205,30 @@ fixed3 calculateAmbientLight(half3 normalWorld)
}

////////////////////////////////////////
// Light Packing Functions (this stuff gets messy!)
// Light Packing Functions
//

#if defined(_DIFFUSE_RAMP)

inline fixed3 calculateLightDiffuse(fixed3 lightColor, half3 normal, half3 lightDirection, float attenuation)
inline fixed3 calculateLightDiffuse(fixed3 lightColor, half3 viewNormal, half3 lightViewDir, float attenuation)
{
float angleDot = max(0, dot(normal, lightDirection));
fixed3 diffuse = calculateRampedDiffuse(lightColor, attenuation, angleDot);
return diffuse;
float angleDot = max(0, dot(viewNormal, lightViewDir));
return calculateRampedDiffuse(lightColor, attenuation, angleDot);
}

#else

inline fixed3 calculateLightDiffuse(fixed3 attenuatedLightColor, half3 normal, half3 lightDirection)
inline fixed3 calculateLightDiffuse(fixed3 attenuatedLightColor, half3 viewNormal, half3 lightViewDir)
{
float angleDot = max(0, dot(normal, lightDirection));
fixed3 diffuse = attenuatedLightColor * angleDot;
return diffuse;
float angleDot = max(0, dot(viewNormal, lightViewDir));
return attenuatedLightColor * angleDot;
}

#endif // _NORMALMAP


#if defined(PER_PIXEL_LIGHTING)

inline VertexLightInfo getVertexLightAttenuatedInfoWorldSpace(int index, float3 viewPos)
{
VertexLightInfo lightInfo = getVertexLightAttenuatedInfo(index, viewPos);

//Convert light direction from view space to world space
lightInfo.lightDirection = normalize(mul((float3x3)UNITY_MATRIX_V, lightInfo.lightDirection));

return lightInfo;
}

#define VERTEX_LIGHT_0_DIR VertexLightInfo0.xyz
#define VERTEX_LIGHT_0_R VertexLightInfo4.x
#define VERTEX_LIGHT_0_G VertexLightInfo4.y
Expand Down Expand Up @@ -277,36 +265,36 @@ inline VertexLightInfo getVertexLightAttenuatedInfoWorldSpace(int index, float3

#define PACK_VERTEX_LIGHT_DIFFUSE(index, output, lightInfo) \
{ \
output.LIGHT_DIFFUSE_ATTEN_##index = lightInfo.attenuationSqrt; \
output.LIGHT_DIFFUSE_ATTEN_##index = lightInfo.attenuation; \
}

#define ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, vertexLightColor, normalDirection, vertexLightDir) \
#define ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, lightColor, viewNormal, lightViewDir) \
{ \
diffuse += calculateLightDiffuse(vertexLightColor, normalDirection, vertexLightDir, input.LIGHT_DIFFUSE_ATTEN_##index); \
diffuse += calculateLightDiffuse(lightColor, viewNormal, lightViewDir, input.LIGHT_DIFFUSE_ATTEN_##index); \
}
#else
#define PACK_VERTEX_LIGHT_DIFFUSE(index, output, lightInfo)
#define ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, vertexLightColor, normalDirection, vertexLightDir) \
#define ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, lightColor, viewNormal, lightViewDir) \
{ \
diffuse += calculateLightDiffuse(vertexLightColor, normalDirection, vertexLightDir); \
diffuse += calculateLightDiffuse(lightColor, viewNormal, lightViewDir); \
}
#endif

#define PACK_VERTEX_LIGHT(index, output, viewPos) \
{ \
VertexLightInfo lightInfo = getVertexLightAttenuatedInfoWorldSpace(index, viewPos); \
VertexLightInfo lightInfo = getVertexLightAttenuatedInfo(index, viewPos); \
output.VERTEX_LIGHT_##index##_DIR = lightInfo.lightDirection; \
output.VERTEX_LIGHT_##index##_R = lightInfo.lightColor.r; \
output.VERTEX_LIGHT_##index##_G = lightInfo.lightColor.g; \
output.VERTEX_LIGHT_##index##_B = lightInfo.lightColor.b; \
PACK_VERTEX_LIGHT_DIFFUSE(index, output, lightInfo); \
}

#define ADD_VERTEX_LIGHT(index, input, normalDirection, diffuse) \
#define ADD_VERTEX_LIGHT(index, input, viewNormal, diffuse) \
{ \
half3 vertexLightDir = input.VERTEX_LIGHT_##index##_DIR; \
fixed3 vertexLightColor = fixed3(input.VERTEX_LIGHT_##index##_R, input.VERTEX_LIGHT_##index##_G, input.VERTEX_LIGHT_##index##_B); \
ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, vertexLightColor, normalDirection, vertexLightDir) \
half3 lightViewDir = input.VERTEX_LIGHT_##index##_DIR; \
fixed3 lightColor = fixed3(input.VERTEX_LIGHT_##index##_R, input.VERTEX_LIGHT_##index##_G, input.VERTEX_LIGHT_##index##_B); \
ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, lightColor, viewNormal, lightViewDir) \
}

#else //!PER_PIXEL_LIGHTING
Expand All @@ -318,8 +306,8 @@ inline VertexLightInfo getVertexLightAttenuatedInfoWorldSpace(int index, float3
inline fixed3 calculateLightDiffuse(int index, float3 viewPos, half3 viewNormal)
{
VertexLightInfo lightInfo = getVertexLightAttenuatedInfo(index, viewPos);
float diff = max (0, dot (viewNormal, lightInfo.lightDirection));
return lightInfo.lightColor * diff;
float angleDot = max(0, dot(viewNormal, lightInfo.lightDirection));
return lightInfo.lightColor * angleDot;
}

#endif // !PER_PIXEL_LIGHTING
Expand Down Expand Up @@ -405,10 +393,11 @@ fixed4 frag(VertexOutput input) : SV_Target
fixed3 diffuse = fixed3(0,0,0);

//Add each vertex light to diffuse
ADD_VERTEX_LIGHT(0, input, normalWorld, diffuse)
ADD_VERTEX_LIGHT(1, input, normalWorld, diffuse)
ADD_VERTEX_LIGHT(2, input, normalWorld, diffuse)
ADD_VERTEX_LIGHT(3, input, normalWorld, diffuse)
half3 normalView = normalize(mul((float3x3)UNITY_MATRIX_V, normalWorld));
ADD_VERTEX_LIGHT(0, input, normalView, diffuse)
ADD_VERTEX_LIGHT(1, input, normalView, diffuse)
ADD_VERTEX_LIGHT(2, input, normalView, diffuse)
ADD_VERTEX_LIGHT(3, input, normalView, diffuse)

fixed3 lighting = ambient + diffuse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Shader "Spine/Sprite/Pixel Lit"

_BlendTex ("Blend Texture", 2D) = "white" {}
_BlendAmount ("Blend", Range(0,1)) = 0.0

[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _RenderQueue ("__queue", Float) = 0.0
Expand All @@ -36,7 +36,7 @@ Shader "Spine/Sprite/Pixel Lit"

SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Sprite" }
Tags { "Queue"="Transparent" "RenderType"="Sprite" "AlphaDepth"="False" }
LOD 200

Pass
Expand Down Expand Up @@ -64,7 +64,7 @@ Shader "Spine/Sprite/Pixel Lit"
#pragma shader_feature _FOG

#pragma multi_compile_fwdbase
#pragma fragmentoption ARB_precision_hint_fastest
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_fog

#pragma vertex vert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Shader "Spine/Sprite/Unlit"

_BlendTex ("Blend Texture", 2D) = "white" {}
_BlendAmount ("Blend", Range(0,1)) = 0.0

[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _RenderQueue ("__queue", Float) = 0.0
Expand All @@ -25,7 +25,7 @@ Shader "Spine/Sprite/Unlit"

SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Sprite" }
Tags { "Queue"="Transparent" "RenderType"="Sprite" "AlphaDepth"="False" }
LOD 100

Pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Shader "Spine/Sprite/Vertex Lit"

_BlendTex ("Blend Texture", 2D) = "white" {}
_BlendAmount ("Blend", Range(0,1)) = 0.0

[HideInInspector] _SrcBlend ("__src", Float) = 1.0
[HideInInspector] _DstBlend ("__dst", Float) = 0.0
[HideInInspector] _RenderQueue ("__queue", Float) = 0.0
Expand All @@ -36,7 +36,7 @@ Shader "Spine/Sprite/Vertex Lit"

SubShader
{
Tags { "Queue"="Transparent" "RenderType"="Sprite" }
Tags { "Queue"="Transparent" "RenderType"="Sprite" "AlphaDepth"="False" }
LOD 150

Pass
Expand Down

0 comments on commit 3ced1e1

Please sign in to comment.