r3-legacy/src/r3-d3-face-graphics.js

287 lines
7.3 KiB
JavaScript
Raw Normal View History

2019-08-07 05:17:41 +02:00
/**
* R3.D3.Face
* @param parent
* @param apiComponent
* @constructor
*/
R3.D3.Face.Graphics = function(
parent,
apiComponent
) {
__RUNTIME_COMPONENT_MACRO__;
R3.D3.Face.call(
this,
parent,
apiComponent,
linkedObjects
);
R3.D3.API.Face.call(
this,
apiComponent,
apiComponent.v0index,
apiComponent.v1index,
apiComponent.v2index,
apiComponent.materialIndex,
apiComponent.uvs,
apiComponent.color,
apiComponent.vertexColors,
apiComponent.vertexNormals,
apiComponent.normal,
apiComponent.selected
);
this.componentRuntime = R3.Component.GetComponentRuntime(this.parent);
switch (this.componentRuntime) {
case R3.Component.GRAPHICS_RUNTIME :
break;
case R3.Component.PHYSICS_RUNTIME :
break;
}
R3.Component.call(
this,
parent
);
if (this.implementation instanceof R3.Runtime.Graphics) {
/**
* 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.Graphics.prototype = Object.create(R3.D3.Face.prototype);
R3.D3.Face.Graphics.prototype.constructor = R3.D3.Face.Graphics;
/**
* 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.Graphics.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.Graphics.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');
};