From 78b5732879baa80eea5beaa9a5967de72126d7c3 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Fri, 9 Feb 2018 21:18:03 +0100 Subject: [PATCH] instance for buffer g's --- src/game-lib-a-2-utils.js | 22 ++ src/game-lib-d3-api-buffer-geometry-0.js | 2 + src/game-lib-d3-api-face.js | 8 +- src/game-lib-d3-buffer-geometry-a.js | 354 +++++++++++++++++++++++ src/game-lib-d3-geometry-a.js | 21 +- 5 files changed, 385 insertions(+), 22 deletions(-) create mode 100644 src/game-lib-d3-buffer-geometry-a.js diff --git a/src/game-lib-a-2-utils.js b/src/game-lib-a-2-utils.js index 8b0b69d..8da529c 100644 --- a/src/game-lib-a-2-utils.js +++ b/src/game-lib-a-2-utils.js @@ -38,6 +38,28 @@ GameLib.Utils.BuildVectorSource = function(result, name, dimension) { console.warn('unknown dimension : ' + dimension); }; +GameLib.Utils.SortFacesByMaterialIndex = function(faces) { + + /** + * Sorts faces according to material index because later we will create + * groups for each vertice group + */ + faces.sort(function (a, b) { + + if (a.materialIndex < b.materialIndex) { + return -1; + } + + if (a.materialIndex > b.materialIndex) { + return 1; + } + + return 0; + }); + + return faces; +}; + GameLib.Utils.BuildQuaternionSource = function(result, name) { result[name] = {}; result[name].axis = {}; diff --git a/src/game-lib-d3-api-buffer-geometry-0.js b/src/game-lib-d3-api-buffer-geometry-0.js index 7f4220e..ecaa4f5 100644 --- a/src/game-lib-d3-api-buffer-geometry-0.js +++ b/src/game-lib-d3-api-buffer-geometry-0.js @@ -6,6 +6,7 @@ * @param parentEntity * @param faces * @param vertices + * @param colors * @param attributes * @param boundingBox * @param boundingSphere @@ -22,6 +23,7 @@ GameLib.D3.API.BufferGeometry = function( parentEntity, faces, vertices, + colors, attributes, boundingBox, boundingSphere, diff --git a/src/game-lib-d3-api-face.js b/src/game-lib-d3-api-face.js index 6fd2a9d..66fa843 100644 --- a/src/game-lib-d3-api-face.js +++ b/src/game-lib-d3-api-face.js @@ -69,12 +69,16 @@ GameLib.D3.API.Face = function( this.uvs = uvs; if (GameLib.Utils.UndefinedOrNull(color)) { - color = null; + color = new GameLib.API.Color(); } this.color = color; if (GameLib.Utils.UndefinedOrNull(vertexColors)) { - vertexColors = []; + vertexColors = [ + new GameLib.API.Color(), + new GameLib.API.Color(), + new GameLib.API.Color() + ]; } this.vertexColors = vertexColors; diff --git a/src/game-lib-d3-buffer-geometry-a.js b/src/game-lib-d3-buffer-geometry-a.js new file mode 100644 index 0000000..cd71883 --- /dev/null +++ b/src/game-lib-d3-buffer-geometry-a.js @@ -0,0 +1,354 @@ +/** + * GameLib.D3.BufferGeometry + * @param graphics + * @param apiBufferGeometry + * @property bufferGeometryType + * @constructor + */ +GameLib.D3.BufferGeometry = function( + graphics, + apiBufferGeometry +) { + + this.graphics = graphics; + this.graphics.isNotThreeThrow(); + + if (GameLib.Utils.UndefinedOrNull(apiBufferGeometry)) { + apiBufferGeometry = { + bufferGeometryType : GameLib.D3.API.BufferGeometry.BUFFER_GEOMETRY_TYPE_NONE + }; + } + + GameLib.D3.API.BufferGeometry.call( + this, + apiBufferGeometry.id, + apiBufferGeometry.name, + apiBufferGeometry.bufferGeometryType, + apiBufferGeometry.parentEntity, + apiBufferGeometry.faces, + apiBufferGeometry.vertices, + apiBufferGeometry.colors, + apiBufferGeometry.attributes, + apiBufferGeometry.boundingBox, + apiBufferGeometry.boundingSphere, + apiBufferGeometry.drawRange, + apiBufferGeometry.groups, + apiBufferGeometry.index, + apiBufferGeometry.morphAttributes + ); + + this.colors = this.colors.map( + function(color) { + return new GameLib.Color( + this.graphics, + color + ) + }.bind(this) + ); + + this.faces = this.faces.map( + function(face) { + return new GameLib.D3.Face( + this.graphics, + face + ) + }.bind(this) + ); + + this.vertices = this.vertices.map( + function(vertex) { + return new GameLib.D3.Vertex( + this.graphics, + vertex + ) + }.bind(this) + ); + + var linkedObjects = {}; + + switch (this.bufferGeometryType) { + case GameLib.D3.API.BufferGeometry.BUFFER_GEOMETRY_TYPE_EXTRUDE : + case GameLib.D3.API.BufferGeometry.BUFFER_GEOMETRY_TYPE_SHAPE : + linkedObjects.shapes = [GameLib.Curve.Path.D2.Shape]; + break; + case GameLib.D3.API.BufferGeometry.BUFFER_GEOMETRY_TYPE_TEXT : + linkedObjects.font = GameLib.D3.Font; + break; + default : + break; + } + + GameLib.Component.call( + this, + linkedObjects + ); + +}; + +GameLib.D3.BufferGeometry.prototype = Object.create(GameLib.Component.prototype); +GameLib.D3.BufferGeometry.prototype.constructor = GameLib.D3.BufferGeometry; + +GameLib.D3.BufferGeometry.prototype.createInstance = function() { + + /** + * Then we convert to the new BufferGeometry type + */ + this.instance = new THREE.BufferGeometry(); + + /** + * 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( + face.vertexColors[0].r, + face.vertexColors[0].g, + face.vertexColors[0].b, + face.vertexColors[1].r, + face.vertexColors[1].g, + face.vertexColors[1].b, + face.vertexColors[2].r, + face.vertexColors[2].g, + face.vertexColors[2].b + ); + return result; + }.bind(this), + [] + ) + ); + this.instance.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; + }, + [] + ) + ); + this.instance.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; + }, + [] + ) + ); + this.instance.addAttribute('normal', new THREE.BufferAttribute(normals, 3 )); + + this.instance.normalizeNormals(); + + //TODO: check below i don't do this - i used to but i don't think i should + //this.instance.computeVertexNormals(); + + /** + * Do faces setup + */ + this.faces = GameLib.Utils.SortFacesByMaterialIndex(this.faces); + + /** + * 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 + ); + +}; + +GameLib.D3.BufferGeometry.prototype.updateInstance = function(property) { + + if (GameLib.Utils.UndefinedOrNull(this.instance)) { + console.warn('no buffer geometry instance'); + return; + } + + if (property === 'name') { + this.instance.name = this.name; + return; + } + + if (property === 'faces') { + console.warn('todo: faces setup'); + return; + } + + if (property === 'vertices') { + console.warn('todo: vertices setup'); + return; + } + + if (property === 'colors') { + console.warn('todo: colors setup'); + return; + } + + if (property === 'attributes') { + console.warn('todo: attributes setup'); + return; + } + + if (property === 'boundingBox') { + console.warn('boundingBox is read only'); + return; + } + + if (property === 'boundingSphere') { + console.warn('boundingSphere is read only'); + return; + } + + if (property === 'drawRange') { + this.instance.setDrawRange( + this.drawRange.start, + this.drawRange.count + ); + return; + } + + if (property === 'groups') { + console.warn('todo: groups setup'); + return; + } + + if (property === 'index') { + console.warn('index is read only atm'); + return; + } + + if (property === 'morphAttributes') { + console.warn('morphAttributes is read only atm'); + return; + } + +}; + +/** + * Converts a GameLib.D3.BufferGeometry to a GameLib.D3.API.BufferGeometry + * @returns {GameLib.D3.API.BufferGeometry} + */ +GameLib.D3.BufferGeometry.prototype.toApiObject = function() { + + var apiBufferGeometry = new GameLib.D3.API.BufferGeometry( + this.id, + this.name, + this.bufferGeometryType, + GameLib.Utils.IdOrNull(this.parentEntity), + this.faces.map( + function(face) { + return face.toApiObject(); + } + ), + this.vertices.map( + function (vertex) { + return vertex.toApiObject(); + } + ), + this.colors.map( + function(color) { + return color.toApiObject(); + } + ), + null, //fuck attributes + this.boundingBox.toApiObject(), + this.boundingSphere.toApiObject(), + this.drawRange.toApiObject(), + this.groups.map( + function(group){ + return group.toApiObject() + } + ), + null, //fuck index + null //fuck morphAttributes + ); + + return apiBufferGeometry; + +}; diff --git a/src/game-lib-d3-geometry-a.js b/src/game-lib-d3-geometry-a.js index 7b1b198..b812f65 100644 --- a/src/game-lib-d3-geometry-a.js +++ b/src/game-lib-d3-geometry-a.js @@ -118,30 +118,11 @@ GameLib.D3.Geometry = function( GameLib.D3.Geometry.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.Geometry.prototype.constructor = GameLib.D3.Geometry; -/** - * Sorts faces according to material index because later we will create - * groups for each vertice group - */ -GameLib.D3.Geometry.prototype.sortFaces = function() { - this.faces.sort(function (a, b) { - - if (a.materialIndex < b.materialIndex) { - return -1; - } - - if (a.materialIndex > b.materialIndex) { - return 1; - } - - return 0; - }); -}; - GameLib.D3.Geometry.prototype.applyToInstance = function(property) { if (property === 'faces') { - this.sortFaces(); + this.faces = GameLib.Utils.SortFacesByMaterialIndex(this.faces); var standardUvs = [];