r3-legacy/src/game-lib-d3-mesh-plane.js

206 lines
5.0 KiB
JavaScript
Raw Normal View History

2017-06-23 14:31:41 +02:00
/**
* 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(update) {
var geometry = new THREE.PlaneGeometry(
this.width,
this.height,
this.widthSegments,
this.heightSegments
);
/**
* If this geometry is coming from the database, apply the vertex data
*/
if (this.vertices.length > 0) {
this.applyVertexDataToInstance(geometry);
} else {
this.updateVerticesFromGeometryInstance(geometry);
}
this.applyBones(geometry);
var instance = null;
if (this.skeleton) {
instance = new THREE.SkinnedMesh(geometry)
} else {
instance = new THREE.Mesh(geometry);
}
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
) {
var geometry = new THREE.PlaneGeometry(
this.width,
this.height,
this.widthSegments,
this.heightSegments
);
this.instance.geometry = geometry;
this.updateVerticesFromGeometryInstance(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;
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();
for ( var i = 0; i < this.instance.geometry.vertices.length; i++ ) {
this.instance.geometry.vertices[i].z = data[i];
}
this.instance.geometry.verticesNeedUpdate = true;
this.updateVerticesFromGeometryInstance(this.instance.geometry);
// this.updateInstance();
};