axmol/core/renderer/shaders/3D_positionNormalTexture.vert

344 lines
12 KiB
GLSL
Raw Normal View History

2019-07-04 17:47:24 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
https://axis-project.github.io/
2019-07-04 17:47:24 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2014-08-21 10:33:16 +08:00
const char* CC3D_positionNormalTexture_vert = R"(
2014-09-22 18:08:58 +08:00
#ifdef USE_NORMAL_MAPPING
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif
#endif
#if (MAX_POINT_LIGHT_NUM > 0)
2014-09-22 18:08:58 +08:00
uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM];
#endif
#if (MAX_SPOT_LIGHT_NUM > 0)
2014-09-22 18:08:58 +08:00
uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM];
#ifdef USE_NORMAL_MAPPING
uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
#endif
#endif
2014-08-21 10:33:16 +08:00
attribute vec4 a_position;
attribute vec2 a_texCoord;
2014-08-21 11:33:00 +08:00
attribute vec3 a_normal;
#ifdef USE_NORMAL_MAPPING
2015-11-27 13:15:33 +08:00
attribute vec3 a_tangent;
attribute vec3 a_binormal;
#endif
2014-08-21 10:33:16 +08:00
varying vec2 TextureCoordOut;
2014-08-27 16:26:01 +08:00
#ifdef USE_NORMAL_MAPPING
#if MAX_DIRECTIONAL_LIGHT_NUM
2015-11-27 13:15:33 +08:00
varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif
#endif
#if MAX_POINT_LIGHT_NUM
2014-09-22 18:08:58 +08:00
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
#endif
#if MAX_SPOT_LIGHT_NUM
2014-09-22 18:08:58 +08:00
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
#ifdef USE_NORMAL_MAPPING
2015-11-27 13:15:33 +08:00
varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
#endif
#endif
2015-11-27 13:15:33 +08:00
#ifndef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
2014-08-27 16:26:01 +08:00
varying vec3 v_normal;
#endif
#endif
2014-08-21 10:33:16 +08:00
uniform mat4 u_MVPMatrix;
uniform mat4 u_MVMatrix;
uniform mat4 u_PMatrix;
uniform mat3 u_NormalMatrix;
2014-08-21 10:33:16 +08:00
void main(void)
{
vec4 ePosition = u_MVMatrix * a_position;
#ifdef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
vec3 eTangent = normalize(u_NormalMatrix * a_tangent);
vec3 eBinormal = normalize(u_NormalMatrix * a_binormal);
vec3 eNormal = normalize(u_NormalMatrix * a_normal);
#endif
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
{
v_dirLightDirection[i].x = dot(eTangent, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].y = dot(eBinormal, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].z = dot(eNormal, u_DirLightSourceDirection[i]);
}
#endif
2014-08-27 16:26:01 +08:00
#if (MAX_POINT_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
vec3 pointLightDir = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
v_vertexToPointLightDirection[i].x = dot(eTangent, pointLightDir);
v_vertexToPointLightDirection[i].y = dot(eBinormal, pointLightDir);
v_vertexToPointLightDirection[i].z = dot(eNormal, pointLightDir);
}
#endif
2015-11-27 13:15:33 +08:00
#if (MAX_SPOT_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
vec3 spotLightDir = u_SpotLightSourcePosition[i] - ePosition.xyz;
v_vertexToSpotLightDirection[i].x = dot(eTangent, spotLightDir);
v_vertexToSpotLightDirection[i].y = dot(eBinormal, spotLightDir);
v_vertexToSpotLightDirection[i].z = dot(eNormal, spotLightDir);
v_spotLightDirection[i].x = dot(eTangent, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].y = dot(eBinormal, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].z = dot(eNormal, u_SpotLightSourceDirection[i]);
}
#endif
#else
#if (MAX_POINT_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
}
#endif
2015-11-27 13:15:33 +08:00
#if (MAX_SPOT_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
}
#endif
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
v_normal = u_NormalMatrix * a_normal;
#endif
#endif
2014-08-27 16:26:01 +08:00
2014-08-21 10:33:16 +08:00
TextureCoordOut = a_texCoord;
TextureCoordOut.y = 1.0 - TextureCoordOut.y;
gl_Position = u_PMatrix * ePosition;
2014-08-21 10:33:16 +08:00
}
)";
2014-08-21 10:33:16 +08:00
const char* CC3D_skinPositionNormalTexture_vert = R"(
2014-09-22 18:08:58 +08:00
#ifdef USE_NORMAL_MAPPING
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
uniform vec3 u_DirLightSourceDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif
#endif
#if (MAX_POINT_LIGHT_NUM > 0)
2014-09-22 18:08:58 +08:00
uniform vec3 u_PointLightSourcePosition[MAX_POINT_LIGHT_NUM];
#endif
#if (MAX_SPOT_LIGHT_NUM > 0)
2014-09-22 18:08:58 +08:00
uniform vec3 u_SpotLightSourcePosition[MAX_SPOT_LIGHT_NUM];
#ifdef USE_NORMAL_MAPPING
uniform vec3 u_SpotLightSourceDirection[MAX_SPOT_LIGHT_NUM];
#endif
#endif
2014-09-22 18:08:58 +08:00
2014-08-21 10:33:16 +08:00
attribute vec3 a_position;
attribute vec4 a_blendWeight;
attribute vec4 a_blendIndex;
attribute vec2 a_texCoord;
2014-08-21 11:33:00 +08:00
attribute vec3 a_normal;
#ifdef USE_NORMAL_MAPPING
2015-11-27 13:15:33 +08:00
attribute vec3 a_tangent;
attribute vec3 a_binormal;
#endif
2014-08-21 11:33:00 +08:00
2014-08-21 10:33:16 +08:00
const int SKINNING_JOINT_COUNT = 60;
// Uniforms
uniform vec4 u_matrixPalette[SKINNING_JOINT_COUNT * 3];
uniform mat4 u_MVMatrix;
uniform mat3 u_NormalMatrix;
uniform mat4 u_PMatrix;
2014-08-21 10:33:16 +08:00
// Varyings
varying vec2 TextureCoordOut;
2014-08-27 16:26:01 +08:00
#ifdef USE_NORMAL_MAPPING
#if MAX_DIRECTIONAL_LIGHT_NUM
2015-11-27 13:15:33 +08:00
varying vec3 v_dirLightDirection[MAX_DIRECTIONAL_LIGHT_NUM];
#endif
#endif
#if MAX_POINT_LIGHT_NUM
2014-09-22 18:08:58 +08:00
varying vec3 v_vertexToPointLightDirection[MAX_POINT_LIGHT_NUM];
#endif
#if MAX_SPOT_LIGHT_NUM
2014-09-22 18:08:58 +08:00
varying vec3 v_vertexToSpotLightDirection[MAX_SPOT_LIGHT_NUM];
#ifdef USE_NORMAL_MAPPING
2015-11-27 13:15:33 +08:00
varying vec3 v_spotLightDirection[MAX_SPOT_LIGHT_NUM];
#endif
#endif
2015-11-27 13:15:33 +08:00
#ifndef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
2014-08-27 16:26:01 +08:00
varying vec3 v_normal;
#endif
#endif
2014-08-21 10:33:16 +08:00
2015-11-27 13:15:33 +08:00
void getPositionAndNormal(out vec4 position, out vec3 normal, out vec3 tangent, out vec3 binormal)
2014-08-21 10:33:16 +08:00
{
float blendWeight = a_blendWeight[0];
int matrixIndex = int (a_blendIndex[0]) * 3;
vec4 matrixPalette1 = u_matrixPalette[matrixIndex] * blendWeight;
vec4 matrixPalette2 = u_matrixPalette[matrixIndex + 1] * blendWeight;
vec4 matrixPalette3 = u_matrixPalette[matrixIndex + 2] * blendWeight;
2014-08-21 10:33:16 +08:00
blendWeight = a_blendWeight[1];
if (blendWeight > 0.0)
{
matrixIndex = int(a_blendIndex[1]) * 3;
matrixPalette1 += u_matrixPalette[matrixIndex] * blendWeight;
matrixPalette2 += u_matrixPalette[matrixIndex + 1] * blendWeight;
matrixPalette3 += u_matrixPalette[matrixIndex + 2] * blendWeight;
2014-10-20 13:41:26 +08:00
blendWeight = a_blendWeight[2];
if (blendWeight > 0.0)
{
matrixIndex = int(a_blendIndex[2]) * 3;
matrixPalette1 += u_matrixPalette[matrixIndex] * blendWeight;
matrixPalette2 += u_matrixPalette[matrixIndex + 1] * blendWeight;
matrixPalette3 += u_matrixPalette[matrixIndex + 2] * blendWeight;
2014-10-20 13:41:26 +08:00
blendWeight = a_blendWeight[3];
if (blendWeight > 0.0)
{
matrixIndex = int(a_blendIndex[3]) * 3;
matrixPalette1 += u_matrixPalette[matrixIndex] * blendWeight;
matrixPalette2 += u_matrixPalette[matrixIndex + 1] * blendWeight;
matrixPalette3 += u_matrixPalette[matrixIndex + 2] * blendWeight;
}
}
2014-08-21 10:33:16 +08:00
}
2014-08-21 11:33:00 +08:00
vec4 p = vec4(a_position, 1.0);
position.x = dot(p, matrixPalette1);
position.y = dot(p, matrixPalette2);
position.z = dot(p, matrixPalette3);
position.w = p.w;
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
2014-08-27 19:12:49 +08:00
vec4 n = vec4(a_normal, 0.0);
2014-08-21 11:33:00 +08:00
normal.x = dot(n, matrixPalette1);
normal.y = dot(n, matrixPalette2);
normal.z = dot(n, matrixPalette3);
#ifdef USE_NORMAL_MAPPING
2015-11-27 13:15:33 +08:00
vec4 t = vec4(a_tangent, 0.0);
tangent.x = dot(t, matrixPalette1);
tangent.y = dot(t, matrixPalette2);
tangent.z = dot(t, matrixPalette3);
vec4 b = vec4(a_binormal, 0.0);
binormal.x = dot(b, matrixPalette1);
binormal.y = dot(b, matrixPalette2);
binormal.z = dot(b, matrixPalette3);
#endif
#endif
2014-08-21 10:33:16 +08:00
}
void main()
{
2014-08-21 11:33:00 +08:00
vec4 position;
vec3 normal;
2015-11-27 13:15:33 +08:00
vec3 tangent;
vec3 binormal;
getPositionAndNormal(position, normal, tangent, binormal);
vec4 ePosition = u_MVMatrix * position;
2014-08-27 16:26:01 +08:00
#ifdef USE_NORMAL_MAPPING
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
vec3 eTangent = normalize(u_NormalMatrix * tangent);
vec3 eBinormal = normalize(u_NormalMatrix * binormal);
vec3 eNormal = normalize(u_NormalMatrix * normal);
#endif
2014-08-27 16:26:01 +08:00
#if (MAX_DIRECTIONAL_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
for (int i = 0; i < MAX_DIRECTIONAL_LIGHT_NUM; ++i)
{
v_dirLightDirection[i].x = dot(eTangent, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].y = dot(eBinormal, u_DirLightSourceDirection[i]);
v_dirLightDirection[i].z = dot(eNormal, u_DirLightSourceDirection[i]);
}
#endif
2015-11-27 13:15:33 +08:00
#if (MAX_POINT_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
vec3 pointLightDir = u_PointLightSourcePosition[i].xyz - ePosition.xyz;
v_vertexToPointLightDirection[i].x = dot(eTangent, pointLightDir);
v_vertexToPointLightDirection[i].y = dot(eBinormal, pointLightDir);
v_vertexToPointLightDirection[i].z = dot(eNormal, pointLightDir);
}
#endif
2015-11-27 13:15:33 +08:00
#if (MAX_SPOT_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
vec3 spotLightDir = u_SpotLightSourcePosition[i] - ePosition.xyz;
v_vertexToSpotLightDirection[i].x = dot(eTangent, spotLightDir);
v_vertexToSpotLightDirection[i].y = dot(eBinormal, spotLightDir);
v_vertexToSpotLightDirection[i].z = dot(eNormal, spotLightDir);
v_spotLightDirection[i].x = dot(eTangent, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].y = dot(eBinormal, u_SpotLightSourceDirection[i]);
v_spotLightDirection[i].z = dot(eNormal, u_SpotLightSourceDirection[i]);
}
#endif
#else
#if (MAX_POINT_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
for (int i = 0; i < MAX_POINT_LIGHT_NUM; ++i)
{
v_vertexToPointLightDirection[i] = u_PointLightSourcePosition[i].xyz- ePosition.xyz;
}
#endif
2015-11-27 13:15:33 +08:00
#if (MAX_SPOT_LIGHT_NUM > 0)
2015-11-27 13:15:33 +08:00
for (int i = 0; i < MAX_SPOT_LIGHT_NUM; ++i)
{
v_vertexToSpotLightDirection[i] = u_SpotLightSourcePosition[i] - ePosition.xyz;
}
#endif
2015-11-27 13:15:33 +08:00
#if ((MAX_DIRECTIONAL_LIGHT_NUM > 0) || (MAX_POINT_LIGHT_NUM > 0) || (MAX_SPOT_LIGHT_NUM > 0))
v_normal = u_NormalMatrix * normal;
#endif
#endif
2014-08-27 16:26:01 +08:00
2014-08-21 10:33:16 +08:00
TextureCoordOut = a_texCoord;
TextureCoordOut.y = 1.0 - TextureCoordOut.y;
gl_Position = u_PMatrix * ePosition;
2014-08-21 10:33:16 +08:00
}
)";