/
morphTargetsBlock.ts
322 lines (272 loc) · 13.6 KB
/
morphTargetsBlock.ts
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import { NodeMaterialBlock } from "../../nodeMaterialBlock";
import { NodeMaterialBlockConnectionPointTypes } from "../../Enums/nodeMaterialBlockConnectionPointTypes";
import type { NodeMaterialBuildState } from "../../nodeMaterialBuildState";
import { NodeMaterialBlockTargets } from "../../Enums/nodeMaterialBlockTargets";
import type { NodeMaterialConnectionPoint } from "../../nodeMaterialBlockConnectionPoint";
import type { AbstractMesh } from "../../../../Meshes/abstractMesh";
import type { NodeMaterial, NodeMaterialDefines } from "../../nodeMaterial";
import type { Effect } from "../../../effect";
import type { Mesh } from "../../../../Meshes/mesh";
import { MaterialHelper } from "../../../materialHelper";
import { VertexBuffer } from "../../../../Buffers/buffer";
import { InputBlock } from "../Input/inputBlock";
import { RegisterClass } from "../../../../Misc/typeStore";
import "../../../../Shaders/ShadersInclude/morphTargetsVertexDeclaration";
import "../../../../Shaders/ShadersInclude/morphTargetsVertexGlobalDeclaration";
/**
* Block used to add morph targets support to vertex shader
*/
export class MorphTargetsBlock extends NodeMaterialBlock {
private _repeatableContentAnchor: string;
/**
* Create a new MorphTargetsBlock
* @param name defines the block name
*/
public constructor(name: string) {
super(name, NodeMaterialBlockTargets.Vertex);
this.registerInput("position", NodeMaterialBlockConnectionPointTypes.Vector3);
this.registerInput("normal", NodeMaterialBlockConnectionPointTypes.Vector3);
this.registerInput("tangent", NodeMaterialBlockConnectionPointTypes.Vector4);
this.tangent.acceptedConnectionPointTypes.push(NodeMaterialBlockConnectionPointTypes.Vector3);
this.registerInput("uv", NodeMaterialBlockConnectionPointTypes.Vector2);
this.registerOutput("positionOutput", NodeMaterialBlockConnectionPointTypes.Vector3);
this.registerOutput("normalOutput", NodeMaterialBlockConnectionPointTypes.Vector3);
this.registerOutput("tangentOutput", NodeMaterialBlockConnectionPointTypes.Vector3);
this.registerOutput("uvOutput", NodeMaterialBlockConnectionPointTypes.Vector2);
}
/**
* Gets the current class name
* @returns the class name
*/
public getClassName() {
return "MorphTargetsBlock";
}
/**
* Gets the position input component
*/
public get position(): NodeMaterialConnectionPoint {
return this._inputs[0];
}
/**
* Gets the normal input component
*/
public get normal(): NodeMaterialConnectionPoint {
return this._inputs[1];
}
/**
* Gets the tangent input component
*/
public get tangent(): NodeMaterialConnectionPoint {
return this._inputs[2];
}
/**
* Gets the tangent input component
*/
public get uv(): NodeMaterialConnectionPoint {
return this._inputs[3];
}
/**
* Gets the position output component
*/
public get positionOutput(): NodeMaterialConnectionPoint {
return this._outputs[0];
}
/**
* Gets the normal output component
*/
public get normalOutput(): NodeMaterialConnectionPoint {
return this._outputs[1];
}
/**
* Gets the tangent output component
*/
public get tangentOutput(): NodeMaterialConnectionPoint {
return this._outputs[2];
}
/**
* Gets the tangent output component
*/
public get uvOutput(): NodeMaterialConnectionPoint {
return this._outputs[3];
}
public initialize(state: NodeMaterialBuildState) {
state._excludeVariableName("morphTargetInfluences");
}
public autoConfigure(material: NodeMaterial) {
if (!this.position.isConnected) {
let positionInput = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "position");
if (!positionInput) {
positionInput = new InputBlock("position");
positionInput.setAsAttribute();
}
positionInput.output.connectTo(this.position);
}
if (!this.normal.isConnected) {
let normalInput = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "normal");
if (!normalInput) {
normalInput = new InputBlock("normal");
normalInput.setAsAttribute("normal");
}
normalInput.output.connectTo(this.normal);
}
if (!this.tangent.isConnected) {
let tangentInput = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "tangent");
if (!tangentInput) {
tangentInput = new InputBlock("tangent");
tangentInput.setAsAttribute("tangent");
}
tangentInput.output.connectTo(this.tangent);
}
if (!this.uv.isConnected) {
let uvInput = material.getInputBlockByPredicate((b) => b.isAttribute && b.name === "uv");
if (!uvInput) {
uvInput = new InputBlock("uv");
uvInput.setAsAttribute("uv");
}
uvInput.output.connectTo(this.uv);
}
}
public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
if ((<Mesh>mesh).morphTargetManager) {
const morphTargetManager = (<Mesh>mesh).morphTargetManager;
if (morphTargetManager?.isUsingTextureForTargets && morphTargetManager.numInfluencers !== defines["NUM_MORPH_INFLUENCERS"]) {
defines.markAsAttributesDirty();
}
}
if (!defines._areAttributesDirty) {
return;
}
MaterialHelper.PrepareDefinesForMorphTargets(mesh, defines);
}
public bind(effect: Effect, nodeMaterial: NodeMaterial, mesh?: Mesh) {
if (mesh && mesh.morphTargetManager && mesh.morphTargetManager.numInfluencers > 0) {
MaterialHelper.BindMorphTargetParameters(mesh, effect);
if (mesh.morphTargetManager.isUsingTextureForTargets) {
mesh.morphTargetManager._bind(effect);
}
}
}
public replaceRepeatableContent(vertexShaderState: NodeMaterialBuildState, fragmentShaderState: NodeMaterialBuildState, mesh: AbstractMesh, defines: NodeMaterialDefines) {
const position = this.position;
const normal = this.normal;
const tangent = this.tangent;
const uv = this.uv;
const positionOutput = this.positionOutput;
const normalOutput = this.normalOutput;
const tangentOutput = this.tangentOutput;
const uvOutput = this.uvOutput;
const state = vertexShaderState;
const repeatCount = defines.NUM_MORPH_INFLUENCERS as number;
const manager = (<Mesh>mesh).morphTargetManager;
const hasNormals = manager && manager.supportsNormals && defines["NORMAL"];
const hasTangents = manager && manager.supportsTangents && defines["TANGENT"];
const hasUVs = manager && manager.supportsUVs && defines["UV1"];
let injectionCode = "";
if (manager?.isUsingTextureForTargets && repeatCount > 0) {
injectionCode += `float vertexID;\r\n`;
}
for (let index = 0; index < repeatCount; index++) {
injectionCode += `#ifdef MORPHTARGETS\r\n`;
if (manager?.isUsingTextureForTargets) {
injectionCode += `vertexID = float(gl_VertexID) * morphTargetTextureInfo.x;\r\n`;
injectionCode += `${positionOutput.associatedVariableName} += (readVector3FromRawSampler(${index}, vertexID) - ${position.associatedVariableName}) * morphTargetInfluences[${index}];\r\n`;
injectionCode += `vertexID += 1.0;\r\n`;
} else {
injectionCode += `${positionOutput.associatedVariableName} += (position${index} - ${position.associatedVariableName}) * morphTargetInfluences[${index}];\r\n`;
}
if (hasNormals) {
injectionCode += `#ifdef MORPHTARGETS_NORMAL\r\n`;
if (manager?.isUsingTextureForTargets) {
injectionCode += `${normalOutput.associatedVariableName} += (readVector3FromRawSampler(${index}, vertexID) - ${normal.associatedVariableName}) * morphTargetInfluences[${index}];\r\n`;
injectionCode += `vertexID += 1.0;\r\n`;
} else {
injectionCode += `${normalOutput.associatedVariableName} += (normal${index} - ${normal.associatedVariableName}) * morphTargetInfluences[${index}];\r\n`;
}
injectionCode += `#endif\r\n`;
}
if (hasUVs) {
injectionCode += `#ifdef MORPHTARGETS_UV\r\n`;
if (manager?.isUsingTextureForTargets) {
injectionCode += `${uvOutput.associatedVariableName} += (readVector3FromRawSampler(${index}, vertexID).xy - ${uv.associatedVariableName}) * morphTargetInfluences[${index}];\r\n`;
injectionCode += `vertexID += 1.0;\r\n`;
} else {
injectionCode += `${uvOutput.associatedVariableName}.xy += (uv_${index} - ${uv.associatedVariableName}.xy) * morphTargetInfluences[${index}];\r\n`;
}
injectionCode += `#endif\r\n`;
}
if (hasTangents) {
injectionCode += `#ifdef MORPHTARGETS_TANGENT\r\n`;
if (manager?.isUsingTextureForTargets) {
injectionCode += `${tangentOutput.associatedVariableName} += (readVector3FromRawSampler(${index}, vertexID) - ${tangent.associatedVariableName}) * morphTargetInfluences[${index}];\r\n`;
} else {
injectionCode += `${tangentOutput.associatedVariableName}.xyz += (tangent${index} - ${tangent.associatedVariableName}.xyz) * morphTargetInfluences[${index}];\r\n`;
}
injectionCode += `#endif\r\n`;
}
injectionCode += `#endif\r\n`;
}
state.compilationString = state.compilationString.replace(this._repeatableContentAnchor, injectionCode);
if (repeatCount > 0) {
for (let index = 0; index < repeatCount; index++) {
state.attributes.push(VertexBuffer.PositionKind + index);
if (hasNormals) {
state.attributes.push(VertexBuffer.NormalKind + index);
}
if (hasTangents) {
state.attributes.push(VertexBuffer.TangentKind + index);
}
if (hasUVs) {
state.attributes.push(VertexBuffer.UVKind + "_" + index);
}
}
}
}
protected _buildBlock(state: NodeMaterialBuildState) {
super._buildBlock(state);
// Register for defines
state.sharedData.blocksWithDefines.push(this);
// Register for binding
state.sharedData.bindableBlocks.push(this);
// Register for repeatable content generation
state.sharedData.repeatableContentBlocks.push(this);
// Emit code
const position = this.position;
const normal = this.normal;
const tangent = this.tangent;
const uv = this.uv;
const positionOutput = this.positionOutput;
const normalOutput = this.normalOutput;
const tangentOutput = this.tangentOutput;
const uvOutput = this.uvOutput;
const comments = `//${this.name}`;
state.uniforms.push("morphTargetInfluences");
state.uniforms.push("morphTargetTextureInfo");
state.uniforms.push("morphTargetTextureIndices");
state.samplers.push("morphTargets");
state._emitFunctionFromInclude("morphTargetsVertexGlobalDeclaration", comments);
state._emitFunctionFromInclude("morphTargetsVertexDeclaration", comments, {
repeatKey: "maxSimultaneousMorphTargets",
});
state.compilationString += `${this._declareOutput(positionOutput, state)} = ${position.associatedVariableName};\r\n`;
state.compilationString += `#ifdef NORMAL\r\n`;
state.compilationString += `${this._declareOutput(normalOutput, state)} = ${normal.associatedVariableName};\r\n`;
state.compilationString += `#else\r\n`;
state.compilationString += `${this._declareOutput(normalOutput, state)} = vec3(0., 0., 0.);\r\n`;
state.compilationString += `#endif\r\n`;
state.compilationString += `#ifdef TANGENT\r\n`;
state.compilationString += `${this._declareOutput(tangentOutput, state)} = ${tangent.associatedVariableName};\r\n`;
state.compilationString += `#else\r\n`;
state.compilationString += `${this._declareOutput(tangentOutput, state)} = vec3(0., 0., 0.);\r\n`;
state.compilationString += `#endif\r\n`;
state.compilationString += `#ifdef UV1\r\n`;
state.compilationString += `${this._declareOutput(uvOutput, state)} = ${uv.associatedVariableName};\r\n`;
state.compilationString += `#else\r\n`;
state.compilationString += `${this._declareOutput(uvOutput, state)} = vec2(0., 0.);\r\n`;
state.compilationString += `#endif\r\n`;
// Repeatable content
this._repeatableContentAnchor = state._repeatableContentAnchor;
state.compilationString += this._repeatableContentAnchor;
return this;
}
}
RegisterClass("BABYLON.MorphTargetsBlock", MorphTargetsBlock);