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(minify({
ext:{
src:'-debug.js',
min:'.js'
src:'.js',
min:'-min.js'
}
}))
.pipe(gulp.dest('./build/'));

View File

@ -1,14 +1,18 @@
/**
* Physics Broadphase Superset
* @param id
* @param name
* @param broadphaseType
* @param name String
* @param broadphaseType Number
* @param engine GameLib.D3.Engine
* @param createInstance Boolean
* @constructor
*/
GameLib.D3.Broadphase = function(
id,
name,
broadphaseType
broadphaseType,
engine,
createInstance
) {
this.id = id;
@ -18,11 +22,50 @@ GameLib.D3.Broadphase = function(
this.name = name;
if (typeof broadphaseType == 'undefined') {
console.warn('undefined broadphase type');
throw new Error('undefined broadphase type');
broadphaseType = GameLib.D3.Broadphase.BROADPHASE_TYPE_NAIVE;
}
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.b = b;
this.a = a;
};
if (typeof module !== 'undefined') {
module.exports = GameLib.D3.Color;
}
};

View File

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

View File

@ -49,9 +49,12 @@ GameLib.D3.FlyControls = function(
this.element.requestPointerLock = this.element.requestPointerLock || this.element.mozRequestPointerLock || this.element.webkitRequestPointerLock;
document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock;
}
};
/**
* Go forward / backward on mouse wheel
* @param event
*/
GameLib.D3.FlyControls.prototype.onMouseWheel = function(event) {
this.moveForward = true;
this.applyTranslation(event.wheelDelta * 0.001);
@ -59,42 +62,39 @@ GameLib.D3.FlyControls.prototype.onMouseWheel = function(event) {
this.moveForward = false;
};
/**
* Start rotating the camera on mouse middle button down
* @param 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) {
this.canRotate = true;
this.canvas.addEventListener('mousemove', this.mouseMoveCallback, false);
}
};
/**
* Stop rotating on middle mouse button down
* @param 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) {
this.canRotate = false;
this.canvas.removeEventListener('mousemove', this.mouseMoveCallback);
}
};
/**
* Apply current yaw and pitch to camera
*/
GameLib.D3.FlyControls.prototype.applyRotation = function() {
this.camera.rotation.set(this.pitch, this.yaw, 0, "YXZ");
};
/**
* Apply current position to camera
* @param deltaTime
*/
GameLib.D3.FlyControls.prototype.applyTranslation = function(deltaTime) {
var direction = new this.THREE.Vector3(0, 0, -1);
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);
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
if(this.moveForward) {
var newPos = direction.normalize();
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);
this.camera.position.x += forward.x * (deltaTime * this.flySpeed);
this.camera.position.y += forward.y * (deltaTime * this.flySpeed);
this.camera.position.z += forward.z * (deltaTime * this.flySpeed);
} else if(this.moveBackward) {
var newPos = direction.normalize();
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);
this.camera.position.x -= forward.x * (deltaTime * this.flySpeed);
this.camera.position.y -= forward.y * (deltaTime * this.flySpeed);
this.camera.position.z -= forward.z * (deltaTime * this.flySpeed);
}
if(this.moveLeft) {
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
var newPos = right;
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);
this.camera.position.x -= right.x * (deltaTime * this.flySpeed);
this.camera.position.y -= right.y * (deltaTime * this.flySpeed);
this.camera.position.z -= right.z * (deltaTime * this.flySpeed);
} else if(this.moveRight) {
var forward = direction.normalize();
var right = forward.cross(new this.THREE.Vector3(0, 1, 0));
var newPos = right;
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);
this.camera.position.x += right.x * (deltaTime * this.flySpeed);
this.camera.position.y += right.y * (deltaTime * this.flySpeed);
this.camera.position.z += right.z * (deltaTime * this.flySpeed);
}
// Absolute Y-Axis
if(this.moveUp) {
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) {
this.applyRotation();
this.applyTranslation(deltaTime);
};
/**
* Rotate on mouse move
* @param event
*/
GameLib.D3.FlyControls.prototype.onMouseMove = function ( event ) {
if (this.canRotate) {
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 ) {
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 ) {
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,
sizeY,
matrix
) {
this.sizeX = sizeX || 0;
this.sizeY = sizeY || 0;
if (typeof sizeX == 'undefined') {
sizeX = 0;
}
this.sizeX = sizeX;
// 2D Array with height data
// Column-major
this.matrix = matrix || [];
if (typeof sizeY == 'undefined') {
sizeY = 0;
}
this.sizeY = sizeY;
if (typeof matrix == 'undefined') {
matrix = [];
}
this.matrix = matrix;
};
// Note: this currently only works for cannon!
GameLib.D3.GenerateThreeMeshFromHeightField = function (
heightFieldShape
// Physics type.....
GameLib.D3.Heightmap.prototype.generateThreeMeshFromHeightField = function(
THREE,
heightFieldShape,
engine
) {
var geometry = new THREE.Geometry();
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 }));
};
GameLib.D3.GenerateHeightmapDataFromImage = function (
GameLib.D3.Heightmap.prototype.generateHeightmapDataFromImage = function (
imagePath,
callback // receives HeightmapData instance as the first argument
) {

View File

@ -75,16 +75,6 @@ GameLib.D3.World.prototype.createWorldInstance = function() {
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;