[unity] Updated Sprites shader code.

This commit is contained in:
pharan 2016-12-14 04:14:30 +08:00
parent db36661be1
commit 3ced1e1daf
7 changed files with 67 additions and 70 deletions

View File

@ -90,26 +90,6 @@ inline half3 calculateNormalFromBumpMap(float2 texUV, half3 tangentWorld, half3
#endif // _NORMALMAP #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 // Blending functions
// //

View File

@ -39,11 +39,9 @@ inline half3 calculateSpriteWorldNormal(VertexInput vertex)
//Rotate fixed normal by inverse camera matrix to convert the fixed normal into world space //Rotate fixed normal by inverse camera matrix to convert the fixed normal into world space
float3x3 invView = transpose((float3x3)UNITY_MATRIX_VP); float3x3 invView = transpose((float3x3)UNITY_MATRIX_VP);
float3 normal = _FixedNormal.xyz; float3 normal = _FixedNormal.xyz;
#if UNITY_REVERSED_Z #if UNITY_REVERSED_Z
normal.z = -normal.z; normal.z = -normal.z;
#endif #endif
return normalize(mul(invView, normal)); return normalize(mul(invView, normal));
#endif // !MESH_NORMALS #endif // !MESH_NORMALS
} }
@ -84,6 +82,36 @@ inline half3 calculateSpriteWorldBinormal(half3 normalWorld, half3 tangentWorld,
#endif // _NORMALMAP #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 // Rim Lighting functions
// //

View File

@ -55,10 +55,10 @@ inline fixed3 calculateLightDiffuse(VertexOutput input, float3 normalWorld)
float3 lightWorldDirection = normalize(_WorldSpaceLightPos0.xyz - input.posWorld.xyz * _WorldSpaceLightPos0.w); float3 lightWorldDirection = normalize(_WorldSpaceLightPos0.xyz - input.posWorld.xyz * _WorldSpaceLightPos0.w);
float attenuation = LIGHT_ATTENUATION(input); float attenuation = LIGHT_ATTENUATION(input);
float angleDot = dotClamped(normalWorld, lightWorldDirection); float angleDot = max(0, dot(normalWorld, lightWorldDirection));
#if defined(_DIFFUSE_RAMP) #if defined(_DIFFUSE_RAMP)
fixed3 lightDiffuse = calculateRampedDiffuse(_LightColor0.rgb, sqrt(attenuation), angleDot); fixed3 lightDiffuse = calculateRampedDiffuse(_LightColor0.rgb, attenuation, angleDot);
#else #else
fixed3 lightDiffuse = _LightColor0.rgb * (attenuation * angleDot); fixed3 lightDiffuse = _LightColor0.rgb * (attenuation * angleDot);
#endif // _DIFFUSE_RAMP #endif // _DIFFUSE_RAMP
@ -147,7 +147,7 @@ fixed4 fragBase(VertexOutput input) : SV_Target
fixed3 diffuse = calculateLightDiffuse(input, normalWorld); fixed3 diffuse = calculateLightDiffuse(input, normalWorld);
//Combine along with vertex lighting for the base lighting pass //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) APPLY_EMISSION(lighting, input.texcoord)

View File

@ -126,7 +126,7 @@ struct VertexLightInfo
fixed3 lightColor; fixed3 lightColor;
#if defined(_DIFFUSE_RAMP) #if defined(_DIFFUSE_RAMP)
float attenuationSqrt; float attenuation;
#endif // _DIFFUSE_RAMP #endif // _DIFFUSE_RAMP
}; };
@ -134,7 +134,7 @@ inline VertexLightInfo getVertexLightAttenuatedInfo(int index, float3 viewPos)
{ {
VertexLightInfo lightInfo; 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; lightInfo.lightDirection = unity_LightPosition[index].xyz - viewPos.xyz * unity_LightPosition[index].w;
float lengthSq = dot(lightInfo.lightDirection, lightInfo.lightDirection); float lengthSq = dot(lightInfo.lightDirection, lightInfo.lightDirection);
@ -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 using a diffuse ramp texture then need to pass through the lights attenuation, otherwise premultiply the light color with it
#if defined(_DIFFUSE_RAMP) #if defined(_DIFFUSE_RAMP)
lightInfo.lightColor = unity_LightColor[index].rgb; lightInfo.lightColor = unity_LightColor[index].rgb;
lightInfo.attenuationSqrt = sqrt(attenuation); lightInfo.attenuation = attenuation;
#else #else
lightInfo.lightColor = unity_LightColor[index].rgb * attenuation; lightInfo.lightColor = unity_LightColor[index].rgb * attenuation;
#endif // _DIFFUSE_RAMP #endif // _DIFFUSE_RAMP
@ -205,25 +205,23 @@ fixed3 calculateAmbientLight(half3 normalWorld)
} }
//////////////////////////////////////// ////////////////////////////////////////
// Light Packing Functions (this stuff gets messy!) // Light Packing Functions
// //
#if defined(_DIFFUSE_RAMP) #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)); float angleDot = max(0, dot(viewNormal, lightViewDir));
fixed3 diffuse = calculateRampedDiffuse(lightColor, attenuation, angleDot); return calculateRampedDiffuse(lightColor, attenuation, angleDot);
return diffuse;
} }
#else #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)); float angleDot = max(0, dot(viewNormal, lightViewDir));
fixed3 diffuse = attenuatedLightColor * angleDot; return attenuatedLightColor * angleDot;
return diffuse;
} }
#endif // _NORMALMAP #endif // _NORMALMAP
@ -231,16 +229,6 @@ inline fixed3 calculateLightDiffuse(fixed3 attenuatedLightColor, half3 normal, h
#if defined(PER_PIXEL_LIGHTING) #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_DIR VertexLightInfo0.xyz
#define VERTEX_LIGHT_0_R VertexLightInfo4.x #define VERTEX_LIGHT_0_R VertexLightInfo4.x
#define VERTEX_LIGHT_0_G VertexLightInfo4.y #define VERTEX_LIGHT_0_G VertexLightInfo4.y
@ -277,24 +265,24 @@ inline VertexLightInfo getVertexLightAttenuatedInfoWorldSpace(int index, float3
#define PACK_VERTEX_LIGHT_DIFFUSE(index, output, lightInfo) \ #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 #else
#define PACK_VERTEX_LIGHT_DIFFUSE(index, output, lightInfo) #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 #endif
#define PACK_VERTEX_LIGHT(index, output, viewPos) \ #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##_DIR = lightInfo.lightDirection; \
output.VERTEX_LIGHT_##index##_R = lightInfo.lightColor.r; \ output.VERTEX_LIGHT_##index##_R = lightInfo.lightColor.r; \
output.VERTEX_LIGHT_##index##_G = lightInfo.lightColor.g; \ output.VERTEX_LIGHT_##index##_G = lightInfo.lightColor.g; \
@ -302,11 +290,11 @@ inline VertexLightInfo getVertexLightAttenuatedInfoWorldSpace(int index, float3
PACK_VERTEX_LIGHT_DIFFUSE(index, output, lightInfo); \ 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; \ half3 lightViewDir = input.VERTEX_LIGHT_##index##_DIR; \
fixed3 vertexLightColor = fixed3(input.VERTEX_LIGHT_##index##_R, input.VERTEX_LIGHT_##index##_G, input.VERTEX_LIGHT_##index##_B); \ 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, vertexLightColor, normalDirection, vertexLightDir) \ ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, lightColor, viewNormal, lightViewDir) \
} }
#else //!PER_PIXEL_LIGHTING #else //!PER_PIXEL_LIGHTING
@ -318,8 +306,8 @@ inline VertexLightInfo getVertexLightAttenuatedInfoWorldSpace(int index, float3
inline fixed3 calculateLightDiffuse(int index, float3 viewPos, half3 viewNormal) inline fixed3 calculateLightDiffuse(int index, float3 viewPos, half3 viewNormal)
{ {
VertexLightInfo lightInfo = getVertexLightAttenuatedInfo(index, viewPos); VertexLightInfo lightInfo = getVertexLightAttenuatedInfo(index, viewPos);
float diff = max (0, dot (viewNormal, lightInfo.lightDirection)); float angleDot = max(0, dot(viewNormal, lightInfo.lightDirection));
return lightInfo.lightColor * diff; return lightInfo.lightColor * angleDot;
} }
#endif // !PER_PIXEL_LIGHTING #endif // !PER_PIXEL_LIGHTING
@ -405,10 +393,11 @@ fixed4 frag(VertexOutput input) : SV_Target
fixed3 diffuse = fixed3(0,0,0); fixed3 diffuse = fixed3(0,0,0);
//Add each vertex light to diffuse //Add each vertex light to diffuse
ADD_VERTEX_LIGHT(0, input, normalWorld, diffuse) half3 normalView = normalize(mul((float3x3)UNITY_MATRIX_V, normalWorld));
ADD_VERTEX_LIGHT(1, input, normalWorld, diffuse) ADD_VERTEX_LIGHT(0, input, normalView, diffuse)
ADD_VERTEX_LIGHT(2, input, normalWorld, diffuse) ADD_VERTEX_LIGHT(1, input, normalView, diffuse)
ADD_VERTEX_LIGHT(3, input, normalWorld, diffuse) ADD_VERTEX_LIGHT(2, input, normalView, diffuse)
ADD_VERTEX_LIGHT(3, input, normalView, diffuse)
fixed3 lighting = ambient + diffuse; fixed3 lighting = ambient + diffuse;

View File

@ -36,7 +36,7 @@ Shader "Spine/Sprite/Pixel Lit"
SubShader SubShader
{ {
Tags { "Queue"="Transparent" "RenderType"="Sprite" } Tags { "Queue"="Transparent" "RenderType"="Sprite" "AlphaDepth"="False" }
LOD 200 LOD 200
Pass Pass
@ -64,7 +64,7 @@ Shader "Spine/Sprite/Pixel Lit"
#pragma shader_feature _FOG #pragma shader_feature _FOG
#pragma multi_compile_fwdbase #pragma multi_compile_fwdbase
#pragma fragmentoption ARB_precision_hint_fastest #pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_fog #pragma multi_compile_fog
#pragma vertex vert #pragma vertex vert

View File

@ -25,7 +25,7 @@ Shader "Spine/Sprite/Unlit"
SubShader SubShader
{ {
Tags { "Queue"="Transparent" "RenderType"="Sprite" } Tags { "Queue"="Transparent" "RenderType"="Sprite" "AlphaDepth"="False" }
LOD 100 LOD 100
Pass Pass

View File

@ -36,7 +36,7 @@ Shader "Spine/Sprite/Vertex Lit"
SubShader SubShader
{ {
Tags { "Queue"="Transparent" "RenderType"="Sprite" } Tags { "Queue"="Transparent" "RenderType"="Sprite" "AlphaDepth"="False" }
LOD 150 LOD 150
Pass Pass