-
Notifications
You must be signed in to change notification settings - Fork 1
/
SkinningProgram.js
executable file
·101 lines (83 loc) · 2.94 KB
/
SkinningProgram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {createProgram} from "./webgl/webglHelper.js";
import {ProgramObject} from "./webgl/ProgramObject.js";
export class SkinningProgram extends ProgramObject {
/**
* @private
* @type {number}
*/
#maxJointNum = 1;
/**
* @public
* @param {number} maxJointNum
*/
constructor(maxJointNum) {
super();
this.#maxJointNum = maxJointNum;
}
/**
* @inheritDoc
*/
createProgram = (gl2) => {
// language=GLSL
const vertexShaderSource = `#version 300 es
in vec3 position;
in vec3 normal;
in vec4 weight;
in uvec4 bone;
out vec4 vColor;
uniform mat4 mMatrix;
uniform mat4 vpMatrix;
uniform vec4 baseColorFactor;
uniform vec3 directionalLightDirection;
uniform vec4 ambientLightColor;
uniform vec4 directionalLightColor;
uniform mat4 boneMatrix[${this.#maxJointNum}];
void main(void)
{
mat4 skinningMatrix = boneMatrix[bone.x] * weight.x;
skinningMatrix += boneMatrix[bone.y] * weight.y;
skinningMatrix += boneMatrix[bone.z] * weight.z;
skinningMatrix += boneMatrix[bone.w] * weight.w;
vec4 skinnedPosition = skinningMatrix * vec4(position, 1.0);
// float(bone.x) * weight.x;
// vec4 skinnedPosition = vec4(position, 1.0);
gl_Position = (vpMatrix * mMatrix) * skinnedPosition;
mat3 skinningNormalMatrix = mat3(skinningMatrix);
skinningNormalMatrix = inverse(skinningNormalMatrix);
skinningNormalMatrix = transpose(skinningNormalMatrix);
vec3 skinnedNormal = normalize(skinningNormalMatrix * normal);
// vec3 skinnedNormal = normalize(normal);
mat3 nMatrix = mat3(mMatrix);
nMatrix = inverse(nMatrix);
nMatrix = transpose(nMatrix);
vec3 worldNormal = normalize(nMatrix * skinnedNormal);
float diffuse = dot(worldNormal, normalize(directionalLightDirection));
diffuse = clamp(diffuse, 0.0, 1.0);
//vColor = vec4(worldNormal, 1.0);
vColor = baseColorFactor * (ambientLightColor + diffuse * directionalLightColor);
}
`;
// language=GLSL
const fragmentShaderSource = `#version 300 es
precision mediump float;
in vec4 vColor;
out vec4 outColor;
uniform float collidedColorFactor;
void main(void)
{
outColor = vColor * collidedColorFactor;
}
`;
this.program = createProgram(gl2, vertexShaderSource, fragmentShaderSource);
this.uniformLocation = {
mMatrix: gl2.getUniformLocation(this.program, "mMatrix"),
vpMatrix: gl2.getUniformLocation(this.program, "vpMatrix"),
baseColorFactor: gl2.getUniformLocation(this.program, "baseColorFactor"),
directionalLightDirection: gl2.getUniformLocation(this.program, "directionalLightDirection"),
collidedColorFactor: gl2.getUniformLocation(this.program, "collidedColorFactor"),
ambientLightColor: gl2.getUniformLocation(this.program, "ambientLightColor"),
directionalLightColor: gl2.getUniformLocation(this.program, "directionalLightColor"),
boneMatrix: gl2.getUniformLocation(this.program, "boneMatrix")
};
};
}