updates to new structure

beta.r3js.org
Theunis J. Botha 2016-10-28 11:47:50 +02:00
parent dcb598d778
commit 5247174264
8 changed files with 701 additions and 317 deletions

View File

@ -1,14 +1,17 @@
/**
*
* @param sizeX Number
* @param sizeY Number
* @param matrix matrix 2D Array with height data (Column Major)
* @param elementSize Number
* @param heightScale Number
* @constructor
*/
GameLib.D3.Heightmap = function(
sizeX,
sizeY,
matrix
matrix,
elementSize,
heightScale
) {
if (typeof sizeX == 'undefined') {
sizeX = 0;
@ -24,50 +27,84 @@ GameLib.D3.Heightmap = function(
matrix = [];
}
this.matrix = matrix;
if (typeof elementSize == 'undefined') {
elementSize = 10;
}
this.elementSize = elementSize;
if (typeof heightScale == 'undefined') {
heightScale = 15;
}
this.elementSize = heightScale;
};
GameLib.D3.Heightmap.prototype.generateThreeMeshFromHeightField = function(
THREE,
heightFieldShape,
/**
* Creates a graphics instance mesh from the graphics, physics and physics shape
* @param graphics GameLib.D3.Graphics
* @param shape GameLib.D3.Shape
* @param engine GameLib.D3.Engine
* @returns {THREE.Mesh|this.meshes}
*/
GameLib.D3.Heightmap.GenerateInstanceMesh = function(
graphics,
shape,
engine
) {
var geometry = new THREE.Geometry();
graphics.isNotThreeThrow();
engine.isNotCannonThrow();
var v0 = new this.physics.CANNON.Vec3();
var v1 = new this.physics.CANNON.Vec3();
var v2 = new this.physics.CANNON.Vec3();
var geometry = new graphics.instance.Geometry();
var shape = heightFieldShape;
for (var xi = 0; xi < shape.data.length - 1; xi++) {
for (var yi = 0; yi < shape.data[xi].length - 1; yi++) {
var v0 = new engine.instance.Vec3();
var v1 = new engine.instance.Vec3();
var v2 = new engine.instance.Vec3();
for (var xi = 0; xi < shape.instance.data.length - 1; xi++) {
for (var yi = 0; yi < shape.instance.data[xi].length - 1; yi++) {
for (var k = 0; k < 2; k++) {
shape.getConvexTrianglePillar(xi, yi, k===0);
v0.copy(shape.pillarConvex.vertices[0]);
v1.copy(shape.pillarConvex.vertices[1]);
v2.copy(shape.pillarConvex.vertices[2]);
v0.vadd(shape.pillarOffset, v0);
v1.vadd(shape.pillarOffset, v1);
v2.vadd(shape.pillarOffset, v2);
shape.instance.getConvexTrianglePillar(xi, yi, k===0);
v0.copy(shape.instance.pillarConvex.vertices[0]);
v1.copy(shape.instance.pillarConvex.vertices[1]);
v2.copy(shape.instance.pillarConvex.vertices[2]);
v0.vadd(shape.instance.pillarOffset, v0);
v1.vadd(shape.instance.pillarOffset, v1);
v2.vadd(shape.instance.pillarOffset, v2);
geometry.vertices.push(
new THREE.Vector3(v0.x, v0.y, v0.z),
new THREE.Vector3(v1.x, v1.y, v1.z),
new THREE.Vector3(v2.x, v2.y, v2.z)
new graphics.instance.Vector3(v0.x, v0.y, v0.z),
new graphics.instance.Vector3(v1.x, v1.y, v1.z),
new graphics.instance.Vector3(v2.x, v2.y, v2.z)
);
var i = geometry.vertices.length - 3;
geometry.faces.push(new THREE.Face3(i, i+1, i+2));
geometry.faces.push(new graphics.instance.Face3(i, i+1, i+2));
}
}
}
geometry.computeBoundingSphere();
geometry.computeFaceNormals();
return new THREE.Mesh(geometry, new THREE.MeshNormalMaterial({ wireframe : false, shading : THREE.SmoothShading }));
return new graphics.instance.Mesh(
geometry,
new graphics.instance.MeshNormalMaterial(
{
wireframe: false,
shading : graphics.instance.SmoothShading
}
)
);
};
GameLib.D3.Heightmap.prototype.generateHeightmapDataFromImage = function (
/**
* Needs an RGBA
* @param imagePath string
* @param heightScale
* @param callback
* @constructor
*/
GameLib.D3.Heightmap.GenerateHeightmapDataFromImage = function (
imagePath,
callback // receives HeightmapData instance as the first argument
heightScale,
callback
) {
var img = new Image();
@ -92,18 +129,25 @@ GameLib.D3.Heightmap.prototype.generateHeightmapDataFromImage = function (
var sizeX = img.width,
sizeY = img.height;
for (var i = 0; i < sizeX; i++) {
for (var x = 0; x < sizeX; x++) {
matrix.push([]);
for (var j = 0; j < sizeY; j++) {
var height = (heightList[(sizeX - i) + j * sizeY] / 255) * 15;
matrix[i].push(height);
var height = (heightList[(sizeX - x) + j * sizeY] / 255) * heightScale;
matrix[x].push(height);
}
}
// todo: delete canvas here
callback(new GameLib.D3.HeightmapData(sizeX, sizeY, matrix));
callback(
new GameLib.D3.Heightmap(
sizeX,
sizeY,
matrix,
10,
heightScale
)
);
};
img.src = imagePath;

View File

@ -1,16 +1,63 @@
/**
* Physics Raycast Vehicle Superset
* TODO: body + wheels[]
*
* @param engine GamLib.D3.Engine
* @param chassisBody GamLib.D3.RigidBody
* @constructor
*/
GameLib.D3.RaycastVehicle = function(
engine,
chassisBody
) {
this.vehicleObject = null;
this.engine = engine;
this.engine.isNotCannonThrow();
this.chassisBody = chassisBody;
this.instance = this.createInstance();
};
GameLib.D3.RaycastVehicle.prototype.GetWheelInfo = function(
/**
* private
* @returns {GameLib.D3.RaycastVehicle|GameLib.D3.Physics.RaycastVehicle|*}
*/
GameLib.D3.RaycastVehicle.prototype.createInstance = function() {
var instance = new this.engine.instance.RaycastVehicle({
chassisBody: this.chassisBody.instance
});
) {
// note: need a way to determine which engine we are currently using
return this.vehicleObject.wheelInfos;
this.instance = instance;
return instance;
};
/**
* Adds a raycast wheel to this vehicle
* @param wheel GameLib.D3.RaycastWheel
*/
GameLib.D3.RaycastVehicle.prototype.addWheel = function (
wheel
) {
var options = {};
for (var property in wheel) {
if (wheel.hasOwnProperty(property)){
if (property == 'engine') {
continue;
}
options[property] = wheel[property];
}
}
this.instance.addWheel(options);
};
/**
* Eventually we can wrap wheels
* @returns {*}
* @constructor
*/
GameLib.D3.RaycastVehicle.prototype.getWheelInfo = function() {
return this.instance.wheelInfos;
};

View File

@ -0,0 +1,168 @@
GameLib.D3.RaycastWheel = function(
engine,
chassisConnectionPointLocal,
chassisConnectionPointWorld,
directionLocal,
directionWorld,
axleLocal,
axleWorld,
suspensionRestLength,
suspensionMaxLength,
radius,
suspensionStiffness,
dampingCompression,
dampingRelaxation,
frictionSlip,
steering,
rotation,
deltaRotation,
rollInfluence,
maxSuspensionForce,
isFrontWheel,
clippedInvContactDotSuspension,
suspensionRelativeVelocity,
suspensionForce,
skidInfo,
suspensionLength,
maxSuspensionTravel,
useCustomSlidingRotationalSpeed,
customSlidingRotationalSpeed
) {
this.engine = engine;
this.engine.isNotCannonThrow();
if(typeof chassisConnectionPointLocal == 'undefined') {
chassisConnectionPointLocal = new this.engine.instance.Vec3();
}
this.chassisConnectionPointLocal = chassisConnectionPointLocal;
if(typeof chassisConnectionPointWorld == 'undefined') {
chassisConnectionPointWorld = new this.engine.instance.Vec3();
}
this.chassisConnectionPointWorld = chassisConnectionPointWorld;
if(typeof directionLocal == 'undefined') {
directionLocal = new this.engine.instance.Vec3();
}
this.directionLocal = directionLocal;
if(typeof directionWorld == 'undefined') {
directionWorld = new this.engine.instance.Vec3();
}
this.directionWorld = directionWorld;
if(typeof axleLocal == 'undefined') {
axleLocal = new this.engine.instance.Vec3();
}
this.axleLocal = axleLocal;
if(typeof axleWorld == 'undefined') {
axleWorld = new this.engine.instance.Vec3();
}
this.axleWorld = axleWorld;
if(typeof suspensionRestLength == 'undefined') {
suspensionRestLength = 1;
}
this.suspensionRestLength = suspensionRestLength;
if(typeof suspensionMaxLength == 'undefined') {
suspensionMaxLength = 2;
}
this.suspensionMaxLength = suspensionMaxLength;
if(typeof radius == 'undefined') {
radius = 1;
}
this.radius = radius;
if(typeof suspensionStiffness == 'undefined') {
suspensionStiffness = 100;
}
this.suspensionStiffness = suspensionStiffness;
if(typeof dampingCompression == 'undefined') {
dampingCompression = 10;
}
this.dampingCompression = dampingCompression;
if(typeof dampingRelaxation == 'undefined') {
dampingRelaxation = 10;
}
this.dampingRelaxation = dampingRelaxation;
if(typeof frictionSlip == 'undefined') {
frictionSlip = 10000;
}
this.frictionSlip = frictionSlip;
if(typeof steering == 'undefined') {
steering = 0;
}
this.steering = steering;
if(typeof rotation == 'undefined') {
rotation = 0;
}
this.rotation = rotation;
if(typeof deltaRotation == 'undefined') {
deltaRotation = 0;
}
this.deltaRotation = deltaRotation;
if(typeof rollInfluence == 'undefined') {
rollInfluence = 0.01;
}
this.rollInfluence = rollInfluence;
if(typeof maxSuspensionForce == 'undefined') {
maxSuspensionForce = Number.MAX_VALUE;
}
this.maxSuspensionForce = maxSuspensionForce;
if(typeof isFrontWheel == 'undefined') {
isFrontWheel = true;
}
this.isFrontWheel = isFrontWheel;
if(typeof clippedInvContactDotSuspension == 'undefined') {
clippedInvContactDotSuspension = 1;
}
this.clippedInvContactDotSuspension = clippedInvContactDotSuspension;
if(typeof suspensionRelativeVelocity == 'undefined') {
suspensionRelativeVelocity = 0;
}
this.suspensionRelativeVelocity = suspensionRelativeVelocity;
if(typeof suspensionForce == 'undefined') {
suspensionForce = 0;
}
this.suspensionForce = suspensionForce;
if(typeof skidInfo == 'undefined') {
skidInfo = 0;
}
this.skidInfo = skidInfo;
if(typeof suspensionLength == 'undefined') {
suspensionLength = 0;
}
this.suspensionLength = suspensionLength;
if(typeof maxSuspensionTravel == 'undefined') {
maxSuspensionTravel = 1;
}
this.maxSuspensionTravel = maxSuspensionTravel;
if(typeof useCustomSlidingRotationalSpeed == 'undefined') {
useCustomSlidingRotationalSpeed = false;
}
this.useCustomSlidingRotationalSpeed = useCustomSlidingRotationalSpeed;
if(typeof customSlidingRotationalSpeed == 'undefined') {
customSlidingRotationalSpeed = -0.1;
}
this.customSlidingRotationalSpeed = customSlidingRotationalSpeed;
};

View File

@ -3,14 +3,72 @@
* TODO: body + wheels[]
* @constructor
*/
GameLib.D3.RigidVehicle = function(
GameLib.D3.RigidBodyVehicle = function(
engine
) {
this.vehicleObject = null;
this.engine = engine;
this.engine.isNotCannonThrow();
this.instance = null;
};
GameLib.D3.RigidVehicle.prototype.GetWheelInfo = function(
) {
// note: need a way to determine which engine we are currently using
return this.vehicleObject.wheelBodies;
/**
*
* @returns {*}
*/
GameLib.D3.RigidBodyVehicle.prototype.getWheelInfo = function() {
return this.instance.wheelBodies;
};
/**
*
* @param chassisBody
* @returns {GameLib.D3.RigidVehicle|GameLib.D3.Physics.RigidVehicle}
*/
GameLib.D3.RigidBodyVehicle.prototype.createInstance = function(
chassisBody // Physics.RigidBody
) {
var vehicle = new this.engine.instance.RigidVehicle({
chassisBody: chassisBody.bodyObject
});
this.instance = vehicle;
return vehicle;
};
/**
* Adds a wheel to this rigid body vehicle
* @param rigidBody GameLib.D3.RigidBody
* @param position
* @param axis
* @param direction
* @constructor
*/
GameLib.D3.RigidBodyVehicle.prototype.addWheel = function(
rigidBody,
position,
axis,
direction
) {
this.instance.addWheel({
body: rigidBody.instance,
position: new this.engine.instance.Vec3(
position.x,
position.y,
position.z
),
axis: new this.engine.instance.Vec3(
axis.x,
axis.y,
axis.z
),
direction: new this.engine.instance.Vec3(
direction.x,
direction.y,
direction.z
)
});
};

View File

@ -1,5 +1,6 @@
/**
* RigidBody Superset
* @param engine GameLib.D3.Engine
* @param mass
* @param friction
* @param position
@ -14,11 +15,12 @@
* @param collisionFilterGroup
* @param collisionFilterMask
* @param fixedRotation
* @param shape
* @param shape GameLib.D3.Shape
* @returns {GameLib.D3.Physics.RigidBody}
* @constructor
*/
GameLib.D3.RigidBody = function(
engine,
mass,
friction,
position,
@ -52,43 +54,93 @@ GameLib.D3.RigidBody = function(
this.fixedRotation = typeof fixedRotation == "undefined" ? false : fixedRotation;
this.shape = typeof shape == "undefined" ? null : shape;
this.rigidBodyInstance = this.createRigidBodyInstance();
this.engine = engine;
this.engine.isNotCannonThrow();
this.instance = this.createInstance();
};
/**
*
* private function
* @returns {*}
*/
GameLib.D3.RigidBody.prototype.createRigidBodyInstance = function(
engine
) {
GameLib.D3.RigidBody.prototype.createInstance = function() {
var rigidBody = null;
var instance = new this.engine.instance.Body({
mass: mass,
friction: this.friction,
position: new this.engine.instance.Vec3(
this.position.x,
this.position.y,
this.position.z
),
velocity: new this.engine.instance.Vec3(
this.velocity.x,
this.velocity.y,
this.velocity.z
),
quaternion: new this.engine.instance.Quaternion(
this.quaternion.x,
this.quaternion.y,
this.quaternion.z,
this.quaternion.w
),
angularVelocity: new this.engine.instance.Vec3(
this.angularVelocity.x,
this.angularVelocity.y,
this.angularVelocity.z
),
linearDamping: this.linearDamping,
angularDamping: this.angularDamping,
allowSleep: this.allowSleep,
sleepSpeedLimit: this.sleepSpeedLimit,
sleepTimeLimit: this.sleepTimeLimit,
collisionFilterGroup: this.collisionFilterGroup,
collisionFilterMask: this.collisionFilterMask,
fixedRotation: this.fixedRotation,
shape: this.shape.instance
});
// Create the bodyObject
if(engine.engineType == GameLib.D3.Engine.ENGINE_TYPE_CANNON) {
rigidBody = new engine.instance.Body(
{
mass: mass,
friction: friction,
position: new engine.instance.Vec3(position.x, position.y, position.z),
velocity: new engine.instance.Vec3(velocity.x, velocity.y, velocity.z),
quaternion: new engine.instance.Quaternion(quaternion.x, quaternion.y, quaternion.z, quaternion.w),
angularVelocity: new engine.instance.Vec3(angularVelocity.x, angularVelocity.y, angularVelocity.z),
linearDamping: linearDamping,
angularDamping: angularDamping,
allowSleep: allowSleep,
sleepSpeedLimit: sleepSpeedLimit,
sleepTimeLimit: sleepTimeLimit,
collisionFilterGroup: collisionFilterGroup,
collisionFilterMask: collisionFilterMask,
fixedRotation: fixedRotation,
shape: shape
}
);
this.instance = instance;
}
return rigidBody;
return instance;
};
/**
* Adds a shape to this rigid body
* @param shape GameLib.D3.Shape
* @param offset GameLib.D3.Vector3
* @param orientation GameLib.D3.Vector4
* @constructor
*/
GameLib.D3.RigidBody.prototype.addShape = function(
shape,
offset,
orientation
) {
if (!offset) {
offset = new GameLib.D3.Vector3(0,0,0);
}
if (!orientation) {
orientation = new GameLib.D3.Vector4(0,0,0,1);
}
this.instance.addShape(
shape.instance,
new this.engine.instance.Vec3(
offset.x,
offset.y,
offset.z
),
new this.engine.instance.Quaternion(
orientation.x,
orientation.y,
orientation.z,
orientation.w
)
);
};

View File

@ -1,37 +1,177 @@
/**
* Physics Shape Superset
* @param engine GameLib.D3.Engine
* @param shapeType
* @param scale GameLib.D3.Vector3
* @param vertices Number[]
* @param indices Number[]
* @param radius Number
* @param halfExtensions GameLib.D3.Vector3
* @param radiusTop Number
* @param radiusBottom Number
* @param height Number
* @param numSegments Number
* @param heightmap GameLib.D3.Heightmap
* @param elementSize
* @constructor
*/
GameLib.D3.Physics.Shape = function(
shapeObject, // Physics engine specific
shapeType
GameLib.D3.Shape = function(
engine,
shapeType,
scale,
vertices,
indices,
radius,
halfExtensions,
radiusTop,
radiusBottom,
height,
numSegments,
heightmap
) {
this.shapeObject = shapeObject;
this.engine = engine;
this.engine.isNotCannonThrow();
this.shapeType = shapeType;
this.scale = new GameLib.D3.Vector3(1, 1, 1);
this.instance = this.createInstance();
if (typeof scale == 'undefined') {
scale = new GameLib.D3.Vector3(1, 1, 1)
}
this.scale = scale;
if (typeof vertices == 'undefined') {
vertices = [];
}
this.vertices = vertices;
if (typeof indices == 'undefined') {
indices = [];
}
this.indices = indices;
if (typeof radius == 'undefined') {
radius = 1;
}
this.radius = radius;
if (typeof halfExtensions == 'undefined') {
halfExtensions = new GameLib.D3.Vector3(1,1,1);
}
this.halfExtensions = halfExtensions;
if (typeof radiusTop == 'undefined') {
radiusTop = 1;
}
this.radiusTop = radiusTop;
if (typeof radiusBottom == 'undefined') {
radiusBottom = 1;
}
this.radiusBottom = radiusBottom;
if (typeof height == 'undefined') {
height = 1;
}
this.height = height;
if (typeof numSegments == 'undefined') {
numSegments = 1;
}
this.numSegments = numSegments;
if (typeof heightmap == 'undefined') {
heightmap = new GameLib.D3.Heightmap();
}
this.heightmap = heightmap;
};
GameLib.D3.Physics.SHAPE_TYPE_SPHERE = 1;
GameLib.D3.Physics.SHAPE_TYPE_BOX = 2;
GameLib.D3.Physics.SHAPE_TYPE_TRIMESH = 3;
GameLib.D3.Physics.SHAPE_TYPE_CYLINDER = 4;
/**
* Shape constants
* @type {number}
*/
GameLib.D3.Shape.SHAPE_TYPE_SPHERE = 1;
GameLib.D3.Shape.SHAPE_TYPE_BOX = 2;
GameLib.D3.Shape.SHAPE_TYPE_TRIMESH = 3;
GameLib.D3.Shape.SHAPE_TYPE_CYLINDER = 4;
GameLib.D3.Shape.SHAPE_TYPE_HEIGHT_MAP = 5;
GameLib.D3.Shape.SHAPE_TYPE_CONVEX_HULL = 6;
/**
*
*/
GameLib.D3.Shape.prototype.createInstance = function() {
var instance = null;
if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_TRIMESH) {
instance = new this.engine.instance.Trimesh(
this.vertices,
this.indices
);
} else if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_SPHERE) {
instance = new this.engine.instance.Sphere(
this.radius
);
} else if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_BOX) {
instance = new this.engine.instance.Box(
new this.engine.instance.Vec3(
this.halfExtensions.x,
this.halfExtensions.y,
this.halfExtensions.z
)
);
} else if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_CYLINDER) {
instance = new this.engine.instance.Cylinder(
this.radiusTop,
this.radiusBottom,
this.height,
this.numSegments
);
} else if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_HEIGHT_MAP) {
instance = new this.engine.instance.Heightfield(
this.heightmap.matrix,
{
elementSize: this.heightmap.elementSize
}
);
} else if (this.shapeType == GameLib.D3.Shape.SHAPE_TYPE_CONVEX_HULL) {
console.warn('Shape type not implemented: ' + this.shapeType);
throw new Error('Shape type not implemented: ' + this.shapeType);
} else {
console.warn('Shape type not implemented: ' + this.shapeType);
throw new Error('Shape type not implemented: ' + this.shapeType);
}
this.instance = instance;
return instance;
};
GameLib.D3.Physics.Shape.prototype.Update = function() {
if(this.physics.engineType === GameLib.D3.Physics.TYPE_CANNON) {
if(this.shapeType === GameLib.D3.Physics.SHAPE_TYPE_TRIMESH) {
this.shapeObject.setScale(
new this.physics.CANNON.Vec3(
this.scale.x,
this.scale.y,
this.scale.z
)
);
this.shapeObject.updateAABB();
this.shapeObject.updateNormals();
this.shapeObject.updateEdges();
this.shapeObject.updateBoundingSphereRadius();
this.shapeObject.updateTree();
}
/**
* update
*/
GameLib.D3.Shape.prototype.update = function(
engine
) {
engine.isNotCannonThrow();
if(this.shapeType === GameLib.D3.Shape.SHAPE_TYPE_TRIMESH) {
this.instance.setScale(
new engine.instance.Vec3(
this.scale.x,
this.scale.y,
this.scale.z
)
);
this.instance.updateAABB();
this.instance.updateNormals();
this.instance.updateEdges();
this.instance.updateBoundingSphereRadius();
this.instance.updateTree();
}
};

View File

@ -1,21 +1,25 @@
GameLib.D3.SkyBox = function (
graphics
) {
this.id = null;
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.texturesFolder = null;
};
GameLib.D3.SkyBox.prototype.Load = function (
GameLib.D3.SkyBox.prototype.load = function (
texturesFolder
) {
this.texturesFolder = texturesFolder;
this.textures = [];
this.materials = [];
this.mesh = {};
this.scene = new THREE.Scene();
this.scene = new this.graphics.instance.Scene();
this.textureCube = null;
var textureLoader = new THREE.TextureLoader();
var textureLoader = new this.graphics.instance.TextureLoader();
// this textures are used to display the skybox
this.textures.push(textureLoader.load(this.texturesFolder + "px.png"));
@ -27,29 +31,29 @@ GameLib.D3.SkyBox.prototype.Load = function (
// assign textures to each cube face
for (var i = 0; i < 6; i ++) {
this.materials.push(new THREE.MeshBasicMaterial({ map: this.textures[i] }));
this.materials.push(new this.graphics.instance.MeshBasicMaterial({ map: this.textures[i] }));
}
// create cube geometry
this.mesh = new THREE.Mesh(new THREE.CubeGeometry(1, 1, 1), new THREE.MeshFaceMaterial(this.materials));
this.mesh.applyMatrix(new THREE.Matrix4().makeScale(1, 1, -1));
this.mesh = new this.graphics.instance.Mesh(new this.graphics.instance.CubeGeometry(1, 1, 1), new this.graphics.instance.MeshFaceMaterial(this.materials));
this.mesh.applyMatrix(new this.graphics.instance.Matrix4().makeScale(1, 1, -1));
this.scene.add(this.mesh);
// Load env textureCube
// this is used for reflections on meshes
// mesh.material.envMap = this.textureCube;
this.textureCube = new THREE.CubeTextureLoader().load([
this.textureCube = new this.graphics.instance.CubeTextureLoader().load([
this.texturesFolder + "px.png", this.texturesFolder + "nx.png",
this.texturesFolder + "py.png", this.texturesFolder + "ny.png",
this.texturesFolder + "pz.png", this.texturesFolder + "nz.png"
]);
};
GameLib.D3.SkyBox.prototype.Render = function (
GameLib.D3.SkyBox.prototype.render = function (
threeRenderer,
threeCamera
) {
var cameraPosition = new THREE.Vector3(threeCamera.position.x, threeCamera.position.y, threeCamera.position.z);
var cameraPosition = new this.graphics.instance.Vector3(threeCamera.position.x, threeCamera.position.y, threeCamera.position.z);
threeCamera.position.set(0, 0, 0);

View File

@ -51,226 +51,76 @@ GameLib.D3.World = function(
}
this.rigidBodies = rigidBodies;
this.engine = null;
this.worldInstance = null;
/**
* We only set the physics property if we pass it in the constructor,
* because we don't always want the physics object (ex. when we store this world to the API - we also don't then
* want to store the custom worlds - we want to generate them after loading from API)
*/
if (engine) {
this.engine = engine;
this.worldInstance = this.createWorldInstance();
}
};
GameLib.D3.World.prototype.createWorldInstance = function() {
this.engine = engine;
this.engine.isNotCannonThrow();
var customWorld = new this.engine.instance.World();
var cannonBroadphase = null;
customWorld.broadphase = cannonBroadphase;
var cannonSolver = null;
if (this.solver.solverType == GameLib.D3.Physics.SPLIT_SOLVER) {
cannonSolver = new this.engine.instance.SplitSolver();
} else if (this.solver.solverType == GameLib.D3.Physics.GS_SOLVER) {
cannonSolver = new this.engine.instance.GSSolver();
cannonSolver.iterations = this.solver.iterations;
}
customWorld.solver = cannonSolver;
customWorld.gravity.x = this.gravity.x;
customWorld.gravity.y = this.gravity.y;
customWorld.gravity.z = this.gravity.z;
for (var b = 0; b < this.rigidBodies.length; b++) {
var customBody = this.createCustomBody(this.rigidBodies[b]);
//customWorld.AddRigidBody();
}
customWorld.name = this.name;
return customWorld;
this.instance = this.createInstance();
};
GameLib.D3.World.prototype.AddShape = function(
shape, // d3.physics.shape
rigidBody,
offset, // vec3
orientation //quaternion
/**
* private
* @returns {GameLib.D3.World|GameLib.D3.Physics.World|*}
*/
GameLib.D3.World.prototype.createInstance = function() {
var instance = new this.engine.instance.World();
instance.broadphase = this.broadphase.instance;
instance.solver = this.solver.instance;
instance.gravity.x = this.gravity.x;
instance.gravity.y = this.gravity.y;
instance.gravity.z = this.gravity.z;
instance.name = this.name;
return instance;
};
/**
*
* @param rigidBody GameLib.D3.RigidBody
* @constructor
*/
GameLib.D3.World.prototype.addRigidBody = function(
rigidBody
) {
shape.shape = shape;
/**
* TODO:: fix this?
*/
if (this.physics.engineType === GameLib.D3.Physics.TYPE_CANNON) {
var _offset = null;
var _orientation = null;
if(offset != null && typeof offset !== 'undefined') {
_offset = new this.physics.CANNON.Vec3(offset.x, offset.y, offset.z);
}
if(orientation != null && typeof orientation !== 'undefined') {
_orientation = new this.physics.CANNON.Quaternion(orientation.x, orientation.y, orientation.z, orientation.w);
}
rigidBody.bodyObject.addShape(shape.shapeObject, _offset, _orientation);
}
this.instance.addBody(rigidBody.instance);
};
GameLib.D3.World.prototype.Wheel = function() {
};
GameLib.D3.World.prototype.CreateRigidVehicle = function(
chassisBody // Physics.RigidBody
) {
var rigidVehicle = new GameLib.D3.Physics.RigidVehicle();
if (this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) {
var vehicle = new this.physics.CANNON.RigidVehicle({
chassisBody: chassisBody.bodyObject
});
rigidVehicle.vehicleObject = vehicle;
return rigidVehicle;
}
};
GameLib.D3.World.prototype.CreateRaycastVehicle = function(
chassisBody // Physics.RigidBody
) {
var raycastVehicle = new GameLib.D3.Physics.RaycastVehicle();
if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) {
var vehicle = new this.physics.CANNON.RaycastVehicle({
chassisBody: chassisBody.bodyObject
});
raycastVehicle.vehicleObject = vehicle;
return raycastVehicle;
}
};
GameLib.D3.World.prototype.AddWheelToRigidVehicle = function(
vehicle,
rigidBody,
position,
axis,
direction
) {
if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) {
vehicle.vehicleObject.addWheel({
body: rigidBody.bodyObject,
position: new this.physics.CANNON.Vec3(position.x, position.y, position.z),
axis: new this.physics.CANNON.Vec3(axis.x, axis.y, axis.z),
direction: new this.physics.CANNON.Vec3(direction.x, direction.y, direction.z)
});
}
};
GameLib.D3.World.prototype.AddWheelToRaycastVehicle = function (
vehicle, // physics.raycastvehicle
options // cannon options
) {
if (this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) {
vehicle.vehicleObject.addWheel(options);
} else {
console.log("function for engine not implemented");
}
};
GameLib.D3.World.prototype.CreateTriMeshShape = function(
vertices, // flat list of floats
indices // flat list of floats
) {
if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) {
return new GameLib.D3.Physics.Shape(new this.physics.CANNON.Trimesh(vertices, indices), GameLib.D3.Physics.SHAPE_TYPE_TRIMESH);
}
};
GameLib.D3.World.prototype.CreateSphereShape = function (
radius
) {
if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) {
return new GameLib.D3.Physics.Shape(new this.physics.CANNON.Sphere(radius), GameLib.D3.Physics.SHAPE_TYPE_SPHERE);
}
};
GameLib.D3.World.prototype.CreateBoxShape = function(
halfExtensions // vec3
) {
if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) {
return new GameLib.D3.Physics.Shape(new this.physics.CANNON.Box(new this.physics.CANNON.Vec3(halfExtensions.x, halfExtensions.y, halfExtensions.z)), GameLib.D3.Physics.SHAPE_TYPE_BOX);
}
};
GameLib.D3.World.prototype.CreateCylinderShape = function(
radiusTop,
radiusBottom,
height,
numSegments
) {
if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) {
return new GameLib.D3.Physics.Shape(new this.physics.CANNON.Cylinder(radiusTop, radiusBottom, height, numSegments), GameLib.D3.Physics.SHAPE_TYPE_CYLINDER);
}
};
GameLib.D3.World.prototype.AddRigidBody = function(
rigidBody // Physics.RigidBody
) {
if(this.physics.engineType === GameLib.D3.Physics.TYPE_CANNON) {
this.worldObject.addBody(rigidBody.bodyObject);
}
};
GameLib.D3.World.prototype.AddVehicle = function(
/**
*
* @param vehicle (GameLib.D3.RigidBodyVehicle | GameLib.D3.RaycastVehicle)
* @constructor
*/
GameLib.D3.World.prototype.addVehicle = function(
vehicle // note: physics.vehicle
) {
if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) {
vehicle.vehicleObject.addToWorld(this.worldObject);
}
vehicle.instance.addToWorld(this.world.instance);
};
GameLib.D3.World.prototype.Step = function(
GameLib.D3.World.prototype.step = function(
timeStep
) {
if(this.physics.engineType == GameLib.D3.Physics.TYPE_CANNON) {
// todo: figure out, why this call to internal step is more stable for trimesh collisions.....
//this.worldObject.internalStep(timeStep);
//return;
var now = performance.now() / 1000;
if(!this.lastCallTime){
// last call time not saved, cant guess elapsed time. Take a simple step.
this.worldObject.step(timeStep);
this.lastCallTime = now;
return;
}
var timeSinceLastCall = now - this.lastCallTime;
this.worldObject.step(timeStep, timeSinceLastCall);
// todo: figure out, why this call to internal step is more stable for trimesh collisions.....
//this.worldObject.internalStep(timeStep);
//return;
var now = performance.now() / 1000;
if(!this.lastCallTime){
// last call time not saved, cant guess elapsed time. Take a simple step.
this.instance.step(timeStep);
this.lastCallTime = now;
return;
}
var timeSinceLastCall = now - this.lastCallTime;
this.instance.step(timeStep, timeSinceLastCall);
this.lastCallTime = now;
};
GameLib.D3.World.prototype.GetIndexedVertices = function(
@ -291,7 +141,17 @@ GameLib.D3.World.prototype.GetIndexedVertices = function(
};
GameLib.D3.World.prototype.GenerateWireframeViewMesh = function(
/**
* TODO: FIX
* @param triangleMeshShape
* @param normalLength
* @param scale
* @param opacity
* @param wireframeColor
* @returns {THREE.Mesh|this.meshes}
* @constructor
*/
GameLib.D3.World.GenerateWireframeViewMesh = function(
triangleMeshShape,
normalLength,
scale,
@ -350,7 +210,18 @@ GameLib.D3.World.prototype.GenerateWireframeViewMesh = function(
return wireframeTHREEMesh;
};
GameLib.D3.World.prototype.GenerateTriangleCollisionMesh = function(
/**
* TODO: FIX
* @param threeMesh
* @param mass
* @param friction
* @param createCollisionSubMeshes
* @param facesPerSubsection
* @param subsectionsToMerge
* @returns {Array}
* @constructor
*/
GameLib.D3.World.GenerateTriangleCollisionMesh = function(
threeMesh,
mass, // default = 0
friction, // default = 10