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

289 lines
7.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
2017-09-05 05:22:52 +02:00
* @param heightMapScale
* @param isHeightMap
2017-06-23 14:31:41 +02:00
* @constructor
*/
GameLib.D3.Mesh.Plane = function (
graphics,
apiMesh,
width,
height,
widthSegments,
2017-08-29 21:58:11 +02:00
heightSegments,
heightMapScale,
isHeightMap
2017-06-23 14:31:41 +02:00
) {
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;
2017-08-29 21:58:11 +02:00
if (GameLib.Utils.UndefinedOrNull(heightMapScale)) {
heightMapScale = 1;
}
this.heightMapScale = heightMapScale;
if (GameLib.Utils.UndefinedOrNull(isHeightMap)) {
isHeightMap = false;
}
this.isHeightMap = isHeightMap;
GameLib.D3.Mesh.call(
2017-06-23 14:31:41 +02:00
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;
2017-06-28 17:09:06 +02:00
GameLib.D3.Mesh.Plane.prototype.createInstance = function() {
2017-06-23 14:31:41 +02:00
var geometry = null;
2017-06-23 14:31:41 +02:00
/**
2017-08-29 21:58:11 +02:00
* If this geometry is not coming from the database, apply the vertex data from the instance to our runtime object
2017-06-23 14:31:41 +02:00
*/
2017-08-29 21:58:11 +02:00
if (this.vertices.length === 0) {
geometry = new THREE.PlaneGeometry(
this.width,
this.height,
this.widthSegments,
this.heightSegments
);
2017-08-29 21:58:11 +02:00
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
)
)
2017-09-05 05:22:52 +02:00
);
2017-06-23 14:31:41 +02:00
2017-09-05 05:22:52 +02:00
/**
* If this is a heightmap - first generate the z-coordinates
*/
if (this.isHeightMap) {
this.generateHeightMapFromBumpMap();
}
}
2017-08-29 21:58:11 +02:00
/**
* 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;
2017-06-23 14:31:41 +02:00
instance.userData.height = this.height;
instance.userData.widthSegments = this.widthSegments;
instance.userData.heightSegments = this.heightSegments;
2017-08-29 21:58:11 +02:00
instance.userData.heightMapScale = this.heightMapScale;
instance.userData.isHeightMap = this.isHeightMap;
2017-06-23 14:31:41 +02:00
return instance;
};
2017-08-29 21:58:11 +02:00
/**
*
*/
2017-06-23 14:31:41 +02:00
GameLib.D3.Mesh.Plane.prototype.updateInstance = function() {
2017-08-29 21:58:11 +02:00
var geometry = null;
2017-06-23 14:31:41 +02:00
if (
this.instance.userData.width !== this.width ||
this.instance.userData.height !== this.height ||
this.instance.userData.widthSegments !== this.widthSegments ||
2017-08-29 21:58:11 +02:00
this.instance.userData.heightSegments !== this.heightSegments ||
this.instance.userData.isHeightMap !== this.isHeightMap ||
this.instance.userData.heightMapScale !== this.heightMapScale
2017-06-23 14:31:41 +02:00
) {
this.instance.userData.width = this.width;
this.instance.userData.height = this.height;
this.instance.userData.widthSegments = this.widthSegments;
this.instance.userData.heightSegments = this.heightSegments;
2017-08-29 21:58:11 +02:00
this.instance.userData.isHeightMap = this.isHeightMap;
this.instance.userData.heightMapScale = this.heightMapScale;
2017-08-29 21:58:11 +02:00
geometry = new THREE.PlaneGeometry(
2017-06-23 14:31:41 +02:00
this.width,
this.height,
this.widthSegments,
this.heightSegments
);
this.updateVerticesFromGeometryInstance(geometry);
2017-08-24 22:20:40 +02:00
2017-08-29 21:58:11 +02:00
if (this.isHeightMap) {
this.generateHeightMapFromBumpMap();
2017-08-24 22:20:40 +02:00
}
2017-08-29 21:58:11 +02:00
geometry = this.createInstanceGeometry();
this.instance.geometry = geometry;
}
2017-06-23 14:31:41 +02:00
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;
2017-08-29 21:58:11 +02:00
apiMesh.heightMapScale = this.heightMapScale;
apiMesh.isHeightMap = this.isHeightMap;
2017-06-23 14:31:41 +02:00
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,
2017-08-29 21:58:11 +02:00
objectMesh.heightSegments,
objectMesh.heightMapScale,
objectMesh.isHeightMap
2017-06-23 14:31:41 +02:00
);
};
2017-08-29 21:58:11 +02:00
GameLib.D3.Mesh.Plane.prototype.getHeightData = function() {
2017-06-23 14:31:41 +02:00
2017-08-29 21:58:11 +02:00
var i;
2017-06-23 14:31:41 +02:00
2017-08-29 21:58:11 +02:00
var img = this.materials.reduce(
function(result, material) {
if (
material.bumpMap &&
material.bumpMap.image &&
material.bumpMap.image.instance
) {
result = material.bumpMap.image.instance;
}
2017-06-23 14:31:41 +02:00
2017-08-29 21:58:11 +02:00
return result;
},
null
);
2017-06-23 14:31:41 +02:00
2017-08-29 21:58:11 +02:00
if (!img) {
throw new Error('bumpmap image not found');
}
2017-06-23 14:31:41 +02:00
2017-08-29 21:58:11 +02:00
var canvas = document.createElement( 'canvas' );
2017-09-01 17:05:29 +02:00
canvas.width = this.widthSegments + 1;//img.width;
canvas.height = this.heightSegments + 1;//img.height;
2017-08-29 21:58:11 +02:00
var context = canvas.getContext( '2d' );
2017-06-23 14:31:41 +02:00
2017-09-01 17:05:29 +02:00
var size = (this.widthSegments + 1) * (this.heightSegments + 1);
2017-08-29 21:58:11 +02:00
var data = new Float32Array( size );
2017-06-23 14:31:41 +02:00
2017-09-01 17:05:29 +02:00
context.drawImage(img,0,0, canvas.width, canvas.height);
2017-06-23 14:31:41 +02:00
2017-08-29 21:58:11 +02:00
for (i = 0; i < size; i ++ ) {
data[i] = 0
}
2017-09-01 17:05:29 +02:00
var imgd = context.getImageData(0, 0, (this.widthSegments + 1), (this.heightSegments + 1));
2017-08-29 21:58:11 +02:00
var pix = imgd.data;
2017-06-23 14:31:41 +02:00
2017-08-29 21:58:11 +02:00
var j=0;
for (i = 0; i<pix.length; i +=4) {
var all = pix[i]+pix[i+1]+pix[i+2];
data[j++] = all/(12*this.heightMapScale);
}
2017-09-01 17:05:29 +02:00
// data = GameLib.Utils.InterpolateArray(data, (this.widthSegments + 1) * (this.heightSegments + 1));
2017-06-23 14:31:41 +02:00
2017-09-01 17:05:29 +02:00
return data;
2017-06-23 14:31:41 +02:00
};
/**
*
* @returns {THREE.PlaneGeometry}
*/
GameLib.D3.Mesh.Plane.prototype.generateHeightMapFromBumpMap = function() {
2017-06-23 14:31:41 +02:00
var data = this.getHeightData();
this.vertices.map(
function(vertex, index) {
vertex.position.z = data[index];
}
);
2017-08-29 21:58:11 +02:00
// var geometry = this.createInstanceGeometry();
//
// this.instance.geometry = geometry;
2017-06-23 14:31:41 +02:00
//
// 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;
2017-06-23 14:31:41 +02:00
//this.updateVerticesFromGeometryInstance(this.instance.geometry);
2017-06-23 14:31:41 +02:00
// this.updateInstance();
};