Skip to content

Commit

Permalink
Merge pull request #352 from tsherif/morph-update-events
Browse files Browse the repository at this point in the history
morphGeometry update events
  • Loading branch information
xeolabs committed May 7, 2015
2 parents 7fdc78f + 76b5b9c commit cb72059
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
18 changes: 17 additions & 1 deletion examples/geometry_morphTargets.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
-khtml-user-select: none;
-webkit-user-select: none;
}

#morph-target-pair, #morph-factor {
font-size: 16px;
}
</style>

<script src="../api/latest/scenejs.js"></script>
Expand All @@ -20,7 +24,9 @@
<body>

<div id="infoDark">
<a href="http://scenejs.org">SceneJS</a> - geometry morph targets - trivial demo
<a href="http://scenejs.org">SceneJS</a> - geometry morph targets - trivial demo<br>
Current target pair: <span id="morph-target-pair">0-1</span><br>
Current interpolation factor: <span id="morph-factor">0</span>
</div>

<script>
Expand Down Expand Up @@ -149,6 +155,16 @@

var factor = 0;
var factorInc = 0.01;
var targetPairElement = document.getElementById("morph-target-pair");
var factorElement = document.getElementById("morph-factor");

myMorph.on("frameUpdate", function(frame) {
targetPairElement.innerText = frame.key1 + "-" + frame.key2;
});

myMorph.on("update", function(frame) {
factorElement.innerText = frame.factor.toFixed(3);
});

scene.on("tick",
function () {
Expand Down
28 changes: 28 additions & 0 deletions src/core/scene/morphGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ new (function () {
var key1 = core.key1;
var key2 = core.key2;

var oldFactor = core.factor;

if (factor < keys[0]) {
key1 = 0;
key2 = 1;
Expand All @@ -313,12 +315,25 @@ new (function () {
}
}

var frameUpdate = key1 != core.key1;

/* Normalise factor to range [0.0..1.0] for the target frame
*/
core.factor = (factor - keys[key1]) / (keys[key2] - keys[key1]);

var morphUpdate = frameUpdate || oldFactor != core.factor;

core.key1 = key1;
core.key2 = key2;

if (morphUpdate) {
var currentFrame = this.getCurrentFrame();
this.publish("update", currentFrame);
if (frameUpdate) {
this.publish("frameUpdate", currentFrame);
}
}

this._engine.display.imageDirty = true;
};

Expand All @@ -334,6 +349,19 @@ new (function () {
return this._core.targets;
};

SceneJS.MorphGeometry.prototype.getCurrentFrame = function () {
var core = this._core;
var key1 = core.key1;
var key2 = core.key2;
return {
key1: key1,
key2: key2,
factor: core.factor,
target1: core.targets[key1],
target2: core.targets[key2]
}
};

SceneJS.MorphGeometry.prototype._compile = function (ctx) {

if (!this._core.hash) {
Expand Down

0 comments on commit cb72059

Please sign in to comment.