r3-legacy/bak/r3-d3-mesh-plane.js

344 lines
8.3 KiB
JavaScript
Raw Permalink 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
2018-04-09 10:05:13 +02:00
* @param graphics R3.GraphicsRuntime
2017-12-30 11:39:07 +01:00
* @param apiMeshPlane
2017-06-23 14:31:41 +02:00
* @constructor
*/
2018-04-09 10:05:13 +02:00
R3.D3.Mesh.Plane = function (
2017-06-23 14:31:41 +02:00
graphics,
2017-12-30 11:39:07 +01:00
apiMeshPlane
2017-06-23 14:31:41 +02:00
) {
2017-12-30 11:39:07 +01:00
2017-06-23 14:31:41 +02:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
2018-04-09 10:05:13 +02:00
if (R3.Utils.UndefinedOrNull(apiMeshPlane)) {
2017-12-30 11:39:07 +01:00
apiMeshPlane = {
2018-04-09 10:05:13 +02:00
meshType : R3.D3.API.Mesh.MESH_TYPE_PLANE
2017-12-04 13:23:15 +01:00
};
}
2018-04-09 10:05:13 +02:00
R3.D3.API.Mesh.Plane.call(
2017-12-30 11:39:07 +01:00
this,
apiMeshPlane,
apiMeshPlane.width,
apiMeshPlane.height,
apiMeshPlane.widthSegments,
apiMeshPlane.heightSegments,
apiMeshPlane.heightMapScale,
apiMeshPlane.isHeightMap,
apiMeshPlane.isDotMap,
2018-01-15 22:17:03 +01:00
apiMeshPlane.dotMapScale,
apiMeshPlane.dotMapOffset,
apiMeshPlane.dotMapWeight,
2017-12-30 12:43:58 +01:00
apiMeshPlane.dotObject
2017-12-30 11:39:07 +01:00
);
2017-09-20 15:24:15 +02:00
2018-04-09 10:05:13 +02:00
this.dotMapScale = new R3.Vector3(
2018-01-15 22:17:03 +01:00
this.graphics,
this.dotMapScale,
this
);
2018-04-09 10:05:13 +02:00
this.dotMapOffset = new R3.Vector3(
2018-01-15 22:17:03 +01:00
this.graphics,
this.dotMapOffset,
this
);
2018-04-09 10:05:13 +02:00
this.dotMapWeight = new R3.Vector3(
2018-01-15 22:17:03 +01:00
this.graphics,
this.dotMapWeight,
this
);
2018-01-11 14:33:32 +01:00
this.dots = [];
2018-04-09 10:05:13 +02:00
R3.D3.Mesh.call(
2017-06-23 14:31:41 +02:00
this,
2017-12-30 11:39:07 +01:00
graphics,
apiMeshPlane
2017-06-23 14:31:41 +02:00
);
2017-12-30 11:39:07 +01:00
2017-06-23 14:31:41 +02:00
};
2018-04-09 10:05:13 +02:00
R3.D3.Mesh.Plane.prototype = Object.create(R3.D3.Mesh.prototype);
R3.D3.Mesh.Plane.prototype.constructor = R3.D3.Mesh.Plane;
2017-06-23 14:31:41 +02:00
2018-04-09 10:05:13 +02:00
R3.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);
2017-09-05 13:18:10 +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
*/
2018-04-09 10:05:13 +02:00
R3.D3.Mesh.prototype.createInstance.call(this);
2017-08-29 21:58:11 +02:00
2017-12-30 12:43:58 +01:00
if (this.isDotMap && this.dotObject) {
2018-01-11 14:33:32 +01:00
this.generateDotMap();
2017-09-20 15:24:15 +02:00
}
2017-06-23 14:31:41 +02:00
};
2017-08-29 21:58:11 +02:00
/**
*
*/
2018-04-09 10:05:13 +02:00
R3.D3.Mesh.Plane.prototype.updateInstance = function(property) {
2017-06-23 14:31:41 +02:00
2017-08-29 21:58:11 +02:00
var geometry = null;
2017-12-30 12:43:58 +01:00
if (
property === 'width' ||
property === 'height' ||
property === 'widthSegments' ||
property === 'heightSegments' ||
property === 'isHeightMap' ||
property === 'heightMapScale'
2017-06-23 14:31:41 +02:00
) {
2017-12-30 12:43:58 +01: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) {
2017-12-30 12:43:58 +01:00
this.generateHeightMapFromBumpMap();
}
2017-08-29 21:58:11 +02:00
2017-12-30 12:43:58 +01:00
geometry = this.createInstanceGeometry();
2017-08-29 21:58:11 +02:00
this.instance.geometry = geometry;
2017-12-30 12:43:58 +01:00
}
2017-09-20 15:24:15 +02:00
2017-12-30 12:43:58 +01:00
if (
property === 'isDotMap' ||
2018-01-15 22:17:03 +01:00
property === 'dotMapScale' ||
property === 'dotMapOffset' ||
property === 'dotMapWeight' ||
2017-12-30 12:43:58 +01:00
property === 'dotObject'
) {
2018-01-12 14:57:15 +01:00
this.dots.map(
function(dot){
this.parentScene.instance.remove(dot);
dot.geometry.dispose();
dot.material.dispose();
}.bind(this)
);
if (this.isDotMap) {
this.generateDotMap();
}
2017-09-20 15:24:15 +02:00
}
2017-06-23 14:31:41 +02:00
2018-04-09 10:05:13 +02:00
R3.D3.Mesh.prototype.updateInstance.call(this, property);
2017-06-23 14:31:41 +02:00
};
/**
2018-04-09 10:05:13 +02:00
* Converts a R3.D3.Mesh.Plane to a R3.D3.API.Mesh.Plane
* @returns {R3.D3.API.Mesh.Plane}
2017-06-23 14:31:41 +02:00
*/
2018-04-09 10:05:13 +02:00
R3.D3.Mesh.Plane.prototype.toApiObject = function() {
2017-06-23 14:31:41 +02:00
2018-04-09 10:05:13 +02:00
var apiMesh = R3.D3.Mesh.prototype.toApiObject.call(this);
2018-04-09 10:05:13 +02:00
var apiMeshPlane = new R3.D3.API.Mesh.Plane(
apiMesh,
2017-12-30 12:43:58 +01:00
this.width,
this.height,
this.widthSegments,
this.heightSegments,
this.heightMapScale,
this.isHeightMap,
this.isDotMap,
2018-01-15 22:17:03 +01:00
this.dotMapScale.toApiObject(),
this.dotMapOffset.toApiObject(),
this.dotMapWeight.toApiObject(),
2018-04-09 10:05:13 +02:00
R3.Utils.IdOrNull(this.dotObject)
2017-12-30 12:43:58 +01:00
);
2017-06-23 14:31:41 +02:00
2017-12-30 12:43:58 +01:00
return apiMeshPlane;
2017-06-23 14:31:41 +02:00
};
2017-06-23 14:31:41 +02:00
2018-04-09 10:05:13 +02:00
R3.D3.Mesh.Plane.prototype.generateDotMap = function() {
2018-01-11 14:33:32 +01:00
this.dots = [];
var data = this.getHeightData();
var width = Math.sqrt(data.length);
var height = width;
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++ ) {
2018-01-15 22:17:03 +01:00
var z = data[(y * width) + x];
if (z < 0.1) {
continue;
}
2018-01-12 14:57:15 +01:00
var geometry;
var material;
2018-01-12 14:57:15 +01:00
if (this.dotObject) {
geometry = this.dotObject.instance.geometry.clone();
material = this.dotObject.materials[0].instance;
} else {
geometry = new THREE.BoxBufferGeometry(0.5, 0.5, 0.5);
material = this.materials[0].instance;
}
2018-01-12 14:57:15 +01:00
var dot = new THREE.Mesh(geometry, material);
2018-01-15 20:16:47 +01:00
dot.name = 'dot ' + this.dots.length;
2018-01-15 22:17:03 +01:00
dot.position.x = x * this.dotMapWeight.x + this.dotMapOffset.x;
dot.position.y = height - (y * this.dotMapWeight.y + this.dotMapOffset.y);
dot.position.z = z * this.dotMapWeight.z + this.dotMapOffset.z;
2018-01-11 14:33:32 +01:00
2018-01-15 22:17:03 +01:00
dot.scale.x = z * this.dotMapScale.x + 0.00001;
dot.scale.y = z * this.dotMapScale.y + 0.00001;
dot.scale.z = z * this.dotMapScale.z + 0.00001;
2018-01-11 14:33:32 +01:00
this.parentScene.instance.add(dot);
this.dots.push(dot);
}
}
};
2017-06-23 14:31:41 +02:00
/**
*
* @returns {THREE.PlaneGeometry}
*/
2018-04-09 10:05:13 +02:00
R3.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();
};
2018-04-09 10:05:13 +02:00
R3.D3.Mesh.Plane.prototype.createPhysicsObjects = function() {
2018-04-09 10:05:13 +02:00
R3.Event.Emit(
R3.Event.GET_PHYSICS_RUNTIME,
null,
2017-10-27 15:31:51 +02:00
function(physics){
/**
* Create the plane shape
2018-04-09 10:05:13 +02:00
* @type {R3.D3.API.Shape}
*/
2018-04-09 10:05:13 +02:00
var apiShapePlane = new R3.D3.API.Shape(
null,
'Shape Plane (' + this.name + ')'
);
apiShapePlane.parentMesh = this;
2018-04-09 10:05:13 +02:00
var shapePlane = new R3.D3.Shape.Plane(
2017-10-27 15:31:51 +02:00
physics,
apiShapePlane
);
2018-04-09 10:05:13 +02:00
var apiRigidBody = new R3.D3.API.RigidBody(
null,
'Rigid Body (' + this.name + ')',
0,
null,
2018-04-09 10:05:13 +02:00
new R3.API.Vector3(
this.position.x,
this.position.y,
this.position.z
),
2018-04-09 10:05:13 +02:00
new R3.API.Quaternion(
this.quaternion.x,
this.quaternion.y,
this.quaternion.z,
this.quaternion.w,
2018-04-09 10:05:13 +02:00
new R3.API.Vector3(
this.quaternion.axis.x,
this.quaternion.axis.y,
this.quaternion.axis.z
),
this.quaternion.angle
)
);
apiRigidBody.shapes.push(shapePlane);
apiRigidBody.parentMesh = this;
/**
* Construct the rigid body
2018-04-09 10:05:13 +02:00
* @type {R3.D3.RigidBody}
*/
2018-04-09 10:05:13 +02:00
var rigidBody = new R3.D3.RigidBody(
2017-10-27 15:31:51 +02:00
physics,
apiRigidBody
);
2018-04-09 10:05:13 +02:00
if (this.parentEntity instanceof R3.Entity) {
this.parentEntity.addComponent(shapePlane);
this.parentEntity.addComponent(rigidBody);
}
}.bind(this),
function(error){
console.log(error.message);
throw new Error(error.message);
}
);
};