source: trunk/src/easymesh/shiny.lolfx @ 2146

Last change on this file since 2146 was 2146, checked in by touky, 7 years ago

shiny.lolfx now has a PointLight & a DirectionalLight.
Sorry for all the fine projects that use this shader !

File size: 4.7 KB
Line 
1
2[vert.glsl]
3#version 120
4
5attribute vec3 in_Vertex;
6attribute vec3 in_Normal;
7attribute vec4 in_Color;
8
9uniform mat4 in_ModelView;
10uniform mat4 in_View;
11uniform mat4 in_Proj;
12uniform mat3 in_NormalMat;
13
14varying vec4 pass_Vertex; /* View space */
15varying vec3 pass_TNormal;
16varying vec4 pass_Color;
17
18void main(void)
19{
20    vec4 vertex = in_ModelView * vec4(in_Vertex, 1.0);
21    vec3 tnorm = normalize(in_NormalMat * in_Normal);
22
23    pass_Vertex = vertex;
24    pass_TNormal = tnorm;
25    pass_Color = in_Color;
26
27    gl_Position = in_Proj * vertex;
28}
29
30[frag.glsl]
31#version 120
32
33#if defined GL_ES
34precision highp float;
35#endif
36
37uniform float in_Damage;
38uniform mat4 in_View;
39
40varying vec4 pass_Vertex; /* View space */
41varying vec3 pass_TNormal;
42varying vec4 pass_Color;
43
44// FIXME: the light direction should be passed in the code
45vec3 in_LightDir = vec3(-0.3, -0.3, -0.7);
46vec4 in_Light2_Pos = vec4(0.0, 10.0, 0.0, 1.0);
47float in_Light2_Radius = 20.0;
48vec3 in_Light2_diffuse = vec3(0.4, 0.4, 1.0);
49
50void main(void)
51{
52    /* Material properties */
53    vec3 specular_reflect = vec3(0.8, 0.75, 0.4);
54    float specular_power = 60.0;
55
56    /* World properties */
57    float ambient_mul = 0.5;
58    vec3 ambient_color = vec3(0.0, 0.0, 0.0);
59    vec3 diffuse_color = vec3(0.4, 0.4, 0.4);
60    vec3 specular_color = vec3(1.0, 1.0, 0.6);
61
62    vec3 ambient = ambient_color;
63    vec3 specular = vec3(0.0, 0.0, 0.0);
64    vec3 diffuse = vec3(0.0, 0.0, 0.0);
65    vec3 s = vec3(0.0, 0.0, 0.0);
66    vec3 v = vec3(0.0, 0.0, 0.0);
67    vec3 r = vec3(0.0, 0.0, 0.0);
68    float sdotn = 0.0;
69
70    //Light calculation for directional light
71    s = normalize(-in_LightDir);
72    v = normalize(-pass_Vertex.xyz);
73    r = reflect(s, pass_TNormal);
74
75    sdotn = max(dot(s, pass_TNormal), 0.0);
76    diffuse += diffuse_color * sdotn;
77    if (sdotn > 0.0)
78        specular += specular_color * specular_reflect
79                 * pow(max(dot(r, v), 0.0), specular_power);
80
81   
82    //Light calculation for point light
83        vec3 tmpLightDir = (in_View * in_Light2_Pos).xyz - pass_Vertex.xyz;
84        float light_radius_mod = max(0.0, 1.0 - (length(tmpLightDir) / in_Light2_Radius));
85    s = normalize(tmpLightDir);
86    v = normalize(-pass_Vertex.xyz);
87    r = reflect(-s, pass_TNormal);
88
89    sdotn = max(dot(s, pass_TNormal), 0.0);
90    diffuse += in_Light2_diffuse * min(sdotn, light_radius_mod);
91    if (sdotn > 0.0 && light_radius_mod > 0.0)
92        specular += specular_color * min(specular_reflect, light_radius_mod)
93                 * pow(max(dot(r, v), 0.0), specular_power);
94   
95
96    vec3 light = ambient + diffuse + specular;
97
98    vec4 real_color = in_Damage * vec4(1.2, 1.2, 1.2, 1.0)
99                    + (1.0 - in_Damage) * pass_Color;
100    gl_FragColor = real_color * vec4(light, 1.0);
101}
102
103[vert.hlsl]
104
105void main(float3 in_Vertex : POSITION,
106          float3 in_Normal : NORMAL,
107          float4 in_Color : COLOR,
108          uniform float4x4 in_ModelView,
109          uniform float4x4 in_Model,
110          uniform float4x4 in_Proj,
111          uniform float3x3 in_NormalMat,
112          out float4 pass_Vertex : TEXCOORD0,
113          out float3 pass_TNormal : TEXCOORD1,
114          out float4 pass_Color : COLOR,
115          out float4 out_Position : POSITION)
116{
117    float4 eye = mul(in_ModelView, float4(in_Vertex, 1.0));
118    float3 tnorm = normalize(mul(in_NormalMat, in_Normal));
119
120    pass_Vertex = eye;
121    pass_TNormal = tnorm;
122#ifdef _XBOX
123    pass_Color = in_Color.abgr;
124#else
125    pass_Color = in_Color;
126#endif
127
128    out_Position = mul(in_Proj, eye);
129}
130
131[frag.hlsl]
132
133void main(float4 pass_Vertex : TEXCOORD0,
134          float3 pass_TNormal : TEXCOORD1,
135          float4 pass_Color : COLOR,
136          uniform float in_Damage,
137          out float4 out_FragColor : COLOR)
138{
139    float3 in_LightDir = float3(0.3, 0.3, 0.7);
140
141    /* Material properties */
142    float3 specular_reflect = float3(0.8, 0.75, 0.4);
143    float specular_power = 60.0;
144
145    /* World properties */
146    float ambient_mul = 0.5;
147    float3 ambient_color = float3(0.25, 0.2, 0.35);
148    float3 diffuse_color = float3(1.0, 1.0, 0.6);
149    float3 specular_color = float3(1.0, 1.0, 0.6);
150
151    float3 s = normalize(in_LightDir); /* normalize(pass_Vertex - lightpos); */
152    float3 v = normalize(-pass_Vertex.xyz);
153    float3 r = reflect(-s, pass_TNormal);
154
155    float3 ambient = ambient_color;
156    float sdotn = max(dot(s, pass_TNormal), 0.0);
157    float3 diffuse = diffuse_color * sdotn;
158    float3 specular = float3(0.0, 0.0, 0.0);
159    if (sdotn > 0.0)
160        specular = specular_color * specular_reflect
161                 * pow(max(dot(r, v), 0.0), specular_power);
162    float3 light = ambient + diffuse + specular;
163
164    float4 real_color = in_Damage * float4(1.2, 1.2, 1.2, 1.0)
165                      + (1.0 - in_Damage) * pass_Color;
166    out_FragColor = real_color * float4(light, 1.0);
167}
168
Note: See TracBrowser for help on using the repository browser.