start to fix - at 'Heightmap'

beta.r3js.org
Theunis J. Botha 2016-10-14 13:08:22 +02:00
parent d002814c40
commit 48c44abf93
9 changed files with 4802 additions and 82 deletions

2
build/game-lib-min.js vendored Normal file

File diff suppressed because one or more lines are too long

4668
build/game-lib.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -11,8 +11,8 @@ gulp.task(
.pipe(concat('game-lib.js')) .pipe(concat('game-lib.js'))
.pipe(minify({ .pipe(minify({
ext:{ ext:{
src:'-debug.js', src:'.js',
min:'.js' min:'-min.js'
} }
})) }))
.pipe(gulp.dest('./build/')); .pipe(gulp.dest('./build/'));

View File

@ -1,14 +1,18 @@
/** /**
* Physics Broadphase Superset * Physics Broadphase Superset
* @param id * @param id
* @param name * @param name String
* @param broadphaseType * @param broadphaseType Number
* @param engine GameLib.D3.Engine
* @param createInstance Boolean
* @constructor * @constructor
*/ */
GameLib.D3.Broadphase = function( GameLib.D3.Broadphase = function(
id, id,
name, name,
broadphaseType broadphaseType,
engine,
createInstance
) { ) {
this.id = id; this.id = id;
@ -18,11 +22,50 @@ GameLib.D3.Broadphase = function(
this.name = name; this.name = name;
if (typeof broadphaseType == 'undefined') { if (typeof broadphaseType == 'undefined') {
console.warn('undefined broadphase type'); broadphaseType = GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE;
throw new Error('undefined broadphase type'); }
this.broadphaseType = broadphaseType;
if (typeof engine == 'undefined') {
engine = null;
}
this.engine = engine;
this.instance = null;
if (createInstance) {
this.createInstance();
}
};
/**
* Creates a custom Broadphase instance based on the engine type
*/
GameLib.D3.Broadphase.prototype.createInstance = function() {
if (!(this.engine instanceof GameLib.D3.Engine)) {
console.warn('No Engine');
throw new Error('No Engine');
} }
this.broadphaseType = broadphaseType; this.engine.isNotCannonThrow();
var instance = null;
if (this.broadphaseType == GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE) {
instance = new this.engine.instance.NaiveBroadphase();
} else if (this.broadphaseType == GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID) {
instance = new this.engine.instance.GridBroadphase();
} else if (this.broadphaseType == GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP) {
instance = new this.engine.instance.SAPBroardphase();
} else {
console.warn('Unsupported broadphase type: ' + this.broadphaseType);
throw new Error('Unsupported broadphase type: ' + this.broadphaseType);
}
this.instance = instance;
return instance;
}; };
/** /**

View File

@ -11,8 +11,4 @@ GameLib.D3.Color = function(r, g, b, a) {
this.g = g; this.g = g;
this.b = b; this.b = b;
this.a = a; this.a = a;
}; };
if (typeof module !== 'undefined') {
module.exports = GameLib.D3.Color;
}

View File

@ -1,7 +1,7 @@
/** /**
* Engine Superset * Engine Superset
* @param engineType * @param engineType
* @param instance * @param instance {CANNON | Ammo | Goblin}
* @constructor * @constructor
*/ */
GameLib.D3.Engine = function( GameLib.D3.Engine = function(
@ -30,7 +30,6 @@ GameLib.D3.Engine.prototype.isNotCannonThrow = function() {
} }
}; };
/** /**
* True if Ammo physics * True if Ammo physics
* @returns {boolean} * @returns {boolean}
@ -53,8 +52,4 @@ GameLib.D3.Engine.prototype.isGoblin = function() {
*/ */
GameLib.D3.Engine.ENGINE_TYPE_CANNON = 0x1; GameLib.D3.Engine.ENGINE_TYPE_CANNON = 0x1;
GameLib.D3.Engine.ENGINE_TYPE_AMMO = 0x2; GameLib.D3.Engine.ENGINE_TYPE_AMMO = 0x2;
GameLib.D3.Engine.ENGINE_TYPE_GOBLIN = 0x3; GameLib.D3.Engine.ENGINE_TYPE_GOBLIN = 0x3;
if (typeof module !== 'undefined') {
module.exports = GameLib.D3.Engine;
}

View File

@ -49,9 +49,12 @@ GameLib.D3.FlyControls = function(
this.element.requestPointerLock = this.element.requestPointerLock || this.element.mozRequestPointerLock || this.element.webkitRequestPointerLock; this.element.requestPointerLock = this.element.requestPointerLock || this.element.mozRequestPointerLock || this.element.webkitRequestPointerLock;
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock; document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
} }
}; };
/**
* Go forward / backward on mouse wheel
* @param event
*/
GameLib.D3.FlyControls.prototype.onMouseWheel = function(event) { GameLib.D3.FlyControls.prototype.onMouseWheel = function(event) {
this.moveForward = true; this.moveForward = true;
this.applyTranslation(event.wheelDelta * 0.001); this.applyTranslation(event.wheelDelta * 0.001);
@ -59,42 +62,39 @@ GameLib.D3.FlyControls.prototype.onMouseWheel = function(event) {
this.moveForward = false; this.moveForward = false;
}; };
/**
* Start rotating the camera on mouse middle button down
* @param event
*/
GameLib.D3.FlyControls.prototype.onMouseDown = function(event) { GameLib.D3.FlyControls.prototype.onMouseDown = function(event) {
// if (event.button == 0) {
// this.canRotate = true;
// this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
// if (this.havePointerLock) {
// this.element.requestPointerLock();
// }
// }
if (event.button == 1) { if (event.button == 1) {
this.canRotate = true; this.canRotate = true;
this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false); this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
} }
}; };
/**
* Stop rotating on middle mouse button down
* @param event
*/
GameLib.D3.FlyControls.prototype.onMouseUp = function(event) { GameLib.D3.FlyControls.prototype.onMouseUp = function(event) {
// if (event.button == 0) {
// this.canRotate = false;
// this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
// if (this.havePointerLock) {
// document.exitPointerLock();
// }
// }
if (event.button == 1) { if (event.button == 1) {
this.canRotate = false; this.canRotate = false;
this.canvas.removeEventListener('mousemove', this.mouseMoveCallback); this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
} }
}; };
/**
* Apply current yaw and pitch to camera
*/
GameLib.D3.FlyControls.prototype.applyRotation = function() { GameLib.D3.FlyControls.prototype.applyRotation = function() {
this.camera.rotation.set(this.pitch, this.yaw, 0, "YXZ"); this.camera.rotation.set(this.pitch, this.yaw, 0, "YXZ");
}; };
/**
* Apply current position to camera
* @param deltaTime
*/
GameLib.D3.FlyControls.prototype.applyTranslation = function(deltaTime) { GameLib.D3.FlyControls.prototype.applyTranslation = function(deltaTime) {
var direction = new this.THREE.Vector3(0, 0, -1); var direction = new this.THREE.Vector3(0, 0, -1);
var rotation = new this.THREE.Euler(0, 0, 0, "YXZ"); var rotation = new this.THREE.Euler(0, 0, 0, "YXZ");
@ -102,41 +102,35 @@ GameLib.D3.FlyControls.prototype.applyTranslation = function(deltaTime) {
direction = direction.applyEuler(rotation); direction = direction.applyEuler(rotation);
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
if(this.moveForward) { if(this.moveForward) {
var newPos = direction.normalize(); this.camera.position.x += forward.x * (deltaTime * this.flySpeed);
this.camera.position.x += newPos.x * (deltaTime * this.flySpeed); this.camera.position.y += forward.y * (deltaTime * this.flySpeed);
this.camera.position.y += newPos.y * (deltaTime * this.flySpeed); this.camera.position.z += forward.z * (deltaTime * this.flySpeed);
this.camera.position.z += newPos.z * (deltaTime * this.flySpeed);
} else if(this.moveBackward) { } else if(this.moveBackward) {
var newPos = direction.normalize(); this.camera.position.x -= forward.x * (deltaTime * this.flySpeed);
this.camera.position.x -= newPos.x * (deltaTime * this.flySpeed); this.camera.position.y -= forward.y * (deltaTime * this.flySpeed);
this.camera.position.y -= newPos.y * (deltaTime * this.flySpeed); this.camera.position.z -= forward.z * (deltaTime * this.flySpeed);
this.camera.position.z -= newPos.z * (deltaTime * this.flySpeed);
} }
if(this.moveLeft) { if(this.moveLeft) {
var forward = direction.normalize(); this.camera.position.x -= right.x * (deltaTime * this.flySpeed);
var right = forward.cross(new this.THREE.Vector3(0, 1, 0)); this.camera.position.y -= right.y * (deltaTime * this.flySpeed);
var newPos = right; this.camera.position.z -= right.z * (deltaTime * this.flySpeed);
this.camera.position.x -= newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y -= newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z -= newPos.z * (deltaTime * this.flySpeed);
} else if(this.moveRight) { } else if(this.moveRight) {
var forward = direction.normalize(); this.camera.position.x += right.x * (deltaTime * this.flySpeed);
var right = forward.cross(new this.THREE.Vector3(0, 1, 0)); this.camera.position.y += right.y * (deltaTime * this.flySpeed);
var newPos = right; this.camera.position.z += right.z * (deltaTime * this.flySpeed);
this.camera.position.x += newPos.x * (deltaTime * this.flySpeed);
this.camera.position.y += newPos.y * (deltaTime * this.flySpeed);
this.camera.position.z += newPos.z * (deltaTime * this.flySpeed);
} }
// Absolute Y-Axis
if(this.moveUp) { if(this.moveUp) {
this.camera.position.y += (deltaTime * this.flySpeed); this.camera.position.y += (deltaTime * this.flySpeed);
@ -148,11 +142,20 @@ GameLib.D3.FlyControls.prototype.applyTranslation = function(deltaTime) {
} }
}; };
/**
* This update function should be called from the animation function in order to apply the 'frame rate independent'
* movement to the camera
* @param deltaTime
*/
GameLib.D3.FlyControls.prototype.update = function(deltaTime) { GameLib.D3.FlyControls.prototype.update = function(deltaTime) {
this.applyRotation(); this.applyRotation();
this.applyTranslation(deltaTime); this.applyTranslation(deltaTime);
}; };
/**
* Rotate on mouse move
* @param event
*/
GameLib.D3.FlyControls.prototype.onMouseMove = function ( event ) { GameLib.D3.FlyControls.prototype.onMouseMove = function ( event ) {
if (this.canRotate) { if (this.canRotate) {
var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0; var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
@ -163,6 +166,10 @@ GameLib.D3.FlyControls.prototype.onMouseMove = function ( event ) {
} }
}; };
/**
* Keyboard controls
* @param event
*/
GameLib.D3.FlyControls.prototype.onKeyDown = function ( event ) { GameLib.D3.FlyControls.prototype.onKeyDown = function ( event ) {
switch ( event.keyCode ) { switch ( event.keyCode ) {
@ -192,6 +199,10 @@ GameLib.D3.FlyControls.prototype.onKeyDown = function ( event ) {
} }
}; };
/**
* Keyboard controls
* @param event
*/
GameLib.D3.FlyControls.prototype.onKeyUp = function ( event ) { GameLib.D3.FlyControls.prototype.onKeyUp = function ( event ) {
switch ( event.keyCode ) { switch ( event.keyCode ) {

View File

@ -1,22 +1,36 @@
GameLib.D3.HeightmapData = function ( /**
*
* @param sizeX Number
* @param sizeY Number
* @param matrix matrix 2D Array with height data (Column Major)
* @constructor
*/
GameLib.D3.Heightmap = function(
sizeX, sizeX,
sizeY, sizeY,
matrix matrix
) { ) {
this.sizeX = sizeX || 0; if (typeof sizeX == 'undefined') {
this.sizeY = sizeY || 0; sizeX = 0;
}
this.sizeX = sizeX;
// 2D Array with height data if (typeof sizeY == 'undefined') {
// Column-major sizeY = 0;
this.matrix = matrix || []; }
this.sizeY = sizeY;
if (typeof matrix == 'undefined') {
matrix = [];
}
this.matrix = matrix;
}; };
// Note: this currently only works for cannon! GameLib.D3.Heightmap.prototype.generateThreeMeshFromHeightField = function(
GameLib.D3.GenerateThreeMeshFromHeightField = function ( THREE,
heightFieldShape heightFieldShape,
// Physics type..... engine
) { ) {
var geometry = new THREE.Geometry(); var geometry = new THREE.Geometry();
var v0 = new this.physics.CANNON.Vec3(); var v0 = new this.physics.CANNON.Vec3();
@ -50,7 +64,8 @@ GameLib.D3.GenerateThreeMeshFromHeightField = function (
return new THREE.Mesh(geometry, new THREE.MeshNormalMaterial({ wireframe : false, shading : THREE.SmoothShading })); return new THREE.Mesh(geometry, new THREE.MeshNormalMaterial({ wireframe : false, shading : THREE.SmoothShading }));
}; };
GameLib.D3.GenerateHeightmapDataFromImage = function (
GameLib.D3.Heightmap.prototype.generateHeightmapDataFromImage = function (
imagePath, imagePath,
callback // receives HeightmapData instance as the first argument callback // receives HeightmapData instance as the first argument
) { ) {

View File

@ -75,16 +75,6 @@ GameLib.D3.World.prototype.createWorldInstance = function() {
var cannonBroadphase = null; var cannonBroadphase = null;
if (this.broadphase.broadphaseType == GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE) {
cannonBroadphase = new this.engine.instance.NaiveBroadphase();
} else if (this.broadphase.broadphaseType == GameLib.D3.Broadphase.BROADPHASE_TYPE_GRID) {
cannonBroadphase = new this.engine.instance.GridBroadphase();
} else if (this.broadphase.broadphaseType == GameLib.D3.Broadphase.BROADPHASE_TYPE_SAP) {
cannonBroadphase = new this.engine.instance.SAPBroardphase();
} else {
console.warn('Unsupported broadphase type: ' + this.broadphase.broadphaseType);
throw new Error('Unsupported broadphase type: ' + this.broadphase.broadphaseType);
}
customWorld.broadphase = cannonBroadphase; customWorld.broadphase = cannonBroadphase;