|
- #ifndef DIRECTIONAL_LIGHT_COUNT
- #define DIRECTIONAL_LIGHT_COUNT 0
- #endif
- #ifndef SPOT_LIGHT_COUNT
- #define SPOT_LIGHT_COUNT 0
- #endif
- #ifndef POINT_LIGHT_COUNT
- #define POINT_LIGHT_COUNT 0
- #endif
- #if (DIRECTIONAL_LIGHT_COUNT > 0) || (POINT_LIGHT_COUNT > 0) || (SPOT_LIGHT_COUNT > 0) || defined(OUTLINE) || defined(OUTLINE_WITH_Z) || defined(RIM_LIGHTING)
- #define LIGHTING
- #endif
-
- #ifndef TERRAIN_LAYER_COUNT
- #define TERRAIN_LAYER_COUNT 0
- #endif
-
- ///////////////////////////////////////////////////////////
- // Atributes
- attribute vec4 a_position;
-
- #if defined(SKINNING)
- attribute vec4 a_blendWeights;
- attribute vec4 a_blendIndices;
- #endif
-
- attribute vec2 a_texCoord;
-
- #if defined(LIGHTMAP)
- attribute vec2 a_texCoord1;
- #endif
-
- #if defined(LIGHTING)
- attribute vec3 a_normal;
-
- #if defined(BUMPED)
- attribute vec3 a_tangent;
- attribute vec3 a_binormal;
- #endif
-
- #endif
- attribute vec4 a_color;
-
-
- ///////////////////////////////////////////////////////////
- // Uniforms
- uniform mat4 u_worldViewProjectionMatrix;
- uniform mat4 u_projectionMatrix;
- uniform mat4 u_worldMatrix;
-
- #if defined(SKINNING)
- uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
- #endif
-
- #if defined(LIGHTING)
- uniform mat4 u_inverseTransposeWorldViewMatrix;
-
- #if defined(SPECULAR) || (POINT_LIGHT_COUNT > 0) || (SPOT_LIGHT_COUNT > 0)
- uniform mat4 u_worldViewMatrix;
- #endif
-
- #if defined(BUMPED) && (DIRECTIONAL_LIGHT_COUNT > 0)
- uniform vec3 u_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT];
- #endif
-
- #if (POINT_LIGHT_COUNT > 0)
- uniform vec3 u_pointLightPosition[POINT_LIGHT_COUNT];
- #endif
-
- #if (SPOT_LIGHT_COUNT > 0)
- uniform vec3 u_spotLightPosition[SPOT_LIGHT_COUNT];
- #if defined(BUMPED)
- uniform vec3 u_spotLightDirection[SPOT_LIGHT_COUNT];
- #endif
- #endif
-
- #if defined(SPECULAR) || defined(RIM_LIGHTING)
- uniform vec3 u_cameraPosition;
- #endif
-
- #endif
-
- #if defined(TEXTURE_REPEAT)
- uniform vec2 u_textureRepeat;
- #endif
-
- #if defined(TEXTURE_OFFSET)
- uniform vec2 u_textureOffset;
- #endif
-
- ///////////////////////////////////////////////////////////
- // Varyings
- varying vec2 v_texCoord;
-
- varying vec4 v_color;
-
- #if defined(LIGHTMAP)
- varying vec2 v_texCoord1;
- #endif
-
- #if defined(LIGHTING)
-
- #if !defined(BUMPED)
- varying vec3 v_normalVector;
- #endif
-
- #if defined(BUMPED) && (DIRECTIONAL_LIGHT_COUNT > 0)
- varying vec3 v_directionalLightDirection[DIRECTIONAL_LIGHT_COUNT];
- #endif
-
- #if (POINT_LIGHT_COUNT > 0)
- varying vec3 v_vertexToPointLightDirection[POINT_LIGHT_COUNT];
- #endif
-
- #if (SPOT_LIGHT_COUNT > 0)
- varying vec3 v_vertexToSpotLightDirection[SPOT_LIGHT_COUNT];
- #if defined(BUMPED)
- varying vec3 v_spotLightDirection[SPOT_LIGHT_COUNT];
- #endif
- #endif
-
- #if defined(SPECULAR)
- varying vec3 v_cameraDirection;
- #endif
-
- #include "lighting.vert"
-
- #endif
-
- #if defined(SKINNING)
- #include "skinning.vert"
- #else
- #include "skinning-none.vert"
- #endif
-
- #if defined(OUTLINE) || defined(OUTLINE_WITH_Z)
- // 外描边的描边粗细程度
- uniform float u_outlineSize;
- #endif
-
- #if defined(RIM_LIGHTING)
- varying vec3 v_worldNormal;
- varying vec4 v_worldPosition;
- #endif
-
- #if defined(NO_Z_FIGHTING)
- // 深度偏移值
- uniform float u_depthOffset;
- #endif
-
- #if TERRAIN_LAYER_COUNT >= 1
- varying vec2 v_texCoordTerrainLayer1;
- uniform vec2 v_texCoordTerrainRepeat1;
- #if TERRAIN_LAYER_COUNT >= 2
- varying vec2 v_texCoordTerrainLayer2;
- uniform vec2 v_texCoordTerrainRepeat2;
- #if TERRAIN_LAYER_COUNT >= 3
- varying vec2 v_texCoordTerrainLayer3;
- uniform vec2 v_texCoordTerrainRepeat3;
- #if TERRAIN_LAYER_COUNT >= 4
- varying vec2 v_texCoordTerrainLayer4;
- uniform vec2 v_texCoordTerrainRepeat4;
- #endif
- #endif
- #endif
- #endif
-
-
- varying float v_fogFactor; //fog factor
- //fog parameter
- uniform vec4 u_fogparam; // .x, .y, .z, .w stand for fog density, fog start, fog end, fog type. fog type 0, no fog, fog type 1 linear fog, fog type 2 exp fog, fog type 3 exp2 fog
-
- vec2 TransformViewToProjection (vec2 v)
- {
- return vec2(v.x*u_projectionMatrix[0][0], v.y*u_projectionMatrix[1][1]);
- }
-
- void main()
- {
- vec4 position = getPosition();
- gl_Position = u_worldViewProjectionMatrix * position;
-
- #if defined(LIGHTING)
- vec3 normal = getNormal();
- // Transform the normal, tangent and binormals to view space.
- mat3 inverseTransposeWorldViewMatrix = mat3(u_inverseTransposeWorldViewMatrix[0].xyz, u_inverseTransposeWorldViewMatrix[1].xyz, u_inverseTransposeWorldViewMatrix[2].xyz);
- vec3 normalVector = normalize(inverseTransposeWorldViewMatrix * normal);
- #if defined(BUMPED)
- vec3 tangent = getTangent();
- vec3 binormal = getBinormal();
- vec3 tangentVector = normalize(inverseTransposeWorldViewMatrix * tangent);
- vec3 binormalVector = normalize(inverseTransposeWorldViewMatrix * binormal);
- mat3 tangentSpaceTransformMatrix = mat3(tangentVector.x, binormalVector.x, normalVector.x, tangentVector.y, binormalVector.y, normalVector.y, tangentVector.z, binormalVector.z, normalVector.z);
- applyLight(position, tangentSpaceTransformMatrix);
- #else
- v_normalVector = normalVector;
- applyLight(position);
- #endif
- #endif
-
- // 计算外描边,让顶点向法线外移动几个像素
- #if defined(OUTLINE)
- vec2 offset = normalize(TransformViewToProjection(normalVector.xy));
- gl_Position.xy += offset * u_outlineSize;
- //gl_Position.z += u_outlineSize;
- #elif defined(OUTLINE_WITH_Z)
- vec2 offset = normalize(TransformViewToProjection(normalVector.xy));
- gl_Position.xy += offset * gl_Position.z * u_outlineSize;
- //gl_Position.z += gl_Position.z * u_outlineSize;
- #endif
-
- // 计算内描边,计算法线与摄像机的夹角,然后根据夹角与0度的差异计算颜色
- #if defined(RIM_LIGHTING)
- v_worldNormal = (u_worldMatrix * vec4(normal.xyz , 1)).xyz;
- v_worldPosition = u_worldMatrix * position;
- #endif
-
- #if defined(NO_Z_FIGHTING)
- gl_Position.z -= u_depthOffset;
- #endif
-
- v_texCoord = a_texCoord;
-
- #if defined(TEXTURE_REPEAT)
- v_texCoord *= u_textureRepeat;
- #endif
-
- #if defined(TEXTURE_OFFSET)
- v_texCoord += u_textureOffset;
- #endif
-
- #if defined(LIGHTMAP)
- v_texCoord1 = a_texCoord1;
- #endif
-
- #if defined(VERTEX_COLOR)
- v_color = a_color;
- #endif
-
-
- #if TERRAIN_LAYER_COUNT >= 1
- v_texCoordTerrainLayer1 = a_texCoord * v_texCoordTerrainRepeat1;
- #if TERRAIN_LAYER_COUNT >= 2
- v_texCoordTerrainLayer2 = a_texCoord * v_texCoordTerrainRepeat2;
- #if TERRAIN_LAYER_COUNT >= 3
- v_texCoordTerrainLayer3 = a_texCoord * v_texCoordTerrainRepeat3;
- #if TERRAIN_LAYER_COUNT >= 4
- v_texCoordTerrainLayer4 = a_texCoord * v_texCoordTerrainRepeat4;
- #endif
- #endif
- #endif
- #endif
-
-
- #if defined(FOG)
- // linear fog
- #if FOG == 1
- float z = gl_Position.z;//(u_worldViewMatrix * position).z;
- v_fogFactor = (u_fogparam.z - z) / (u_fogparam.z - u_fogparam.y);
- v_fogFactor = clamp(v_fogFactor, 0.0, 1.0);
- // exp fog
- #elif FOG == 2
- float z = gl_Position.z;//(u_worldViewMatrix * position).z;
- const float LOG2 = 1.442695;
- v_fogFactor = exp2( -u_fogparam.x *
- z *
- LOG2 );
- v_fogFactor = clamp(v_fogFactor, 0.0, 1.0);
- // exp2 fog
- #elif FOG == 3
- float z = gl_Position.z;//(u_worldViewMatrix * position).z;
- const float LOG2 = 1.442695;
- v_fogFactor = exp2( -u_fogparam.x *
- u_fogparam.x *
- z *
- z *
- LOG2 );
- v_fogFactor = clamp(v_fogFactor, 0.0, 1.0);
- #endif
- #endif
- }
|