237 lines
5.7 KiB
JavaScript
237 lines
5.7 KiB
JavaScript
/**
|
|
* 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
|
|
) {
|
|
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;
|
|
|
|
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 coming from the database, apply the vertex data
|
|
*/
|
|
if (this.vertices.length > 0) {
|
|
|
|
geometry = this.createInstanceGeometry();
|
|
|
|
} else {
|
|
|
|
geometry = new THREE.PlaneGeometry(
|
|
this.width,
|
|
this.height,
|
|
this.widthSegments,
|
|
this.heightSegments
|
|
);
|
|
|
|
this.updateVerticesFromGeometryInstance(geometry);
|
|
|
|
geometry = this.createInstanceGeometry();
|
|
}
|
|
|
|
var materialInstances = this.materials.reduce(
|
|
function(result, material) {
|
|
result.push(material.instance);
|
|
return result;
|
|
},
|
|
[]
|
|
);
|
|
|
|
var instance = new THREE.Mesh(geometry, materialInstances);
|
|
|
|
instance.userData.width = this.width;
|
|
instance.userData.height = this.height;
|
|
instance.userData.widthSegments = this.widthSegments;
|
|
instance.userData.heightSegments = this.heightSegments;
|
|
|
|
this.createInstanceDefaults(instance);
|
|
|
|
return instance;
|
|
};
|
|
|
|
GameLib.D3.Mesh.Plane.prototype.updateInstance = function() {
|
|
|
|
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.width = this.width;
|
|
this.instance.userData.height = this.height;
|
|
this.instance.userData.widthSegments = this.widthSegments;
|
|
this.instance.userData.heightSegments = this.heightSegments;
|
|
|
|
var geometry = new THREE.PlaneGeometry(
|
|
this.width,
|
|
this.height,
|
|
this.widthSegments,
|
|
this.heightSegments
|
|
);
|
|
|
|
this.updateVerticesFromGeometryInstance(geometry);
|
|
|
|
this.instance.geometry = this.createInstanceGeometry();
|
|
|
|
if (this.helper) {
|
|
this.removeHelper();
|
|
this.createHelper();
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
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
|
|
);
|
|
|
|
};
|
|
|
|
GameLib.D3.Mesh.Plane.prototype.getHeightData = function(img,scale) {
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(scale)) {
|
|
scale = 1;
|
|
}
|
|
|
|
if (GameLib.Utils.UndefinedOrNull(img)) {
|
|
img = this.instance.material.bumpMap.image;
|
|
}
|
|
|
|
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 ( var 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 (var i = 0; i<pix.length; i +=4) {
|
|
var all = pix[i]+pix[i+1]+pix[i+2];
|
|
data[j++] = all/(12*scale);
|
|
}
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
/**
|
|
*
|
|
* @returns {THREE.PlaneGeometry}
|
|
*/
|
|
GameLib.D3.Mesh.Plane.prototype.generateHeightMapFromBumpMap = function() {
|
|
|
|
var data = this.getHeightData();
|
|
|
|
this.vertices.map(
|
|
function(vertex, index) {
|
|
vertex.position.z = data[index];
|
|
}
|
|
);
|
|
|
|
var geometry = this.createInstanceGeometry();
|
|
|
|
this.instance.geometry = geometry;
|
|
|
|
//
|
|
// var vertices = this.instance.geometry.attributes.position.array;
|
|
//
|
|
// for ( var i = 0; i < vertices.length; i += 3 ) {
|
|
// vertices[i+2] = data[i];
|
|
// }
|
|
//
|
|
// this.instance.geometry.attributes.position.needsUpdate = true;
|
|
|
|
//this.updateVerticesFromGeometryInstance(this.instance.geometry);
|
|
|
|
// this.updateInstance();
|
|
};
|