Skip to content

Commit 5d82b32

Browse files
author
Steven Yau
committed
Added cache to rebuildgraphs
commit ef9004c75f49dac534cc627abad9a7a5a1929c15 Author: Steven Yau <syau@snaphcat.com> Date: Fri May 1 17:16:07 2020 +0100 Added cache to rebuildgraphs
1 parent 548f090 commit 5d82b32

File tree

2 files changed

+142
-26
lines changed

2 files changed

+142
-26
lines changed

src/core/hash.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
Object.assign(pc, (function () {
2+
function hashCalc(hash, value) {
3+
hash = ((hash << 5) - hash) + value;
4+
// Convert to 32bit integer
5+
hash |= 0;
6+
return hash;
7+
}
8+
29
return {
310
/**
411
* @private
@@ -11,11 +18,49 @@ Object.assign(pc, (function () {
1118
hashCode: function (str) {
1219
var hash = 0;
1320
for (var i = 0, len = str.length; i < len; i++) {
14-
hash = ((hash << 5) - hash) + str.charCodeAt(i);
15-
// Convert to 32bit integer
16-
hash |= 0;
21+
hash = hashCalc(hash, str.charCodeAt(i));
1722
}
1823
return hash;
24+
},
25+
26+
hashCurve: function (curve) {
27+
var hash = curve.type;
28+
var keys = curve.keys;
29+
for (var i = 0, len = keys.length; i < len; i++) {
30+
hash = hashCalc(hash, Math.round(keys[i][0] * 10000));
31+
hash = hashCalc(hash, Math.round(keys[i][1] * 10000));
32+
}
33+
return hash;
34+
},
35+
36+
hashCurveSet: function (curveSet) {
37+
var hash = curveSet._type;
38+
var curves = curveSet.curves;
39+
var keys;
40+
41+
for (var i = 0; i < curves.length; ++i) {
42+
keys = curves[i].keys;
43+
for (var j = 0, len = keys.length; j < len; j++) {
44+
hash = hashCalc(hash, Math.round(keys[j][0] * 10000));
45+
hash = hashCalc(hash, Math.round(keys[j][1] * 10000));
46+
}
47+
}
48+
return hash;
49+
},
50+
51+
hash2Float32Arrays: function (floatArray1, floatArray2) {
52+
var hash = 0;
53+
var i;
54+
55+
for (i = 0; i < floatArray1.length; ++i) {
56+
hash = hashCalc(hash, Math.round(floatArray1[i] * 10000));
57+
}
58+
59+
for (i = 0; i < floatArray2.length; ++i) {
60+
hash = hashCalc(hash, Math.round(floatArray2[i] * 10000));
61+
}
62+
63+
return hash;
1964
}
2065
};
2166
}()));

src/scene/particle-system/particle-emitter.js

Lines changed: 94 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,68 @@ Object.assign(pc, function () {
360360
return sub;
361361
}
362362

363+
// Test for IE11
364+
var quantizeCache = new Map();
365+
var quantizeSetCache = new Map();
366+
var quantizeSetClamp01Cache = new Map();
367+
var divGraphCache = new Map();
368+
var divMaxCache = new Map();
369+
370+
function getQuantizeFromCache(graph, precision) {
371+
var hash = pc.hashCurve(graph);
372+
var outGraph = quantizeCache.get(hash);
373+
if (!outGraph) {
374+
outGraph = graph.quantize(precision);
375+
quantizeCache.set(hash, outGraph);
376+
}
377+
378+
return outGraph;
379+
}
380+
381+
function getQuantizeSetFromCache(graph, precision) {
382+
var hash = pc.hashCurveSet(graph);
383+
var outGraph = quantizeSetCache.get(hash);
384+
if (!outGraph) {
385+
outGraph = graph.quantize(precision);
386+
quantizeSetCache.set(hash, outGraph);
387+
}
388+
389+
return outGraph;
390+
}
391+
392+
function getQuantizeSetClamp01FromCache(graph, precision) {
393+
var hash = pc.hashCurveSet(graph);
394+
var outGraph = quantizeSetClamp01Cache.get(hash);
395+
if (!outGraph) {
396+
outGraph = graph.quantizeClamped(precision, 0, 1);
397+
quantizeSetClamp01Cache.set(hash, outGraph);
398+
}
399+
400+
return outGraph;
401+
}
402+
403+
function getDivGraphFromCache(graph1, graph2, outMax) {
404+
var hash = pc.hash2Float32Arrays(graph1, graph2);
405+
var divGraph = divGraphCache.get(hash);
406+
var divMax;
407+
if (divGraph) {
408+
divMax = divMaxCache.get(hash);
409+
410+
} else {
411+
divMax = new Float32Array(outMax.length);
412+
divGraph = divGraphFrom2Curves(graph1, graph2, divMax);
413+
divGraphCache.set(hash, divGraph);
414+
divMaxCache.set(hash, divMax);
415+
}
416+
417+
for(var i = 0; i < divMax.length; ++i) {
418+
outMax[i] = divMax[i];
419+
}
420+
421+
return divGraph;
422+
}
423+
424+
363425
Object.assign(ParticleEmitter.prototype, {
364426

365427
onChangeCamera: function () {
@@ -689,22 +751,22 @@ Object.assign(pc, function () {
689751
var precision = this.precision;
690752
var gd = this.graphicsDevice;
691753
var i;
692-
693-
this.qLocalVelocity = this.localVelocityGraph.quantize(precision);
694-
this.qVelocity = this.velocityGraph.quantize(precision);
695-
this.qColor = this.colorGraph.quantizeClamped(precision, 0, 1);
696-
this.qRotSpeed = this.rotationSpeedGraph.quantize(precision);
697-
this.qScale = this.scaleGraph.quantize(precision);
698-
this.qAlpha = this.alphaGraph.quantize(precision);
699-
this.qRadialSpeed = this.radialSpeedGraph.quantize(precision);
700-
701-
this.qLocalVelocity2 = this.localVelocityGraph2.quantize(precision);
702-
this.qVelocity2 = this.velocityGraph2.quantize(precision);
703-
this.qColor2 = this.colorGraph2.quantizeClamped(precision, 0, 1);
704-
this.qRotSpeed2 = this.rotationSpeedGraph2.quantize(precision);
705-
this.qScale2 = this.scaleGraph2.quantize(precision);
706-
this.qAlpha2 = this.alphaGraph2.quantize(precision);
707-
this.qRadialSpeed2 = this.radialSpeedGraph2.quantize(precision);
754+
755+
this.qLocalVelocity = getQuantizeSetFromCache(this.localVelocityGraph, precision);
756+
this.qVelocity = getQuantizeSetFromCache(this.velocityGraph, precision);
757+
this.qColor = getQuantizeSetClamp01FromCache(this.colorGraph, precision);
758+
this.qRotSpeed = getQuantizeFromCache(this.rotationSpeedGraph, precision);
759+
this.qScale = getQuantizeFromCache(this.scaleGraph, precision);
760+
this.qAlpha = getQuantizeFromCache(this.alphaGraph, precision);
761+
this.qRadialSpeed = getQuantizeFromCache(this.radialSpeedGraph, precision);
762+
763+
this.qLocalVelocity2 = getQuantizeSetFromCache(this.localVelocityGraph2, precision);
764+
this.qVelocity2 = getQuantizeSetFromCache(this.velocityGraph2, precision);
765+
this.qColor2 = getQuantizeSetClamp01FromCache(this.colorGraph2, precision);
766+
this.qRotSpeed2 = getQuantizeFromCache(this.rotationSpeedGraph2, precision);
767+
this.qScale2 = getQuantizeFromCache(this.scaleGraph2, precision);
768+
this.qAlpha2 = getQuantizeFromCache(this.alphaGraph2, precision);
769+
this.qRadialSpeed2 = getQuantizeFromCache(this.radialSpeedGraph2, precision);
708770

709771
for (i = 0; i < precision; i++) {
710772
this.qRotSpeed[i] *= pc.math.DEG_TO_RAD;
@@ -718,13 +780,22 @@ Object.assign(pc, function () {
718780
this.scaleUMax = [0];
719781
this.alphaUMax = [0];
720782
this.radialSpeedUMax = [0];
721-
this.qLocalVelocityDiv = divGraphFrom2Curves(this.qLocalVelocity, this.qLocalVelocity2, this.localVelocityUMax);
722-
this.qVelocityDiv = divGraphFrom2Curves(this.qVelocity, this.qVelocity2, this.velocityUMax);
723-
this.qColorDiv = divGraphFrom2Curves(this.qColor, this.qColor2, this.colorUMax);
724-
this.qRotSpeedDiv = divGraphFrom2Curves(this.qRotSpeed, this.qRotSpeed2, this.rotSpeedUMax);
725-
this.qScaleDiv = divGraphFrom2Curves(this.qScale, this.qScale2, this.scaleUMax);
726-
this.qAlphaDiv = divGraphFrom2Curves(this.qAlpha, this.qAlpha2, this.alphaUMax);
727-
this.qRadialSpeedDiv = divGraphFrom2Curves(this.qRadialSpeed, this.qRadialSpeed2, this.radialSpeedUMax);
783+
784+
this.qLocalVelocityDiv = getDivGraphFromCache(this.qLocalVelocity, this.qLocalVelocity2, this.localVelocityUMax);
785+
this.qVelocityDiv = getDivGraphFromCache(this.qVelocity, this.qVelocity2, this.velocityUMax);
786+
this.qColorDiv = getDivGraphFromCache(this.qColor, this.qColor2, this.colorUMax);
787+
this.qRotSpeedDiv = getDivGraphFromCache(this.qRotSpeed, this.qRotSpeed2, this.rotSpeedUMax);
788+
this.qScaleDiv = getDivGraphFromCache(this.qScale, this.qScale2, this.scaleUMax);
789+
this.qAlphaDiv = getDivGraphFromCache(this.qAlpha, this.qAlpha2, this.alphaUMax);
790+
this.qRadialSpeedDiv = getDivGraphFromCache(this.qRadialSpeed, this.qRadialSpeed2, this.radialSpeedUMax);
791+
792+
// this.qLocalVelocityDiv = divGraphFrom2Curves(this.qLocalVelocity, this.qLocalVelocity2, this.localVelocityUMax);
793+
// this.qVelocityDiv = divGraphFrom2Curves(this.qVelocity, this.qVelocity2, this.velocityUMax);
794+
// this.qColorDiv = divGraphFrom2Curves(this.qColor, this.qColor2, this.colorUMax);
795+
// this.qRotSpeedDiv = divGraphFrom2Curves(this.qRotSpeed, this.qRotSpeed2, this.rotSpeedUMax);
796+
// this.qScaleDiv = divGraphFrom2Curves(this.qScale, this.qScale2, this.scaleUMax);
797+
// this.qAlphaDiv = divGraphFrom2Curves(this.qAlpha, this.qAlpha2, this.alphaUMax);
798+
// this.qRadialSpeedDiv = divGraphFrom2Curves(this.qRadialSpeed, this.qRadialSpeed2, this.radialSpeedUMax);
728799

729800
if (this.pack8) {
730801
var umax = [0, 0, 0];

0 commit comments

Comments
 (0)