From bcfa3b6df28a513efd621b51d9e82bff4a633db6 Mon Sep 17 00:00:00 2001 From: polygonboutique Date: Fri, 11 Nov 2016 16:25:39 +0100 Subject: [PATCH 1/3] 3d text, gui scene triggers: onenter, onleave, oninside --- src/game-lib-component-trigger-box-sphere.js | 85 ++++++++++--------- ...ame-lib-component-trigger-sphere-sphere.js | 31 ++++++- src/game-lib-game.js | 2 +- src/game-lib-scene.js | 22 +++-- 4 files changed, 89 insertions(+), 51 deletions(-) diff --git a/src/game-lib-component-trigger-box-sphere.js b/src/game-lib-component-trigger-box-sphere.js index 8ac3d7f..0a90b65 100644 --- a/src/game-lib-component-trigger-box-sphere.js +++ b/src/game-lib-component-trigger-box-sphere.js @@ -5,7 +5,9 @@ GameLib.D3.ComponentTriggerBoxSphere = function( componentId, entitiesToCheck, - callback + onInside, + onEnter, + onLeave ) { this.componentId = componentId || GameLib.D3.Tools.RandomId(); this.parentEntity = null; @@ -14,7 +16,12 @@ GameLib.D3.ComponentTriggerBoxSphere = function( GameLib.D3.Utils.Extend(GameLib.D3.ComponentTriggerBoxSphere, GameLib.D3.ComponentInterface); this.entitiesToCheck = entitiesToCheck || []; - this.callback = callback || null; + this.onInside = onInside || null; + this.onEnter = onEnter || null; + this.onLeave = onLeave || null; + + // runtime code + this.entitiesInside = []; }; if(typeof THREE != "undefined") { @@ -46,44 +53,44 @@ GameLib.D3.ComponentTriggerBoxSphere.prototype.onUpdate = function( if (!entityToCheck.mesh.geometry.boundingSphere) { entityToCheck.mesh.geometry.computeBoundingSphere(); } - - var spherePosition = new THREE.Vector3( - entityToCheck.position.x, - entityToCheck.position.y, - entityToCheck.position.z - ); - - spherePosition.applyMatrix4(ComponentTriggerBoxSphere_BoxInverseTransform); - - var sphere = new THREE.Sphere( - spherePosition, - entityToCheck.mesh.geometry.boundingSphere.radius - ); - - if(this.callback && parentEntity.mesh.geometry.boundingBox.intersectsSphere(sphere)) { - this.callback(parentEntity, entityToCheck); - } - - } else { // no target mesh geometry. - - var spherePosition = new THREE.Vector3( - entityToCheck.position.x, - entityToCheck.position.y, - entityToCheck.position.z - ); - - spherePosition.applyMatrix4(ComponentTriggerBoxSphere_BoxInverseTransform); - - var sphere = new THREE.Sphere( - spherePosition, - 1 - ); - - if(this.callback && parentEntity.mesh.geometry.boundingBox.intersectsSphere(sphere)) { - this.callback(parentEntity, entityToCheck); - } - } + + var spherePosition = new THREE.Vector3( + entityToCheck.position.x, + entityToCheck.position.y, + entityToCheck.position.z + ); + + spherePosition.applyMatrix4(ComponentTriggerBoxSphere_BoxInverseTransform); + + var sphere = new THREE.Sphere( + spherePosition, + entityToCheck.mesh ? entityToCheck.mesh.geometry.boundingSphere.radius : 1 + ); + + if(parentEntity.mesh.geometry.boundingBox.intersectsSphere(sphere)) { + + if(this.entitiesInside.indexOf(entityToCheck) <= -1) { // check if this is the first time the entity enters the trigger + this.entitiesInside.push(entityToCheck); + + if(this.onEnter) { + this.onEnter(parentEntity, entityToCheck); + } + } + + + if(this.onInside) { + this.onInside(parentEntity, entityToCheck); + } + + } else if(this.entitiesInside.indexOf(entityToCheck) > -1) { // entity WAS inside the trigger + this.entitiesInside.splice(this.entitiesInside.indexOf(entityToCheck), 1); + + if(this.onLeave) { + this.onLeave(parentEntity, entityToCheck); + } + } + } } }; \ No newline at end of file diff --git a/src/game-lib-component-trigger-sphere-sphere.js b/src/game-lib-component-trigger-sphere-sphere.js index f6419db..2ad9bdb 100644 --- a/src/game-lib-component-trigger-sphere-sphere.js +++ b/src/game-lib-component-trigger-sphere-sphere.js @@ -5,7 +5,9 @@ GameLib.D3.ComponentTriggerSphereSphere = function( componentId, sphereRadius, entitiesToCheck, - callback + onInside, + onEnter, + onLeave ) { this.componentId = componentId || GameLib.D3.Tools.RandomId(); this.parentEntity = null; @@ -15,7 +17,12 @@ GameLib.D3.ComponentTriggerSphereSphere = function( this.entitiesToCheck = entitiesToCheck || []; this.sphereRadius = sphereRadius || 1.0; - this.callback = callback || null; + this.onInside = onInside || null; + this.onEnter = onEnter || null; + this.onLeave = onLeave || null; + + // runtime code + this.entitiesInside = []; }; if(typeof THREE != "undefined") { @@ -63,8 +70,24 @@ GameLib.D3.ComponentTriggerSphereSphere.prototype.onUpdate = function( var length = ComponentTriggerSphereSphere_sphereToSphere_Vector3.length(); if((length - (ComponentTriggerSphereSphere_targetBoundingSphereRadius + this.sphereRadius)) < 0) { - if(this.callback) { - this.callback(parentEntity, entityToCheck); + + if(this.entitiesInside.indexOf(entityToCheck) <= -1) { // check if this is the first time the entity enters the trigger + this.entitiesInside.push(entityToCheck); + + if(this.onEnter) { + this.onEnter(parentEntity, entityToCheck); + } + } + + if(this.onInside) { + this.onInside(parentEntity, entityToCheck); + } + + } else if(this.entitiesInside.indexOf(entityToCheck) > -1) { // entity WAS inside the trigger + this.entitiesInside.splice(this.entitiesInside.indexOf(entityToCheck), 1); + + if(this.onLeave) { + this.onLeave(parentEntity, entityToCheck); } } } diff --git a/src/game-lib-game.js b/src/game-lib-game.js index 70cdadd..8f70283 100644 --- a/src/game-lib-game.js +++ b/src/game-lib-game.js @@ -31,7 +31,7 @@ GameLib.D3.Game.prototype.render = function( ) { for(var s in this.scenes) { var scene = this.scenes[s]; - scene.render(dt, renderer, camera); + scene.render(dt, renderer, scene.camera); } }; diff --git a/src/game-lib-scene.js b/src/game-lib-scene.js index a838638..4541b06 100644 --- a/src/game-lib-scene.js +++ b/src/game-lib-scene.js @@ -224,10 +224,10 @@ GameLib.D3.Scene.prototype.loadTextures = function(urls, onLoadedCallback) { gameLibTexture.wrapS, gameLibTexture.wrapT, gameLibTexture.magFilter, - gameLibTexture.minFilter, - gameLibTexture.format, - gameLibTexture.textureType, - gameLibTexture.anisotropy + gameLibTexture.minFilter + //gameLibTexture.format, + //gameLibTexture.textureType, + //gameLibTexture.anisotropy ); instanceMaterial[instanceMapId].name = gameLibTexture.name; @@ -735,6 +735,13 @@ GameLib.D3.Scene.FromAPIScene = function( ) }; +/** + * Set on runtime. + */ +GameLib.D3.Scene.prototype.setCamera = function(camera) { + this.sceneCamera = camera; +}; + /** * Updates the scene * @param deltaTime @@ -767,10 +774,11 @@ GameLib.D3.Scene.prototype.lateUpdate = function( */ GameLib.D3.Scene.prototype.render = function( deltaTime, - renderer, - camera + renderer ) { - renderer.render(this.instance, camera); + if(this.sceneCamera) { + renderer.render(this.instance, this.sceneCamera); + } }; /** From 9cfa7da639e8bb3de27d74ea10c48be6b28f3736 Mon Sep 17 00:00:00 2001 From: polygonboutique Date: Mon, 14 Nov 2016 10:58:30 +0100 Subject: [PATCH 2/3] added box-box trigger modified box-sphere trigger. (calculate a new bb from the current transform) modifed sphere-sphere (scale the boundingsphere radius by the maximum scale component of the entity.) --- src/game-lib-component-trigger-box-box.js | 74 +++++++++++++++++++ src/game-lib-component-trigger-box-sphere.js | 23 +++--- ...ame-lib-component-trigger-sphere-sphere.js | 13 +++- 3 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 src/game-lib-component-trigger-box-box.js diff --git a/src/game-lib-component-trigger-box-box.js b/src/game-lib-component-trigger-box-box.js new file mode 100644 index 0000000..c999894 --- /dev/null +++ b/src/game-lib-component-trigger-box-box.js @@ -0,0 +1,74 @@ +// +// this component operates on it's parent entity's bounding box. +// so, you can't use this component with a null-mesh. +// +GameLib.D3.ComponentTriggerBoxBox = function( + componentId, + entitiesToCheck, + onInside, + onEnter, + onLeave +) { + this.componentId = componentId || GameLib.D3.Tools.RandomId(); + this.parentEntity = null; + + // Todo: this should be executed somewhere in game-lib-z, so that we don't execute it on every construction of an object. + GameLib.D3.Utils.Extend(GameLib.D3.ComponentTriggerBoxBox, GameLib.D3.ComponentInterface); + + this.entitiesToCheck = entitiesToCheck || []; + this.onInside = onInside || null; + this.onEnter = onEnter || null; + this.onLeave = onLeave || null; + + // runtime code + this.entitiesInside = []; +}; + +if(typeof THREE != "undefined") { + ComponentTriggerBoxBox_BB = new THREE.Box3(); + ComponentTriggerBoxBox_BBEntity = new THREE.Box3(); +} + + +///////////////////////// Methods to override ////////////////////////// +GameLib.D3.ComponentTriggerBoxBox.prototype.onUpdate = function( + deltaTime, + parentEntity +) { + + if(parentEntity.mesh) { + // Calculate the current transform here, using the position, orientation and scale of the entity. + + ComponentTriggerBoxBox_BB.setFromObject(parentEntity.mesh); + + for(var e in this.entitiesToCheck) { + var entityToCheck = this.entitiesToCheck[e]; + + ComponentTriggerBoxBox_BBEntity.setFromObject(entityToCheck.mesh); + + if(ComponentTriggerBoxBox_BB.intersectsBox(ComponentTriggerBoxBox_BBEntity)) { + + if(this.entitiesInside.indexOf(entityToCheck) <= -1) { // check if this is the first time the entity enters the trigger + this.entitiesInside.push(entityToCheck); + + if(this.onEnter) { + this.onEnter(parentEntity, entityToCheck); + } + } + + + if(this.onInside) { + this.onInside(parentEntity, entityToCheck); + } + + } else if(this.entitiesInside.indexOf(entityToCheck) > -1) { // entity WAS inside the trigger + this.entitiesInside.splice(this.entitiesInside.indexOf(entityToCheck), 1); + + if(this.onLeave) { + this.onLeave(parentEntity, entityToCheck); + } + } + + } + } +}; \ No newline at end of file diff --git a/src/game-lib-component-trigger-box-sphere.js b/src/game-lib-component-trigger-box-sphere.js index 0a90b65..2f91a2c 100644 --- a/src/game-lib-component-trigger-box-sphere.js +++ b/src/game-lib-component-trigger-box-sphere.js @@ -25,7 +25,6 @@ GameLib.D3.ComponentTriggerBoxSphere = function( }; if(typeof THREE != "undefined") { - ComponentTriggerBoxSphere_BoxInverseTransform = new THREE.Matrix4(); ComponentTriggerBoxSphere_TargetPosition_Vec3 = new THREE.Vector3(); ComponentTriggerBoxSphere_TargetRadius = 0.0; } @@ -39,11 +38,8 @@ GameLib.D3.ComponentTriggerBoxSphere.prototype.onUpdate = function( if(parentEntity.mesh) { // Calculate the current transform here, using the position, orientation and scale of the entity. - if(!parentEntity.mesh.geometry.boundingBox) { - parentEntity.mesh.geometry.computeBoundingBox(); - } - - ComponentTriggerBoxSphere_BoxInverseTransform.getInverse(parentEntity.mesh.matrix); + var bb = new THREE.Box3(); + bb.setFromObject(parentEntity.mesh); for(var e in this.entitiesToCheck) { var entityToCheck = this.entitiesToCheck[e]; @@ -61,14 +57,23 @@ GameLib.D3.ComponentTriggerBoxSphere.prototype.onUpdate = function( entityToCheck.position.z ); - spherePosition.applyMatrix4(ComponentTriggerBoxSphere_BoxInverseTransform); + var maxScale = Math.max( + entityToCheck.scale.x, + Math.max( + entityToCheck.scale.y, + entityToCheck.scale.z + ) + ); var sphere = new THREE.Sphere( spherePosition, - entityToCheck.mesh ? entityToCheck.mesh.geometry.boundingSphere.radius : 1 + entityToCheck.mesh + ? entityToCheck.mesh.geometry.boundingSphere.radius * maxScale + : 1 ); - if(parentEntity.mesh.geometry.boundingBox.intersectsSphere(sphere)) { + + if(bb.intersectsSphere(sphere)) { if(this.entitiesInside.indexOf(entityToCheck) <= -1) { // check if this is the first time the entity enters the trigger this.entitiesInside.push(entityToCheck); diff --git a/src/game-lib-component-trigger-sphere-sphere.js b/src/game-lib-component-trigger-sphere-sphere.js index 2ad9bdb..9cc5b97 100644 --- a/src/game-lib-component-trigger-sphere-sphere.js +++ b/src/game-lib-component-trigger-sphere-sphere.js @@ -1,5 +1,7 @@ // // uses the entity position + mesh bounding sphere to check if intersections +// this is a bit jerky to use. +// so please feel free to use the box-sphere & box-box triggers instead. // GameLib.D3.ComponentTriggerSphereSphere = function( componentId, @@ -54,9 +56,16 @@ GameLib.D3.ComponentTriggerSphereSphere.prototype.onUpdate = function( ); if(entityToCheck.mesh && entityToCheck.mesh.geometry.boundingSphere) { - ComponentTriggerSphereSphere_targetBoundingSphereRadius = entityToCheck.mesh.geometry.boundingSphere.radius; + var maxScale = Math.max( + entityToCheck.scale.x, + Math.max( + entityToCheck.scale.y, + entityToCheck.scale.z + ) + ); + ComponentTriggerSphereSphere_targetBoundingSphereRadius = entityToCheck.mesh.geometry.boundingSphere.radius * maxScale; } else { - ComponentTriggerSphereSphere_targetBoundingSphereRadius = 0.0; + ComponentTriggerSphereSphere_targetBoundingSphereRadius = 1.0; } // do sphere sphere collision From 81d7bc76c578cd7d95574844256154b94805acc4 Mon Sep 17 00:00:00 2001 From: polygonboutique Date: Mon, 14 Nov 2016 14:14:51 +0100 Subject: [PATCH 3/3] added waypoint system. added "break-loop" return val for trigger box components --- src/game-lib-component-trigger-box-box.js | 25 ++++++++++++++++--- src/game-lib-component-trigger-box-sphere.js | 25 ++++++++++++++++--- ...ame-lib-component-trigger-sphere-sphere.js | 25 ++++++++++++++++--- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/game-lib-component-trigger-box-box.js b/src/game-lib-component-trigger-box-box.js index c999894..6d92be7 100644 --- a/src/game-lib-component-trigger-box-box.js +++ b/src/game-lib-component-trigger-box-box.js @@ -7,7 +7,8 @@ GameLib.D3.ComponentTriggerBoxBox = function( entitiesToCheck, onInside, onEnter, - onLeave + onLeave, + onSetParent ) { this.componentId = componentId || GameLib.D3.Tools.RandomId(); this.parentEntity = null; @@ -19,6 +20,7 @@ GameLib.D3.ComponentTriggerBoxBox = function( this.onInside = onInside || null; this.onEnter = onEnter || null; this.onLeave = onLeave || null; + this.onSetParent = onSetParent || null; // runtime code this.entitiesInside = []; @@ -52,23 +54,38 @@ GameLib.D3.ComponentTriggerBoxBox.prototype.onUpdate = function( this.entitiesInside.push(entityToCheck); if(this.onEnter) { - this.onEnter(parentEntity, entityToCheck); + if(this.onEnter(parentEntity, entityToCheck)) { + break; + } } } if(this.onInside) { - this.onInside(parentEntity, entityToCheck); + if(this.onInside(parentEntity, entityToCheck)) { + break; + } } } else if(this.entitiesInside.indexOf(entityToCheck) > -1) { // entity WAS inside the trigger this.entitiesInside.splice(this.entitiesInside.indexOf(entityToCheck), 1); if(this.onLeave) { - this.onLeave(parentEntity, entityToCheck); + if(this.onLeave(parentEntity, entityToCheck)) { + break; + } } } } } +}; + +GameLib.D3.ComponentTriggerBoxBox.prototype.onSetParentEntity = function( + parentScene, + parentEntity +) { + if(this.onSetParent) { + this.onSetParent(parentScene, parentEntity); + } }; \ No newline at end of file diff --git a/src/game-lib-component-trigger-box-sphere.js b/src/game-lib-component-trigger-box-sphere.js index 2f91a2c..e4cfa37 100644 --- a/src/game-lib-component-trigger-box-sphere.js +++ b/src/game-lib-component-trigger-box-sphere.js @@ -7,7 +7,8 @@ GameLib.D3.ComponentTriggerBoxSphere = function( entitiesToCheck, onInside, onEnter, - onLeave + onLeave, + onSetParent ) { this.componentId = componentId || GameLib.D3.Tools.RandomId(); this.parentEntity = null; @@ -19,6 +20,7 @@ GameLib.D3.ComponentTriggerBoxSphere = function( this.onInside = onInside || null; this.onEnter = onEnter || null; this.onLeave = onLeave || null; + this.onSetParent = onSetParent || null; // runtime code this.entitiesInside = []; @@ -79,23 +81,38 @@ GameLib.D3.ComponentTriggerBoxSphere.prototype.onUpdate = function( this.entitiesInside.push(entityToCheck); if(this.onEnter) { - this.onEnter(parentEntity, entityToCheck); + if(this.onEnter(parentEntity, entityToCheck)) { + break; + } } } if(this.onInside) { - this.onInside(parentEntity, entityToCheck); + if(this.onInside(parentEntity, entityToCheck)) { + break; + } } } else if(this.entitiesInside.indexOf(entityToCheck) > -1) { // entity WAS inside the trigger this.entitiesInside.splice(this.entitiesInside.indexOf(entityToCheck), 1); if(this.onLeave) { - this.onLeave(parentEntity, entityToCheck); + if(this.onLeave(parentEntity, entityToCheck)) { + break; + } } } } } +}; + +GameLib.D3.ComponentTriggerBoxSphere.prototype.onSetParentEntity = function( + parentScene, + parentEntity +) { + if(this.onSetParent) { + this.onSetParent(parentScene, parentEntity); + } }; \ No newline at end of file diff --git a/src/game-lib-component-trigger-sphere-sphere.js b/src/game-lib-component-trigger-sphere-sphere.js index 9cc5b97..ac573ee 100644 --- a/src/game-lib-component-trigger-sphere-sphere.js +++ b/src/game-lib-component-trigger-sphere-sphere.js @@ -9,7 +9,8 @@ GameLib.D3.ComponentTriggerSphereSphere = function( entitiesToCheck, onInside, onEnter, - onLeave + onLeave, + onSetParent ) { this.componentId = componentId || GameLib.D3.Tools.RandomId(); this.parentEntity = null; @@ -22,6 +23,7 @@ GameLib.D3.ComponentTriggerSphereSphere = function( this.onInside = onInside || null; this.onEnter = onEnter || null; this.onLeave = onLeave || null; + this.onSetParent = onSetParent || null; // runtime code this.entitiesInside = []; @@ -84,20 +86,35 @@ GameLib.D3.ComponentTriggerSphereSphere.prototype.onUpdate = function( this.entitiesInside.push(entityToCheck); if(this.onEnter) { - this.onEnter(parentEntity, entityToCheck); + if(this.onEnter(parentEntity, entityToCheck)) { + break; + } } } if(this.onInside) { - this.onInside(parentEntity, entityToCheck); + if(this.onInside(parentEntity, entityToCheck)) { + break; + } } } else if(this.entitiesInside.indexOf(entityToCheck) > -1) { // entity WAS inside the trigger this.entitiesInside.splice(this.entitiesInside.indexOf(entityToCheck), 1); if(this.onLeave) { - this.onLeave(parentEntity, entityToCheck); + if(this.onLeave(parentEntity, entityToCheck)) { + break; + } } } } +}; + +GameLib.D3.ComponentTriggerSphereSphere.prototype.onSetParentEntity = function( + parentScene, + parentEntity +) { + if(this.onSetParent) { + this.onSetParent(parentScene, parentEntity); + } }; \ No newline at end of file