From 123b89cf265bd8b52b735226a4fa36075f2be22c Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Sat, 10 Feb 2018 14:44:24 +0100 Subject: [PATCH] just before introducing geometry base --- src/game-lib-a-2-utils.js | 25 + ...curve-path.js => game-lib-curve-path-a.js} | 0 ...path-d2.js => game-lib-curve-path-d2-a.js} | 0 src/game-lib-d3-api-geometry-a.js | 252 ++++++++ ...try-0.js => game-lib-d3-api-geometry-b.js} | 0 src/game-lib-d3-api-mesh-0.js | 88 +-- src/game-lib-d3-buffer-geometry-a.js | 150 ++++- src/game-lib-d3-buffer-geometry-box.js | 103 +++ src/game-lib-d3-geometry-a.js | 110 ++++ src/game-lib-d3-geometry-buffer-a.js | 258 -------- src/game-lib-d3-mesh-0.js | 595 ++++++------------ src/game-lib-d3-raycaster.js | 2 +- src/game-lib-system-gui.js | 12 + 13 files changed, 906 insertions(+), 689 deletions(-) rename src/{game-lib-curve-path.js => game-lib-curve-path-a.js} (100%) rename src/{game-lib-curve-path-d2.js => game-lib-curve-path-d2-a.js} (100%) create mode 100644 src/game-lib-d3-api-geometry-a.js rename src/{game-lib-d3-api-geometry-0.js => game-lib-d3-api-geometry-b.js} (100%) create mode 100644 src/game-lib-d3-buffer-geometry-box.js delete mode 100644 src/game-lib-d3-geometry-buffer-a.js diff --git a/src/game-lib-a-2-utils.js b/src/game-lib-a-2-utils.js index 8da529c..2d6b6e9 100644 --- a/src/game-lib-a-2-utils.js +++ b/src/game-lib-a-2-utils.js @@ -38,6 +38,31 @@ GameLib.Utils.BuildVectorSource = function(result, name, dimension) { console.warn('unknown dimension : ' + dimension); }; +/** + * Returns all 'instances' of the array, or null if an 'instance' is undefined + * @constructor + * @param array + */ +GameLib.Utils.GetArrayInstances = function(array) { + return array.reduce( + function(result, object) { + + if (result === null) { + return result; + } + + if (GameLib.Utils.UndefinedOrNull(object.instance)) { + result = null; + } else { + result.push(object.instance); + } + + return result; + }, + [] + ); +}; + GameLib.Utils.SortFacesByMaterialIndex = function(faces) { /** diff --git a/src/game-lib-curve-path.js b/src/game-lib-curve-path-a.js similarity index 100% rename from src/game-lib-curve-path.js rename to src/game-lib-curve-path-a.js diff --git a/src/game-lib-curve-path-d2.js b/src/game-lib-curve-path-d2-a.js similarity index 100% rename from src/game-lib-curve-path-d2.js rename to src/game-lib-curve-path-d2-a.js diff --git a/src/game-lib-d3-api-geometry-a.js b/src/game-lib-d3-api-geometry-a.js new file mode 100644 index 0000000..5353d38 --- /dev/null +++ b/src/game-lib-d3-api-geometry-a.js @@ -0,0 +1,252 @@ +/** + * GameLib.D3.API.GeometryBase + * @param id + * @param name + * @param geometryType + * @param parentEntity + * @param boundingBox + * @param boundingSphere + * @param faces + * @param vertices + * @constructor + */ +GameLib.D3.API.GeometryBase = function( + id, + name, + geometryType, + parentEntity, + boundingBox, + boundingSphere, + faces, + vertices +) { + if (GameLib.Utils.UndefinedOrNull(id)) { + id = GameLib.Utils.RandomId(); + } + this.id = id; + + if (GameLib.Utils.UndefinedOrNull(geometryType)) { + geometryType = GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_BASE; + } + this.geometryType = geometryType; + + if (GameLib.Utils.UndefinedOrNull(name)) { + + switch (this.geometryType) { + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_NORMAL : + name = 'Geometry'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_BOX : + name = 'Geometry Box'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_CIRCLE : + name = 'Geometry Circle'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_CONE : + name = 'Geometry Cone'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_CYLINDER : + name = 'Geometry Cylinder'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_DODECAHEDRON : + name = 'Geometry Dodecahedron'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_EDGES : + name = 'Geometry Edges'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_EXTRUDE : + name = 'Geometry Extrude'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_ICOSAHEDRON : + name = 'Geometry Icosahedron'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_LATHE : + name = 'Geometry Lathe'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_OCTAHEDRON : + name = 'Geometry Octahedron'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_PARAMETRIC : + name = 'Geometry Parametric'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_PLANE : + name = 'Geometry Plane'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_POLYHEDRON : + name = 'Geometry Polyhedron'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_RING : + name = 'Geometry Ring'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_SHAPE : + name = 'Geometry Shape'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_SPHERE : + name = 'Geometry Sphere'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TETRAHEDRON : + name = 'Geometry Tetrahedron'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TEXT : + name = 'Geometry Text'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TORUS : + name = 'Geometry Torus'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TORUS_KNOT : + name = 'Geometry Torus Knot'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TUBE : + name = 'Geometry Tube'; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_WIREFRAME : + name = 'Geometry Wireframe'; + break; + default : + console.warn('no nice name for geometry'); + name = 'Geometry'; + } + + name += ' (' + id + ')'; + } + this.name = name; + + if (GameLib.Utils.UndefinedOrNull(boundingBox)) { + boundingBox = new GameLib.API.Box3(); + } + this.boundingBox = boundingBox; + + if (GameLib.Utils.UndefinedOrNull(boundingSphere)) { + boundingSphere = new GameLib.API.Sphere(); + } + this.boundingSphere = boundingSphere; + + if (GameLib.Utils.UndefinedOrNull(faces)) { + faces = []; + } + this.faces = faces; + + if (GameLib.Utils.UndefinedOrNull(vertices)) { + vertices = []; + } + this.vertices = vertices; + + GameLib.API.Component.call( + this, + GameLib.D3.API.Geometry.GetComponentType(this.geometryType), + parentEntity + ); +}; + +GameLib.D3.API.GeometryBase.prototype = Object.create(GameLib.API.Component.prototype); +GameLib.D3.API.GeometryBase.prototype.constructor = GameLib.D3.API.GeometryBase; + +GameLib.D3.API.GeometryBase.GetComponentType = function(geometryType) { + + var componentType = null; + + switch (geometryType) { + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_NORMAL : + componentType = GameLib.Component.GEOMETRY; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_BOX : + componentType = GameLib.Component.GEOMETRY_BOX; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_CIRCLE : + componentType = GameLib.Component.GEOMETRY_CIRCLE; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_CONE : + componentType = GameLib.Component.GEOMETRY_CONE; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_CYLINDER : + componentType = GameLib.Component.GEOMETRY_CYLINDER; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_DODECAHEDRON : + componentType = GameLib.Component.GEOMETRY_DODECAHEDRON; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_EDGES : + componentType = GameLib.Component.GEOMETRY_EDGES; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_EXTRUDE : + componentType = GameLib.Component.GEOMETRY_EXTRUDE; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_ICOSAHEDRON : + componentType = GameLib.Component.GEOMETRY_ICOSAHEDRON; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_LATHE : + componentType = GameLib.Component.GEOMETRY_LATHE; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_OCTAHEDRON : + componentType = GameLib.Component.GEOMETRY_OCTAHEDRON; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_PARAMETRIC : + componentType = GameLib.Component.GEOMETRY_PARAMETRIC; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_PLANE : + componentType = GameLib.Component.GEOMETRY_PLANE; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_POLYHEDRON : + componentType = GameLib.Component.GEOMETRY_POLYHEDRON; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_RING : + componentType = GameLib.Component.GEOMETRY_RING; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_SHAPE : + componentType = GameLib.Component.GEOMETRY_SHAPE; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_SPHERE : + componentType = GameLib.Component.GEOMETRY_SPHERE; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TETRAHEDRON : + componentType = GameLib.Component.GEOMETRY_TETRAHEDRON; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TEXT : + componentType = GameLib.Component.GEOMETRY_TEXT; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TORUS : + componentType = GameLib.Component.GEOMETRY_TORUS; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TORUS_KNOT : + componentType = GameLib.Component.GEOMETRY_TORUS_KNOT; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TUBE : + componentType = GameLib.Component.GEOMETRY_TUBE; + break; + case GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_WIREFRAME : + componentType = GameLib.Component.GEOMETRY_WIREFRAME; + break; + default: + throw new Error('unhandled geometry type: ' + geometryType); + } + + return componentType; +}; + +/** + * GeometryBase Type + * @type {number} + */ +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_BASE = 0x0; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_NORMAL = 0x1; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_BOX = 0x2; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_CIRCLE = 0x3; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_CONE = 0x4; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_CYLINDER = 0x5; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_DODECAHEDRON = 0x6; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_EDGES = 0x7; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_EXTRUDE = 0x8; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_ICOSAHEDRON = 0x9; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_LATHE = 0xa; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_OCTAHEDRON = 0xb; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_PARAMETRIC = 0xc; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_PLANE = 0xd; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_POLYHEDRON = 0xe; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_RING = 0xf; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_SHAPE = 0x10; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_SPHERE = 0x11; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TETRAHEDRON = 0x12; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TEXT = 0x13; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TORUS = 0x14; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TORUS_KNOT = 0x15; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_TUBE = 0x16; +GameLib.D3.API.GeometryBase.GEOMETRY_TYPE_WIREFRAME = 0x17; diff --git a/src/game-lib-d3-api-geometry-0.js b/src/game-lib-d3-api-geometry-b.js similarity index 100% rename from src/game-lib-d3-api-geometry-0.js rename to src/game-lib-d3-api-geometry-b.js diff --git a/src/game-lib-d3-api-mesh-0.js b/src/game-lib-d3-api-mesh-0.js index 9be5c9f..a9e2b34 100644 --- a/src/game-lib-d3-api-mesh-0.js +++ b/src/game-lib-d3-api-mesh-0.js @@ -4,10 +4,11 @@ * @param name * @param meshType * @param parentEntity - * @param excludeFromEnvironment + * @param geometry * @param materials * @param parentMesh * @param parentScene + * @param excludeFromEnvironment * @param skeleton * @param position * @param quaternion @@ -19,6 +20,9 @@ * @param visible * @param castShadow * @param receiveShadow + * @param drawMode + * @param morphTargetInfluences + * @param morphTargetDictionary * @constructor */ GameLib.D3.API.Mesh = function( @@ -26,10 +30,11 @@ GameLib.D3.API.Mesh = function( name, meshType, parentEntity, - excludeFromEnvironment, + geometry, materials, parentMesh, parentScene, + excludeFromEnvironment, skeleton, position, quaternion, @@ -40,7 +45,10 @@ GameLib.D3.API.Mesh = function( useQuaternion, visible, castShadow, - receiveShadow + receiveShadow, + drawMode, + morphTargetInfluences, + morphTargetDictionary ) { if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -88,20 +96,17 @@ GameLib.D3.API.Mesh = function( } this.name = name; - if (GameLib.Utils.UndefinedOrNull(excludeFromEnvironment)) { - excludeFromEnvironment = false; + if (GameLib.Utils.UndefinedOrNull(geometry)) { + geometry = null; } - this.excludeFromEnvironment = excludeFromEnvironment; + this.geometry = geometry; - if (GameLib.Utils.UndefinedOrNull(vertices)) { - vertices = []; + if (GameLib.Utils.UndefinedOrNull(materials)) { + materials = [ + new GameLib.D3.API.Material.Phong() + ]; } - this.vertices = vertices; - - if (GameLib.Utils.UndefinedOrNull(faces)) { - faces = []; - } - this.faces = faces; + this.materials = materials; if (GameLib.Utils.UndefinedOrNull(parentMesh)) { parentMesh = null; @@ -113,28 +118,16 @@ GameLib.D3.API.Mesh = function( } this.parentScene = parentScene; + if (GameLib.Utils.UndefinedOrNull(excludeFromEnvironment)) { + excludeFromEnvironment = false; + } + this.excludeFromEnvironment = excludeFromEnvironment; + if (GameLib.Utils.UndefinedOrNull(skeleton)) { skeleton = null; } this.skeleton = skeleton; - if (GameLib.Utils.UndefinedOrNull(skinIndices)) { - skinIndices = []; - } - this.skinIndices = skinIndices; - - if (GameLib.Utils.UndefinedOrNull(skinWeights)) { - skinWeights = []; - } - this.skinWeights = skinWeights; - - if (GameLib.Utils.UndefinedOrNull(materials) || (materials instanceof Array && materials.length === 0)) { - materials = [ - new GameLib.D3.API.Material.Basic() - ]; - } - this.materials = materials; - if (GameLib.Utils.UndefinedOrNull(position)) { position = new GameLib.API.Vector3(0,0,0); } @@ -160,21 +153,11 @@ GameLib.D3.API.Mesh = function( } this.up = up; - if (GameLib.Utils.UndefinedOrNull(modelMatrix)) { - modelMatrix = new GameLib.API.Matrix4(); - } - this.modelMatrix = modelMatrix; - if (GameLib.Utils.UndefinedOrNull(renderOrder)) { renderOrder = 0; } this.renderOrder = renderOrder; - if (GameLib.Utils.UndefinedOrNull(isBufferMesh)) { - isBufferMesh = false; - } - this.isBufferMesh = isBufferMesh; - if (GameLib.Utils.UndefinedOrNull(useQuaternion)) { useQuaternion = true; } @@ -195,6 +178,21 @@ GameLib.D3.API.Mesh = function( } this.receiveShadow = receiveShadow; + if (GameLib.Utils.UndefinedOrNull(drawMode)) { + drawMode = GameLib.D3.API.Mesh.DRAW_MODE_TRIANGLES; + } + this.drawMode = drawMode; + + if (GameLib.Utils.UndefinedOrNull(morphTargetInfluences)) { + morphTargetInfluences = []; + } + this.morphTargetInfluences = morphTargetInfluences; + + if (GameLib.Utils.UndefinedOrNull(morphTargetDictionary)) { + morphTargetDictionary = null; + } + this.morphTargetDictionary = morphTargetDictionary; + var componentType = GameLib.Component.MESH; if (this.meshType === GameLib.D3.API.Mesh.MESH_TYPE_PLANE) { @@ -245,3 +243,11 @@ GameLib.D3.API.Mesh.MESH_TYPE_BOX = 0x5; GameLib.D3.API.Mesh.MESH_TYPE_CYLINDER = 0x6; GameLib.D3.API.Mesh.MESH_TYPE_TEXT = 0x7; GameLib.D3.API.Mesh.MESH_TYPE_LINE = 0x8; + +/** + * Draw Modes + * @type {number} + */ +GameLib.D3.API.Mesh.DRAW_MODE_TRIANGLES = 0; +GameLib.D3.API.Mesh.DRAW_MODE_TRIANGLE_STRIP = 1; +GameLib.D3.API.Mesh.DRAW_MODE_TRIANGLE_FAN = 2; diff --git a/src/game-lib-d3-buffer-geometry-a.js b/src/game-lib-d3-buffer-geometry-a.js index a91aa5b..d11002c 100644 --- a/src/game-lib-d3-buffer-geometry-a.js +++ b/src/game-lib-d3-buffer-geometry-a.js @@ -80,6 +80,16 @@ GameLib.D3.BufferGeometry.prototype.constructor = GameLib.D3.BufferGeometry; GameLib.D3.BufferGeometry.prototype.createInstance = function() { + if (GameLib.Utils.Defined(this.instance)) { + + /** + * We already have our object - just call our parent and return + */ + GameLib.Component.prototype.createInstance.call(this); + + return; + } + /** * Then we convert to the new BufferGeometry type */ @@ -229,12 +239,14 @@ GameLib.D3.BufferGeometry.prototype.createInstance = function() { groupIndexCounts.reduce( function(start, group) { - geometry.addGroup(start, group.count, group.index); + this.instance.addGroup(start, group.count, group.index); return start + group.count; - }, + }.bind(this), 0 ); + GameLib.Component.prototype.createInstance.call(this); + }; GameLib.D3.BufferGeometry.prototype.updateInstance = function(property) { @@ -346,3 +358,137 @@ GameLib.D3.BufferGeometry.prototype.toApiObject = function() { return apiBufferGeometry; }; + +/** + * Update GameLib.D3.BufferGeometry from instance + */ +GameLib.D3.BufferGeometry.prototype.updateFromInstance = function() { + + var vertices = this.instance.getAttribute('position').array; + + var uvs = this.instance.getAttribute('uv').array; + + this.instance.groups.map(function(group){ + + var materialIndex = group.materialIndex; + + var start = group.start; + + var count = group.count; + + var faceIndexes = []; + + var indexedUvs = []; + + for (var i = start; i < count; i ++) { + + var vertex = new GameLib.D3.Vertex( + this.graphics, + new GameLib.D3.API.Vertex( + new GameLib.Vector3( + this.graphics, + new GameLib.API.Vector3( + vertices[i*3], + vertices[i*3 + 1], + vertices[i*3 + 2] + ) + ) + ) + ); + + var uv = new GameLib.Vector2( + this.graphics, + new GameLib.API.Vector2( + uvs[i*2], + uvs[i*2 + 1] + ) + ); + + indexedUvs.push(uv); + + var vertexIndex = this.vertices.reduce( + function(result, indexedVertex, currentIndex){ + if (indexedVertex.position.equals(vertex.position)) { + result = currentIndex; + } + return result; + }, + -1 + ); + + var faceIndex = vertexIndex; + + if (vertexIndex === -1) { + this.vertices.push(vertex); + faceIndex = this.vertices.length - 1; + } + + faceIndexes.push(faceIndex); + + if (faceIndexes.length === 3) { + + this.faces.push( + new GameLib.D3.Face( + this.graphics, + new GameLib.D3.API.Face( + null, + null, + faceIndexes[0], + faceIndexes[1], + faceIndexes[2], + materialIndex, + [[indexedUvs[0], indexedUvs[1], indexedUvs[2]]] + ) + ) + ); + + indexedUvs = []; + faceIndexes = []; + } + } + + }.bind(this)); + +}; + +/** + * Clears all groups + */ +GameLib.D3.BufferGeometry.prototype.clearGroups = function() { + this.instance.clearGroups(); + this.groups = this.instance.groups; +}; + +/** + * Clears all groups + */ +GameLib.D3.BufferGeometry.prototype.singleMaterial = function() { + + this.instance.clearGroups(); + + this.instance.addGroup( + 0, + this.instance.index.count, + 0 + ); + + this.groups = this.instance.groups; +}; + +/** + * Compute Vertex Normals + */ +GameLib.D3.BufferGeometry.prototype.computeVertexNormals = function() { + + this.instance.computeVertexNormals(); + +}; + +/** + * Compute Vertex Normals + */ +GameLib.D3.BufferGeometry.prototype.toNonIndexed = function() { + + this.instance.toNonIndexed(); + +}; \ No newline at end of file diff --git a/src/game-lib-d3-buffer-geometry-box.js b/src/game-lib-d3-buffer-geometry-box.js new file mode 100644 index 0000000..1ab00a7 --- /dev/null +++ b/src/game-lib-d3-buffer-geometry-box.js @@ -0,0 +1,103 @@ +/** + * GameLib.D3.BufferGeometry.Box + * @param graphics GameLib.GraphicsRuntime + * @param apiBoxBufferGeometry + * @constructor + */ +GameLib.D3.BufferGeometry.Box = function( + graphics, + apiBoxBufferGeometry +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiBoxBufferGeometry)) { + apiBoxBufferGeometry = { + bufferGeometryType : GameLib.D3.API.BufferGeometry.BUFFER_GEOMETRY_TYPE_BOX + }; + } + + GameLib.D3.API.BufferGeometry.Box.call( + this, + apiBoxBufferGeometry, + apiBoxBufferGeometry.width, + apiBoxBufferGeometry.height, + apiBoxBufferGeometry.depth, + apiBoxBufferGeometry.widthSegments, + apiBoxBufferGeometry.heightSegments, + apiBoxBufferGeometry.depthSegments + ); + + GameLib.D3.BufferGeometry.call( + this, + this.graphics, + apiBoxBufferGeometry + ); + +}; + +GameLib.D3.BufferGeometry.Box.prototype = Object.create(GameLib.D3.BufferGeometry.prototype); +GameLib.D3.BufferGeometry.Box.prototype.constructor = GameLib.D3.BufferGeometry.Box; + +/** + * Creates a light instance + * @returns {*} + */ +GameLib.D3.BufferGeometry.Box.prototype.createInstance = function() { + + this.instance = new THREE.BoxBufferGeometry( + this.width, + this.height, + this.depth, + this.widthSegments, + this.heightSegments, + this.depthSegments + ); + + GameLib.D3.BufferGeometry.prototype.createInstance.call(this); +}; + +/** + * Updates the instance with the current state + */ +GameLib.D3.BufferGeometry.Box.prototype.updateInstance = function(property, oldTarget) { + + if ( + property === 'width' || + property === 'height' || + property === 'depth' || + property === 'widthSegments' || + property === 'heightSegments' || + property === 'depthSegments' + ) { + console.warn('todo: check if i can update this without creating a new instance'); + //this.createInstance(); + this.instance.width = this.width; + this.instance.attributes.position.needsUpdate = true; + return; + } + + GameLib.D3.BufferGeometry.prototype.updateInstance.call(this, property); +}; + +/** + * Converts a GameLib.D3.BufferGeometry to a GameLib.D3.API.BufferGeometry + * @returns {GameLib.D3.API.BufferGeometry} + */ +GameLib.D3.BufferGeometry.Box.prototype.toApiObject = function() { + + var apiBufferGeometry = GameLib.D3.BufferGeometry.prototype.toApiObject.call(this); + + var apiBoxBufferGeometry = new GameLib.D3.API.BufferGeometry.Box( + apiBufferGeometry, + this.width, + this.height, + this.depth, + this.widthSegments, + this.heightSegments, + this.depthSegments + ); + + return apiBoxBufferGeometry; +}; diff --git a/src/game-lib-d3-geometry-a.js b/src/game-lib-d3-geometry-a.js index b812f65..95c9a77 100644 --- a/src/game-lib-d3-geometry-a.js +++ b/src/game-lib-d3-geometry-a.js @@ -456,3 +456,113 @@ GameLib.D3.Geometry.prototype.toApiObject = function() { return apiGeometry; }; + +/** + * Update GameLib.D3.Geometry from instance + */ +GameLib.D3.Geometry.prototype.updateFromInstance = function() { + + var processed = 0; + + this.instance.faces.map(function(face, faceIndex){ + + processed++; + + if (processed % 100 === 0) { + console.log('processed ' + processed + ' faces'); + } + + this.faces.push( + new GameLib.D3.Face( + this.graphics, + new GameLib.D3.API.Face( + null, + null, + face.a, + face.b, + face.c, + face.materialIndex, + [[ + new GameLib.API.Vector2( + this.instance.faceVertexUvs[0][faceIndex][0].x, + this.instance.faceVertexUvs[0][faceIndex][0].y + ), + new GameLib.API.Vector2( + this.instance.faceVertexUvs[0][faceIndex][1].x, + this.instance.faceVertexUvs[0][faceIndex][1].y + ), + new GameLib.API.Vector2( + this.instance.faceVertexUvs[0][faceIndex][2].x, + this.instance.faceVertexUvs[0][faceIndex][2].y + ) + ]], + new GameLib.Color( + this.graphics, + new GameLib.API.Color( + face.color.r, + face.color.g, + face.color.b + ) + ), + face.vertexColors.map(function(vertexColor){ + return new GameLib.Color( + this.graphics, + new GameLib.API.Color( + vertexColor.r, + vertexColor.g, + vertexColor.b + ) + ) + }.bind(this)), + face.vertexNormals.map(function(vertexNormal){ + return new GameLib.Vector3( + this.graphics, + new GameLib.API.Vector3( + vertexNormal.x, + vertexNormal.y, + vertexNormal.z + ) + ) + }.bind(this)), + new GameLib.Vector3( + this.graphics, + new GameLib.API.Vector3( + face.normal.x, + face.normal.y, + face.normal.z + ) + ) + ) + ) + ) + }.bind(this)); + + processed = 0; + + this.instance.vertices.map(function(vertex){ + + processed++; + + if (processed % 100 === 0) { + console.log('processed ' + processed + ' vertices'); + } + + this.vertices.push( + new GameLib.D3.Vertex( + this.graphics, + new GameLib.D3.API.Vertex( + new GameLib.Vector3( + this.graphics, + new GameLib.API.Vector3( + vertex.x, + vertex.y, + vertex.z + ) + ) + ) + ) + ) + + }.bind(this)); + +}; \ No newline at end of file diff --git a/src/game-lib-d3-geometry-buffer-a.js b/src/game-lib-d3-geometry-buffer-a.js deleted file mode 100644 index 8ae19d4..0000000 --- a/src/game-lib-d3-geometry-buffer-a.js +++ /dev/null @@ -1,258 +0,0 @@ -/** - * GameLib.D3.Geometry.Buffer - * @param graphics - * @param apiGeometry - * @property geometryType - * @constructor - */ -GameLib.D3.BufferGeometry = function( - graphics, - apiGeometry -) { - - this.graphics = graphics; - this.graphics.isNotThreeThrow(); - - if (GameLib.Utils.UndefinedOrNull(apiGeometry)) { - apiGeometry = { - geometryType : GameLib.D3.API.Geometry.GEOMETRY_TYPE_BUFFER - }; - } - - GameLib.D3.Geometry.call( - this, - apiGeometry - ); -}; - -GameLib.D3.Geometry.Buffer.prototype = Object.create(GameLib.D3.Geometry.prototype); -GameLib.D3.Geometry.Buffer.prototype.constructor = GameLib.D3.Geometry.Buffer; - -GameLib.D3.Geometry.Buffer.prototype.createInstance = function() { - - /** - * First we try it this way - we setup things the old-school way - */ - GameLib.D3.Geometry.prototype.createInstance.call(this); - - /** - * Then we convert to the new BufferGeometry type - */ - this.instance = new THREE.BufferGeometry().fromGeometry(this.instance); - - // /** - // * Setup mesh vertices positions - // * @type {Float32Array} - // */ - // var vertices = new Float32Array( - // - // this.faces.reduce( - // function(result, face){ - // result.push(this.vertices[face.v0index].position.x); - // result.push(this.vertices[face.v0index].position.y); - // result.push(this.vertices[face.v0index].position.z); - // result.push(this.vertices[face.v1index].position.x); - // result.push(this.vertices[face.v1index].position.y); - // result.push(this.vertices[face.v1index].position.z); - // result.push(this.vertices[face.v2index].position.x); - // result.push(this.vertices[face.v2index].position.y); - // result.push(this.vertices[face.v2index].position.z); - // return result; - // }.bind(this), - // [] - // ) - // - // ); - // - // this.instance.addAttribute('position', new THREE.BufferAttribute(vertices, 3)); - // - // /** - // * Setyp mesh vertices colors - // */ - // var colors = new Float32Array( - // this.faces.reduce( - // function(result, face){ - // result.push(1,1,1,1,1,1,1,1,1); - // // result.push(face.color.r); - // // result.push(face.color.g); - // // result.push(face.color.b); - // // result.push(face.color.r); - // // result.push(face.color.g); - // // result.push(face.color.b); - // // result.push(face.color.r); - // // result.push(face.color.g); - // // result.push(face.color.b); - // return result; - // }.bind(this), - // [] - // ) - // ); - // geometry.addAttribute('color', new THREE.BufferAttribute(colors, 3, true)); - // - // /** - // * Setup face UVs - // */ - // var uvs = new Float32Array( - // this.faces.reduce( - // function(result, face) { - // - // face.uvs[0].map( - // function(uv) { - // result.push(uv.x); - // result.push(uv.y); - // } - // ); - // - // return result; - // }, - // [] - // ) - // ); - // geometry.addAttribute('uv', new THREE.BufferAttribute(uvs, 2)); - // - // var normals = new Float32Array( - // this.faces.reduce( - // function(result, face) { - // - // result.push( - // face.normal.x, - // face.normal.y, - // face.normal.z - // ); - // result.push( - // face.normal.x, - // face.normal.y, - // face.normal.z - // ); - // result.push( - // face.normal.x, - // face.normal.y, - // face.normal.z - // ); - // return result; - // }, - // [] - // ) - // ); - // geometry.addAttribute('normal', new THREE.BufferAttribute( normals, 3 )); - // - // geometry.computeVertexNormals(); - // - // - // /** - // * Setup material groups - this means creating a new group for each material index change - // * We know faces are sorted according to material index - // */ - // var groupIndexCounts = this.faces.reduce( - // function(result, face) { - // - // var currentGroup = result.pop(); - // - // if (currentGroup.index !== face.materialIndex) { - // /** - // * We have a new group - // */ - // result.push(currentGroup); - // result.push({ - // index: face.materialIndex, - // count: 3 - // }) - // } else { - // currentGroup.count += 3; - // result.push(currentGroup); - // } - // - // return result; - // }, - // [ - // { - // index : 0, - // count : 0 - // } - // ] - // ); - // - // groupIndexCounts.reduce( - // function(start, group) { - // geometry.addGroup(start, group.count, group.index); - // return start + group.count; - // }, - // 0 - // ); - // - // - // } else { - // - // - // } - - - -}; - -GameLib.D3.Geometry.Buffer.prototype.updateInstance = function(property) { - - if (GameLib.Utils.UndefinedOrNull(this.instance)) { - console.warn('no geometry instance'); - return; - } - - - - GameLib.D3.Geometry.prototype.updateInstance.call(this, property); -}; - -/** - * Converts a GameLib.D3.Geometry to a GameLib.D3.API.Geometry - * @returns {GameLib.D3.API.Geometry} - */ -GameLib.D3.Geometry.Buffer.prototype.toApiObject = function() { - - var apiGeometry = new GameLib.D3.API.Geometry( - this.id, - this.name, - this.geometryType, - this.isBufferGeometry, - GameLib.Utils.IdOrNull(this.parentEntity), - this.boundingBox.toApiObject(), - this.boundingSphere.toApiObject(), - this.colors.map( - function(color) { - return color.toApiObject(); - } - ), - this.faces.map( - function(face) { - return face.toApiObject(); - } - ), - this.lineDistances, - this.morphTargets, - this.morphNormals, - this.skinWeights.map( - function(skinWeight) { - return skinWeight.toApiObject(); - } - ), - this.skinIndices.map( - function(skinIndex) { - return skinIndex.toApiObject(); - } - ), - this.vertices.map( - function (vertex) { - return vertex.toApiObject(); - } - ), - this.verticesNeedsUpdate, - this.elementsNeedUpdate, - this.uvsNeedUpdate, - this.normalsNeedUpdate, - this.colorsNeedUpdate, - this.groupsNeedUpdate, - this.lineDistancesNeedUpdate - ); - - return apiGeometry; - -}; diff --git a/src/game-lib-d3-mesh-0.js b/src/game-lib-d3-mesh-0.js index 08ca7be..b727fb4 100644 --- a/src/game-lib-d3-mesh-0.js +++ b/src/game-lib-d3-mesh-0.js @@ -2,6 +2,7 @@ * GameLib.D3.Mesh * @param graphics GameLib.GraphicsRuntime * @param apiMesh GameLib.D3.API.Mesh + * @property geometry * @constructor */ GameLib.D3.Mesh = function ( @@ -22,51 +23,42 @@ GameLib.D3.Mesh = function ( apiMesh.id, apiMesh.name, apiMesh.meshType, - apiMesh.excludeFromEnvironment, - apiMesh.vertices, - apiMesh.faces, + apiMesh.parentEntity, + apiMesh.geometry, apiMesh.materials, apiMesh.parentMesh, apiMesh.parentScene, + apiMesh.excludeFromEnvironment, apiMesh.skeleton, - apiMesh.skinIndices, - apiMesh.skinWeights, apiMesh.position, apiMesh.quaternion, apiMesh.rotation, apiMesh.scale, apiMesh.up, - apiMesh.modelMatrix, apiMesh.renderOrder, - apiMesh.isBufferMesh, apiMesh.useQuaternion, apiMesh.visible, apiMesh.castShadow, apiMesh.receiveShadow, - apiMesh.parentEntity + apiMesh.drawMode, + apiMesh.morphTargetInfluences, + apiMesh.morphTargetDictionary ); - this.faces = this.faces.map( - function(face) { - return new GameLib.D3.Face( - this.graphics, - face - ) - }.bind(this) - ); + if ( + GameLib.Utils.Defined(this.geometry) && + GameLib.Utils.Defined(this.geometry.componentType) + ) { + this.geometry = GameLib.Component.ConstructFromObject(this.geometry); + } this.materials = this.materials.map( function(material) { - if (typeof material === 'string') { - return material; - } - - var info = GameLib.Component.GetComponentInfo(material.componentType); - if (material instanceof info.apiConstructor) { - return new info.constructor(this.graphics, material); + if (GameLib.Utils.Defined(material.componentType)) { + return GameLib.Component.ConstructFromObject(material); } else { - console.warn('unhandled component type'); + return material; } }.bind(this) @@ -79,15 +71,6 @@ GameLib.D3.Mesh = function ( ); } - this.vertices = this.vertices.map( - function (vertex) { - return new GameLib.D3.Vertex( - this.graphics, - vertex - ); - }.bind(this) - ); - this.position = new GameLib.Vector3( this.graphics, this.position, @@ -118,22 +101,11 @@ GameLib.D3.Mesh = function ( this ); - this.modelMatrix = new GameLib.Matrix4( - this.graphics, - this.modelMatrix, - this - ); - - this.dimensions = new GameLib.Vector3( - this.graphics, - new GameLib.API.Vector3(), - this - ); - var linkedObjects = { 'parentMesh' : GameLib.D3.Mesh, 'parentScene' : GameLib.D3.Scene, 'materials' : [GameLib.D3.Material], + 'geometry' : GameLib.D3.BufferGeometry, 'skeleton' : GameLib.D3.Skeleton }; @@ -162,92 +134,65 @@ GameLib.D3.Mesh = function ( GameLib.D3.Mesh.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.Mesh.prototype.constructor = GameLib.D3.Mesh; -GameLib.D3.Mesh.prototype.lookAt = function(vector) { - this.instance.lookAt( - new THREE.Vector3( - vector.x, - vector.y, - vector.z - ) - ); - - this.rotation.x = this.instance.rotation.x; - this.rotation.y = this.instance.rotation.y; - this.rotation.z = this.instance.rotation.z; - - this.quaternion.x = this.instance.quaternion.x; - this.quaternion.y = this.instance.quaternion.y; - this.quaternion.z = this.instance.quaternion.z; - this.quaternion.w = this.instance.quaternion.w; - -}; - -GameLib.D3.Mesh.prototype.createInstanceGeometry = function(instanceGeometry) { - - if (instanceGeometry instanceof THREE.Geometry) { - - if (this.isBufferMesh) { - return new THREE.BufferGeometry().fromGeometry(instanceGeometry); - } - } - - - - - return geometry; -}; /** * Creates a mesh instance or updates it */ GameLib.D3.Mesh.prototype.createInstance = function() { - var geometry = this.createInstanceGeometry(); + if ( + GameLib.Utils.UndefinedOrNull(this.geometry) || + GameLib.Utils.UndefinedOrNull(this.geometry.instance) + ) { + console.warn('geometry not ready for this mesh ' + this.name); + return; + } - if (this.skeleton) { + var materialInstances = GameLib.Utils.GetArrayInstances(this.materials); - if (this.materials.length === 1) { - this.instance = new THREE.SkinnedMesh( - geometry, - this.materials[0].instance - ) - } else { - this.instance = new THREE.SkinnedMesh( - geometry, - this.materials.map( - function(material) { - return material.instance; - } - ) - ) - } + if ( + GameLib.Utils.UndefinedOrNull(materialInstances) + ) { + console.warn('materials not ready for this mesh ' + this.name); + return; + } + + if (this.skeleton) { + + this.instance = new THREE.SkinnedMesh( + this.geometry.instance, + materialInstances + ); this.instance.add(this.skeleton.rootBoneInstance); this.instance.bind(this.skeleton.instance); - } else { - if (this.materials.length === 1) { - this.instance = new THREE.Mesh( - geometry, - this.materials[0].instance - ) - } else { - this.instance = new THREE.Mesh( - geometry, - this.materials.map( - function(material) { - return material.instance; - } - ) - ) - } - } + } else { + this.instance = new THREE.Mesh( + this.geometry.instance, + materialInstances + ); + } this.instance.name = this.name; - if (this.parentMesh && this.parentMesh.instance) { - this.parentMesh.add(this.instance, this); + if (GameLib.Utils.Defined(this.parentMesh)) { + + if (GameLib.Utils.UndefinedOrNull(this.parentMesh.instance)) { + console.warn('parent mesh not linked at time of create instance'); + } else { + this.parentMesh.add(this.instance, this); + } + + } + + if (GameLib.Utils.Defined(this.parentScene)) { + if (GameLib.Utils.UndefinedOrNull(this.parentScene.instance)) { + console.warn('parent scene not linked at time of create instance'); + } else { + this.parentScene.add(this.instance, this); + } } this.instance.position.x = this.position.x; @@ -289,6 +234,16 @@ GameLib.D3.Mesh.prototype.createInstance = function() { this.instance.receiveShadow = this.receiveShadow; + this.instance.drawMode = this.drawMode; + + /** + * The rest we update from the instance + */ + this.morphTargetInfluences = this.instance.morphTargetInfluences; + this.morphTargetDictionary = this.instance.morphTargetDictionary; + + console.log('mesh instance created: ' + this.name); + GameLib.Component.prototype.createInstance.call(this); }; @@ -301,6 +256,72 @@ GameLib.D3.Mesh.prototype.updateInstance = function(property) { console.warn('unknown mesh property update'); } + if (property === 'geometry') { + + if (GameLib.Utils.Defined(this.geometry)) { + + if (GameLib.Utils.UndefinedOrNull(this.geometry.instance)) { + console.warn('the geometry is not linked or constructed properly'); + return; + } + + if (GameLib.Utils.UndefinedOrNull(this.instance)) { + this.createInstance(); + } else { + this.instance.geometry = this.geometry.instance; + } + + } + + return; + } + + if (property === 'materials') { + + var materialInstances = GameLib.Utils.GetArrayInstances(this.materials); + + if (materialInstances) { + + if (GameLib.Utils.UndefinedOrNull(this.instance)) { + this.createInstance(); + } else { + this.instance.material = materialInstances; + } + + console.warn('materials not ready for this mesh ' + this.name); + return; + + } else { + + if (GameLib.Utils.UndefinedOrNull(this.instance)) { + /** + * Do nothing + */ + } else { + + console.warn('warning - assigning no material to this mesh instance'); + /** + * Assign no material to this mesh - ok - I guess you know what you are doing. + * @type {null} + */ + this.instance.material = null; + } + + } + + return; + } + + if (GameLib.Utils.UndefinedOrNull(this.instance)) { + console.warn('mesh instance not ready - needs a material and geometry'); + return; + } + + if (property === 'name') { + this.instance.name = this.name; + return; + } + if (property === 'excludeFromEnvironment') { GameLib.Event.Emit( GameLib.Event.EXCLUDE_FROM_ENVIRONMENT, @@ -310,23 +331,17 @@ GameLib.D3.Mesh.prototype.updateInstance = function(property) { ) } - if (property === 'isBufferMesh') { - if (( this.isBufferMesh && !(this.instance.geometry instanceof THREE.BufferGeometry)) || - ( !this.isBufferMesh && (this.instance.geometry instanceof THREE.BufferGeometry))) - { - /** - * The buffer geometry needs updating - */ - this.instance.geometry = this.createInstanceGeometry(this.instance.geometry); - } - } - if ( property === 'rotation' || property === 'quaternion' || property === 'useQuaternion' ) { this.updateInstanceRotation(); + if (this.helper) { + this.removeHelper(); + this.createHelper(); + } + return; } if (property === 'parentMesh') { @@ -335,14 +350,29 @@ GameLib.D3.Mesh.prototype.updateInstance = function(property) { this.instance.parent = this.parentMesh.instance; } } + if (this.helper) { + this.removeHelper(); + this.createHelper(); + } + return; } if (property === 'scale') { this.updateInstanceScale(); + if (this.helper) { + this.removeHelper(); + this.createHelper(); + } + return; } if (property === 'position') { this.updateInstancePosition(); + if (this.helper) { + this.removeHelper(); + this.createHelper(); + } + return; } if (property === 'up') { @@ -352,30 +382,6 @@ GameLib.D3.Mesh.prototype.updateInstance = function(property) { return; } - if (property === 'name') { - this.instance.name = this.name; - return; - } - - if (property === 'materials') { - if (this.materials.length === 1 && this.materials[0].instance) { - this.instance.material = this.materials[0].instance; - } else { - - var materialInstances = this.materials.map( - function(material) { - - var materialInstance = material.instance; - - return materialInstance; - } - ); - - this.instance.material = materialInstances; - } - return; - } - if (property === 'renderOrder') { this.instance.renderOrder = this.renderOrder; return; @@ -396,10 +402,10 @@ GameLib.D3.Mesh.prototype.updateInstance = function(property) { return; } - if (this.helper) { - this.removeHelper(); - this.createHelper(); - } + if (property === 'drawMode') { + this.instance.drawMode = this.drawMode; + return; + } /** * 'parentScene' is handled by LinkingSystem @@ -407,6 +413,28 @@ GameLib.D3.Mesh.prototype.updateInstance = function(property) { GameLib.Component.prototype.updateInstance.call(this, property); }; + +GameLib.D3.Mesh.prototype.lookAt = function(vector) { + + this.instance.lookAt( + new THREE.Vector3( + vector.x, + vector.y, + vector.z + ) + ); + + this.rotation.x = this.instance.rotation.x; + this.rotation.y = this.instance.rotation.y; + this.rotation.z = this.instance.rotation.z; + + this.quaternion.x = this.instance.quaternion.x; + this.quaternion.y = this.instance.quaternion.y; + this.quaternion.z = this.instance.quaternion.z; + this.quaternion.w = this.instance.quaternion.w; + +}; + GameLib.D3.Mesh.prototype.invertWindingOrder = function() { this.faces.forEach( @@ -431,216 +459,6 @@ GameLib.D3.Mesh.prototype.invertWindingOrder = function() { }; -/** - * Apply geometry data to our GameLib object from the geometry instance (reverse of applyVertexData) - * @param geometryInstance - */ -GameLib.D3.Mesh.prototype.updateVerticesFromGeometryInstance = function(geometryInstance) { - - /** - * Setup vertices - */ - this.vertices = []; - - this.faces = []; - - if (geometryInstance instanceof THREE.BufferGeometry) { - - var vertices = geometryInstance.getAttribute('position').array; - - var uvs = geometryInstance.getAttribute('uv').array; - - geometryInstance.groups.map(function(group){ - - var materialIndex = group.materialIndex; - - var start = group.start; - - var count = group.count; - - var faceIndexes = []; - - var indexedUvs = []; - - for (var i = start; i < count; i ++) { - - var vertex = new GameLib.D3.Vertex( - this.graphics, - new GameLib.D3.API.Vertex( - new GameLib.Vector3( - this.graphics, - new GameLib.API.Vector3( - vertices[i*3], - vertices[i*3 + 1], - vertices[i*3 + 2] - ) - ) - ) - ); - - var uv = new GameLib.Vector2( - this.graphics, - new GameLib.API.Vector2( - uvs[i*2], - uvs[i*2 + 1] - ) - ); - - indexedUvs.push(uv); - - var vertexIndex = this.vertices.reduce( - function(result, indexedVertex, currentIndex){ - if (indexedVertex.position.equals(vertex.position)) { - result = currentIndex; - } - return result; - }, - -1 - ); - - var faceIndex = vertexIndex; - - if (vertexIndex === -1) { - this.vertices.push(vertex); - faceIndex = this.vertices.length - 1; - } - - faceIndexes.push(faceIndex); - - if (faceIndexes.length === 3) { - - this.faces.push( - new GameLib.D3.Face( - this.graphics, - new GameLib.D3.API.Face( - null, - null, - faceIndexes[0], - faceIndexes[1], - faceIndexes[2], - materialIndex, - [[indexedUvs[0], indexedUvs[1], indexedUvs[2]]] - ) - ) - ); - - indexedUvs = []; - faceIndexes = []; - } - } - - }.bind(this)); - - - - console.log('todo : update vertices from buffer geometry'); - } else { - - var processed = 0; - - geometryInstance.faces.map(function(face, faceIndex){ - - processed++; - - if (processed % 100 === 0) { - console.log('processed ' + processed + ' faces'); - } - - this.faces.push( - new GameLib.D3.Face( - this.graphics, - new GameLib.D3.API.Face( - null, - null, - face.a, - face.b, - face.c, - face.materialIndex, - [[ - new GameLib.API.Vector2( - geometryInstance.faceVertexUvs[0][faceIndex][0].x, - geometryInstance.faceVertexUvs[0][faceIndex][0].y - ), - new GameLib.API.Vector2( - geometryInstance.faceVertexUvs[0][faceIndex][1].x, - geometryInstance.faceVertexUvs[0][faceIndex][1].y - ), - new GameLib.API.Vector2( - geometryInstance.faceVertexUvs[0][faceIndex][2].x, - geometryInstance.faceVertexUvs[0][faceIndex][2].y - ) - ]], - new GameLib.Color( - this.graphics, - new GameLib.API.Color( - face.color.r, - face.color.g, - face.color.b - ) - ), - face.vertexColors.map(function(vertexColor){ - return new GameLib.Color( - this.graphics, - new GameLib.API.Color( - vertexColor.r, - vertexColor.g, - vertexColor.b - ) - ) - }.bind(this)), - face.vertexNormals.map(function(vertexNormal){ - return new GameLib.Vector3( - this.graphics, - new GameLib.API.Vector3( - vertexNormal.x, - vertexNormal.y, - vertexNormal.z - ) - ) - }.bind(this)), - new GameLib.Vector3( - this.graphics, - new GameLib.API.Vector3( - face.normal.x, - face.normal.y, - face.normal.z - ) - ) - ) - ) - ) - }.bind(this)); - - processed = 0; - - geometryInstance.vertices.map(function(vertex){ - - processed++; - - if (processed % 100 === 0) { - console.log('processed ' + processed + ' vertices'); - } - - this.vertices.push( - new GameLib.D3.Vertex( - this.graphics, - new GameLib.D3.API.Vertex( - new GameLib.Vector3( - this.graphics, - new GameLib.API.Vector3( - vertex.x, - vertex.y, - vertex.z - ) - ) - ) - ) - ) - }.bind(this)); - } - -}; - GameLib.D3.Mesh.prototype.applyBones = function(geometry) { /** @@ -812,40 +630,30 @@ GameLib.D3.Mesh.prototype.toApiObject = function() { this.id, this.name, this.meshType, - this.excludeFromEnvironment, - this.vertices.map( - function (vertex) { - return vertex.toApiObject(); - } - ), - this.faces.map( - function(face){ - return face.toApiObject(); - } - ), + GameLib.Utils.IdOrNull(this.parentEntity), + GameLib.Utils.IdOrNull(this.geometry), this.materials.map( function(material) { - return GameLib.Utils.IdOrNull(material); + return GameLib.Utils.IdOrNull(material) } ), GameLib.Utils.IdOrNull(this.parentMesh), GameLib.Utils.IdOrNull(this.parentScene), + this.excludeFromEnvironment, GameLib.Utils.IdOrNull(this.skeleton), - this.skinIndices, - this.skinWeights, this.position.toApiObject(), this.quaternion.toApiObject(), this.rotation.toApiObject(), this.scale.toApiObject(), this.up.toApiObject(), - this.modelMatrix.toApiObject(), this.renderOrder, - this.isBufferMesh, this.useQuaternion, this.visible, this.castShadow, this.receiveShadow, - GameLib.Utils.IdOrNull(this.parentEntity) + this.drawMode +// this.morphTargetInfluences, +// this.morphTargetDictionary ); return apiMesh; @@ -1101,6 +909,19 @@ GameLib.D3.Mesh.prototype.getChildrenComponents = function() { var components = []; + this.buildIdToObject(); + + Object.keys(this.idToObject).map( + function(objectId) { + if (this.id !== objectId) { + components.push(this.idToObject[objectId]); + } + }.bind(this) + ); + + return components; + /* + this.materials.map( function (material) { @@ -1142,11 +963,11 @@ GameLib.D3.Mesh.prototype.getChildrenComponents = function() { }.bind(this) ); - +*/ /** * Push RigidBodies */ - var rigidBodies = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.RIGID_BODY); +/* var rigidBodies = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.RIGID_BODY); rigidBodies.map( function(rigidBody) { @@ -1156,11 +977,11 @@ GameLib.D3.Mesh.prototype.getChildrenComponents = function() { }.bind(this) ); - +*/ /** * Push Shapes */ - var shapes = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.SHAPE); +/* var shapes = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.SHAPE); shapes.map( function(shape) { @@ -1170,8 +991,8 @@ GameLib.D3.Mesh.prototype.getChildrenComponents = function() { }.bind(this) ); - - return components; +*/ +// return components; }; /** diff --git a/src/game-lib-d3-raycaster.js b/src/game-lib-d3-raycaster.js index 9637021..5c44b88 100644 --- a/src/game-lib-d3-raycaster.js +++ b/src/game-lib-d3-raycaster.js @@ -181,7 +181,7 @@ GameLib.D3.Raycaster.prototype.getIntersectedObjects = function(meshes) { { mesh: mesh, distance : intersects[0].distance, - face: mesh.faces[intersects[0].faceIndex], + face: mesh.geometry.faces[intersects[0].faceIndex], faceIndex: intersects[0].faceIndex } ); diff --git a/src/game-lib-system-gui.js b/src/game-lib-system-gui.js index 56fb68e..2072432 100644 --- a/src/game-lib-system-gui.js +++ b/src/game-lib-system-gui.js @@ -988,6 +988,18 @@ GameLib.System.GUI.prototype.buildControl = function(folder, componentTemplate, } ) ); + } else if (property === 'drawMode') { + controllers.push( + folder.add( + object, + property, + { + 'triangles' : GameLib.D3.API.Mesh.DRAW_MODE_TRIANGLES, + 'triangle strip' : GameLib.D3.API.Mesh.DRAW_MODE_TRIANGLE_STRIP, + 'triangle fan' : GameLib.D3.API.Mesh.DRAW_MODE_TRIANGLE_FAN + } + ) + ); } else if (property === 'shadowType') { controllers.push( folder.add(