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

Last change on this file since 2350 was 2289, checked in by sam, 7 years ago

easymesh: interface the shiny shader with new Light objects.

File size: 5.4 KB
RevLine 
[1386]1
[1928]2[vert.glsl]
[1492]3#version 120
[1386]4
5attribute vec3 in_Vertex;
6attribute vec3 in_Normal;
7attribute vec4 in_Color;
[1589]8
[1386]9uniform mat4 in_ModelView;
[2145]10uniform mat4 in_View;
[1386]11uniform mat4 in_Proj;
12uniform mat3 in_NormalMat;
[1589]13
[2145]14varying vec4 pass_Vertex; /* View space */
[1765]15varying vec3 pass_TNormal;
[1386]16varying vec4 pass_Color;
17
18void main(void)
19{
[2145]20    vec4 vertex = in_ModelView * vec4(in_Vertex, 1.0);
[1765]21    vec3 tnorm = normalize(in_NormalMat * in_Normal);
22
[2145]23    pass_Vertex = vertex;
[1765]24    pass_TNormal = tnorm;
25    pass_Color = in_Color;
26
[2145]27    gl_Position = in_Proj * vertex;
[1765]28}
29
[1928]30[frag.glsl]
[1765]31#version 120
32
33#if defined GL_ES
34precision highp float;
35#endif
36
37uniform float in_Damage;
[2145]38uniform mat4 in_View;
[2148]39uniform mat4 in_Inv_View;
[1765]40
[2289]41uniform vec4 u_Lights[8 * 2];
42
[2145]43varying vec4 pass_Vertex; /* View space */
[1765]44varying vec3 pass_TNormal;
45varying vec4 pass_Color;
46
[2289]47#if 0
[2148]48//Cube Light
49vec4 in_Light3_Pos = vec4(-10.0, 10.0, 5.0, 1.0);
[2149]50vec3 in_Light3_Size_Inner = vec3(3.0, 1.0, 3.0);
51vec3 in_Light3_Size_Outer = vec3(15.0, 15.0, 15.0);
[2148]52vec3 in_Light3_diffuse = vec3(0.4, 1.0, 0.4);
[2289]53#endif
[2148]54
[1769]55void main(void)
56{
[1386]57    /* Material properties */
58    vec3 specular_reflect = vec3(0.8, 0.75, 0.4);
[1441]59    float specular_power = 60.0;
[1386]60
61    /* World properties */
[2289]62    vec3 ambient = vec3(0.1, 0.1, 0.1);
[2145]63    vec3 specular = vec3(0.0, 0.0, 0.0);
64    vec3 diffuse = vec3(0.0, 0.0, 0.0);
[1386]65
[2289]66    /* Light precalculations */
67    vec3 v = normalize(-pass_Vertex.xyz);
[2143]68
[2289]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;
[2232]75
[2289]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        }
[2143]88
[2289]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    }
[2232]95
[2289]96#if 0
[2148]97    //Light calculation for cube light
[2289]98    vec3 specular_color = vec3(1.0, 1.0, 0.6);
[2232]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;
[2143]102
[2232]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));
[2289]104    float light_radius_mod = min(light_radius.x, min(light_radius.y, light_radius.z));
[2148]105
[2232]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    }
[2148]117    diffuse += in_Light3_diffuse * min(sdotn, light_radius_mod);
[2232]118    //----------
[2289]119#endif
[2148]120
[1386]121    vec3 light = ambient + diffuse + specular;
122
[2289]123    vec4 real_color = mix(pass_Color, vec4(1.2, 1.2, 1.2, 1.0), in_Damage);
[1765]124    gl_FragColor = real_color * vec4(light, 1.0);
[1386]125}
126
[1928]127[vert.hlsl]
[1386]128
129void main(float3 in_Vertex : POSITION,
130          float3 in_Normal : NORMAL,
131          float4 in_Color : COLOR,
132          uniform float4x4 in_ModelView,
[2145]133          uniform float4x4 in_Model,
[1386]134          uniform float4x4 in_Proj,
135          uniform float3x3 in_NormalMat,
[2145]136          out float4 pass_Vertex : TEXCOORD0,
[1769]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
[2145]144    pass_Vertex = eye;
[1769]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
[1928]155[frag.hlsl]
[1769]156
[2145]157void main(float4 pass_Vertex : TEXCOORD0,
[1769]158          float3 pass_TNormal : TEXCOORD1,
159          float4 pass_Color : COLOR,
[1386]160          uniform float in_Damage,
[1769]161          out float4 out_FragColor : COLOR)
[1386]162{
[1769]163    float3 in_LightDir = float3(0.3, 0.3, 0.7);
[1615]164
[1769]165    /* Material properties */
[1386]166    float3 specular_reflect = float3(0.8, 0.75, 0.4);
[1441]167    float specular_power = 60.0;
[1769]168
169    /* World properties */
[1441]170    float ambient_mul = 0.5;
[1386]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);
[1769]174
[2145]175    float3 s = normalize(in_LightDir); /* normalize(pass_Vertex - lightpos); */
176    float3 v = normalize(-pass_Vertex.xyz);
[1769]177    float3 r = reflect(-s, pass_TNormal);
178
[1386]179    float3 ambient = ambient_color;
[1769]180    float sdotn = max(dot(s, pass_TNormal), 0.0);
[1386]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
[1769]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);
[1386]191}
192
Note: See TracBrowser for help on using the repository browser.