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

219 lines
4.4 KiB
JavaScript

/**
* Face
* @param id
* @param name
* @param v0index
* @param v1index
* @param v2index
* @param materialIndex
* @param uvs [[v0uv (GameLib.Vector2), v1uv(GameLib.Vector2), v2uv(GameLib.Vector2)]]
* @param color
* @param vertexColors
* @param vertexNormals
* @param normal
* @constructor
*/
GameLib.D3.API.Face = function(
id,
name,
v0index,
v1index,
v2index,
materialIndex,
uvs,
color,
vertexColors,
vertexNormals,
normal
) {
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'Face ' + id;
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(v0index)) {
v0index = -1;
}
this.v0index = v0index;
if (GameLib.Utils.UndefinedOrNull(v1index)) {
v1index = -1;
}
this.v1index = v1index;
if (GameLib.Utils.UndefinedOrNull(v2index)) {
v2index = -1;
}
this.v2index = v2index;
if (GameLib.Utils.UndefinedOrNull(materialIndex)) {
materialIndex = -1;
}
this.materialIndex = materialIndex;
if (GameLib.Utils.UndefinedOrNull(uvs)) {
uvs = [[]];
}
this.uvs = uvs;
if (GameLib.Utils.UndefinedOrNull(color)) {
color = null;
}
this.color = color;
if (GameLib.Utils.UndefinedOrNull(vertexColors)) {
vertexColors = [];
}
this.vertexColors = vertexColors;
if (GameLib.Utils.UndefinedOrNull(vertexNormals)) {
vertexNormals = [];
}
this.vertexNormals = vertexNormals;
if (GameLib.Utils.UndefinedOrNull(normal)) {
normal = null;
}
this.normal = normal;
};
/**
* We don't inherit from component - it makes the entitymanager too heavy - all faces end up in the register etc..
*/
// GameLib.D3.API.Face.prototype = Object.create(GameLib.Component.prototype);
// GameLib.D3.API.Face.prototype.constructor = GameLib.D3.API.Face;
/**
* Returns an API Face from a data object
* @constructor
* @param objectFace
*/
GameLib.D3.API.Face.FromObject = function(objectFace) {
var apiUvs = objectFace.uvs.reduce(
function(result, uvArray, index) {
result[index] = uvArray.reduce(
function(uvResult, uv) {
uvResult.push(GameLib.API.Vector2.FromObject(uv));
return uvResult;
},
[]
);
return result;
},
[]
);
var apiVertexColors = objectFace.vertexColors.map(
function(vertexColor) {
return GameLib.API.Color.FromObject(vertexColor);
}
);
var apiColor = null;
if (objectFace.color) {
apiColor = GameLib.API.Color.FromObject(objectFace.color);
}
var apiVertexNormals = objectFace.vertexNormals.map(
function(vertexNormal) {
return GameLib.API.Vector3.FromObject(vertexNormal);
}
);
var apiNormal = null;
if (objectFace.normal) {
apiNormal = GameLib.API.Vector3.FromObject(objectFace.normal);
}
return new GameLib.D3.API.Face(
objectFace.id,
objectFace.name,
objectFace.v0index,
objectFace.v1index,
objectFace.v2index,
objectFace.materialIndex,
apiUvs,
apiColor,
apiVertexColors,
apiVertexNormals,
apiNormal
);
};
/**
* Clone a Face
* @returns {GameLib.D3.API.Face}
*/
GameLib.D3.API.Face.prototype.clone = function(){
return new GameLib.D3.API.Face(
this.id,
this.name,
this.v0index,
this.v1index,
this.v2index,
this.materialIndex,
this.uvs,
this.color,
this.vertexColors,
this.vertexNormals,
this.normal
);
};
/**
* Returns true if two triangles are equal (their vertex indices match in some order)
* @param triangle
* @returns {boolean}
*/
GameLib.D3.API.Face.prototype.equals = function(triangle) {
return (
(
(this.v0index === triangle.v0index) &&
(this.v1index === triangle.v1index) &&
(this.v2index === triangle.v2index)
)
||
(
(this.v0index === triangle.v0index) &&
(this.v1index === triangle.v2index) &&
(this.v2index === triangle.v1index)
)
||
(
(this.v0index === triangle.v1index) &&
(this.v1index === triangle.v0index) &&
(this.v2index === triangle.v2index)
)
||
(
(this.v0index === triangle.v1index) &&
(this.v1index === triangle.v2index) &&
(this.v2index === triangle.v0index)
)
||
(
(this.v0index === triangle.v2index) &&
(this.v1index === triangle.v0index) &&
(this.v2index === triangle.v1index)
)
||
(
(this.v0index === triangle.v2index) &&
(this.v1index === triangle.v1index) &&
(this.v2index === triangle.v0index)
)
);
};