140 lines
2.9 KiB
JavaScript
140 lines
2.9 KiB
JavaScript
|
/**
|
||
|
* Raycaster for GameLib.D3
|
||
|
* @param graphics GameLib.D3.Graphics
|
||
|
* @param parentObject
|
||
|
* @param apiRaycaster
|
||
|
* @constructor
|
||
|
*/
|
||
|
GameLib.D3.Raycaster = function(
|
||
|
graphics,
|
||
|
parentObject,
|
||
|
apiRaycaster
|
||
|
) {
|
||
|
|
||
|
this.graphics = graphics;
|
||
|
this.graphics.isNotThreeThrow();
|
||
|
|
||
|
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
|
||
|
parentObject = null;
|
||
|
}
|
||
|
this.parentObject = parentObject;
|
||
|
|
||
|
GameLib.D3.API.Raycaster.call(
|
||
|
this,
|
||
|
apiRaycaster.position,
|
||
|
apiRaycaster.direction
|
||
|
);
|
||
|
|
||
|
this.position = new GameLib.Vector3(
|
||
|
this.graphics,
|
||
|
this,
|
||
|
this.position
|
||
|
);
|
||
|
|
||
|
this.direction = new GameLib.Vector3(
|
||
|
this.graphics,
|
||
|
this,
|
||
|
this.direction
|
||
|
);
|
||
|
|
||
|
this.instance = this.createInstance();
|
||
|
};
|
||
|
|
||
|
GameLib.D3.Raycaster.prototype = Object.create(GameLib.D3.API.Raycaster.prototype);
|
||
|
GameLib.D3.Raycaster.prototype.constructor = GameLib.D3.Raycaster;
|
||
|
|
||
|
/**
|
||
|
* Creates or updates a raycaster instance
|
||
|
* @param update
|
||
|
*/
|
||
|
GameLib.D3.Raycaster.prototype.createInstance = function(update) {
|
||
|
|
||
|
var instance = null;
|
||
|
|
||
|
if (update) {
|
||
|
instance = this.instance;
|
||
|
} else {
|
||
|
instance = new THREE.Raycaster();
|
||
|
}
|
||
|
|
||
|
instance.set(
|
||
|
this.position.instance,
|
||
|
this.direction.instance
|
||
|
);
|
||
|
|
||
|
return instance;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Sets the direction and position of this raycaster
|
||
|
* @param position GameLib.Vector3
|
||
|
* @param direction GameLib.Vector3
|
||
|
*/
|
||
|
GameLib.D3.Raycaster.prototype.set = function(
|
||
|
position,
|
||
|
direction
|
||
|
) {
|
||
|
this.position = position;
|
||
|
this.direction = direction;
|
||
|
|
||
|
this.position.updateInstance();
|
||
|
this.direction.updateInstance();
|
||
|
|
||
|
this.createInstance(true);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Sets the direction of this raycaster
|
||
|
* @param direction GameLib.Vector3
|
||
|
*/
|
||
|
GameLib.D3.Raycaster.prototype.setDirection = function(
|
||
|
direction
|
||
|
) {
|
||
|
this.direction = direction;
|
||
|
|
||
|
this.direction.updateInstance();
|
||
|
|
||
|
this.createInstance(true);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Sets the position of this raycaster
|
||
|
* @param position GameLib.Vector3
|
||
|
*/
|
||
|
GameLib.D3.Raycaster.prototype.setPosition = function(
|
||
|
position
|
||
|
) {
|
||
|
this.position = position;
|
||
|
|
||
|
this.position.updateInstance();
|
||
|
|
||
|
this.createInstance(true);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Returns the face normal (if any) of an intersection between current ray position, direction and a provided mesh
|
||
|
* @param mesh GameLib.D3.Mesh
|
||
|
* @returns {null | GameLib.Vector3}
|
||
|
*/
|
||
|
GameLib.D3.Raycaster.prototype.getFaceNormal = function(mesh) {
|
||
|
|
||
|
var normal = null;
|
||
|
|
||
|
var intersect = this.instance.intersectObject(
|
||
|
mesh.instance
|
||
|
);
|
||
|
|
||
|
if (intersect && intersect.length > 0) {
|
||
|
normal = new GameLib.Vector3(
|
||
|
this.graphics,
|
||
|
this,
|
||
|
new GameLib.API.Vector3(
|
||
|
intersect[0].face.normal.x,
|
||
|
intersect[0].face.normal.y,
|
||
|
intersect[0].face.normal.z
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return normal;
|
||
|
};
|