1uniform mat4 g_WorldViewProjectionMatrix; 2uniform mat4 g_WorldViewMatrix; 3uniform mat3 g_NormalMatrix; 4uniform mat4 g_ViewMatrix; 5 6uniform vec4 g_LightColor; 7uniform vec4 g_LightPosition; 8uniform vec4 g_AmbientLightColor; 9 10uniform float m_Shininess; 11 12attribute vec3 inPosition; 13attribute vec3 inNormal; 14attribute vec2 inTexCoord; 15attribute vec4 inTangent; 16 17varying vec3 vNormal; 18varying vec2 texCoord; 19varying vec3 vPosition; 20varying vec3 vnPosition; 21varying vec3 vViewDir; 22varying vec3 vnViewDir; 23varying vec4 vLightDir; 24varying vec4 vnLightDir; 25 26varying vec3 lightVec; 27 28varying vec4 AmbientSum; 29varying vec4 DiffuseSum; 30varying vec4 SpecularSum; 31 32#ifdef TRI_PLANAR_MAPPING 33 varying vec4 wVertex; 34 varying vec3 wNormal; 35#endif 36 37// JME3 lights in world space 38void lightComputeDir(in vec3 worldPos, in vec4 color, in vec4 position, out vec4 lightDir){ 39 float posLight = step(0.5, color.w); 40 vec3 tempVec = position.xyz * sign(posLight - 0.5) - (worldPos * posLight); 41 lightVec.xyz = tempVec; 42 float dist = length(tempVec); 43 lightDir.w = clamp(1.0 - position.w * dist * posLight, 0.0, 1.0); 44 lightDir.xyz = tempVec / vec3(dist); 45} 46 47 48void main(){ 49 vec4 pos = vec4(inPosition, 1.0); 50 gl_Position = g_WorldViewProjectionMatrix * pos; 51 #ifdef TERRAIN_GRID 52 texCoord = inTexCoord * 2.0; 53 #else 54 texCoord = inTexCoord; 55 #endif 56 57 vec3 wvPosition = (g_WorldViewMatrix * pos).xyz; 58 vec3 wvNormal = normalize(g_NormalMatrix * inNormal); 59 vec3 viewDir = normalize(-wvPosition); 60 61 vec4 wvLightPos = (g_ViewMatrix * vec4(g_LightPosition.xyz,clamp(g_LightColor.w,0.0,1.0))); 62 wvLightPos.w = g_LightPosition.w; 63 vec4 lightColor = g_LightColor; 64 65 //-------------------------- 66 // specific to normal maps: 67 //-------------------------- 68 #if defined(NORMALMAP) || defined(NORMALMAP_1) || defined(NORMALMAP_2) || defined(NORMALMAP_3) || defined(NORMALMAP_4) || defined(NORMALMAP_5) || defined(NORMALMAP_6) || defined(NORMALMAP_7) || defined(NORMALMAP_8) || defined(NORMALMAP_9) || defined(NORMALMAP_10) || defined(NORMALMAP_11) 69 vec3 wvTangent = normalize(g_NormalMatrix * inTangent.xyz); 70 vec3 wvBinormal = cross(wvNormal, wvTangent); 71 72 mat3 tbnMat = mat3(wvTangent, wvBinormal * -inTangent.w,wvNormal); 73 74 vPosition = wvPosition * tbnMat; 75 vViewDir = viewDir * tbnMat; 76 lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir); 77 vLightDir.xyz = (vLightDir.xyz * tbnMat).xyz; 78 #else 79 80 //------------------------- 81 // general to all lighting 82 //------------------------- 83 vNormal = wvNormal; 84 85 vPosition = wvPosition; 86 vViewDir = viewDir; 87 88 lightComputeDir(wvPosition, lightColor, wvLightPos, vLightDir); 89 90 #endif 91 92 //computing spot direction in view space and unpacking spotlight cos 93 // spotVec=(g_ViewMatrix *vec4(g_LightDirection.xyz,0.0) ); 94 // spotVec.w=floor(g_LightDirection.w)*0.001; 95 // lightVec.w = fract(g_LightDirection.w); 96 97 AmbientSum = vec4(0.2, 0.2, 0.2, 1.0) * g_AmbientLightColor; // Default: ambient color is dark gray 98 DiffuseSum = lightColor; 99 SpecularSum = lightColor; 100 101 102#ifdef TRI_PLANAR_MAPPING 103 wVertex = vec4(inPosition,0.0); 104 wNormal = inNormal; 105#endif 106 107}