forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorph-instance.js
More file actions
381 lines (300 loc) · 12.6 KB
/
morph-instance.js
File metadata and controls
381 lines (300 loc) · 12.6 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { BLENDEQUATION_ADD, BLENDMODE_ONE, PIXELFORMAT_RGBA32F, PIXELFORMAT_RGBA16F } from '../graphics/constants.js';
import { createShaderFromCode } from '../graphics/program-lib/utils.js';
import { drawQuadWithShader } from '../graphics/simple-post-effect.js';
import { RenderTarget } from '../graphics/render-target.js';
import { Morph } from './morph.js';
// vertex shader used to add morph targets from textures into render target
var textureMorphVertexShader =
'attribute vec2 vertex_position;\n' +
'varying vec2 uv0;\n' +
'void main(void) {\n' +
' gl_Position = vec4(vertex_position, 0.5, 1.0);\n' +
' uv0 = vertex_position.xy * 0.5 + 0.5;\n' +
'}\n';
/**
* @class
* @name MorphInstance
* @classdesc An instance of {@link Morph}. Contains weights to assign to every {@link MorphTarget}, manages selection of active morph targets.
* @param {Morph} morph - The {@link Morph} to instance.
*/
class MorphInstance {
constructor(morph) {
this.morph = morph;
morph.incRefCount();
this.device = morph.device;
this.meshInstance = null;
// weights
this._weights = [];
for (var v = 0; v < morph._targets.length; v++) {
this.setWeight(v, morph._targets[v].defaultWeight);
}
// temporary array of targets with non-zero weight
this._activeTargets = [];
if (morph.useTextureMorph) {
// shader cache
this.shaderCache = {};
// max number of morph targets rendered at a time (each uses single texture slot)
this.maxSubmitCount = this.device.maxTextures;
// array for max number of weights
this._shaderMorphWeights = new Float32Array(this.maxSubmitCount);
// create render targets to morph targets into
var createRT = function (name, textureVar) {
// render to appropriate, RGBA formats, we cannot render to RGB float / half float format in WEbGL
var format = morph._renderTextureFormat === Morph.FORMAT_FLOAT ? PIXELFORMAT_RGBA32F : PIXELFORMAT_RGBA16F;
this[textureVar] = morph._createTexture(name, format);
return new RenderTarget({
colorBuffer: this[textureVar],
depth: false
});
}.bind(this);
if (morph.morphPositions) {
this.rtPositions = createRT("MorphRTPos", "texturePositions");
}
if (morph.morphNormals) {
this.rtNormals = createRT("MorphRTNrm", "textureNormals");
}
// texture params
this._textureParams = new Float32Array([morph.morphTextureWidth, morph.morphTextureHeight,
1 / morph.morphTextureWidth, 1 / morph.morphTextureHeight]);
// resolve possible texture names
for (var i = 0; i < this.maxSubmitCount; i++) {
this["morphBlendTex" + i] = this.device.scope.resolve("morphBlendTex" + i);
}
this.morphFactor = this.device.scope.resolve("morphFactor[0]");
// true indicates render target textures are full of zeros to avoid rendering to them when all weights are zero
this.zeroTextures = false;
} else { // vertex attribute based morphing
// max number of morph targets rendered at a time
this.maxSubmitCount = 8;
// weights of active vertex buffers in format used by rendering
this._shaderMorphWeights = new Float32Array(this.maxSubmitCount); // whole array
this._shaderMorphWeightsA = new Float32Array(this._shaderMorphWeights.buffer, 0, 4); // first 4 elements
this._shaderMorphWeightsB = new Float32Array(this._shaderMorphWeights.buffer, 4 * 4, 4); // second 4 elements
// pre-allocate array of active vertex buffers used by rendering
this._activeVertexBuffers = new Array(this.maxSubmitCount);
}
}
/**
* @function
* @name MorphInstance#destroy
* @description Frees video memory allocated by this object.
*/
destroy() {
this.meshInstance = null;
// don't destroy shader as it's in the cache and can be used by other materials
this.shader = null;
const morph = this.morph;
if (morph) {
// decrease ref count
this.morph = null;
morph.decRefCount();
// destroy morph
if (morph.getRefCount() < 1) {
morph.destroy();
}
}
if (this.rtPositions) {
this.rtPositions.destroy();
this.rtPositions = null;
}
if (this.texturePositions) {
this.texturePositions.destroy();
this.texturePositions = null;
}
if (this.rtNormals) {
this.rtNormals.destroy();
this.rtNormals = null;
}
if (this.textureNormals) {
this.textureNormals.destroy();
this.textureNormals = null;
}
}
/**
* @function
* @name MorphInstance#clone
* @description Clones a MorphInstance. The returned clone uses the same {@link Morph} and weights are set to defaults.
* @returns {MorphInstance} A clone of the specified MorphInstance.
*/
clone() {
var clone = new MorphInstance(this.morph);
return clone;
}
/**
* @function
* @name MorphInstance#getWeight
* @description Gets current weight of the specified morph target.
* @param {number} index - An index of morph target.
* @returns {number} Weight.
*/
getWeight(index) {
return this._weights[index];
}
/**
* @function
* @name MorphInstance#setWeight
* @description Sets weight of the specified morph target.
* @param {number} index - An index of morph target.
* @param {number} weight - Weight.
*/
setWeight(index, weight) {
this._weights[index] = weight;
this._dirty = true;
}
// generates fragment shader to blend number of textures using specified weights
_getFragmentShader(numTextures) {
var i, fragmentShader = '';
if (numTextures > 0) {
fragmentShader += 'varying vec2 uv0;\n' +
'uniform highp float morphFactor[' + numTextures + '];\n';
}
for (i = 0; i < numTextures; i++) {
fragmentShader += 'uniform highp sampler2D morphBlendTex' + i + ';\n';
}
fragmentShader += 'void main (void) {\n' +
' highp vec4 color = vec4(0, 0, 0, 1);\n';
for (i = 0; i < numTextures; i++) {
fragmentShader += ' color.xyz += morphFactor[' + i + '] * texture2D(morphBlendTex' + i + ', uv0).xyz;\n';
}
fragmentShader += ' gl_FragColor = color;\n' +
'}\n';
return fragmentShader;
}
// creates complete shader for texture based morphing
_getShader(count) {
var shader = this.shaderCache[count];
// if shader is not in cache, generate one
if (!shader) {
var fs = this._getFragmentShader(count);
shader = createShaderFromCode(this.device, textureMorphVertexShader, fs, "textureMorph" + count);
this.shaderCache[count] = shader;
}
return shader;
}
_updateTextureRenderTarget(renderTarget, srcTextureName) {
var device = this.device;
// blend curently set up textures to render target
var submitBatch = function (usedCount, blending) {
// factors
this.morphFactor.setValue(this._shaderMorphWeights);
// alpha blending - first pass gets none, following passes are additive
device.setBlending(blending);
if (blending) {
device.setBlendFunction(BLENDMODE_ONE, BLENDMODE_ONE);
device.setBlendEquation(BLENDEQUATION_ADD);
}
// render quad with shader for required number of textures
var shader = this._getShader(usedCount);
drawQuadWithShader(device, renderTarget, shader, undefined, undefined, blending);
}.bind(this);
// set up parameters for active blend targets
var usedCount = 0;
var blending = false;
var count = this._activeTargets.length;
for (var i = 0; i < count; i++) {
var activeTarget = this._activeTargets[i];
var tex = activeTarget.target[srcTextureName];
if (tex) {
// texture
this["morphBlendTex" + usedCount].setValue(tex);
// weight
this._shaderMorphWeights[usedCount] = activeTarget.weight;
// submit if batch is full
usedCount++;
if (usedCount >= this.maxSubmitCount) {
submitBatch(usedCount, blending);
usedCount = 0;
blending = true;
}
}
}
// leftover batch, or just to clear texture
if (usedCount > 0 || (count === 0 && !this.zeroTextures)) {
submitBatch(usedCount, blending);
}
}
_updateTextureMorph() {
var device = this.device;
// #if _DEBUG
device.pushMarker("MorphUpdate");
// #endif
// update textures if active targets, or no active targets and textures need to be cleared
if (this._activeTargets.length > 0 || !this.zeroTextures) {
// blend morph targets into render targets
this._updateTextureRenderTarget(this.rtPositions, 'texturePositions');
this._updateTextureRenderTarget(this.rtNormals, 'textureNormals');
// textures were cleared if no active targets
this.zeroTextures = this._activeTargets.length === 0;
}
// #if _DEBUG
device.popMarker("");
// #endif
}
_updateVertexMorph() {
// prepare 8 slots for rendering. these are supported combinations: PPPPPPPP, NNNNNNNN, PPPPNNNN
var i, count = this.maxSubmitCount;
for (i = 0; i < count; i++) {
this._shaderMorphWeights[i] = 0;
this._activeVertexBuffers[i] = null;
}
var posIndex = 0;
var nrmIndex = this.morph.morphPositions ? 4 : 0;
var target;
for (i = 0; i < this._activeTargets.length; i++) {
target = this._activeTargets[i].target;
if (target._vertexBufferPositions) {
this._activeVertexBuffers[posIndex] = target._vertexBufferPositions;
this._shaderMorphWeights[posIndex] = this._activeTargets[i].weight;
posIndex++;
}
if (target._vertexBufferNormals) {
this._activeVertexBuffers[nrmIndex] = target._vertexBufferNormals;
this._shaderMorphWeights[nrmIndex] = this._activeTargets[i].weight;
nrmIndex++;
}
}
}
/**
* @function
* @name MorphInstance#update
* @description Selects active morph targets and prepares morph for rendering. Called automatically by renderer.
*/
update() {
this._dirty = false;
var targets = this.morph._targets;
// collect active targets, reuse objects in _activeTargets array to avoid allocations
var activeCount = 0, activeTarget;
var i, absWeight, epsilon = 0.00001;
for (i = 0; i < targets.length; i++) {
absWeight = Math.abs(this.getWeight(i));
if (absWeight > epsilon) {
// create new object if needed
if (this._activeTargets.length <= activeCount) {
this._activeTargets[activeCount] = {};
}
activeTarget = this._activeTargets[activeCount++];
activeTarget.absWeight = absWeight;
activeTarget.weight = this.getWeight(i);
activeTarget.target = targets[i];
}
}
this._activeTargets.length = activeCount;
// if there's more active targets then rendering supports
var maxActiveTargets = this.morph.maxActiveTargets;
if (this._activeTargets.length > maxActiveTargets) {
// sort them by absWeight
this._activeTargets.sort(function (l, r) {
return (l.absWeight < r.absWeight) ? 1 : (r.absWeight < l.absWeight ? -1 : 0);
});
// remove excess
this._activeTargets.length = maxActiveTargets;
}
// prepare for rendering
if (this.morph.useTextureMorph) {
this._updateTextureMorph();
} else {
this._updateVertexMorph();
}
}
}
export { MorphInstance };