diff --git a/src/game-lib-d3-api-mesh.js b/src/game-lib-d3-api-mesh.js index 3257967..b635ff0 100644 --- a/src/game-lib-d3-api-mesh.js +++ b/src/game-lib-d3-api-mesh.js @@ -99,7 +99,7 @@ GameLib.D3.API.Mesh = function( this.skinWeights = skinWeights; if (GameLib.Utils.UndefinedOrNull(materials)) { - materials = [new GameLib.D3.API.Material(null, GameLib.D3.Material.MATERIAL_TYPE_BASIC)]; + materials = [new GameLib.D3.API.Material(null, GameLib.D3.Material.MATERIAL_TYPE_STANDARD, 'Material Mesh (' + this.id + ')')]; } this.materials = materials; diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index 1c76ecb..e95c64b 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -453,6 +453,14 @@ GameLib.D3.Mesh.prototype.createInstanceGeometry = function(instanceGeometry) { geometry.computeFaceNormals(); // geometry.computeBoundingBox(); geometry.computeVertexNormals(); + + // geometry.verticesNeedUpdate = true; + // geometry.elementsNeedUpdate = true; + // geometry.morphTargetsNeedUpdate = true; + // geometry.uvsNeedUpdate = true; + // geometry.normalsNeedUpdate = true; + // geometry.colorsNeedUpdate = true; + // geometry.tangentsNeedUpdate = true; } /** @@ -477,29 +485,49 @@ GameLib.D3.Mesh.prototype.createInstance = function() { var instance = null; + var instanceMaterials = function(result, material){ - result.push(material.instance); + material.instance.needsUpdate = true; + result.push(material.instance); return result; }; if (this.skeleton) { - instance = new THREE.SkinnedMesh( - geometry, - this.materials.reduce( - instanceMaterials, - [] - ) - ); + + if (this.materials.length === 1) { + instance = new THREE.SkinnedMesh( + geometry, + this.materials[0].instance + ) + } else { + instance = new THREE.SkinnedMesh( + geometry, + this.materials.map( + function(material) { + return material.instance; + } + ) + ) + } + instance.add(this.skeleton.rootBoneInstance); instance.bind(this.skeleton.instance); } else { - instance = new THREE.Mesh( - geometry, - this.materials.reduce( - instanceMaterials, - [] - ) - ); + if (this.materials.length === 1) { + instance = new THREE.Mesh( + geometry, + this.materials[0].instance + ) + } else { + instance = new THREE.Mesh( + geometry, + this.materials.map( + function(material) { + return material.instance; + } + ) + ) + } } this.createInstanceDefaults(instance); @@ -523,7 +551,16 @@ GameLib.D3.Mesh.prototype.updateInstance = function() { this.instance.geometry = this.createInstanceGeometry(this.instance.geometry); } - if (this.parentMesh && this.parentMesh.loaded) { + this.quaternion.axis.instance.x = this.quaternion.axis.x; + this.quaternion.axis.instance.y = this.quaternion.axis.y; + this.quaternion.axis.instance.z = this.quaternion.axis.z; + // + // var v = new THREE.Vector3(); + // v.applyAxisAngle(this.quaternion.axis.instance, this.quaternion.angle); + + this.quaternion.instance.setFromAxisAngle(this.quaternion.axis.instance, this.quaternion.angle); + + if (this.parentMesh && this.parentMesh.loaded) { if (this.instance.parent !== this.parentMesh.instance) { this.instance.parent = this.parentMesh.instance; @@ -533,9 +570,13 @@ GameLib.D3.Mesh.prototype.updateInstance = function() { this.instance.position.y = this.localPosition.y; this.instance.position.z = this.localPosition.z; - this.instance.rotation.x = this.localRotation.x; - this.instance.rotation.y = this.localRotation.y; - this.instance.rotation.z = this.localRotation.z; + // this.localRotation.x = this.localRotation.instance.x; + // this.localRotation.y = this.localRotation.instance.y; + // this.localRotation.z = this.localRotation.instance.z; + + // this.instance.rotation.x = this.localRotation.x; + // this.instance.rotation.y = this.localRotation.y; + // this.instance.rotation.z = this.localRotation.z; this.instance.scale.x = this.localScale.x; this.instance.scale.y = this.localScale.y; @@ -543,10 +584,10 @@ GameLib.D3.Mesh.prototype.updateInstance = function() { } else { - this.instance.quaternion.x = this.quaternion.x; - this.instance.quaternion.y = this.quaternion.y; - this.instance.quaternion.z = this.quaternion.z; - this.instance.quaternion.w = this.quaternion.w; + // this.localRotation.instance.applyAxisAngle(this.quaternion.axis.instance, this.quaternion.angle); + // this.localRotation.x = this.localRotation.instance.x; + // this.localRotation.y = this.localRotation.instance.y; + // this.localRotation.z = this.localRotation.instance.z; this.instance.position.x = this.position.x + this.localPosition.x; this.instance.position.y = this.position.y + this.localPosition.y; @@ -556,16 +597,19 @@ GameLib.D3.Mesh.prototype.updateInstance = function() { this.instance.scale.y = this.scale.y * this.localScale.y; this.instance.scale.z = this.scale.z * this.localScale.z; - this.instance.up.x = this.up.x; - this.instance.up.y = this.up.y; - this.instance.up.z = this.up.z; - this.instance.rotateX(this.localRotation.x); - this.instance.rotateY(this.localRotation.y); - this.instance.rotateZ(this.localRotation.z); + // this.instance.rotateX(this.localRotation.x); + // this.instance.rotateY(this.localRotation.y); + // this.instance.rotateZ(this.localRotation.z); } - this.instance.name = this.name; + this.instance.quaternion.copy(this.quaternion.instance); + + this.instance.up.x = this.up.x; + this.instance.up.y = this.up.y; + this.instance.up.z = this.up.z; + + this.instance.name = this.name; if (this.materials.length === 1 && this.materials[0].instance) { this.instance.material = this.materials[0].instance; @@ -579,7 +623,7 @@ GameLib.D3.Mesh.prototype.updateInstance = function() { // this.helper.updateInstance(); } - this.computeBoundingBox(this.instance.geometry); + // this.computeBoundingBox(this.instance.geometry); }; diff --git a/src/game-lib-d3-mesh-plane.js b/src/game-lib-d3-mesh-plane.js index 9d9e0b3..cc803be 100644 --- a/src/game-lib-d3-mesh-plane.js +++ b/src/game-lib-d3-mesh-plane.js @@ -82,18 +82,7 @@ GameLib.D3.Mesh.Plane.prototype.createInstance = function() { this.updateVerticesFromGeometryInstance(geometry); - this.materials.push( - new GameLib.D3.Material( - this.graphics, - new GameLib.D3.API.Material( - null, - GameLib.D3.Material.MATERIAL_TYPE_STANDARD, - 'Material Plane ' + this.id - ) - ) - ); - - /** + /** * If this is a heightmap - first generate the z-coordinates */ if (this.isHeightMap) { diff --git a/src/game-lib-d3-rigid-body.js b/src/game-lib-d3-rigid-body.js index 2c44146..be56b89 100644 --- a/src/game-lib-d3-rigid-body.js +++ b/src/game-lib-d3-rigid-body.js @@ -133,16 +133,16 @@ GameLib.D3.RigidBody.prototype.updateInstance = function() { this.instance.position.y = this.position.y; this.instance.position.z = this.position.z; - this.instance.quaternion.x = this.quaternion.x; - this.instance.quaternion.y = this.quaternion.y; - this.instance.quaternion.z = this.quaternion.z; - this.instance.quaternion.w = this.quaternion.w; + this.instance.quaternion.setFromAxisAngle(new CANNON.Vec3(this.quaternion.axis.x, this.quaternion.axis.y, this.quaternion.axis.z), this.quaternion.angle); this.instance.velocity.x = this.velocity.x; this.instance.velocity.y = this.velocity.y; this.instance.velocity.z = this.velocity.z; - this.instance.angularVelocity = this.angularVelocity; + this.instance.angularVelocity.x = this.angularVelocity.x; + this.instance.angularVelocity.y = this.angularVelocity.y; + this.instance.angularVelocity.z = this.angularVelocity.z; + this.instance.linearDamping = this.linearDamping; this.instance.angularDamping = this.angularDamping; this.instance.allowSleep = this.allowSleep; diff --git a/src/game-lib-d3-shape-height-map.js b/src/game-lib-d3-shape-height-map.js index 7c78c55..98ae00b 100644 --- a/src/game-lib-d3-shape-height-map.js +++ b/src/game-lib-d3-shape-height-map.js @@ -2,7 +2,7 @@ * * @param physics * @param apiShape - * @param zValues + * @param heightData * @param minValue * @param maxValue * @param elementSize @@ -11,7 +11,7 @@ GameLib.D3.Shape.HeightMap = function ( physics, apiShape, - zValues, + heightData, minValue, maxValue, elementSize @@ -19,10 +19,10 @@ GameLib.D3.Shape.HeightMap = function ( this.physics = physics; this.physics.isNotCannonThrow(); - if (GameLib.Utils.UndefinedOrNull(zValues)) { - zValues = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]; + if (GameLib.Utils.UndefinedOrNull(heightData)) { + heightData = [[10, 10, 10], [10, 10, 10], [10, 10, 10]]; } - this.zValues = zValues; + this.heightData = heightData; if (GameLib.Utils.UndefinedOrNull(minValue)) { minValue = 0; @@ -56,10 +56,8 @@ GameLib.D3.Shape.HeightMap.prototype.constructor = GameLib.D3.Shape.HeightMap; GameLib.D3.Shape.HeightMap.prototype.createInstance = function() { var instance = new CANNON.Heightfield( - this.zValues, + this.heightData, { - minValue : this.minValue, - maxValue : this.maxValue, elemSize : this.elementSize } ); @@ -71,9 +69,9 @@ GameLib.D3.Shape.HeightMap.prototype.createInstance = function() { * Update instance */ GameLib.D3.Shape.HeightMap.prototype.updateInstance = function() { - this.instance.data = this.zValues; - this.instance.minValue = this.minValue; - this.instance.maxValue = this.maxValue; + this.instance.data = this.heightData; + // this.instance.minValue = this.minValue; + // this.instance.maxValue = this.maxValue; this.instance.elemSize = this.elemSize; this.instance.update(); // this.instance.updateBoundingSphereRadius(); @@ -84,7 +82,7 @@ GameLib.D3.Shape.HeightMap.prototype.updateInstance = function() { GameLib.D3.Shape.HeightMap.prototype.toApiObject = function() { var apiShape = GameLib.D3.Shape.prototype.toApiObject.call(this); - apiShape.zValues = this.zValues; + apiShape.heightData = this.heightData; apiShape.minValue = this.minValue; apiShape.maxValue = this.maxValue; apiShape.elemSize = this.elemSize; @@ -105,36 +103,47 @@ GameLib.D3.Shape.HeightMap.prototype.setFromMesh = function() { var dim1Array = Array.prototype.slice.call(this.parentMesh.getHeightData()); - var w = 0; + // var w = this.parentMesh.widthSegments + 1; + // + // var h = 0; - var h = 0; + // var offset = 0; - var width = this.parentMesh.widthSegments + 1; + this.heightData = []; - var height = this.parentMesh.heightSegments + 1; + for (var x = 0; x <= this.parentMesh.widthSegments; x++) { - var dim2Array = dim1Array.reduce( - function(result, value) { + this.heightData[x] = []; - result[h].push(value); + for (var y = 0; y <= this.parentMesh.heightSegments; y++) { - w++; + this.heightData[x][y] = dim1Array[((x * (this.parentMesh.widthSegments + 1)) + y)]; - if (w === this.parentMesh.widthSegments) { - w = 0; - h++; - result[h] = []; - } + } - return result; - }.bind(this), - [[]] - ); + } - this.zValues = dim2Array; - this.minValue = 0; - this.maxValue = (this.parentMesh.widthSegments + 1) * (this.parentMesh.heightSegments + 1); + + // this.heightData = dim1Array.reduce( + // function(result, value) { + // + // result[h].push(value); + // + // w--; + // + // if (w === 0) { + // w = this.parentMesh.widthSegments; + // + // if (h < this.parentMesh.heightSegments) { + // h++; + // } + // } + // + // return result; + // }.bind(this), + // result + // ); this.updateInstance(); }; @@ -146,7 +155,7 @@ GameLib.D3.Shape.HeightMap.FromObject = function(physics, objectShape) { return new GameLib.D3.Shape.HeightMap( physics, apiShape, - objectShape.zValues, + objectShape.heightData, objectShape.minValue, objectShape.maxValue, objectShape.elemSize diff --git a/src/game-lib-system-gui.js b/src/game-lib-system-gui.js index b69dd49..7c99955 100644 --- a/src/game-lib-system-gui.js +++ b/src/game-lib-system-gui.js @@ -262,13 +262,51 @@ GameLib.System.GUI.prototype.buildQuaternionControl = function(folder, component this.controller(folder, object, property, 'w', step, listen, affected); this.controller(folder, object, property, 'angle', step, listen, affected); + folder.add( + object[property]['axis'], + 'x', + -1, + 1, + 0.01 + ).onChange( + function(value) { + affected.map(function(component){ + component[property]['axis'].x = Number(value); + component.updateInstance(); + }) + } + ); - object = object[property]; - property = 'axis'; + folder.add( + object[property]['axis'], + 'y', + -1, + 1, + 0.01 + ).onChange( + function(value) { + affected.map(function(component){ + component[property]['axis'].y = Number(value); + component.updateInstance(); + }) + } + ); + + folder.add( + object[property]['axis'], + 'z', + -1, + 1, + 0.01 + ).onChange( + function(value) { + affected.map(function(component){ + component[property]['axis'].z = Number(value); + component.updateInstance(); + }) + } + ); - this.controller(folder, object, property, 'x', step, listen, affected); - this.controller(folder, object, property, 'y', step, listen, affected); - this.controller(folder, object, property, 'z', step, listen, affected); }; GameLib.System.GUI.prototype.buildVectorControl = function(folder, componentTemplate, property) { @@ -321,11 +359,21 @@ GameLib.System.GUI.prototype.buildVectorControl = function(folder, componentTemp * @param folder * @param componentTemplate */ -GameLib.System.GUI.prototype.buildEntitySelectionControl = function(folder, componentTemplate) { +GameLib.System.GUI.prototype.buildParentSelectionControl = function(folder, componentTemplate, property) { - var options = GameLib.EntityManager.Instance.queryComponents(GameLib.Entity).reduce( - function(result, entity) { - result[entity.name] = entity; + var constructor = null; + + if (property === 'parentEntity') { + constructor = GameLib.Entity; + } + + if (property === 'parentMesh') { + constructor = GameLib.D3.Mesh + } + + var options = GameLib.EntityManager.Instance.queryComponents(constructor).reduce( + function(result, object) { + result[object.name] = object; return result; }, { @@ -337,47 +385,44 @@ GameLib.System.GUI.prototype.buildEntitySelectionControl = function(folder, comp var affected = componentTemplate.affected; - folder.add(object, 'parentEntity', options).listen().onChange( + folder.add(object, property, options).listen().onChange( function(value) { - var newEntity = null; + var newComponent = null; - if (value !== 'null') { - newEntity = GameLib.EntityManager.Instance.findEntityById(value); - } + if (value !== 'null') { + newComponent = GameLib.EntityManager.Instance.findComponentById(value); + } - affected.map( - function(component) { + affected.map( + function(component) { - component.parentEntity = newEntity; + component[property] = newComponent; - GameLib.Event.Emit( - GameLib.Event.PARENT_ENTITY_CHANGE, - { - originalEntity : this.initialValue, - newEntity : newEntity, - object : component - } - ); - }.bind(this) - ); + if (property === 'parentEntity') { + GameLib.Event.Emit( + GameLib.Event.PARENT_ENTITY_CHANGE, + { + originalEntity : this.initialValue, + newEntity : newComponent, + object : component + } + ); + } - GameLib.Event.Emit( - GameLib.Event.BUILD_GUI, - null - ); - this.initialValue = newEntity; - } - ).onFinishChange( - function(){ - // GameLib.Event.Emit( - // GameLib.Event.BUILD_GUI, - // { - // - // } - // ) + }.bind(this) + ); + + if (property === 'parentEntity') { + GameLib.Event.Emit( + GameLib.Event.BUILD_GUI, + null + ); + } + + this.initialValue = newComponent; } ); }; @@ -981,7 +1026,8 @@ GameLib.System.GUI.prototype.buildControl = function(folder, componentTemplate, ) { controllers.push(folder.add(object, property, -255, 255, 1)); } else if ( - property === 'aspect' + property === 'aspect' || + property === 'wireframeLineWidth' ) { controllers.push(folder.add(object, property, 0, 5, 0.001)); } else if ( @@ -996,12 +1042,12 @@ GameLib.System.GUI.prototype.buildControl = function(folder, componentTemplate, ) { controllers.push(folder.add(object, property, 1, 1000, 1)); } else if ( - property === 'angle' || property === 'width' || property === 'height' || - property === 'depth' + property === 'depth' || + property === 'radius' ) { - controllers.push(folder.add(object, property, -1000, 1000, 1)); + controllers.push(folder.add(object, property, -1000, 1000, 0.1)); } else if ( property === 'near' || property === 'distanceGrain' || @@ -1015,11 +1061,12 @@ GameLib.System.GUI.prototype.buildControl = function(folder, componentTemplate, ) { controllers.push(folder.add(object, property, -100, 100, 0.001)); } else if ( - property === 'radius' + property === 'rrradius' ) { controllers.push(folder.add(object, property, 0, 1000, 0.01)); } else if ( - property === 'thetaLength' + property === 'thetaLength' || + property === 'angle' ) { controllers.push(folder.add(object, property, -Math.PI * 2, Math.PI * 2, 0.01)); } else { @@ -1467,8 +1514,11 @@ GameLib.System.GUI.prototype.buildGUI = function(data) { this.buildQuaternionControl(folder, componentTemplate, templateProperty); } - if (templateProperty === 'parentEntity') { - this.buildEntitySelectionControl(folder, componentTemplate); + if ( + templateProperty === 'parentEntity' || + templateProperty === 'parentMesh' + ) { + this.buildParentSelectionControl(folder, componentTemplate, templateProperty); continue; } diff --git a/src/game-lib-system-linking.js b/src/game-lib-system-linking.js index 5b8367d..4fbe3da 100644 --- a/src/game-lib-system-linking.js +++ b/src/game-lib-system-linking.js @@ -685,12 +685,18 @@ GameLib.System.Linking.prototype.materialTypeChanged = function(data) { if (inUse) { - mesh.instance.material = mesh.materials.map(function(material){ - return material.instance; - }); + if (mesh.materials.length === 1) { + mesh.instance.material = mesh.materials[0].instance + } else { + mesh.instance.material = mesh.materials.map( + function(material) { + return material.instance; + } + ) + } mesh.instance.geometry.uvsNeedUpdate = true; - + mesh.instance.material.needsUpdate = true; } } diff --git a/src/game-lib-system-visualization.js b/src/game-lib-system-visualization.js index 0c70feb..c6a0059 100644 --- a/src/game-lib-system-visualization.js +++ b/src/game-lib-system-visualization.js @@ -48,38 +48,61 @@ GameLib.System.Visualization.prototype.start = function() { GameLib.System.Visualization.prototype.visualize = function(data) { - var shape = this.instance; + var shape = data.shape; - var geometry = new THREE.Geometry(); + var parentMesh = shape.parentMesh; - var v0 = new CANNON.Vec3(); - var v1 = new CANNON.Vec3(); - var v2 = new CANNON.Vec3(); - for (var xi = 0; xi < shape.data.length - 1; xi++) { - for (var yi = 0; yi < shape.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); - 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) - ); - var i = geometry.vertices.length - 3; - geometry.faces.push(new THREE.Face3(i, i+1, i+2)); + shape.setFromMesh(); + + var apiMesh = new GameLib.D3.API.Mesh(); + + apiMesh.name = 'Visualization Mesh for Shape ' + shape.name; + + if (shape instanceof GameLib.D3.Shape.HeightMap) { + var v0 = new CANNON.Vec3(); + var v1 = new CANNON.Vec3(); + var v2 = new CANNON.Vec3(); + for (var xi = 0; xi < shape.heightData.length - 1; xi++) { + for (var yi = 0; yi < shape.heightData[xi].length - 1; yi++) { + for (var k = 0; k < 2; k++) { + 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); + apiMesh.vertices.push( + new GameLib.D3.API.Vertex( + new GameLib.API.Vector3(v0.x, v0.y, v0.z) + ), + new GameLib.D3.API.Vertex( + new GameLib.API.Vector3(v1.x, v1.y, v1.z) + ), + new GameLib.D3.API.Vertex( + new GameLib.API.Vector3(v2.x, v2.y, v2.z) + ) + ); + var i = apiMesh.vertices.length - 3; + apiMesh.faces.push( + new GameLib.D3.API.Face( + null, + null, + i, + i+1, + i+2 + ) + ); + } } } } - geometry.computeBoundingSphere(); - geometry.computeFaceNormals(); - mesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial()); - this.mesh = mesh; + new GameLib.D3.Mesh( + this.graphics, + apiMesh + ); + }; GameLib.System.Visualization.prototype.stopVisualize = function(data) {