/** * Mesh Superset - The apiMesh properties get moved into the Mesh object itself, and then the instance is created * @param graphics GameLib.D3.Graphics * @param apiMesh GameLib.D3.API.Mesh * @param width * @param height * @param widthSegments * @param heightSegments * @constructor */ GameLib.D3.Mesh.Plane = function ( graphics, apiMesh, width, height, widthSegments, heightSegments, heightMapScale, isHeightMap ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); if (GameLib.Utils.UndefinedOrNull(width)) { width = 1; } this.width = width; if (GameLib.Utils.UndefinedOrNull(height)) { height = 1; } this.height = height; if (GameLib.Utils.UndefinedOrNull(widthSegments)) { widthSegments = 1; } this.widthSegments = widthSegments; if (GameLib.Utils.UndefinedOrNull(heightSegments)) { heightSegments = 1 } this.heightSegments = heightSegments; if (GameLib.Utils.UndefinedOrNull(heightMapScale)) { heightMapScale = 1; } this.heightMapScale = heightMapScale; if (GameLib.Utils.UndefinedOrNull(isHeightMap)) { isHeightMap = false; } this.isHeightMap = isHeightMap; GameLib.D3.Mesh.call( this, this.graphics, apiMesh ); }; GameLib.D3.Mesh.Plane.prototype = Object.create(GameLib.D3.Mesh.prototype); GameLib.D3.Mesh.Plane.prototype.constructor = GameLib.D3.Mesh.Plane; GameLib.D3.Mesh.Plane.prototype.createInstance = function() { var geometry = null; /** * If this geometry is not coming from the database, apply the vertex data from the instance to our runtime object */ if (this.vertices.length === 0) { geometry = new THREE.PlaneGeometry( this.width, this.height, this.widthSegments, this.heightSegments ); 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) { this.generateHeightMapFromBumpMap(); } /** * Now construct the mesh instance */ var instance = GameLib.D3.Mesh.prototype.createInstance.call(this); /** * Apply some plane specific data to the instance */ instance.userData.width = this.width; instance.userData.height = this.height; instance.userData.widthSegments = this.widthSegments; instance.userData.heightSegments = this.heightSegments; instance.userData.heightMapScale = this.heightMapScale; instance.userData.isHeightMap = this.isHeightMap; return instance; }; /** * */ GameLib.D3.Mesh.Plane.prototype.updateInstance = function() { var geometry = null; if ( this.instance.userData.width !== this.width || this.instance.userData.height !== this.height || this.instance.userData.widthSegments !== this.widthSegments || this.instance.userData.heightSegments !== this.heightSegments || this.instance.userData.isHeightMap !== this.isHeightMap || this.instance.userData.heightMapScale !== this.heightMapScale ) { this.instance.userData.width = this.width; this.instance.userData.height = this.height; this.instance.userData.widthSegments = this.widthSegments; this.instance.userData.heightSegments = this.heightSegments; this.instance.userData.isHeightMap = this.isHeightMap; this.instance.userData.heightMapScale = this.heightMapScale; geometry = new THREE.PlaneGeometry( this.width, this.height, this.widthSegments, this.heightSegments ); this.updateVerticesFromGeometryInstance(geometry); if (this.isHeightMap) { this.generateHeightMapFromBumpMap(); } geometry = this.createInstanceGeometry(); this.instance.geometry = geometry; } GameLib.D3.Mesh.prototype.updateInstance.call(this); }; /** * Converts a GameLib.D3.Mesh to a GameLib.D3.API.Mesh * @returns {GameLib.D3.API.Mesh} */ GameLib.D3.Mesh.Plane.prototype.toApiObject = function() { var apiMesh = GameLib.D3.Mesh.prototype.toApiObject.call(this); apiMesh.width = this.width; apiMesh.height = this.height; apiMesh.widthSegments = this.widthSegments; apiMesh.heightSegments = this.heightSegments; apiMesh.heightMapScale = this.heightMapScale; apiMesh.isHeightMap = this.isHeightMap; return apiMesh; }; /** * TODO fix all this weird loading shit * Converts a standard object mesh to a GameLib.D3.Mesh * @param graphics GameLib.D3.Graphics * @param objectMesh {Object} * @constructor */ GameLib.D3.Mesh.Plane.FromObject = function(graphics, objectMesh) { var apiMesh = GameLib.D3.API.Mesh.FromObject(objectMesh); return new GameLib.D3.Mesh.Plane( graphics, apiMesh, objectMesh.width, objectMesh.height, objectMesh.widthSegments, objectMesh.heightSegments, objectMesh.heightMapScale, objectMesh.isHeightMap ); }; GameLib.D3.Mesh.Plane.prototype.getHeightData = function() { var i; var img = this.materials.reduce( function(result, material) { if ( material.bumpMap && material.bumpMap.image && material.bumpMap.image.instance ) { result = material.bumpMap.image.instance; } return result; }, null ); if (!img) { throw new Error('bumpmap image not found'); } var canvas = document.createElement( 'canvas' ); canvas.width = img.width; canvas.height = img.height; var context = canvas.getContext( '2d' ); var size = img.width * img.height; var data = new Float32Array( size ); context.drawImage(img,0,0); for (i = 0; i < size; i ++ ) { data[i] = 0 } var imgd = context.getImageData(0, 0, img.width, img.height); var pix = imgd.data; var j=0; for (i = 0; i