BabylonJS - locallyTranslate
by gwenaelhagenmuller
HTML
<script src="https://rawgithub.com/BabylonJS/Babylon.js/v1.8.0/babylon.1.8.0.js"></script>
<button id="setLocalTranslation">setLocalTranslation</button>
<button id="locallyTranslate">locallyTranslate</button>
<canvas id="renderCanvas"></canvas>
CSS
#renderCanvas {
width: 100%;
height: 100%;
min-height: 100%;
}
JavaScript
var cube;
var child;
var coordinates;
var i = 0;
document.getElementById("setLocalTranslation").onclick = function() {
BABYLON.Mesh.prototype.setLocalTranslation = setLocalTranslation;
child.setLocalTranslation(new BABYLON.Vector3(0.0, 0.0, i++ * -0.8));
coordinates = false;
};
document.getElementById("locallyTranslate").onclick = function() {
BABYLON.Mesh.prototype.setLocalTranslation = locallyTranslate;
child.setLocalTranslation(new BABYLON.Vector3(0.0, 0.0, -0.2));
coordinates = true;
};
BABYLON.Mesh.prototype.computeWorldMatrix = function (force) {
if (this._currentRenderId == this._scene.getRenderId() || !force && this.isSynchronized()) {
this._childrenFlag = false;
return this._worldMatrix;
}
this._childrenFlag = true;
this._cache.infiniteDistance = this.infiniteDistance;
this._cache.position.copyFrom(this.position);
this._cache.scaling.copyFrom(this.scaling);
this._cache.pivotMatrixUpdated = false;
this._currentRenderId = this._scene.getRenderId();
// Scaling
BABYLON.Matrix.ScalingToRef(this.scaling.x, this.scaling.y, this.scaling.z, this._localScaling);
// Rotation
if (this.rotationQuaternion) {
this.rotationQuaternion.toRotationMatrix(this._localRotation);
this._cache.rotationQuaternion.copyFrom(this.rotationQuaternion);
} else {
BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._localRotation);
this._cache.rotation.copyFrom(this.rotation);
}
// Translation
if (this.infiniteDistance) {
var camera = this._scene.activeCamera;
BABYLON.Matrix.TranslationToRef(this.position.x + camera.position.x, this.position.y + camera.position.y, this.position.z + camera.position.z, this._localTranslation);
} else {
BABYLON.Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._localTranslation);
}
//...