280 lines
7.2 KiB
JavaScript
280 lines
7.2 KiB
JavaScript
|
/**
|
||
|
* R3.D3.Face
|
||
|
* @constructor
|
||
|
* @param implementation
|
||
|
* @param apiFace
|
||
|
*/
|
||
|
R3.D3.Face = function(
|
||
|
implementation,
|
||
|
apiFace
|
||
|
) {
|
||
|
|
||
|
this.implementation = implementation;
|
||
|
if (implementation instanceof R3.GraphicsRuntime) {
|
||
|
this.implementation.isNotThreeThrow();
|
||
|
} else if (implementation instanceof R3.PhysicsRuntime) {
|
||
|
this.implementation.isNotCannonThrow();
|
||
|
} else {
|
||
|
throw new Error('Unhandled implementation : ' + implementation);
|
||
|
}
|
||
|
|
||
|
if (R3.Utils.UndefinedOrNull(apiFace)) {
|
||
|
apiFace = {};
|
||
|
}
|
||
|
|
||
|
R3.D3.API.Face.call(
|
||
|
this,
|
||
|
apiFace.id,
|
||
|
apiFace.name,
|
||
|
apiFace.v0index,
|
||
|
apiFace.v1index,
|
||
|
apiFace.v2index,
|
||
|
apiFace.materialIndex,
|
||
|
apiFace.uvs,
|
||
|
apiFace.color,
|
||
|
apiFace.vertexColors,
|
||
|
apiFace.vertexNormals,
|
||
|
apiFace.normal,
|
||
|
apiFace.selected,
|
||
|
apiFace.parentGeometry,
|
||
|
apiFace.parentEntity
|
||
|
);
|
||
|
|
||
|
if (this.implementation instanceof R3.GraphicsRuntime) {
|
||
|
/**
|
||
|
* physics faces have no color... a little sad right?
|
||
|
*/
|
||
|
this.color = new R3.Color(
|
||
|
this.implementation,
|
||
|
this.color,
|
||
|
this
|
||
|
);
|
||
|
|
||
|
this.vertexColors = this.vertexColors.map(function(vertexColor){
|
||
|
|
||
|
return new R3.Color(
|
||
|
this.implementation,
|
||
|
vertexColor,
|
||
|
this
|
||
|
);
|
||
|
/*
|
||
|
if (vertexColor instanceof R3.Color) {
|
||
|
return vertexColor;
|
||
|
}
|
||
|
|
||
|
if (vertexColor instanceof R3.API.Color) {
|
||
|
|
||
|
}
|
||
|
|
||
|
console.warn('unknown vertex color type', vertexColor);*/
|
||
|
}.bind(this));
|
||
|
|
||
|
}
|
||
|
|
||
|
this.vertexNormals = this.vertexNormals.map(
|
||
|
function(vertexNormal) {
|
||
|
return new R3.Vector3(
|
||
|
this.implementation,
|
||
|
vertexNormal,
|
||
|
this
|
||
|
);
|
||
|
}.bind(this)
|
||
|
);
|
||
|
|
||
|
this.uvs = this.uvs.reduce(
|
||
|
|
||
|
function(result, uvs, uvSet) {
|
||
|
|
||
|
result[uvSet] = uvs.reduce(
|
||
|
function(uvResult, uv) {
|
||
|
|
||
|
uvResult.push(
|
||
|
new R3.Vector2(
|
||
|
this.implementation,
|
||
|
uv,
|
||
|
this
|
||
|
)
|
||
|
);
|
||
|
|
||
|
return uvResult;
|
||
|
}.bind(this),
|
||
|
[]
|
||
|
);
|
||
|
|
||
|
return result;
|
||
|
|
||
|
|
||
|
}.bind(this),
|
||
|
[]
|
||
|
);
|
||
|
|
||
|
this.normal = new R3.Vector3(
|
||
|
this.implementation,
|
||
|
this.normal,
|
||
|
this
|
||
|
);
|
||
|
|
||
|
};
|
||
|
|
||
|
R3.D3.Face.prototype = Object.create(R3.Component.prototype);
|
||
|
R3.D3.Face.prototype.constructor = R3.D3.Face;
|
||
|
|
||
|
/**
|
||
|
* We don't follow the standard procedure for Faces - We don't want them in the EntityManager registry - so
|
||
|
* they don't call component createinstance
|
||
|
* @param parentGeometry
|
||
|
*/
|
||
|
R3.D3.Face.prototype.createInstance = function(parentGeometry) {
|
||
|
|
||
|
this.instance = new THREE.Face3(
|
||
|
this.v0index,
|
||
|
this.v1index,
|
||
|
this.v2index
|
||
|
);
|
||
|
|
||
|
if (this.normal) {
|
||
|
this.instance.normal = new THREE.Vector3(
|
||
|
this.normal.x,
|
||
|
this.normal.y,
|
||
|
this.normal.z
|
||
|
);
|
||
|
}
|
||
|
|
||
|
if (this.color) {
|
||
|
this.instance.color = new THREE.Color(
|
||
|
this.color.r,
|
||
|
this.color.g,
|
||
|
this.color.b
|
||
|
)
|
||
|
}
|
||
|
|
||
|
this.instance.materialIndex = this.materialIndex;
|
||
|
|
||
|
if (R3.Utils.UndefinedOrNull(parentGeometry)) {
|
||
|
console.warn('please pass a parentmesh to face createInstance()');
|
||
|
}
|
||
|
this.parentGeometry = parentGeometry;
|
||
|
};
|
||
|
|
||
|
R3.D3.Face.prototype.updateInstance = function(property, uvSet, uvIndex) {
|
||
|
|
||
|
if (property === 'materialIndex') {
|
||
|
this.instance.materialIndex = this.materialIndex;
|
||
|
this.parentGeometry.instance.groupsNeedUpdate = true;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (property === 'uvs') {
|
||
|
|
||
|
this.uvs[uvSet][uvIndex].instance.x = this.uvs[uvSet][uvIndex].x;
|
||
|
this.uvs[uvSet][uvIndex].instance.y = this.uvs[uvSet][uvIndex].y;
|
||
|
this.parentGeometry.instance.uvsNeedUpdate = true;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
console.warn('todo: update face property: ' + property);
|
||
|
};
|
||
|
|
||
|
R3.D3.Face.prototype.toApiObject = function() {
|
||
|
|
||
|
return new R3.D3.API.Face(
|
||
|
this.id,
|
||
|
this.name,
|
||
|
this.v0index,
|
||
|
this.v1index,
|
||
|
this.v2index,
|
||
|
this.materialIndex,
|
||
|
this.uvs.reduce(
|
||
|
function(result, uvArray, index) {
|
||
|
|
||
|
result[index] = uvArray.reduce(
|
||
|
function(uvResult, uv) {
|
||
|
|
||
|
if (uv instanceof R3.Vector2) {
|
||
|
uvResult.push(uv.toApiObject());
|
||
|
} else {
|
||
|
console.warn('unknown uv type - cannot commit to API');
|
||
|
}
|
||
|
|
||
|
return uvResult;
|
||
|
}.bind(this),
|
||
|
[]
|
||
|
);
|
||
|
|
||
|
return result;
|
||
|
|
||
|
}.bind(this),
|
||
|
[]
|
||
|
),
|
||
|
this.color.toApiObject(),
|
||
|
this.vertexColors.map(function(vertexColor){
|
||
|
return vertexColor.toApiObject();
|
||
|
}),
|
||
|
this.vertexNormals.map(function(vertexNormal){
|
||
|
return vertexNormal.toApiObject();
|
||
|
}),
|
||
|
this.normal.toApiObject(),
|
||
|
this.selected
|
||
|
);
|
||
|
};
|
||
|
|
||
|
|
||
|
R3.D3.Face.prototype.createHelper = function(mesh) {
|
||
|
|
||
|
this.backupProperties = {
|
||
|
color : {
|
||
|
r: this.color.r,
|
||
|
g: this.color.g,
|
||
|
b: this.color.b
|
||
|
},
|
||
|
|
||
|
material : {
|
||
|
emissive : {
|
||
|
r: mesh.materials[this.materialIndex].emissive.r,
|
||
|
g: mesh.materials[this.materialIndex].emissive.g,
|
||
|
b: mesh.materials[this.materialIndex].emissive.b
|
||
|
}
|
||
|
},
|
||
|
vertexColors : mesh.materials[this.materialIndex].vertexColors
|
||
|
};
|
||
|
|
||
|
this.instance.vertexColors = [
|
||
|
new THREE.Color(1,0,0),
|
||
|
new THREE.Color(0,1,0),
|
||
|
new THREE.Color(0,0,1)
|
||
|
];
|
||
|
|
||
|
// this.instance.color.r = 1;
|
||
|
// this.instance.color.g = 0;
|
||
|
// this.instance.color.b = 0;
|
||
|
|
||
|
//
|
||
|
// mesh.materials[this.materialIndex].emissive.r = 0.5;
|
||
|
// mesh.materials[this.materialIndex].emissive.g = 0.5;
|
||
|
// mesh.materials[this.materialIndex].emissive.b = 0.5;
|
||
|
// mesh.materials[this.materialIndex].updateInstance('emissive');
|
||
|
|
||
|
mesh.materials[this.materialIndex].vertexColors = R3.D3.API.Material.TYPE_VERTEX_COLORS;
|
||
|
mesh.materials[this.materialIndex].updateInstance('vertexColors');
|
||
|
|
||
|
mesh.instance.geometry.elementsNeedUpdate = true;
|
||
|
|
||
|
};
|
||
|
|
||
|
R3.D3.Face.prototype.removeHelper = function(mesh) {
|
||
|
|
||
|
this.instance.color.r = this.backupProperties.color.r;
|
||
|
this.instance.color.g = this.backupProperties.color.g;
|
||
|
this.instance.color.b = this.backupProperties.color.b;
|
||
|
|
||
|
mesh.instance.geometry.colorsNeedUpdate = true;
|
||
|
|
||
|
// mesh.materials[this.materialIndex].emissive.r = this.backupProperties.material.emissive.r;
|
||
|
// mesh.materials[this.materialIndex].emissive.g = this.backupProperties.material.emissive.g;
|
||
|
// mesh.materials[this.materialIndex].emissive.b = this.backupProperties.material.emissive.b;
|
||
|
// mesh.materials[this.materialIndex].updateInstance('emissive');
|
||
|
|
||
|
mesh.materials[this.materialIndex].vertexColors = this.backupProperties.vertexColors;
|
||
|
mesh.materials[this.materialIndex].updateInstance('vertexColors');
|
||
|
|
||
|
};
|