mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-04 14:24:53 +08:00
[unity] Updated Sprites shader code.
This commit is contained in:
parent
db36661be1
commit
3ced1e1daf
@ -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
|
||||
//
|
||||
|
||||
@ -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
|
||||
}
|
||||
@ -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
|
||||
//
|
||||
|
||||
@ -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
|
||||
@ -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)
|
||||
|
||||
|
||||
@ -126,7 +126,7 @@ struct VertexLightInfo
|
||||
fixed3 lightColor;
|
||||
|
||||
#if defined(_DIFFUSE_RAMP)
|
||||
float attenuationSqrt;
|
||||
float attenuation;
|
||||
#endif // _DIFFUSE_RAMP
|
||||
};
|
||||
|
||||
@ -134,7 +134,7 @@ 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);
|
||||
|
||||
@ -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
|
||||
@ -205,25 +205,23 @@ 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
|
||||
@ -231,16 +229,6 @@ inline fixed3 calculateLightDiffuse(fixed3 attenuatedLightColor, half3 normal, h
|
||||
|
||||
#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
|
||||
@ -277,24 +265,24 @@ 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; \
|
||||
@ -302,11 +290,11 @@ inline VertexLightInfo getVertexLightAttenuatedInfoWorldSpace(int index, float3
|
||||
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
|
||||
@ -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
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -25,7 +25,7 @@ Shader "Spine/Sprite/Unlit"
|
||||
|
||||
SubShader
|
||||
{
|
||||
Tags { "Queue"="Transparent" "RenderType"="Sprite" }
|
||||
Tags { "Queue"="Transparent" "RenderType"="Sprite" "AlphaDepth"="False" }
|
||||
LOD 100
|
||||
|
||||
Pass
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user