r3-legacy/bak/r3-d3-look-at.js

156 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

2016-12-15 14:53:39 +01:00
/**
* Entities with LookAt component looks to targetPosition (default up is 0,1,0)
2018-04-09 10:05:13 +02:00
* @param graphics R3.GraphicsRuntime
* @param apiLookAt R3.D3.API.LookAt
2016-12-15 14:53:39 +01:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.LookAt = function (
2016-12-15 14:53:39 +01:00
graphics,
apiLookAt
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(apiLookAt)) {
apiLookAt = {};
}
2018-04-09 10:05:13 +02:00
if (apiLookAt instanceof R3.D3.LookAt) {
return apiLookAt;
}
2018-04-09 10:05:13 +02:00
R3.D3.API.LookAt.call(
2016-12-15 14:53:39 +01:00
this,
2016-12-19 17:44:15 +01:00
apiLookAt.id,
apiLookAt.name,
apiLookAt.currentComponent,
apiLookAt.targetComponent,
apiLookAt.targetPositionOffset,
2017-01-02 17:05:40 +01:00
apiLookAt.rotationSpeed,
apiLookAt.parentEntity
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.targetPositionOffset = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.targetPositionOffset,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.lookAtMatrix = new R3.Matrix4(
2016-12-15 14:53:39 +01:00
this.graphics,
this.lookAtMatrix,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.up = new R3.Vector3(
2016-12-15 14:53:39 +01:00
this.graphics,
this.up,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.currentRotation = new R3.Quaternion(
2016-12-15 14:53:39 +01:00
this.graphics,
this.currentRotation,
this
2016-12-15 14:53:39 +01:00
);
2018-04-09 10:05:13 +02:00
this.targetPosition = new R3.Vector3(
this.graphics,
this.targetPosition,
this
);
2017-01-19 17:50:11 +01:00
2018-04-09 10:05:13 +02:00
R3.Component.call(
2017-06-16 15:49:53 +02:00
this,
2018-04-09 10:05:13 +02:00
R3.Component.COMPONENT_LOOK_AT,
2017-06-16 15:49:53 +02:00
{
2018-04-09 10:05:13 +02:00
'currentComponent' : R3.Component,
'targetComponent' : R3.Component
2017-06-16 15:49:53 +02:00
}
);
2016-12-15 14:53:39 +01:00
};
2018-04-09 10:05:13 +02:00
R3.D3.LookAt.prototype = Object.create(R3.D3.API.LookAt.prototype);
R3.D3.LookAt.prototype.constructor = R3.D3.LookAt;
2016-12-19 17:44:15 +01:00
2018-04-09 10:05:13 +02:00
R3.D3.LookAt.prototype.createInstance = function() {
2017-10-23 14:52:35 +02:00
this.instance = true;
2018-04-09 10:05:13 +02:00
R3.Component.prototype.createInstance.call(this);
2017-06-16 15:49:53 +02:00
};
2018-04-09 10:05:13 +02:00
R3.D3.LookAt.prototype.updateInstance = function() {
2017-10-23 14:52:35 +02:00
};
/**
* to API object
2018-04-09 10:05:13 +02:00
* @returns {R3.D3.API.LookAt}
2017-10-23 14:52:35 +02:00
*/
2018-04-09 10:05:13 +02:00
R3.D3.LookAt.prototype.toApiObject = function() {
2016-12-19 17:44:15 +01:00
2018-04-09 10:05:13 +02:00
var apiLookAt = new R3.D3.API.LookAt(
2016-12-19 17:44:15 +01:00
this.id,
this.name,
2018-04-09 10:05:13 +02:00
R3.Utils.IdOrNull(this.currentComponent),
R3.Utils.IdOrNull(this.targetComponent),
2017-05-16 14:51:57 +02:00
this.targetPositionOffset.toApiObject(),
2017-01-02 17:05:40 +01:00
this.rotationSpeed,
2018-04-09 10:05:13 +02:00
R3.Utils.IdOrNull(this.parentEntity)
2016-12-19 17:44:15 +01:00
);
return apiLookAt;
};
2018-04-09 10:05:13 +02:00
R3.D3.LookAt.FromObject = function(graphics, objectComponent) {
2016-12-19 17:44:15 +01:00
2018-04-09 10:05:13 +02:00
var apiLookAt = R3.D3.API.LookAt.FromObject(objectComponent);
2016-12-19 17:44:15 +01:00
2018-04-09 10:05:13 +02:00
return new R3.D3.LookAt(
2016-12-19 17:44:15 +01:00
graphics,
apiLookAt
);
};
/**
2017-01-06 16:53:53 +01:00
* Looks at using time
2016-12-19 17:44:15 +01:00
* @param deltaTime
*/
2018-04-09 10:05:13 +02:00
R3.D3.LookAt.prototype.update = function(deltaTime) {
2016-12-19 17:44:15 +01:00
if (this.currentComponent && this.targetComponent) {
this.targetPosition.x = this.targetComponent.position.x + this.targetPositionOffset.x;
this.targetPosition.y = this.targetComponent.position.y + this.targetPositionOffset.y;
this.targetPosition.z = this.targetComponent.position.z + this.targetPositionOffset.z;
this.targetPosition.updateInstance();
2017-01-09 15:20:48 +01:00
// this.lookAtMatrix.lookAt(
// this.currentComponent.position,
// this.targetPosition,
// this.up
// );
//
2018-04-09 10:05:13 +02:00
// this.currentRotation = new R3.Quaternion(this.graphics, this, new R3.API.Quaternion());
2017-01-09 15:20:48 +01:00
//
// this.currentRotation.setFromRotationMatrix(this.lookAtMatrix);
// var t = deltaTime * this.rotationSpeed;
// t = t * t * t * (t * (6.0 * t - 15.0) + 10.0);
// this.currentRotation.slerp(this.currentRotation, t);
2017-01-09 15:20:48 +01:00
// this.currentRotation.normalize();
//
// this.currentComponent.quaternion.x = this.currentRotation.x;
// this.currentComponent.quaternion.y = this.currentRotation.y;
// this.currentComponent.quaternion.z = this.currentRotation.z;
// this.currentComponent.quaternion.w = this.currentRotation.w;
//
this.currentComponent.lookAt.x = this.targetPosition.x;
this.currentComponent.lookAt.y = this.targetPosition.y;
this.currentComponent.lookAt.z = this.targetPosition.z;
this.currentComponent.lookAt.updateInstance();
}
2016-12-19 17:44:15 +01:00
};