Set material type and shading model based on the most used material type in the model.

This commit is contained in:
kovacsv 2021-10-29 18:33:46 +02:00
parent 494ef4697b
commit a1c77ea263
3 changed files with 59 additions and 14 deletions

View File

@ -207,3 +207,23 @@ OV.ReplaceDefaultMaterialColor = function (model, color)
}
}
};
OV.GetRepresentativeMaterialType = function (model)
{
let maxType = OV.MaterialType.Phong;
let maxCount = 0;
let materialTypeToIndex = new Map ();
for (let i = 0; i < model.MaterialCount (); i++) {
let material = model.GetMaterial (i);
if (!materialTypeToIndex.has (material.type)) {
materialTypeToIndex.set (material.type, 0);
}
let typeCount = materialTypeToIndex.get (material.type) + 1;
materialTypeToIndex.set (material.type, typeCount);
if (typeCount > maxCount) {
maxType = material.type;
maxCount = typeCount;
}
}
return maxType;
};

View File

@ -85,7 +85,7 @@ OV.ThreeNodeTree = class
OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
{
function CreateThreeMaterial (stateHandler, model, materialIndex, params, output)
function CreateThreeMaterial (stateHandler, model, materialIndex, materialType, params, output)
{
function SetTextureParameters (texture, threeTexture)
{
@ -135,7 +135,7 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
}
let threeMaterial = null;
if (material.type === OV.MaterialType.Phong) {
if (materialType === OV.MaterialType.Phong) {
threeMaterial = new THREE.MeshPhongMaterial (materialParams);
let specularColor = new THREE.Color (material.specular.r / 255.0, material.specular.g / 255.0, material.specular.b / 255.0);
if (OV.IsEqual (material.shininess, 0.0)) {
@ -146,7 +146,7 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
LoadTexture (stateHandler, threeMaterial, material.specularMap, (threeTexture) => {
threeMaterial.specularMap = threeTexture;
});
} else if (material.type === OV.MaterialType.Physical) {
} else if (materialType === OV.MaterialType.Physical) {
threeMaterial = new THREE.MeshStandardMaterial (materialParams);
threeMaterial.metalness = material.metalness;
threeMaterial.roughness = material.roughness;
@ -319,10 +319,11 @@ OV.ConvertModelToThreeObject = function (model, params, output, callbacks)
}
let stateHandler = new OV.ThreeConversionStateHandler (callbacks);
let materialType = OV.GetRepresentativeMaterialType (model);
let modelThreeMaterials = [];
for (let materialIndex = 0; materialIndex < model.MaterialCount (); materialIndex++) {
let threeMaterial = CreateThreeMaterial (stateHandler, model, materialIndex, params, output);
let threeMaterial = CreateThreeMaterial (stateHandler, model, materialIndex, materialType, params, output);
modelThreeMaterials.push (threeMaterial);
}

View File

@ -1,3 +1,9 @@
OV.ShadingModelType =
{
Phong : 1,
Physical : 2
};
OV.GetDefaultCamera = function (direction)
{
if (direction === OV.Direction.X) {
@ -22,6 +28,27 @@ OV.GetDefaultCamera = function (direction)
return null;
};
OV.GetShadingTypeOfObject = function (mainObject)
{
let shadingType = null;
mainObject.traverse ((obj) => {
if (shadingType !== null) {
return;
}
if (obj.isMesh) {
for (const material of obj.material) {
if (material.type === 'MeshPhongMaterial') {
shadingType = OV.ShadingModelType.Phong;
} else if (material.type === 'MeshStandardMaterial') {
shadingType = OV.ShadingModelType.Physical;
}
break;
}
}
});
return shadingType;
};
OV.UpVector = class
{
constructor ()
@ -149,18 +176,13 @@ OV.ViewerGeometry = class
}
};
OV.ShadingModelType =
{
Phong : 1,
Physical : 2
};
OV.ShadingModel = class
{
constructor (scene)
{
this.scene = scene;
this.type = OV.ShadingModelType.Phong;
this.ambientLight = new THREE.AmbientLight (0x888888);
this.directionalLight = new THREE.DirectionalLight (0x888888);
this.environment = null;
@ -171,9 +193,10 @@ OV.ShadingModel = class
SetType (type)
{
if (type === OV.ShadingModelType.Phong) {
this.type = type;
if (this.type === OV.ShadingModelType.Phong) {
this.scene.environment = null;
} else if (type === OV.ShadingModelType.Physical) {
} else if (this.type === OV.ShadingModelType.Physical) {
this.scene.environment = this.environment;
}
}
@ -372,8 +395,9 @@ OV.Viewer = class
SetMainObject (object)
{
const shadingType = OV.GetShadingTypeOfObject (object);
this.geometry.SetMainObject (object);
this.shading.SetType (OV.ShadingModelType.Physical);
this.shading.SetType (shadingType);
this.Render ();
}
@ -399,7 +423,7 @@ OV.Viewer = class
function CreateHighlightMaterials (originalMaterials, highlightColor)
{
const highlightMaterial = new THREE.MeshPhongMaterial ({
color : 0x8ec9f0,
color : highlightColor,
side : THREE.DoubleSide
});
let highlightMaterials = [];