source: trunk/src/easymesh/shinydebugUV.lolfx @ 2370

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

render : Added DebugRenderMode & corresponding shaders. naive (VERY) implementation in MeshViewer.

File size: 5.4 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;
39uniform mat4 in_Inv_View;
40
41uniform vec4 u_Lights[8 * 2];
42
43varying vec4 pass_Vertex; /* View space */
44varying vec3 pass_TNormal;
45varying vec4 pass_Color;
46
47#if 0
48//Cube Light
49vec4 in_Light3_Pos = vec4(-10.0, 10.0, 5.0, 1.0);
50vec3 in_Light3_Size_Inner = vec3(3.0, 1.0, 3.0);
51vec3 in_Light3_Size_Outer = vec3(15.0, 15.0, 15.0);
52vec3 in_Light3_diffuse = vec3(0.4, 1.0, 0.4);
53#endif
54
55void main(void)
56{
57    /* Material properties */
58    vec3 specular_reflect = vec3(0.8, 0.75, 0.4);
59    float specular_power = 60.0;
60
61    /* World properties */
62    vec3 ambient = vec3(0.1, 0.1, 0.1);
63    vec3 specular = vec3(0.0, 0.0, 0.0);
64    vec3 diffuse = vec3(0.0, 0.0, 0.0);
65
66    /* Light precalculations */
67    vec3 v = normalize(-pass_Vertex.xyz);
68
69    /* Apply lighting */
70    for (int i = 0; i < 8; i++)
71    {
72        vec4 pos = u_Lights[i * 2];
73        vec4 color = u_Lights[i * 2 + 1];
74        vec3 s, r;
75
76        if (pos.w > 0.0)
77        {
78            /* Point light -- no attenuation yet */
79            s = normalize((in_View * pos).xyz - pass_Vertex.xyz);
80            r = reflect(-s, pass_TNormal);
81        }
82        else
83        {
84            /* Directional light */
85            s = normalize(-pos.xyz);
86            r = reflect(s, pass_TNormal);
87        }
88
89        float sdotn = max(dot(s, pass_TNormal), 0.0);
90        diffuse += color.xyz * sdotn;
91        if (sdotn > 0.0)
92            specular += color.xyz * specular_reflect
93                         * pow(max(dot(r, v), 0.0), specular_power);
94    }
95
96#if 0
97    //Light calculation for cube light
98    vec3 specular_color = vec3(1.0, 1.0, 0.6);
99    vec3 Local_Vertex = (in_Inv_View * pass_Vertex).xyz - (in_Light3_Pos).xyz;
100    vec3 Proj_Vertex = clamp(Local_Vertex.xyz, -in_Light3_Size_Inner, in_Light3_Size_Inner);
101    vec3 new_LightDir = Local_Vertex - Proj_Vertex;
102
103    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));
104    float light_radius_mod = min(light_radius.x, min(light_radius.y, light_radius.z));
105
106    if (length(new_LightDir) == 0.0)
107        sdotn = 1.0;
108    else
109    {
110        new_LightDir = normalize((in_View * vec4(Proj_Vertex + in_Light3_Pos.xyz,1.0)).xyz - pass_Vertex.xyz);
111        sdotn = max(dot(new_LightDir, pass_TNormal), 0.0);
112        r = reflect(-new_LightDir, pass_TNormal);
113        if (sdotn > 0.0 && light_radius_mod > 0.0)
114            specular += specular_color * min(specular_reflect, light_radius_mod)
115                     * pow(max(dot(r, v), 0.0), specular_power);
116    }
117    diffuse += in_Light3_diffuse * min(sdotn, light_radius_mod);
118    //----------
119#endif
120
121    vec3 light = ambient + diffuse + specular;
122
123    vec4 real_color = mix(pass_Color, vec4(1.2, 1.2, 1.2, 1.0), in_Damage);
124    gl_FragColor = real_color * vec4(light, 1.0);
125}
126
127[vert.hlsl]
128
129void main(float3 in_Vertex : POSITION,
130          float3 in_Normal : NORMAL,
131          float4 in_Color : COLOR,
132          uniform float4x4 in_ModelView,
133          uniform float4x4 in_Model,
134          uniform float4x4 in_Proj,
135          uniform float3x3 in_NormalMat,
136          out float4 pass_Vertex : TEXCOORD0,
137          out float3 pass_TNormal : TEXCOORD1,
138          out float4 pass_Color : COLOR,
139          out float4 out_Position : POSITION)
140{
141    float4 eye = mul(in_ModelView, float4(in_Vertex, 1.0));
142    float3 tnorm = normalize(mul(in_NormalMat, in_Normal));
143
144    pass_Vertex = eye;
145    pass_TNormal = tnorm;
146#ifdef _XBOX
147    pass_Color = in_Color.abgr;
148#else
149    pass_Color = in_Color;
150#endif
151
152    out_Position = mul(in_Proj, eye);
153}
154
155[frag.hlsl]
156
157void main(float4 pass_Vertex : TEXCOORD0,
158          float3 pass_TNormal : TEXCOORD1,
159          float4 pass_Color : COLOR,
160          uniform float in_Damage,
161          out float4 out_FragColor : COLOR)
162{
163    float3 in_LightDir = float3(0.3, 0.3, 0.7);
164
165    /* Material properties */
166    float3 specular_reflect = float3(0.8, 0.75, 0.4);
167    float specular_power = 60.0;
168
169    /* World properties */
170    float ambient_mul = 0.5;
171    float3 ambient_color = float3(0.25, 0.2, 0.35);
172    float3 diffuse_color = float3(1.0, 1.0, 0.6);
173    float3 specular_color = float3(1.0, 1.0, 0.6);
174
175    float3 s = normalize(in_LightDir); /* normalize(pass_Vertex - lightpos); */
176    float3 v = normalize(-pass_Vertex.xyz);
177    float3 r = reflect(-s, pass_TNormal);
178
179    float3 ambient = ambient_color;
180    float sdotn = max(dot(s, pass_TNormal), 0.0);
181    float3 diffuse = diffuse_color * sdotn;
182    float3 specular = float3(0.0, 0.0, 0.0);
183    if (sdotn > 0.0)
184        specular = specular_color * specular_reflect
185                 * pow(max(dot(r, v), 0.0), specular_power);
186    float3 light = ambient + diffuse + specular;
187
188    float4 real_color = in_Damage * float4(1.2, 1.2, 1.2, 1.0)
189                      + (1.0 - in_Damage) * pass_Color;
190    out_FragColor = real_color * float4(light, 1.0);
191}
192
Note: See TracBrowser for help on using the repository browser.