• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#define ATTENUATION
2//#define HQ_ATTENUATION
3
4uniform mat4 g_WorldViewProjectionMatrix;
5uniform mat4 g_WorldViewMatrix;
6uniform mat3 g_NormalMatrix;
7uniform mat4 g_ViewMatrix;
8
9uniform vec4 m_Ambient;
10uniform vec4 m_Diffuse;
11uniform vec4 m_Specular;
12uniform float m_Shininess;
13
14uniform vec4 g_LightColor;
15uniform vec4 g_LightPosition;
16uniform vec4 g_AmbientLightColor;
17
18varying vec2 texCoord;
19#ifdef SEPARATE_TEXCOORD
20  varying vec2 texCoord2;
21  attribute vec2 inTexCoord2;
22#endif
23
24varying vec3 AmbientSum;
25varying vec4 DiffuseSum;
26varying vec3 SpecularSum;
27
28attribute vec3 inPosition;
29attribute vec2 inTexCoord;
30attribute vec3 inNormal;
31
32varying vec3 lightVec;
33//varying vec4 spotVec;
34
35#ifdef VERTEX_COLOR
36  attribute vec4 inColor;
37#endif
38
39#ifndef VERTEX_LIGHTING
40  attribute vec4 inTangent;
41
42  #ifndef NORMALMAP
43    varying vec3 vNormal;
44  #endif
45  //varying vec3 vPosition;
46  varying vec3 vViewDir;
47  varying vec4 vLightDir;
48#else
49  varying vec2 vertexLightValues;
50  uniform vec4 g_LightDirection;
51#endif
52
53#ifdef USE_REFLECTION
54    uniform vec3 g_CameraPosition;
55    uniform mat4 g_WorldMatrix;
56
57    uniform vec3 m_FresnelParams;
58    varying vec4 refVec;
59
60
61    /**
62     * Input:
63     * attribute inPosition
64     * attribute inNormal
65     * uniform g_WorldMatrix
66     * uniform g_CameraPosition
67     *
68     * Output:
69     * varying refVec
70     */
71    void computeRef(){
72        vec3 worldPos = (g_WorldMatrix * vec4(inPosition,1.0)).xyz;
73
74        vec3 I = normalize( g_CameraPosition - worldPos  ).xyz;
75        vec3 N = normalize( (g_WorldMatrix * vec4(inNormal, 0.0)).xyz );
76
77        refVec.xyz = reflect(I, N);
78        refVec.w   = m_FresnelParams.x + m_FresnelParams.y * pow(1.0 + dot(I, N), m_FresnelParams.z);
79    }
80#endif
81
82// JME3 lights in world space
83void lightComputeDir(in vec3 worldPos, in vec4 color, in vec4 position, out vec4 lightDir){
84    float posLight = step(0.5, color.w);
85    vec3 tempVec = position.xyz * sign(posLight - 0.5) - (worldPos * posLight);
86    lightVec = tempVec;
87    #ifdef ATTENUATION
88     float dist = length(tempVec);
89     lightDir.w = clamp(1.0 - position.w * dist * posLight, 0.0, 1.0);
90     lightDir.xyz = tempVec / vec3(dist);
91    #else
92     lightDir = vec4(normalize(tempVec), 1.0);
93    #endif
94}
95
96#ifdef VERTEX_LIGHTING
97  float lightComputeDiffuse(in vec3 norm, in vec3 lightdir){
98      return max(0.0, dot(norm, lightdir));
99  }
100
101  float lightComputeSpecular(in vec3 norm, in vec3 viewdir, in vec3 lightdir, in float shiny){
102      if (shiny <= 1.0){
103          return 0.0;
104      }
105      #ifndef LOW_QUALITY
106        vec3 H = (viewdir + lightdir) * vec3(0.5);
107        return pow(max(dot(H, norm), 0.0), shiny);
108      #else
109        return 0.0;
110      #endif
111  }
112
113vec2 computeLighting(in vec3 wvPos, in vec3 wvNorm, in vec3 wvViewDir, in vec4 wvLightPos){
114     vec4 lightDir;
115     lightComputeDir(wvPos, g_LightColor, wvLightPos, lightDir);
116     float spotFallOff = 1.0;
117     if(g_LightDirection.w != 0.0){
118          vec3 L=normalize(lightVec.xyz);
119          vec3 spotdir = normalize(g_LightDirection.xyz);
120          float curAngleCos = dot(-L, spotdir);
121          float innerAngleCos = floor(g_LightDirection.w) * 0.001;
122          float outerAngleCos = fract(g_LightDirection.w);
123          float innerMinusOuter = innerAngleCos - outerAngleCos;
124          spotFallOff = clamp((curAngleCos - outerAngleCos) / innerMinusOuter, 0.0, 1.0);
125     }
126     float diffuseFactor = lightComputeDiffuse(wvNorm, lightDir.xyz);
127     float specularFactor = lightComputeSpecular(wvNorm, wvViewDir, lightDir.xyz, m_Shininess);
128     //specularFactor *= step(0.01, diffuseFactor);
129     return vec2(diffuseFactor, specularFactor) * vec2(lightDir.w)*spotFallOff;
130  }
131#endif
132
133void main(){
134   vec4 pos = vec4(inPosition, 1.0);
135   gl_Position = g_WorldViewProjectionMatrix * pos;
136   texCoord = inTexCoord;
137   #ifdef SEPARATE_TEXCOORD
138      texCoord2 = inTexCoord2;
139   #endif
140
141   vec3 wvPosition = (g_WorldViewMatrix * pos).xyz;
142   vec3 wvNormal  = normalize(g_NormalMatrix * inNormal);
143   vec3 viewDir = normalize(-wvPosition);
144
145       //vec4 lightColor = g_LightColor[gl_InstanceID];
146       //vec4 lightPos   = g_LightPosition[gl_InstanceID];
147       //vec4 wvLightPos = (g_ViewMatrix * vec4(lightPos.xyz, lightColor.w));
148       //wvLightPos.w = lightPos.w;
149
150   vec4 wvLightPos = (g_ViewMatrix * vec4(g_LightPosition.xyz,clamp(g_LightColor.w,0.0,1.0)));
151   wvLightPos.w = g_LightPosition.w;
152   vec4 lightColor = g_LightColor;
153
154   #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
155     vec3 wvTangent = normalize(g_NormalMatrix * inTangent.xyz);
156     vec3 wvBinormal = cross(wvNormal, wvTangent);
157
158     mat3 tbnMat = mat3(wvTangent, wvBinormal * -inTangent.w,wvNormal);
159
160     //vPosition = wvPosition * tbnMat;
161     //vViewDir  = viewDir * tbnMat;
162     vViewDir  = -wvPosition * tbnMat;
163     lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir);
164     vLightDir.xyz = (vLightDir.xyz * tbnMat).xyz;
165   #elif !defined(VERTEX_LIGHTING)
166     vNormal = wvNormal;
167
168     //vPosition = wvPosition;
169     vViewDir = viewDir;
170
171     lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir);
172
173     #ifdef V_TANGENT
174        vNormal = normalize(g_NormalMatrix * inTangent.xyz);
175        vNormal = -cross(cross(vLightDir.xyz, vNormal), vNormal);
176     #endif
177   #endif
178
179   //computing spot direction in view space and unpacking spotlight cos
180//   spotVec = (g_ViewMatrix * vec4(g_LightDirection.xyz, 0.0) );
181//   spotVec.w  = floor(g_LightDirection.w) * 0.001;
182//   lightVec.w = fract(g_LightDirection.w);
183
184   lightColor.w = 1.0;
185   #ifdef MATERIAL_COLORS
186      AmbientSum  = (m_Ambient  * g_AmbientLightColor).rgb;
187      DiffuseSum  =  m_Diffuse  * lightColor;
188      SpecularSum = (m_Specular * lightColor).rgb;
189    #else
190      AmbientSum  = vec3(0.2, 0.2, 0.2) * g_AmbientLightColor.rgb; // Default: ambient color is dark gray
191      DiffuseSum  = lightColor;
192      SpecularSum = vec3(0.0);
193    #endif
194
195    #ifdef VERTEX_COLOR
196      AmbientSum *= inColor.rgb;
197      DiffuseSum *= inColor;
198    #endif
199
200    #ifdef VERTEX_LIGHTING
201       vertexLightValues = computeLighting(wvPosition, wvNormal, viewDir, wvLightPos);
202    #endif
203
204    #ifdef USE_REFLECTION
205        computeRef();
206    #endif
207}