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

239 lines
5.2 KiB
JavaScript
Raw Normal View History

/**
* Raycaster for GameLib.D3
* @param graphics GameLib.D3.Graphics
2016-12-15 14:53:39 +01:00
* @param apiRaycaster
* @constructor
*/
GameLib.D3.Raycaster = function(
graphics,
2016-12-15 14:53:39 +01:00
apiRaycaster
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiRaycaster)) {
apiRaycaster = {};
}
if (apiRaycaster instanceof GameLib.D3.Raycaster) {
return apiRaycaster;
}
2016-12-15 14:53:39 +01:00
GameLib.D3.API.Raycaster.call(
this,
2017-01-09 15:20:48 +01:00
apiRaycaster.id,
apiRaycaster.name,
2016-12-15 14:53:39 +01:00
apiRaycaster.position,
apiRaycaster.direction
);
this.position = new GameLib.Vector3(
this.graphics,
this.position,
this
2016-12-15 14:53:39 +01:00
);
this.direction = new GameLib.Vector3(
this.graphics,
this.direction,
this
2016-12-15 14:53:39 +01:00
);
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.updateInstance = function() {
this.instance = this.createInstance(true);
};
2017-05-16 14:51:57 +02:00
GameLib.D3.Raycaster.prototype.toApiObject = function() {
return new GameLib.D3.API.Raycaster(
2017-01-09 15:20:48 +01:00
this.id,
this.name,
2017-05-16 14:51:57 +02:00
this.position.toApiObject(),
this.direction.toApiObject()
)
};
2017-01-06 16:53:53 +01:00
GameLib.D3.Raycaster.FromObjectRaycaster = function(graphics, parentObject, objectRaycaster) {
var apiRaycaster = GameLib.D3.API.Raycaster.FromObjectRaycaster(objectRaycaster);
var raycaster = new GameLib.D3.Raycaster(
graphics,
apiRaycaster
);
return raycaster;
};
/**
* 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
) {
2017-01-04 16:12:30 +01:00
this.position.x = position.x;
this.position.y = position.y;
this.position.z = position.z;
this.direction.x = direction.x;
this.direction.y = direction.y;
this.direction.z = direction.z;
this.position.updateInstance();
this.direction.updateInstance();
};
/**
* 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
) {
2017-01-04 16:12:30 +01:00
this.direction.x = direction.x;
this.direction.y = direction.y;
this.direction.z = direction.z;
this.direction.updateInstance();
};
/**
* 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
) {
2017-01-03 18:15:03 +01:00
this.position.x = position.x;
this.position.y = position.y;
this.position.z = position.z;
this.position.updateInstance();
};
/**
* Gets all interesected GameLib.D3.Mesh objects
* @param meshes [GameLib.D3.Mesh]
*/
GameLib.D3.Raycaster.prototype.getIntersectedObjects = function(meshes) {
var meshInstances = meshes.map(
function (mesh) {
return mesh.instance;
}
);
var intersects = this.instance.intersectObjects(meshInstances);
return intersects.reduce(
function (result, intersect) {
meshes.map(
function(mesh){
if (mesh.instance === intersect.object){
result.push(
{
mesh : mesh,
distance : intersect.distance
}
);
}
}
);
return result;
},
[]
);
};
/**
* 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,
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
),
this
);
}
return normal;
2017-01-03 18:15:03 +01:00
};
/**
* 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.getIntersectPoint = function(mesh) {
var point = null;
var intersect = this.instance.intersectObject(
mesh.instance
);
if (intersect && intersect.length > 0) {
point = new GameLib.Vector3(
this.graphics,
new GameLib.API.Vector3(
intersect[0].point.x,
intersect[0].point.y,
intersect[0].point.z
),
this
2017-01-03 18:15:03 +01:00
);
}
return point;
};