r3-legacy/src/game-lib-d3-raycaster.js

147 lines
3.1 KiB
JavaScript
Raw Normal View History

/**
* Raycaster for GameLib.D3
* @param graphics GameLib.D3.Graphics
2016-12-15 14:53:39 +01:00
* @param parentObject
* @param apiRaycaster
* @constructor
*/
GameLib.D3.Raycaster = function(
graphics,
2016-12-15 14:53:39 +01:00
parentObject,
apiRaycaster
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(parentObject)) {
parentObject = null;
}
2016-12-15 14:53:39 +01:00
this.parentObject = parentObject;
2016-12-15 14:53:39 +01:00
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();
};
2016-12-15 14:53:39 +01:00
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;
};
GameLib.D3.Raycaster.prototype.toApiRaycaster = function() {
return new GameLib.D3.API.Raycaster(
this.position.toApiVector(),
this.direction.toApiVector()
)
};
/**
* Sets the direction and position of this raycaster
2016-12-15 14:53:39 +01:00
* @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
2016-12-15 14:53:39 +01:00
* @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
2016-12-15 14:53:39 +01:00
* @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
2016-12-15 14:53:39 +01:00
* @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) {
2016-12-15 14:53:39 +01:00
normal = new GameLib.Vector3(
this.graphics,
this,
2016-12-15 14:53:39 +01:00
new GameLib.API.Vector3(
intersect[0].face.normal.x,
intersect[0].face.normal.y,
intersect[0].face.normal.z
)
);
}
return normal;
};