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

276 lines
7.2 KiB
JavaScript

/**
* Raw Mesh API object - should always correspond with the Mesh Schema
* @param id
* @param meshType
* @param name
* @param vertices GameLib.D3.Vertex[]
* @param faces GameLib.D3.Face[]
* @param materials GameLib.D3.API.Material[]
* @param parentMesh
* @param parentScene
* @param skeleton
* @param skinIndices
* @param skinWeights
* @param position GameLib.API.Vector3
* @param quaternion GameLib.API.Quaternion
* @param scale GameLib.API.Vector3
* @param localPosition GameLib.API.Vector3
* @param localRotation GameLib.API.Vector3
* @param localScale GameLib.API.Vector3
* @param up
* @param modelMatrix GameLib.API.Matrix4
* @param parentEntity
* @param renderOrder
* @param isBufferMesh
* @constructor
*/
GameLib.D3.API.Mesh = function(
id,
meshType,
name,
vertices,
faces,
materials,
parentMesh,
parentScene,
skeleton,
skinIndices,
skinWeights,
position,
quaternion,
scale,
localPosition,
localRotation,
localScale,
up,
modelMatrix,
parentEntity,
renderOrder,
isBufferMesh
) {
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(meshType)) {
meshType = GameLib.D3.Mesh.MESH_TYPE_NORMAL;
}
this.meshType = meshType;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'Mesh (' + id + ')';
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(vertices)) {
vertices = [];
}
this.vertices = vertices;
if (GameLib.Utils.UndefinedOrNull(faces)) {
faces = [];
}
this.faces = faces;
if (GameLib.Utils.UndefinedOrNull(parentMesh)) {
parentMesh = null;
}
this.parentMesh = parentMesh;
if (GameLib.Utils.UndefinedOrNull(parentScene)) {
parentScene = null;
}
this.parentScene = parentScene;
if (GameLib.Utils.UndefinedOrNull(skeleton)) {
skeleton = null;
}
this.skeleton = skeleton;
if (GameLib.Utils.UndefinedOrNull(skinIndices)) {
skinIndices = [];
}
this.skinIndices = skinIndices;
if (GameLib.Utils.UndefinedOrNull(skinWeights)) {
skinWeights = [];
}
this.skinWeights = skinWeights;
if (GameLib.Utils.UndefinedOrNull(materials)) {
materials = [new GameLib.D3.API.Material(null, GameLib.D3.Material.MATERIAL_TYPE_STANDARD, 'Material (' + this.name + ')')];
}
this.materials = materials;
if (GameLib.Utils.UndefinedOrNull(position)) {
position = new GameLib.API.Vector3(0,0,0);
}
this.position = position;
if (GameLib.Utils.UndefinedOrNull(quaternion)) {
quaternion = new GameLib.API.Quaternion();
}
this.quaternion = quaternion;
if (GameLib.Utils.UndefinedOrNull(scale)) {
scale = new GameLib.API.Vector3(1,1,1);
}
this.scale = scale;
if (GameLib.Utils.UndefinedOrNull(localPosition)) {
localPosition = new GameLib.API.Vector3(0,0,0);
}
this.localPosition = localPosition;
if (GameLib.Utils.UndefinedOrNull(localRotation)) {
localRotation = new GameLib.API.Vector3(0,0,0);
}
this.localRotation = localRotation;
if (GameLib.Utils.UndefinedOrNull(localScale)) {
localScale = new GameLib.API.Vector3(1,1,1);
}
this.localScale = localScale;
if (GameLib.Utils.UndefinedOrNull(up)) {
up = new GameLib.API.Vector3(0,1,0);
}
this.up = up;
if (GameLib.Utils.UndefinedOrNull(modelMatrix)) {
modelMatrix = new GameLib.API.Matrix4();
}
this.modelMatrix = modelMatrix;
if (GameLib.Utils.UndefinedOrNull(renderOrder)) {
renderOrder = 0;
}
this.renderOrder = renderOrder;
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
}
this.parentEntity = parentEntity;
if (GameLib.Utils.UndefinedOrNull(isBufferMesh)) {
isBufferMesh = false;
}
this.isBufferMesh = isBufferMesh;
};
GameLib.D3.API.Mesh.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.Mesh.prototype.constructor = GameLib.D3.API.Mesh;
/**
* Returns an API Mesh from an Object mesh
* @param objectMesh
* @constructor
*/
GameLib.D3.API.Mesh.FromObject = function (objectMesh){
var apiFaces = [];
if (objectMesh.faces) {
apiFaces = objectMesh.faces.map(
function(face) {
return GameLib.D3.API.Face.FromObject(face);
}
);
}
var apiSkeleton = null;
if (objectMesh.skeleton) {
apiSkeleton = GameLib.D3.API.Skeleton.FromObject(objectMesh.skeleton);
}
var apiMaterials = [];
if (objectMesh.materials) {
apiMaterials = objectMesh.materials.map(
function (objectMaterial) {
/**
* From blender we only get Ids to materials (strings)
*/
if (objectMaterial instanceof Object) {
return GameLib.D3.API.Material.FromObject(objectMaterial);
} else {
return objectMaterial
}
}
)
}
var apiVertices = [];
if (objectMesh.vertices) {
apiVertices = objectMesh.vertices.map(
function (objectVertex) {
return GameLib.D3.API.Vertex.FromObject(objectVertex);
}
)
}
var apiPosition = new GameLib.API.Vector3();
if (objectMesh.position) {
apiPosition = GameLib.API.Vector3.FromObject(objectMesh.position);
}
var apiQuaternion = new GameLib.API.Quaternion();
if (objectMesh.quaternion) {
apiQuaternion = GameLib.API.Quaternion.FromObject(objectMesh.quaternion);
}
var apiScale = new GameLib.API.Vector3(1,1,1);
if (objectMesh.scale) {
apiScale = GameLib.API.Vector3.FromObject(objectMesh.scale);
}
var apiLocalPosition = new GameLib.API.Vector3();
if (objectMesh.localPosition) {
apiLocalPosition = GameLib.API.Vector3.FromObject(objectMesh.localPosition);
}
var apiLocalRotation = new GameLib.API.Vector3();
if (objectMesh.localRotation) {
apiLocalRotation = GameLib.API.Vector3.FromObject(objectMesh.localRotation);
}
var apiLocalScale = new GameLib.API.Vector3(1,1,1);
if (objectMesh.localScale) {
apiLocalScale = GameLib.API.Vector3.FromObject(objectMesh.localScale);
}
var apiUp = new GameLib.API.Vector3(0,1,0);
if (objectMesh.up) {
apiUp = GameLib.API.Vector3.FromObject(objectMesh.up);
}
var apiModelMatrix = new GameLib.API.Matrix4();
if (objectMesh.modelMatrix) {
apiModelMatrix = GameLib.API.Matrix4.FromObject(objectMesh.modelMatrix);
}
return new GameLib.D3.API.Mesh(
objectMesh.id,
objectMesh.meshType,
objectMesh.name,
apiVertices,
apiFaces,
apiMaterials,
objectMesh.parentMesh,
objectMesh.parentScene,
apiSkeleton,
objectMesh.skinIndices,
objectMesh.skinWeights,
apiPosition,
apiQuaternion,
apiScale,
apiLocalPosition,
apiLocalRotation,
apiLocalScale,
apiUp,
apiModelMatrix,
objectMesh.parentEntity,
objectMesh.renderOrder,
objectMesh.isBufferMesh
);
};