BabylonJS - locallyTranslate
HTML
<script src="https://rawgithub.com/BabylonJS/Babylon.js/v1.8.0/babylon.1.8.0.js"></script>
<button id="setPositionWithLocalVector">setPositionWithLocalVector</button>
<button id="locallyTranslate">locallyTranslate</button>
<canvas id="renderCanvas"></canvas>
CSS
html, body, div, canvas {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
JavaScript
var child;
document.getElementById("setPositionWithLocalVector").onclick = function() {
BABYLON.Mesh.prototype.setLocalTranslation = setPositionWithLocalVector;
child.setLocalTranslation(new BABYLON.Vector3(0.0, 0.0, -0.8));
};
document.getElementById("locallyTranslate").onclick = function() {
BABYLON.Mesh.prototype.setLocalTranslation = locallyTranslate;
child.setLocalTranslation(new BABYLON.Vector3(0.0, 0.0, -0.8));
};
// override BABYLON.Mesh.prototype.computeWorldMatrix
// so _localWorld is computed with other matrices
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,...