No need to have a separate material for lines.
This commit is contained in:
parent
4361240842
commit
f546ce65c0
@ -47,7 +47,7 @@ import { TextWriter } from './io/textwriter.js';
|
||||
import { RGBColor, RGBAColor, ColorComponentFromFloat, ColorComponentToFloat, RGBColorFromFloatComponents, SRGBToLinear, LinearToSRGB, IntegerToHexString, RGBColorToHexString, RGBAColorToHexString, HexStringToRGBColor, HexStringToRGBAColor, ArrayToRGBColor, RGBColorIsEqual } from './model/color.js';
|
||||
import { GeneratorParams, Generator, GeneratorHelper, GenerateCuboid, GenerateCone, GenerateCylinder, GenerateSphere, GeneratePlatonicSolid } from './model/generator.js';
|
||||
import { Line } from './model/line.js';
|
||||
import { TextureMap, MaterialBase, LineMaterial, FaceMaterial, PhongMaterial, PhysicalMaterial, TextureMapIsEqual, TextureIsEqual, MaterialType } from './model/material.js';
|
||||
import { TextureMap, MaterialBase, FaceMaterial, PhongMaterial, PhysicalMaterial, TextureMapIsEqual, TextureIsEqual, MaterialType } from './model/material.js';
|
||||
import { Mesh } from './model/mesh.js';
|
||||
import { MeshPrimitiveBuffer, MeshBuffer, ConvertMeshToMeshBuffer } from './model/meshbuffer.js';
|
||||
import { MeshInstanceId, MeshInstance } from './model/meshinstance.js';
|
||||
@ -230,7 +230,6 @@ export {
|
||||
Line,
|
||||
TextureMap,
|
||||
MaterialBase,
|
||||
LineMaterial,
|
||||
FaceMaterial,
|
||||
PhongMaterial,
|
||||
PhysicalMaterial,
|
||||
|
||||
@ -1,16 +1,14 @@
|
||||
export class Line
|
||||
{
|
||||
constructor (v0, v1)
|
||||
constructor (vertices)
|
||||
{
|
||||
this.v0 = v0;
|
||||
this.v1 = v1;
|
||||
|
||||
this.vertices = vertices;
|
||||
this.mat = null;
|
||||
}
|
||||
|
||||
HasVertices ()
|
||||
{
|
||||
return this.v0 !== null && this.v1 !== null;
|
||||
return this.vertices !== null && this.vertices.length >= 2;
|
||||
}
|
||||
|
||||
SetMaterial (mat)
|
||||
@ -21,7 +19,7 @@ export class Line
|
||||
|
||||
Clone ()
|
||||
{
|
||||
let cloned = new Line (this.v0, this.v1);
|
||||
let cloned = new Line ([...this.vertices]);
|
||||
cloned.SetMaterial (this.mat);
|
||||
return cloned;
|
||||
}
|
||||
|
||||
@ -66,9 +66,8 @@ export function TextureMapIsEqual (aTex, bTex)
|
||||
|
||||
export const MaterialType =
|
||||
{
|
||||
Line : 1,
|
||||
Phong : 2,
|
||||
Physical : 3
|
||||
Phong : 1,
|
||||
Physical : 2
|
||||
};
|
||||
|
||||
export class MaterialBase
|
||||
@ -105,14 +104,6 @@ export class MaterialBase
|
||||
}
|
||||
}
|
||||
|
||||
export class LineMaterial extends MaterialBase
|
||||
{
|
||||
constructor (type)
|
||||
{
|
||||
super (MaterialType.Line);
|
||||
}
|
||||
}
|
||||
|
||||
export class FaceMaterial extends MaterialBase
|
||||
{
|
||||
constructor (type)
|
||||
|
||||
@ -1,9 +1,15 @@
|
||||
import { CopyObjectAttributes } from '../core/core.js';
|
||||
import { AddCoord3D, Coord3D, CoordIsEqual3D } from '../geometry/coord3d.js';
|
||||
import { RGBColor } from './color.js';
|
||||
import { LineMaterial, PhongMaterial } from './material.js';
|
||||
import { PhongMaterial } from './material.js';
|
||||
import { CalculateTriangleNormal, IsEmptyMesh } from './meshutils.js';
|
||||
|
||||
const MaterialSource =
|
||||
{
|
||||
Line : 1,
|
||||
Face : 2
|
||||
};
|
||||
|
||||
class ModelFinalizer
|
||||
{
|
||||
constructor (params)
|
||||
@ -13,6 +19,7 @@ class ModelFinalizer
|
||||
defaultMaterialColor : new RGBColor (0, 0, 0)
|
||||
};
|
||||
CopyObjectAttributes (params, this.params);
|
||||
|
||||
this.defaultLineMaterialIndex = null;
|
||||
this.defaultMaterialIndex = null;
|
||||
}
|
||||
@ -141,7 +148,7 @@ class ModelFinalizer
|
||||
for (let i = 0; i < mesh.LineCount (); i++) {
|
||||
let line = mesh.GetLine (i);
|
||||
if (line.mat === null) {
|
||||
line.mat = this.GetDefaultLineMaterialIndex (model);
|
||||
line.mat = this.GetDefaultMaterialIndex (model, MaterialSource.Line);
|
||||
}
|
||||
}
|
||||
|
||||
@ -149,7 +156,7 @@ class ModelFinalizer
|
||||
let triangle = mesh.GetTriangle (i);
|
||||
this.FinalizeTriangle (mesh, triangle, meshStatus);
|
||||
if (triangle.mat === null) {
|
||||
triangle.mat = this.GetDefaultMaterialIndex (model);
|
||||
triangle.mat = this.GetDefaultMaterialIndex (model, MaterialSource.Face);
|
||||
}
|
||||
}
|
||||
|
||||
@ -202,26 +209,28 @@ class ModelFinalizer
|
||||
}
|
||||
}
|
||||
|
||||
GetDefaultLineMaterialIndex (model)
|
||||
GetDefaultMaterialIndex (model, source)
|
||||
{
|
||||
if (this.defaultLineMaterialIndex === null) {
|
||||
let defaultMaterial = new LineMaterial ();
|
||||
defaultMaterial.color = this.params.defaultLineMaterialColor;
|
||||
defaultMaterial.isDefault = true;
|
||||
this.defaultLineMaterialIndex = model.AddMaterial (defaultMaterial);
|
||||
}
|
||||
return this.defaultLineMaterialIndex;
|
||||
}
|
||||
|
||||
GetDefaultMaterialIndex (model)
|
||||
{
|
||||
if (this.defaultMaterialIndex === null) {
|
||||
function GetIndex (model, index, color)
|
||||
{
|
||||
if (index !== null) {
|
||||
return index;
|
||||
}
|
||||
let defaultMaterial = new PhongMaterial ();
|
||||
defaultMaterial.color = this.params.defaultMaterialColor;
|
||||
defaultMaterial.color = color;
|
||||
defaultMaterial.isDefault = true;
|
||||
this.defaultMaterialIndex = model.AddMaterial (defaultMaterial);
|
||||
return model.AddMaterial (defaultMaterial);
|
||||
}
|
||||
|
||||
if (source === MaterialSource.Line) {
|
||||
this.defaultLineMaterialIndex = GetIndex (model, this.defaultLineMaterialIndex, this.params.defaultLineMaterialColor)
|
||||
return this.defaultLineMaterialIndex;
|
||||
} else if (source === MaterialSource.Face) {
|
||||
this.defaultMaterialIndex = GetIndex (model, this.defaultMaterialIndex, this.params.defaultMaterialColor);
|
||||
return this.defaultMaterialIndex;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return this.defaultMaterialIndex;
|
||||
}
|
||||
|
||||
Reset ()
|
||||
|
||||
@ -54,12 +54,12 @@ describe ('Mesh', function() {
|
||||
|
||||
it ('Add Line', function () {
|
||||
var mesh = new OV.Mesh ();
|
||||
var index = mesh.AddLine (new OV.Line (0, 1));
|
||||
var index = mesh.AddLine (new OV.Line ([0, 1]));
|
||||
assert.strictEqual (index, 0);
|
||||
assert.strictEqual (mesh.LineCount (), 1);
|
||||
var line = mesh.GetLine (index);
|
||||
assert.strictEqual (line.v0, 0);
|
||||
assert.strictEqual (line.v1, 1);
|
||||
assert.strictEqual (line.vertices[0], 0);
|
||||
assert.strictEqual (line.vertices[1], 1);
|
||||
});
|
||||
|
||||
it ('Add Triangle', function () {
|
||||
|
||||
@ -41,9 +41,9 @@ describe ('Model', function () {
|
||||
mesh.AddNormal (new OV.Coord3D (0.0, 0.0, 0.0));
|
||||
mesh.AddNormal (new OV.Coord3D (0.0, 0.0, 0.0));
|
||||
mesh.AddTextureUV (new OV.Coord2D (0.0, 0.0));
|
||||
mesh.AddLine (new OV.Line (0, 1));
|
||||
mesh.AddLine (new OV.Line (1, 2));
|
||||
mesh.AddLine (new OV.Line (2, 0));
|
||||
mesh.AddLine (new OV.Line ([0, 1]));
|
||||
mesh.AddLine (new OV.Line ([1, 2]));
|
||||
mesh.AddLine (new OV.Line ([2, 0]));
|
||||
mesh.AddTriangle (new OV.Triangle (0, 1, 2));
|
||||
mesh.AddTriangle (new OV.Triangle (0, 1, 2));
|
||||
mesh.AddTriangle (new OV.Triangle (0, 1, 2));
|
||||
@ -95,14 +95,20 @@ describe ('Model Finalization', function () {
|
||||
var v0 = mesh.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0));
|
||||
var v1 = mesh.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0));
|
||||
var v2 = mesh.AddVertex (new OV.Coord3D (1.0, 1.0, 0.0));
|
||||
mesh.AddLine (new OV.Line (v0, v1));
|
||||
var v3 = mesh.AddVertex (new OV.Coord3D (1.0, 1.0, 0.0));
|
||||
mesh.AddLine (new OV.Line ([v0, v1]));
|
||||
mesh.AddLine (new OV.Line ([v1, v2]));
|
||||
mesh.AddTriangle (new OV.Triangle (v0, v1, v2));
|
||||
mesh.AddTriangle (new OV.Triangle (v0, v2, v3));
|
||||
var model = new OV.Model ();
|
||||
model.AddMesh (mesh);
|
||||
OV.FinalizeModel (model);
|
||||
OV.FinalizeModel (model, {
|
||||
defaultLineMaterialColor : new OV.RGBColor (1, 2, 3),
|
||||
defaultMaterialColor : new OV.RGBColor (4, 5, 6)
|
||||
});
|
||||
assert.strictEqual (model.MaterialCount (), 2);
|
||||
assert.strictEqual (model.GetMaterial (0).type, OV.MaterialType.Line);
|
||||
assert.strictEqual (model.GetMaterial (1).type, OV.MaterialType.Phong);
|
||||
assert.deepStrictEqual (model.GetMaterial (0).color, new OV.RGBColor (1, 2, 3));
|
||||
assert.deepStrictEqual (model.GetMaterial (1).color, new OV.RGBColor (4, 5, 6));
|
||||
});
|
||||
|
||||
it ('Calculate Normal', function () {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user