From 291119d0877ac40a3b5521f7b2239c9e789b924a Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 18 Aug 2012 10:53:24 +0000 Subject: [PATCH] easymesh: per-fragment lighting in the HLSL shader (PS3, Xbox 360). --- src/easymesh/shiny.lolfx | 66 ++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/src/easymesh/shiny.lolfx b/src/easymesh/shiny.lolfx index d1decd54..c415bd28 100644 --- a/src/easymesh/shiny.lolfx +++ b/src/easymesh/shiny.lolfx @@ -42,7 +42,8 @@ varying vec4 pass_Color; // FIXME: the light direction should be passed in the code vec3 in_LightDir = vec3(0.3, 0.3, 0.7); -void main(void) { +void main(void) +{ /* Material properties */ vec3 specular_reflect = vec3(0.8, 0.75, 0.4); float specular_power = 60.0; @@ -79,47 +80,60 @@ void main(float3 in_Vertex : POSITION, uniform float4x4 in_ModelView, uniform float4x4 in_Proj, uniform float3x3 in_NormalMat, + out float4 pass_Eye : TEXCOORD0, + out float3 pass_TNormal : TEXCOORD1, + out float4 pass_Color : COLOR, + out float4 out_Position : POSITION) +{ + float4 eye = mul(in_ModelView, float4(in_Vertex, 1.0)); + float3 tnorm = normalize(mul(in_NormalMat, in_Normal)); + + pass_Eye = eye; + pass_TNormal = tnorm; +#ifdef _XBOX + pass_Color = in_Color.abgr; +#else + pass_Color = in_Color; +#endif + + out_Position = mul(in_Proj, eye); +} + +-- HLSL.Frag -- + +void main(float4 pass_Eye : TEXCOORD0, + float3 pass_TNormal : TEXCOORD1, + float4 pass_Color : COLOR, uniform float in_Damage, - out float4 out_Position : POSITION, - out float4 pass_Color : COLOR) + out float4 out_FragColor : COLOR) { - float3 in_LightDir = normalize(float3(0.3, 0.3, 0.7)); + float3 in_LightDir = float3(0.3, 0.3, 0.7); + /* Material properties */ float3 specular_reflect = float3(0.8, 0.75, 0.4); float specular_power = 60.0; + + /* World properties */ float ambient_mul = 0.5; float3 ambient_color = float3(0.25, 0.2, 0.35); float3 diffuse_color = float3(1.0, 1.0, 0.6); float3 specular_color = float3(1.0, 1.0, 0.6); - float3 tnorm = normalize(mul(in_NormalMat, in_Normal)); - float4 eye = mul(in_ModelView, float4(in_Vertex, 1.0)); - float3 s = in_LightDir; - float3 v = normalize(-eye.xyz); - float3 r = reflect(-s, tnorm); + + float3 s = normalize(in_LightDir); /* normalize(pass_Eye - lightpos); */ + float3 v = normalize(-pass_Eye.xyz); + float3 r = reflect(-s, pass_TNormal); + float3 ambient = ambient_color; - float sdotn = max(dot(s, tnorm), 0.0); + float sdotn = max(dot(s, pass_TNormal), 0.0); float3 diffuse = diffuse_color * sdotn; float3 specular = float3(0.0, 0.0, 0.0); if (sdotn > 0.0) specular = specular_color * specular_reflect * pow(max(dot(r, v), 0.0), specular_power); float3 light = ambient + diffuse + specular; -#ifdef _XBOX - float4 real_color = in_Color.abgr; -#else - float4 real_color = in_Color; -#endif - real_color = in_Damage * float4(1.2, 1.2, 1.2, 1.0) - + (1.0 - in_Damage) * real_color; - pass_Color = real_color * float4(light, 1.0); - out_Position = mul(in_Proj, eye); -} --- HLSL.Frag -- - -void main(float4 pass_Color : COLOR, - out float4 out_FragColor : COLOR) -{ - out_FragColor = pass_Color; + float4 real_color = in_Damage * float4(1.2, 1.2, 1.2, 1.0) + + (1.0 - in_Damage) * pass_Color; + out_FragColor = real_color * float4(light, 1.0); }