diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 163132e..44332d0 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -31,7 +31,7 @@ GameLib.Event.LOGGED_IN = 0xd; GameLib.Event.COMPONENT_CREATED = 0xe; GameLib.Event.COMPONENT_CLONED = 0xf; GameLib.Event.TEXTURE_ANIMATED_CHANGE = 0x10; -//GameLib.Event.PHYSICS_WORLD_INSTANCE_CREATED = 0x11; +GameLib.Event.ANIMATE_TEXTURE_INSTANCE = 0x11; //GameLib.Event.RIGID_BODY_INSTANCE_CREATED = 0x12; //GameLib.Event.TEXTURE_INSTANCE_CREATED = 0x13; GameLib.Event.TEXTURE_INSTANCE_UPDATED = 0x14; @@ -137,8 +137,8 @@ GameLib.Event.GetEventName = function(number) { case 0xd : return 'logged_in'; case 0xe : return 'component_created'; case 0xf : return 'component_cloned'; - case 0x10 : return 'texture_animated_change';//'scene_object_instance_created'; - case 0x11 : return 'unused';//'world_instance_created'; + case 0x10 : return 'texture_animated_change'; + case 0x11 : return 'animate_texture_instance'; case 0x12 : return 'unused';//'rigid_body_instance_created'; case 0x13 : return 'unused';//'texture_instance_created'; case 0x14 : return 'texture_instance_updated'; diff --git a/src/game-lib-d3-api-mesh.js b/src/game-lib-d3-api-mesh.js index a5814dd..619daaa 100644 --- a/src/game-lib-d3-api-mesh.js +++ b/src/game-lib-d3-api-mesh.js @@ -44,7 +44,8 @@ GameLib.D3.API.Mesh = function( parentEntity, renderOrder, isBufferMesh, - useQuaternion + useQuaternion, + visible ) { if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -150,6 +151,11 @@ GameLib.D3.API.Mesh = function( useQuaternion = true; } this.useQuaternion = useQuaternion; + + if (GameLib.Utils.UndefinedOrNull(visible)) { + visible = true; + } + this.visible = visible; }; GameLib.D3.API.Mesh.prototype = Object.create(GameLib.Component.prototype); @@ -252,6 +258,7 @@ GameLib.D3.API.Mesh.FromObject = function (objectMesh){ objectMesh.parentEntity, objectMesh.renderOrder, objectMesh.isBufferMesh, - objectMesh.useQuaternion + objectMesh.useQuaternion, + objectMesh.visible ); }; diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index a03b4cf..8eeb709 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -41,7 +41,8 @@ GameLib.D3.Mesh = function ( apiMesh.parentEntity, apiMesh.renderOrder, apiMesh.isBufferMesh, - apiMesh.useQuaternion + apiMesh.useQuaternion, + apiMesh.visible ); this.faces = this.faces.map( @@ -553,6 +554,8 @@ GameLib.D3.Mesh.prototype.createInstance = function() { this.instance.renderOrder = this.renderOrder; + this.instance.visible = this.visible; + GameLib.Component.prototype.createInstance.call(this); }; @@ -615,6 +618,10 @@ GameLib.D3.Mesh.prototype.updateInstance = function(property) { this.removeHelper(); this.createHelper(); } + + if (property === 'visible') { + this.instance.visible = this.visible; + } }; /** @@ -1041,7 +1048,8 @@ GameLib.D3.Mesh.prototype.toApiObject = function() { GameLib.Utils.IdOrNull(this.parentEntity), this.renderOrder, this.isBufferMesh, - this.useQuaternion + this.useQuaternion, + this.visible ); return apiMesh; diff --git a/src/game-lib-d3-mesh-box.js b/src/game-lib-d3-mesh-box.js index 5003629..68497b3 100644 --- a/src/game-lib-d3-mesh-box.js +++ b/src/game-lib-d3-mesh-box.js @@ -73,7 +73,7 @@ GameLib.D3.Mesh.Box.prototype.createInstance = function() { this.instance.userData.depth = this.depth; }; -GameLib.D3.Mesh.Box.prototype.updateInstance = function() { +GameLib.D3.Mesh.Box.prototype.updateInstance = function(property) { if ( this.instance.userData.width !== this.width || @@ -97,7 +97,7 @@ GameLib.D3.Mesh.Box.prototype.updateInstance = function() { this.instance.geometry = geometry; } - GameLib.D3.Mesh.prototype.updateInstance.call(this); + GameLib.D3.Mesh.prototype.updateInstance.call(this, property); }; diff --git a/src/game-lib-d3-mesh-cylinder.js b/src/game-lib-d3-mesh-cylinder.js index c33f67e..3224374 100644 --- a/src/game-lib-d3-mesh-cylinder.js +++ b/src/game-lib-d3-mesh-cylinder.js @@ -118,7 +118,7 @@ GameLib.D3.Mesh.Cylinder.prototype.createInstance = function() { this.instance.userData.thetaLength = this.thetaLength; }; -GameLib.D3.Mesh.Cylinder.prototype.updateInstance = function() { +GameLib.D3.Mesh.Cylinder.prototype.updateInstance = function(property) { if ( this.instance.userData.radiusTop !== this.radiusTop || @@ -156,7 +156,7 @@ GameLib.D3.Mesh.Cylinder.prototype.updateInstance = function() { this.instance.geometry = geometry; } - GameLib.D3.Mesh.prototype.updateInstance.call(this); + GameLib.D3.Mesh.prototype.updateInstance.call(this, property); }; diff --git a/src/game-lib-d3-mesh-line.js b/src/game-lib-d3-mesh-line.js index eac4ccf..db96d98 100644 --- a/src/game-lib-d3-mesh-line.js +++ b/src/game-lib-d3-mesh-line.js @@ -57,11 +57,11 @@ GameLib.D3.Mesh.Line.prototype.createInstance = function() { this.instance.userData.lineWidth = this.lineWidth; }; -GameLib.D3.Mesh.Line.prototype.updateInstance = function() { +GameLib.D3.Mesh.Line.prototype.updateInstance = function(property) { this.instance.linewidth = this.lineWidth; - GameLib.D3.Mesh.prototype.updateInstance.call(this); + GameLib.D3.Mesh.prototype.updateInstance.call(this, property); }; diff --git a/src/game-lib-d3-mesh-plane.js b/src/game-lib-d3-mesh-plane.js index 6025f4a..1e31abe 100644 --- a/src/game-lib-d3-mesh-plane.js +++ b/src/game-lib-d3-mesh-plane.js @@ -142,7 +142,7 @@ GameLib.D3.Mesh.Plane.prototype.createInstance = function() { /** * */ -GameLib.D3.Mesh.Plane.prototype.updateInstance = function() { +GameLib.D3.Mesh.Plane.prototype.updateInstance = function(property) { var geometry = null; @@ -190,7 +190,7 @@ GameLib.D3.Mesh.Plane.prototype.updateInstance = function() { } } - GameLib.D3.Mesh.prototype.updateInstance.call(this); + GameLib.D3.Mesh.prototype.updateInstance.call(this, property); }; /** diff --git a/src/game-lib-d3-mesh-sphere.js b/src/game-lib-d3-mesh-sphere.js index 8aba1ac..fcfaee6 100644 --- a/src/game-lib-d3-mesh-sphere.js +++ b/src/game-lib-d3-mesh-sphere.js @@ -73,7 +73,7 @@ GameLib.D3.Mesh.Sphere.prototype.createInstance = function() { }; -GameLib.D3.Mesh.Sphere.prototype.updateInstance = function() { +GameLib.D3.Mesh.Sphere.prototype.updateInstance = function(property) { if ( this.instance.userData.widthSegments !== this.widthSegments || @@ -98,7 +98,7 @@ GameLib.D3.Mesh.Sphere.prototype.updateInstance = function() { this.instance.geometry = geometry; } - GameLib.D3.Mesh.prototype.updateInstance.call(this); + GameLib.D3.Mesh.prototype.updateInstance.call(this, property); }; diff --git a/src/game-lib-d3-mesh-text.js b/src/game-lib-d3-mesh-text.js index 33e7b43..5abc03f 100644 --- a/src/game-lib-d3-mesh-text.js +++ b/src/game-lib-d3-mesh-text.js @@ -131,7 +131,7 @@ GameLib.D3.Mesh.Text.prototype.createInstance = function() { this.instance.userData.bevelSegments = this.bevelSegments; }; -GameLib.D3.Mesh.Text.prototype.updateInstance = function() { +GameLib.D3.Mesh.Text.prototype.updateInstance = function(property) { if ( this.instance.userData.text !== this.text || @@ -175,7 +175,7 @@ GameLib.D3.Mesh.Text.prototype.updateInstance = function() { this.instance.geometry = geometry; } - GameLib.D3.Mesh.prototype.updateInstance.call(this); + GameLib.D3.Mesh.prototype.updateInstance.call(this, property); }; diff --git a/src/game-lib-d3-particle.js b/src/game-lib-d3-particle.js index 59703af..36c6bf3 100644 --- a/src/game-lib-d3-particle.js +++ b/src/game-lib-d3-particle.js @@ -121,7 +121,8 @@ GameLib.D3.Particle.SCALE_TYPE_CONSTANT = 0x1; GameLib.D3.Particle.SCALE_TYPE_LINEAR = 0x2; GameLib.D3.Particle.SCALE_TYPE_EXPONENTIAL = 0x3; GameLib.D3.Particle.SCALE_TYPE_RANDOM = 0x4; -GameLib.D3.Particle.SCALE_TYPE_FUNCTION = 0x5; +GameLib.D3.Particle.SCALE_TYPE_RANDOM_X_EQUALS_Y = 0x6; +GameLib.D3.Particle.SCALE_TYPE_FUNCTION = 0x7; GameLib.D3.Particle.ROTATION_TYPE_CONSTANT = 0x1; GameLib.D3.Particle.ROTATION_TYPE_RANDOM = 0x2; @@ -147,32 +148,31 @@ GameLib.D3.Particle.prototype.createInstance = function() { */ GameLib.D3.Particle.prototype.updateInstance = function(property) { console.log('Update Particle Property : ' + property); + + if (this.scaleType === GameLib.D3.Particle.SCALE_TYPE_RANDOM_X_EQUALS_Y) { + console.log(property); + // this.scale.y = this.scale.x; + } }; /** * Clones a particle - this only clones 3rd party instances, so we have less overhead of creating these types of objects */ -GameLib.D3.Particle.prototype.clone = function(camera) { - - var position = camera.instance.position.clone(); - var lookAt = camera.lookAt.instance.clone(); - - var direction = lookAt.sub(position).negate(); - - this.instance.visible = false; - - var instance = this.instance.clone(); - instance.visible = true; - - instance.lookAt(direction); - - instance.userData.direction = this.direction.instance.clone(); - instance.userData.elapsed = 0; - instance.userData.lifeTime = this.lifeTime; - this.mesh.parentScene.instance.add(instance); - return instance; -}; +// GameLib.D3.Particle.prototype.clone = function() { +// +// var clone = GameLib.Component.prototype.clone.call(this); +// +// clone.mesh = this.mesh.clone(); +// +// return clone; +// //var material = this.mesh.materials[0].clone(); +// +// //var texture = material.diffuseMap.clone(); +// +// //this.mesh.materials = +// +// }; /** * Converts a GameLib.D3.Particle to a new GameLib.D3.API.Particle diff --git a/src/game-lib-d3-scene.js b/src/game-lib-d3-scene.js index fd62480..677acf8 100644 --- a/src/game-lib-d3-scene.js +++ b/src/game-lib-d3-scene.js @@ -481,6 +481,8 @@ GameLib.D3.Scene.prototype.addClone = function(component) { } GameLib.Utils.PushUnique(this.clones, component); + + component.parentScene = this; } }; diff --git a/src/game-lib-system-animation.js b/src/game-lib-system-animation.js index 32c597e..db2ee78 100644 --- a/src/game-lib-system-animation.js +++ b/src/game-lib-system-animation.js @@ -15,6 +15,17 @@ GameLib.System.Animation = function( this.latest = {}; this.textures = {}; this.textureIds = []; + + this.animationMeshAddedSubscription = null; + this.animationMeshRemovedSubscription = null; + this.instanceCreatedSubscription = null; + this.removeComponentSubscription = null; + this.textureAnimatedSubscription = null; + + /** + * Sometimes we want to animate texture instances directly, without the overhead of a GameLib.Texture + */ + this.animateTextureInstanceSubscription = null; }; GameLib.System.Animation.prototype = Object.create(GameLib.System.prototype); @@ -67,6 +78,11 @@ GameLib.System.Animation.prototype.start = function() { GameLib.Event.TEXTURE_ANIMATED_CHANGE, this.textureAnimatedChange.bind(this) ); + + this.animateTextureInstanceSubscription = GameLib.Event.Subscribe( + GameLib.Event.ANIMATE_TEXTURE_INSTANCE, + this.animateTextureInstance.bind(this) + ) }; GameLib.System.Animation.prototype.instanceCreated = function(data) { @@ -137,6 +153,32 @@ GameLib.System.Animation.prototype.textureAnimatedChange = function(data) { }; +/** + * This adds a texture instance directly on our textures to be animated - to bypass our GameLib component and linking + * system, which adds too much overhead for managing textures + * @param data + */ +GameLib.System.Animation.prototype.animateTextureInstance = function(data) { + + if (data.texture.repeat.x > 1 || data.texture.repeat.x < 0) { + console.warn('cannot animate a texture with repeat.x greater than 1 or less than 0'); + data.texture.userData.animated = false; + return; + } + + if (data.texture.repeat.y > 1 || data.texture.repeat.y < 0) { + console.warn('cannot animate a texture with repeat.y greater than 1 or less than 0'); + data.texture.userData.animated = false; + return; + } + + data.texture.userData.animated = true; + + this.textures[data.texture.id] = data.texture; + this.textureIds = Object.keys(this.textures); +}; + + GameLib.System.Animation.prototype.beforeRender = function(data) { if (this.paused) { @@ -231,15 +273,21 @@ GameLib.System.Animation.prototype.beforeRender = function(data) { var texture = this.textures[textureId]; - if (texture.reverseAnimation) { + if (texture.reverseAnimation || (texture.userData && texture.userData.reverseAnimation)) { - if (texture.forward) { + if (texture.forward === true || (texture.userData && texture.userData.forward === true)) { texture.offset.x += texture.repeat.x; if (texture.offset.x >= (1 - texture.repeat.x)) { if (texture.offset.y >= (1 - texture.repeat.y)) { texture.offset.x = (1 - texture.repeat.x); texture.offset.y = (1 - texture.repeat.y); - texture.forward = false; + + if (texture.userData) { + texture.userData.forward = false; + } else { + texture.forward = false; + } + } else { texture.offset.x = 0; texture.offset.y += texture.repeat.y; @@ -247,14 +295,18 @@ GameLib.System.Animation.prototype.beforeRender = function(data) { } } - if (!texture.forward) { + if (texture.forward === false || (texture.userData && texture.userData.forward === false)) { texture.offset.x -= texture.repeat.x; if (texture.offset.x <= 0) { texture.offset.x = 0; if (texture.offset.y < texture.repeat.y) { texture.offset.x = texture.repeat.x; texture.offset.y = 0; - texture.forward = true; + if (texture.userData) { + texture.userData.forward = true; + } else { + texture.forward = true; + } } else { texture.offset.x = (1 - texture.repeat.x); texture.offset.y -= texture.repeat.y; @@ -278,7 +330,9 @@ GameLib.System.Animation.prototype.beforeRender = function(data) { } - texture.updateInstance('offset'); + if (!texture.userData) { + texture.updateInstance('offset'); + } }.bind(this) ) @@ -1083,5 +1137,7 @@ GameLib.System.Animation.prototype.stop = function() { this.removeComponentSubscription.remove(); this.textureAnimatedSubscription.remove(); + + this.animateTextureInstanceSubscription.remove(); }; diff --git a/src/game-lib-system-gui.js b/src/game-lib-system-gui.js index a43ebaf..ff9a734 100644 --- a/src/game-lib-system-gui.js +++ b/src/game-lib-system-gui.js @@ -748,6 +748,7 @@ GameLib.System.GUI.prototype.buildControl = function(folder, componentTemplate, 'linear': GameLib.D3.Particle.SCALE_TYPE_LINEAR, 'exponential': GameLib.D3.Particle.SCALE_TYPE_EXPONENTIAL, 'random': GameLib.D3.Particle.SCALE_TYPE_RANDOM, + 'random (x = y)': GameLib.D3.Particle.SCALE_TYPE_RANDOM_X_EQUALS_Y, 'function': GameLib.D3.Particle.SCALE_TYPE_FUNCTION } ) diff --git a/src/game-lib-system-linking.js b/src/game-lib-system-linking.js index 2d9ca43..a67fd68 100644 --- a/src/game-lib-system-linking.js +++ b/src/game-lib-system-linking.js @@ -525,6 +525,10 @@ GameLib.System.Linking.prototype.removeComponent = function(data) { if (component instanceof GameLib.Entity) { GameLib.EntityManager.Instance.removeEntity(component); } + + // if (component instanceof GameLib.D3.Particle) { + // // component.mesh.parentScene.removeObject(component.mesh); + // } }; GameLib.System.Linking.prototype.imageChanged = function(data) { @@ -560,15 +564,15 @@ GameLib.System.Linking.prototype.arrayItemAdded = function(data) { GameLib.System.Linking.prototype.instanceCloned = function(data) { - if (data.component instanceof GameLib.D3.Particle) { - - var mesh = data.component.mesh; - - if (mesh.parentScene && mesh.parentScene.instance) { - data.instance.userData.scene = mesh.parentScene.instance; - mesh.parentScene.instance.add(data.instance); - } - } + // if (data.component instanceof GameLib.D3.Particle) { + // + // var mesh = data.component.mesh; + // + // if (mesh.parentScene && mesh.parentScene.instance) { + // data.instance.userData.scene = mesh.parentScene.instance; + // mesh.parentScene.instance.add(data.instance); + // } + // } }; diff --git a/src/game-lib-system-particle.js b/src/game-lib-system-particle.js index 2bfba7d..138edba 100644 --- a/src/game-lib-system-particle.js +++ b/src/game-lib-system-particle.js @@ -121,15 +121,24 @@ GameLib.System.Particle.prototype.beforeRender = function(data) { particleEngine.particles = particleEngine.particles.reduce( function(result, particle){ - + particle.position.x += particle.userData.direction.x * data.delta * particle.userData.speed; particle.position.y += particle.userData.direction.y * data.delta * particle.userData.speed; particle.position.z += particle.userData.direction.z * data.delta * particle.userData.speed; + + particle.scale.x += particle.userData.scale.x * data.delta; + particle.scale.y += particle.userData.scale.x * data.delta; + particle.scale.z += particle.userData.scale.x * data.delta; particle.quaternion.copy(particleEngine.camera.instance.quaternion); + particle.material.opacity -= 0.033333; + particle.userData.elapsed += data.delta; - if (particle.userData.elapsed > particle.userData.lifeTime) { + if ( + particle.userData.elapsed > particle.userData.lifeTime || + particle.material.opacity < 0 + ) { particle.userData.scene.remove(particle); } else { result.push(particle); @@ -146,14 +155,119 @@ GameLib.System.Particle.prototype.beforeRender = function(data) { particleEngine.elapsed = 0; - var particleInstance = particleEngine.templateParticle.cloneInstance(); + var particle = particleEngine.templateParticle; + + var particleInstance = particle.cloneInstance(); - //particleEngine.templateParticle.parentScene.instance.add(particleInstance); + particleInstance.visible = true; + + // var texture = particleEngine.templateParticle.mesh.materials[0].diffuseMap.clone(); + // + // texture.offset.x = 0; + // texture.offset.y = 0; + // texture.updateInstance('offset'); + // + // var material = particleEngine.templateParticle.mesh.materials[0].instance.clone(); + // + // material.map = texture.instance; + // + // particleInstance.material = material; + + + // var texture = .clone(); + //image.instance); + + // texture.repeat.x = particleEngine.templateParticle.mesh.materials[0].diffuseMap.repeat.x; + // texture.repeat.y = particleEngine.templateParticle.mesh.materials[0].diffuseMap.repeat.y; + + // texture.offset.x = particleEngine.templateParticle.mesh.materials[0].diffuseMap.offset.x; + // texture.offset.y = particleEngine.templateParticle.mesh.materials[0].diffuseMap.offset.y; + + /** + * Ok - here we have a problem - we cannot reliably clone textures without a call to texture.needsUpdate, + * which recompiles the shader and slows down the particle system - dramatically + * @type {THREE.MeshBasicMaterial} + */ + + particleInstance.material = new THREE.MeshBasicMaterial( + { + map : particle.mesh.materials[0].diffuseMap.instance, + transparent : true, + opacity : 1.0, + depthWrite : false + } + ); + + if (particle.positionOffsetType === GameLib.D3.Particle.POSITION_OFFSET_TYPE_CONSTANT) { + particleInstance.position.x += particle.positionOffset.x; + particleInstance.position.y += particle.positionOffset.y; + particleInstance.position.z += particle.positionOffset.z; + } + + var add = 1; + + if (particle.positionOffsetType === GameLib.D3.Particle.POSITION_OFFSET_TYPE_RANDOM) { + + add = GameLib.Utils.GetRandomIntInclusive(1,2); + + if (add === 1) { + particleInstance.position.x += Math.random() * particle.positionOffset.x; + particleInstance.position.y += Math.random() * particle.positionOffset.y; + particleInstance.position.z += Math.random() * particle.positionOffset.z; + } else { + particleInstance.position.x -= Math.random() * particle.positionOffset.x; + particleInstance.position.y -= Math.random() * particle.positionOffset.y; + particleInstance.position.z -= Math.random() * particle.positionOffset.z; + } + } + + if (particle.scaleType === GameLib.D3.Particle.SCALE_TYPE_CONSTANT) { + particleInstance.scale.x = particle.scale.x; + particleInstance.scale.y = particle.scale.y; + particleInstance.scale.z = particle.scale.z; + } + + if (particle.scaleType === GameLib.D3.Particle.SCALE_TYPE_RANDOM) { + + add = GameLib.Utils.GetRandomIntInclusive(1,2); + + if (add === 1) { + particleInstance.scale.x += Math.random() * particle.scale.x; + particleInstance.scale.y += Math.random() * particle.scale.y; + particleInstance.scale.z += Math.random() * particle.scale.z; + } else { + particleInstance.scale.x -= Math.random() * particle.scale.x; + particleInstance.scale.y -= Math.random() * particle.scale.y; + particleInstance.scale.z -= Math.random() * particle.scale.z; + } + } + + if (particle.scaleType === GameLib.D3.Particle.SCALE_TYPE_RANDOM_X_EQUALS_Y) { + + add = GameLib.Utils.GetRandomIntInclusive(1,2); + + var factor = Math.random() * particle.scale.x; + + if (add === 1) { + particleInstance.scale.x += factor; + particleInstance.scale.y += factor; + particleInstance.scale.z += Math.random() * particle.scale.z; + } else { + particleInstance.scale.x -= factor; + particleInstance.scale.y -= factor; + particleInstance.scale.z -= Math.random() * particle.scale.z; + } + } + + particleInstance.userData.direction = particle.direction.clone(); + particleInstance.userData.scale = particle.scale.clone(); + particleInstance.userData.lifeTime = particle.lifeTime; + particleInstance.userData.speed = particle.speed; + particleInstance.userData.scene = particle.mesh.parentScene.instance; - particleInstance.userData.direction = particleEngine.direction.instance.clone(); particleInstance.userData.elapsed = 0; - particleInstance.userData.lifeTime = particleEngine.templateParticle.lifeTime; - particleInstance.userData.speed = particleEngine.templateParticle.speed; + + particleInstance.userData.scene.add(particleInstance); particleEngine.particles.push(particleInstance);