1 | |
---|
2 | [vert.glsl] |
---|
3 | #version 120 |
---|
4 | |
---|
5 | attribute vec3 in_Vertex; |
---|
6 | attribute vec3 in_Normal; |
---|
7 | attribute vec4 in_Color; |
---|
8 | |
---|
9 | uniform mat4 in_ModelView; |
---|
10 | uniform mat4 in_View; |
---|
11 | uniform mat4 in_Proj; |
---|
12 | uniform mat3 in_NormalMat; |
---|
13 | |
---|
14 | varying vec4 pass_Vertex; /* View space */ |
---|
15 | varying vec3 pass_TNormal; |
---|
16 | varying vec4 pass_Color; |
---|
17 | |
---|
18 | void 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 |
---|
34 | precision highp float; |
---|
35 | #endif |
---|
36 | |
---|
37 | uniform float in_Damage; |
---|
38 | uniform mat4 in_View; |
---|
39 | uniform mat4 in_Inv_View; |
---|
40 | |
---|
41 | varying vec4 pass_Vertex; /* View space */ |
---|
42 | varying vec3 pass_TNormal; |
---|
43 | varying vec4 pass_Color; |
---|
44 | |
---|
45 | // FIXME: all the light parameters should be passed in the code |
---|
46 | //Dir Light |
---|
47 | vec3 in_LightDir = vec3(-0.3, -0.3, -0.7); |
---|
48 | |
---|
49 | //Point Light |
---|
50 | vec4 in_Light2_Pos = vec4(20.0, 10.0, 0.0, 1.0); |
---|
51 | float in_Light2_Radius = 20.0; |
---|
52 | vec3 in_Light2_diffuse = vec3(0.4, 0.4, 1.0); |
---|
53 | |
---|
54 | //Cube Light |
---|
55 | vec4 in_Light3_Pos = vec4(-10.0, 10.0, 5.0, 1.0); |
---|
56 | vec3 in_Light3_Size_Inner = vec3(1.0, 1.0, 1.0); |
---|
57 | vec3 in_Light3_Size_Outer = vec3(10.0, 10.0, 10.0); |
---|
58 | vec3 in_Light3_diffuse = vec3(0.4, 1.0, 0.4); |
---|
59 | |
---|
60 | void main(void) |
---|
61 | { |
---|
62 | /* Material properties */ |
---|
63 | vec3 specular_reflect = vec3(0.8, 0.75, 0.4); |
---|
64 | float specular_power = 60.0; |
---|
65 | |
---|
66 | /* World properties */ |
---|
67 | float ambient_mul = 0.5; |
---|
68 | vec3 ambient_color = vec3(0.0, 0.0, 0.0); |
---|
69 | vec3 diffuse_color = vec3(0.4, 0.4, 0.4); |
---|
70 | vec3 specular_color = vec3(1.0, 1.0, 0.6); |
---|
71 | |
---|
72 | vec3 ambient = ambient_color; |
---|
73 | vec3 specular = vec3(0.0, 0.0, 0.0); |
---|
74 | vec3 diffuse = vec3(0.0, 0.0, 0.0); |
---|
75 | vec3 s = vec3(0.0, 0.0, 0.0); |
---|
76 | vec3 v = vec3(0.0, 0.0, 0.0); |
---|
77 | vec3 r = vec3(0.0, 0.0, 0.0); |
---|
78 | float sdotn = 0.0; |
---|
79 | float light_radius_mod = 0.0; |
---|
80 | |
---|
81 | //Light calculation for directional light |
---|
82 | s = normalize(-in_LightDir); |
---|
83 | v = normalize(-pass_Vertex.xyz); |
---|
84 | r = reflect(s, pass_TNormal); |
---|
85 | |
---|
86 | sdotn = max(dot(s, pass_TNormal), 0.0); |
---|
87 | diffuse += diffuse_color * sdotn; |
---|
88 | if (sdotn > 0.0) |
---|
89 | specular += specular_color * specular_reflect |
---|
90 | * pow(max(dot(r, v), 0.0), specular_power); |
---|
91 | //---------- |
---|
92 | |
---|
93 | //Light calculation for point light |
---|
94 | vec3 tmpLightDir = (in_View * in_Light2_Pos).xyz - pass_Vertex.xyz; |
---|
95 | light_radius_mod = max(0.0, 1.0 - (length(tmpLightDir) / in_Light2_Radius)); |
---|
96 | s = normalize(tmpLightDir); |
---|
97 | v = normalize(-pass_Vertex.xyz); |
---|
98 | r = reflect(-s, pass_TNormal); |
---|
99 | |
---|
100 | sdotn = max(dot(s, pass_TNormal), 0.0); |
---|
101 | diffuse += in_Light2_diffuse * min(sdotn, light_radius_mod); |
---|
102 | if (sdotn > 0.0 && light_radius_mod > 0.0) |
---|
103 | specular += specular_color * min(specular_reflect, light_radius_mod) |
---|
104 | * pow(max(dot(r, v), 0.0), specular_power); |
---|
105 | //---------- |
---|
106 | |
---|
107 | //Light calculation for cube light |
---|
108 | vec3 Local_Vertex = (in_Inv_View * pass_Vertex).xyz - (in_Light3_Pos).xyz; |
---|
109 | vec3 Proj_Vertex = clamp(Local_Vertex.xyz, -in_Light3_Size_Inner, in_Light3_Size_Inner); |
---|
110 | vec3 new_LightDir = Local_Vertex - Proj_Vertex; |
---|
111 | |
---|
112 | vec3 light_radius = max(vec3(0.0,0.0,0.0), vec3(1.0,1.0,1.0) - abs(new_LightDir / in_Light3_Size_Outer)); |
---|
113 | light_radius_mod = min(light_radius.x, min(light_radius.y, light_radius.z)); |
---|
114 | |
---|
115 | if (length(new_LightDir) == 0.0) |
---|
116 | sdotn = 1.0; |
---|
117 | else |
---|
118 | { |
---|
119 | new_LightDir = (in_View * vec4(Proj_Vertex + in_Light3_Pos.xyz,1.0)).xyz - pass_Vertex.xyz; |
---|
120 | sdotn = max(dot(normalize(new_LightDir), pass_TNormal), 0.0); |
---|
121 | } |
---|
122 | diffuse += in_Light3_diffuse * min(sdotn, light_radius_mod); |
---|
123 | //---------- |
---|
124 | |
---|
125 | vec3 light = ambient + diffuse + specular; |
---|
126 | |
---|
127 | vec4 real_color = in_Damage * vec4(1.2, 1.2, 1.2, 1.0) |
---|
128 | + (1.0 - in_Damage) * pass_Color; |
---|
129 | gl_FragColor = real_color * vec4(light, 1.0); |
---|
130 | } |
---|
131 | |
---|
132 | [vert.hlsl] |
---|
133 | |
---|
134 | void main(float3 in_Vertex : POSITION, |
---|
135 | float3 in_Normal : NORMAL, |
---|
136 | float4 in_Color : COLOR, |
---|
137 | uniform float4x4 in_ModelView, |
---|
138 | uniform float4x4 in_Model, |
---|
139 | uniform float4x4 in_Proj, |
---|
140 | uniform float3x3 in_NormalMat, |
---|
141 | out float4 pass_Vertex : TEXCOORD0, |
---|
142 | out float3 pass_TNormal : TEXCOORD1, |
---|
143 | out float4 pass_Color : COLOR, |
---|
144 | out float4 out_Position : POSITION) |
---|
145 | { |
---|
146 | float4 eye = mul(in_ModelView, float4(in_Vertex, 1.0)); |
---|
147 | float3 tnorm = normalize(mul(in_NormalMat, in_Normal)); |
---|
148 | |
---|
149 | pass_Vertex = eye; |
---|
150 | pass_TNormal = tnorm; |
---|
151 | #ifdef _XBOX |
---|
152 | pass_Color = in_Color.abgr; |
---|
153 | #else |
---|
154 | pass_Color = in_Color; |
---|
155 | #endif |
---|
156 | |
---|
157 | out_Position = mul(in_Proj, eye); |
---|
158 | } |
---|
159 | |
---|
160 | [frag.hlsl] |
---|
161 | |
---|
162 | void main(float4 pass_Vertex : TEXCOORD0, |
---|
163 | float3 pass_TNormal : TEXCOORD1, |
---|
164 | float4 pass_Color : COLOR, |
---|
165 | uniform float in_Damage, |
---|
166 | out float4 out_FragColor : COLOR) |
---|
167 | { |
---|
168 | float3 in_LightDir = float3(0.3, 0.3, 0.7); |
---|
169 | |
---|
170 | /* Material properties */ |
---|
171 | float3 specular_reflect = float3(0.8, 0.75, 0.4); |
---|
172 | float specular_power = 60.0; |
---|
173 | |
---|
174 | /* World properties */ |
---|
175 | float ambient_mul = 0.5; |
---|
176 | float3 ambient_color = float3(0.25, 0.2, 0.35); |
---|
177 | float3 diffuse_color = float3(1.0, 1.0, 0.6); |
---|
178 | float3 specular_color = float3(1.0, 1.0, 0.6); |
---|
179 | |
---|
180 | float3 s = normalize(in_LightDir); /* normalize(pass_Vertex - lightpos); */ |
---|
181 | float3 v = normalize(-pass_Vertex.xyz); |
---|
182 | float3 r = reflect(-s, pass_TNormal); |
---|
183 | |
---|
184 | float3 ambient = ambient_color; |
---|
185 | float sdotn = max(dot(s, pass_TNormal), 0.0); |
---|
186 | float3 diffuse = diffuse_color * sdotn; |
---|
187 | float3 specular = float3(0.0, 0.0, 0.0); |
---|
188 | if (sdotn > 0.0) |
---|
189 | specular = specular_color * specular_reflect |
---|
190 | * pow(max(dot(r, v), 0.0), specular_power); |
---|
191 | float3 light = ambient + diffuse + specular; |
---|
192 | |
---|
193 | float4 real_color = in_Damage * float4(1.2, 1.2, 1.2, 1.0) |
---|
194 | + (1.0 - in_Damage) * pass_Color; |
---|
195 | out_FragColor = real_color * float4(light, 1.0); |
---|
196 | } |
---|
197 | |
---|