Introduce material source parameter to distinguish materials created for lines and faces.

This commit is contained in:
kovacsv 2023-10-23 12:17:03 +02:00
parent 4f0336c268
commit 978b1dfd19
6 changed files with 31 additions and 25 deletions

View File

@ -70,12 +70,19 @@ export const MaterialType =
Physical : 2
};
export const MaterialSource =
{
Model : 1,
DefaultFace : 2,
DefaultLine : 3
};
export class MaterialBase
{
constructor (type)
{
this.type = type;
this.isDefault = false;
this.source = MaterialSource.Model;
this.name = '';
this.color = new RGBColor (0, 0, 0);
@ -88,7 +95,7 @@ export class MaterialBase
if (this.type !== rhs.type) {
return false;
}
if (this.isDefault !== rhs.isDefault) {
if (this.source !== rhs.source) {
return false;
}
if (this.name !== rhs.name) {

View File

@ -1,15 +1,9 @@
import { CopyObjectAttributes } from '../core/core.js';
import { AddCoord3D, Coord3D, CoordIsEqual3D } from '../geometry/coord3d.js';
import { RGBColor } from './color.js';
import { PhongMaterial } from './material.js';
import { MaterialSource, PhongMaterial } from './material.js';
import { CalculateTriangleNormal, IsEmptyMesh } from './meshutils.js';
const MaterialSource =
{
Line : 1,
Face : 2
};
class ModelFinalizer
{
constructor (params)
@ -148,7 +142,7 @@ class ModelFinalizer
for (let i = 0; i < mesh.LineCount (); i++) {
let line = mesh.GetLine (i);
if (line.mat === null) {
line.mat = this.GetDefaultMaterialIndex (model, MaterialSource.Line);
line.mat = this.GetDefaultMaterialIndex (model, MaterialSource.DefaultLine);
}
}
@ -156,7 +150,7 @@ class ModelFinalizer
let triangle = mesh.GetTriangle (i);
this.FinalizeTriangle (mesh, triangle, meshStatus);
if (triangle.mat === null) {
triangle.mat = this.GetDefaultMaterialIndex (model, MaterialSource.Face);
triangle.mat = this.GetDefaultMaterialIndex (model, MaterialSource.DefaultFace);
}
}
@ -211,22 +205,22 @@ class ModelFinalizer
GetDefaultMaterialIndex (model, source)
{
function GetIndex (model, index, color)
function GetIndex (model, index, source, color)
{
if (index !== null) {
return index;
}
let defaultMaterial = new PhongMaterial ();
defaultMaterial.color = color;
defaultMaterial.isDefault = true;
defaultMaterial.source = source;
return model.AddMaterial (defaultMaterial);
}
if (source === MaterialSource.Line) {
this.defaultLineMaterialIndex = GetIndex (model, this.defaultLineMaterialIndex, this.params.defaultLineMaterialColor);
if (source === MaterialSource.DefaultLine) {
this.defaultLineMaterialIndex = GetIndex (model, this.defaultLineMaterialIndex, MaterialSource.DefaultLine, this.params.defaultLineMaterialColor);
return this.defaultLineMaterialIndex;
} else if (source === MaterialSource.Face) {
this.defaultMaterialIndex = GetIndex (model, this.defaultMaterialIndex, this.params.defaultMaterialColor);
} else if (source === MaterialSource.DefaultFace) {
this.defaultMaterialIndex = GetIndex (model, this.defaultMaterialIndex, MaterialSource.DefaultFace, this.params.defaultMaterialColor);
return this.defaultMaterialIndex;
} else {
return null;

View File

@ -1,5 +1,6 @@
import { BoundingBoxCalculator3D } from '../geometry/box3d.js';
import { Octree } from '../geometry/octree.js';
import { MaterialSource } from './material.js';
import { IsEmptyMesh } from './meshutils.js';
import { Model } from './model.js';
import { Topology } from './topology.js';
@ -99,7 +100,7 @@ export function HasDefaultMaterial (model)
{
for (let i = 0; i < model.MaterialCount (); i++) {
let material = model.GetMaterial (i);
if (material.isDefault && !material.vertexColors) {
if (material.source !== MaterialSource.Model && !material.vertexColors) {
return true;
}
}
@ -110,7 +111,7 @@ export function ReplaceDefaultMaterialsColor (model, color)
{
for (let i = 0; i < model.MaterialCount (); i++) {
let material = model.GetMaterial (i);
if (material.isDefault) {
if (material.source === MaterialSource.DefaultFace) {
material.color = color;
}
}

View File

@ -1,7 +1,7 @@
import { RunTasksBatch } from '../core/taskrunner.js';
import { IsEqual } from '../geometry/geometry.js';
import { CreateObjectUrl, CreateObjectUrlWithMimeType } from '../io/bufferutils.js';
import { MaterialType } from '../model/material.js';
import { MaterialSource, MaterialType } from '../model/material.js';
import { MeshInstance, MeshInstanceId } from '../model/meshinstance.js';
import { IsEmptyMesh } from '../model/meshutils.js';
import { ConvertColorToThreeColor, GetShadingType, ShadingType } from './threeutils.js';
@ -204,7 +204,8 @@ export class ThreeMaterialHandler
threeMaterial.emissiveMap = threeTexture;
});
if (material.isDefault) {
if (material.source !== MaterialSource.Model) {
threeMaterial.userData.source = material.source;
this.conversionOutput.defaultMaterials.push (threeMaterial);
}
@ -225,7 +226,8 @@ export class ThreeMaterialHandler
}
let threeMaterial = new THREE.LineBasicMaterial (materialParams);
if (material.isDefault) {
if (material.source !== MaterialSource.Model) {
threeMaterial.userData.source = material.source;
this.conversionOutput.defaultMaterials.push (threeMaterial);
}

View File

@ -1,6 +1,7 @@
import { Direction } from '../geometry/geometry.js';
import { Importer } from '../import/importer.js';
import { RevokeObjectUrl } from '../io/bufferutils.js';
import { MaterialSource } from '../model/material.js';
import { ConvertModelToThreeObject, ModelToThreeConversionOutput, ModelToThreeConversionParams } from './threeconverter.js';
import { ConvertColorToThreeColor, HasHighpDriverIssue } from './threeutils.js';
@ -95,7 +96,7 @@ export class ThreeModelLoader
{
if (this.defaultMaterials !== null) {
for (let defaultMaterial of this.defaultMaterials) {
if (!defaultMaterial.vertexColors) {
if (!defaultMaterial.vertexColors && defaultMaterial.userData.source === MaterialSource.DefaultFace) {
defaultMaterial.color = ConvertColorToThreeColor (defaultColor);
}
}

View File

@ -7,7 +7,7 @@ import { AddDiv, AddDomElement, ClearDomElement } from '../engine/viewer/domutil
import { SidebarPanel } from './sidebarpanel.js';
import { CreateInlineColorCircle } from './utils.js';
import { GetFileName, IsUrl } from '../engine/io/fileutils.js';
import { MaterialType } from '../engine/model/material.js';
import { MaterialSource, MaterialType } from '../engine/model/material.js';
import { RGBColorToHexString } from '../engine/model/color.js';
import { Unit } from '../engine/model/unit.js';
@ -104,7 +104,8 @@ export class SidebarDetailsPanel extends SidebarPanel
} else if (material.type === MaterialType.Physical) {
typeString = 'Physical';
}
this.AddProperty (table, new Property (PropertyType.Text, 'Source', material.isDefault ? 'Default' : 'Model'));
let materialSource = (material.source !== MaterialSource.Model) ? 'Default' : 'Model';
this.AddProperty (table, new Property (PropertyType.Text, 'Source', materialSource));
this.AddProperty (table, new Property (PropertyType.Text, 'Type', typeString));
if (material.vertexColors) {
this.AddProperty (table, new Property (PropertyType.Text, 'Color', 'Vertex colors'));