Add lines to the mesh data structure.
This commit is contained in:
parent
e89e7a0e1d
commit
cba71f92fa
@ -46,11 +46,12 @@ import { GetFileName, GetFileExtension, RequestUrl, ReadFile, TransformFileHostU
|
||||
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 { TextureMap, MaterialBase, FaceMaterial, PhongMaterial, PhysicalMaterial, TextureMapIsEqual, TextureIsEqual, MaterialType } from './model/material.js';
|
||||
import { Line } from './model/line.js';
|
||||
import { TextureMap, MaterialBase, LineMaterial, 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';
|
||||
import { GetMeshType, CalculateTriangleNormal, TransformMesh, FlipMeshTrianglesOrientation, MeshType } from './model/meshutils.js';
|
||||
import { IsEmptyMesh, CalculateTriangleNormal, TransformMesh, FlipMeshTrianglesOrientation } from './model/meshutils.js';
|
||||
import { Model } from './model/model.js';
|
||||
import { FinalizeModel, CheckModel } from './model/modelfinalization.js';
|
||||
import { IsModelEmpty, GetBoundingBox, GetTopology, IsTwoManifold, HasDefaultMaterial, ReplaceDefaultMaterialColor } from './model/modelutils.js';
|
||||
@ -226,8 +227,10 @@ export {
|
||||
GenerateCylinder,
|
||||
GenerateSphere,
|
||||
GeneratePlatonicSolid,
|
||||
Line,
|
||||
TextureMap,
|
||||
MaterialBase,
|
||||
LineMaterial,
|
||||
FaceMaterial,
|
||||
PhongMaterial,
|
||||
PhysicalMaterial,
|
||||
@ -240,11 +243,10 @@ export {
|
||||
ConvertMeshToMeshBuffer,
|
||||
MeshInstanceId,
|
||||
MeshInstance,
|
||||
GetMeshType,
|
||||
IsEmptyMesh,
|
||||
CalculateTriangleNormal,
|
||||
TransformMesh,
|
||||
FlipMeshTrianglesOrientation,
|
||||
MeshType,
|
||||
Model,
|
||||
FinalizeModel,
|
||||
CheckModel,
|
||||
|
||||
28
source/engine/model/line.js
Normal file
28
source/engine/model/line.js
Normal file
@ -0,0 +1,28 @@
|
||||
export class Line
|
||||
{
|
||||
constructor (v0, v1)
|
||||
{
|
||||
this.v0 = v0;
|
||||
this.v1 = v1;
|
||||
|
||||
this.mat = null;
|
||||
}
|
||||
|
||||
HasVertices ()
|
||||
{
|
||||
return this.v0 !== null && this.v1 !== null;
|
||||
}
|
||||
|
||||
SetMaterial (mat)
|
||||
{
|
||||
this.mat = mat;
|
||||
return this;
|
||||
}
|
||||
|
||||
Clone ()
|
||||
{
|
||||
let cloned = new Line (this.v0, this.v1);
|
||||
cloned.SetMaterial (this.mat);
|
||||
return cloned;
|
||||
}
|
||||
}
|
||||
@ -66,8 +66,9 @@ export function TextureMapIsEqual (aTex, bTex)
|
||||
|
||||
export const MaterialType =
|
||||
{
|
||||
Phong : 1,
|
||||
Physical : 2
|
||||
Line : 1,
|
||||
Phong : 2,
|
||||
Physical : 3
|
||||
};
|
||||
|
||||
export class MaterialBase
|
||||
@ -104,6 +105,14 @@ export class MaterialBase
|
||||
}
|
||||
}
|
||||
|
||||
export class LineMaterial extends MaterialBase
|
||||
{
|
||||
constructor (type)
|
||||
{
|
||||
super (MaterialType.Line);
|
||||
}
|
||||
}
|
||||
|
||||
export class FaceMaterial extends MaterialBase
|
||||
{
|
||||
constructor (type)
|
||||
|
||||
@ -9,6 +9,7 @@ export class Mesh extends ModelObject3D
|
||||
this.vertexColors = [];
|
||||
this.normals = [];
|
||||
this.uvs = [];
|
||||
this.lines = [];
|
||||
this.triangles = [];
|
||||
}
|
||||
|
||||
@ -32,6 +33,11 @@ export class Mesh extends ModelObject3D
|
||||
return this.uvs.length;
|
||||
}
|
||||
|
||||
LineCount ()
|
||||
{
|
||||
return this.lines.length;
|
||||
}
|
||||
|
||||
TriangleCount ()
|
||||
{
|
||||
return this.triangles.length;
|
||||
@ -101,6 +107,17 @@ SetNormal (index, normal)
|
||||
return this.uvs[index];
|
||||
}
|
||||
|
||||
AddLine (line)
|
||||
{
|
||||
this.lines.push (line);
|
||||
return this.lines.length - 1;
|
||||
}
|
||||
|
||||
GetLine (index)
|
||||
{
|
||||
return this.lines[index];
|
||||
}
|
||||
|
||||
AddTriangle (triangle)
|
||||
{
|
||||
this.triangles.push (triangle);
|
||||
@ -163,6 +180,11 @@ SetNormal (index, normal)
|
||||
cloned.AddTextureUV (uv.Clone ());
|
||||
}
|
||||
|
||||
for (let i = 0; i < this.LineCount (); i++) {
|
||||
let line = this.GetLine (i);
|
||||
cloned.AddLine (line.Clone ());
|
||||
}
|
||||
|
||||
for (let i = 0; i < this.TriangleCount (); i++) {
|
||||
let triangle = this.GetTriangle (i);
|
||||
cloned.AddTriangle (triangle.Clone ());
|
||||
|
||||
@ -65,6 +65,11 @@ export class MeshInstance extends ModelObject3D
|
||||
return this.mesh.TextureUVCount ();
|
||||
}
|
||||
|
||||
LineCount ()
|
||||
{
|
||||
return this.mesh.LineCount ();
|
||||
}
|
||||
|
||||
TriangleCount ()
|
||||
{
|
||||
return this.mesh.TriangleCount ();
|
||||
|
||||
@ -1,18 +1,9 @@
|
||||
import { CrossVector3D, SubCoord3D } from '../geometry/coord3d.js';
|
||||
import { Transformation } from '../geometry/transformation.js';
|
||||
|
||||
export const MeshType =
|
||||
export function IsEmptyMesh (mesh)
|
||||
{
|
||||
Empty : 0,
|
||||
TriangleMesh : 1
|
||||
};
|
||||
|
||||
export function GetMeshType (mesh)
|
||||
{
|
||||
if (mesh.TriangleCount () > 0) {
|
||||
return MeshType.TriangleMesh;
|
||||
}
|
||||
return MeshType.Empty;
|
||||
return mesh.LineCount () === 0 && mesh.TriangleCount () === 0;
|
||||
}
|
||||
|
||||
export function CalculateTriangleNormal (v0, v1, v2)
|
||||
|
||||
@ -93,6 +93,15 @@ export class Model extends ModelObject3D
|
||||
return count;
|
||||
}
|
||||
|
||||
LineCount ()
|
||||
{
|
||||
let count = 0;
|
||||
this.EnumerateMeshInstances ((meshInstance) => {
|
||||
count += meshInstance.LineCount ();
|
||||
});
|
||||
return count;
|
||||
}
|
||||
|
||||
TriangleCount ()
|
||||
{
|
||||
let count = 0;
|
||||
|
||||
@ -1,19 +1,23 @@
|
||||
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 { CalculateTriangleNormal, GetMeshType, MeshType } from './meshutils.js';
|
||||
import { LineMaterial, PhongMaterial } from './material.js';
|
||||
import { CalculateTriangleNormal, IsEmptyMesh } from './meshutils.js';
|
||||
|
||||
class ModelFinalizer
|
||||
{
|
||||
constructor (params)
|
||||
{
|
||||
this.params = {
|
||||
getDefaultLineMaterialColor : () => {
|
||||
return new RGBColor (0, 0, 0);
|
||||
},
|
||||
getDefaultMaterialColor : () => {
|
||||
return new RGBColor (0, 0, 0);
|
||||
}
|
||||
};
|
||||
CopyObjectAttributes (params, this.params);
|
||||
this.defaultLineMaterialIndex = null;
|
||||
this.defaultMaterialIndex = null;
|
||||
}
|
||||
|
||||
@ -56,8 +60,7 @@ class ModelFinalizer
|
||||
{
|
||||
for (let meshIndex = 0; meshIndex < model.MeshCount (); meshIndex++) {
|
||||
let mesh = model.GetMesh (meshIndex);
|
||||
let type = GetMeshType (mesh);
|
||||
if (type === MeshType.Empty) {
|
||||
if (IsEmptyMesh (mesh)) {
|
||||
model.RemoveMesh (meshIndex);
|
||||
meshIndex = meshIndex - 1;
|
||||
continue;
|
||||
@ -139,10 +142,16 @@ class ModelFinalizer
|
||||
calculateCurveNormals : false
|
||||
};
|
||||
|
||||
for (let i = 0; i < mesh.LineCount (); i++) {
|
||||
let line = mesh.GetLine (i);
|
||||
if (line.mat === null) {
|
||||
line.mat = this.GetDefaultLineMaterialIndex (model);
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < mesh.TriangleCount (); i++) {
|
||||
let triangle = mesh.GetTriangle (i);
|
||||
this.FinalizeTriangle (mesh, triangle, meshStatus);
|
||||
|
||||
if (triangle.mat === null) {
|
||||
triangle.mat = this.GetDefaultMaterialIndex (model);
|
||||
}
|
||||
@ -197,6 +206,18 @@ class ModelFinalizer
|
||||
}
|
||||
}
|
||||
|
||||
GetDefaultLineMaterialIndex (model)
|
||||
{
|
||||
if (this.defaultLineMaterialIndex === null) {
|
||||
let defaultLineMaterialColor = this.params.getDefaultLineMaterialColor ();
|
||||
let defaultMaterial = new LineMaterial ();
|
||||
defaultMaterial.color = defaultLineMaterialColor;
|
||||
defaultMaterial.isDefault = true;
|
||||
this.defaultLineMaterialIndex = model.AddMaterial (defaultMaterial);
|
||||
}
|
||||
return this.defaultLineMaterialIndex;
|
||||
}
|
||||
|
||||
GetDefaultMaterialIndex (model)
|
||||
{
|
||||
if (this.defaultMaterialIndex === null) {
|
||||
@ -211,6 +232,7 @@ class ModelFinalizer
|
||||
|
||||
Reset ()
|
||||
{
|
||||
this.defaultLineMaterialIndex = null;
|
||||
this.defaultMaterialIndex = null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { BoundingBoxCalculator3D } from '../geometry/box3d.js';
|
||||
import { Octree } from '../geometry/octree.js';
|
||||
import { GetMeshType, MeshType } from './meshutils.js';
|
||||
import { IsEmptyMesh } from './meshutils.js';
|
||||
import { Model } from './model.js';
|
||||
import { Topology } from './topology.js';
|
||||
|
||||
@ -8,7 +8,7 @@ export function IsModelEmpty (model)
|
||||
{
|
||||
let isEmpty = true;
|
||||
model.EnumerateMeshInstances ((meshInstance) => {
|
||||
if (GetMeshType (meshInstance) !== MeshType.Empty) {
|
||||
if (!IsEmptyMesh (meshInstance)) {
|
||||
isEmpty = false;
|
||||
}
|
||||
});
|
||||
|
||||
@ -3,7 +3,7 @@ import { IsEqual } from '../geometry/geometry.js';
|
||||
import { CreateObjectUrl, CreateObjectUrlWithMimeType } from '../io/bufferutils.js';
|
||||
import { MaterialType } from '../model/material.js';
|
||||
import { MeshInstance, MeshInstanceId } from '../model/meshinstance.js';
|
||||
import { GetMeshType, MeshType } from '../model/meshutils.js';
|
||||
import { IsEmptyMesh } from '../model/meshutils.js';
|
||||
import { ConvertColorToThreeColor, GetShadingType, ShadingType } from './threeutils.js';
|
||||
|
||||
import * as THREE from 'three';
|
||||
@ -329,8 +329,7 @@ export function ConvertModelToThreeObject (model, params, output, callbacks)
|
||||
|
||||
function ConvertMesh (threeObject, meshInstance, modelThreeMaterials)
|
||||
{
|
||||
let type = GetMeshType (meshInstance.mesh);
|
||||
if (type === MeshType.TriangleMesh) {
|
||||
if (!IsEmptyMesh (meshInstance.mesh)) {
|
||||
let threeMesh = CreateThreeMesh (meshInstance, modelThreeMaterials);
|
||||
threeObject.add (threeMesh);
|
||||
}
|
||||
|
||||
@ -52,6 +52,16 @@ describe ('Mesh', function() {
|
||||
assert.strictEqual (uv.y, 2.0);
|
||||
});
|
||||
|
||||
it ('Add Line', function () {
|
||||
var mesh = new OV.Mesh ();
|
||||
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);
|
||||
});
|
||||
|
||||
it ('Add Triangle', function () {
|
||||
var mesh = new OV.Mesh ();
|
||||
var triangle = new OV.Triangle (1, 2, 3);
|
||||
|
||||
@ -41,6 +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.AddTriangle (new OV.Triangle (0, 1, 2));
|
||||
mesh.AddTriangle (new OV.Triangle (0, 1, 2));
|
||||
mesh.AddTriangle (new OV.Triangle (0, 1, 2));
|
||||
@ -49,6 +52,7 @@ describe ('Model', function () {
|
||||
assert.strictEqual (model.VertexCount (), 3);
|
||||
assert.strictEqual (model.NormalCount (), 2);
|
||||
assert.strictEqual (model.TextureUVCount (), 1);
|
||||
assert.strictEqual (model.LineCount (), 3);
|
||||
assert.strictEqual (model.TriangleCount (), 4);
|
||||
});
|
||||
|
||||
@ -86,6 +90,21 @@ describe ('Model', function () {
|
||||
});
|
||||
|
||||
describe ('Model Finalization', function () {
|
||||
it ('Create Default Material', function () {
|
||||
var mesh = new OV.Mesh ();
|
||||
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));
|
||||
mesh.AddTriangle (new OV.Triangle (v0, v1, v2));
|
||||
var model = new OV.Model ();
|
||||
model.AddMesh (mesh);
|
||||
OV.FinalizeModel (model);
|
||||
assert.strictEqual (model.MaterialCount (), 2);
|
||||
assert.strictEqual (model.GetMaterial (0).type, OV.MaterialType.Line);
|
||||
assert.strictEqual (model.GetMaterial (1).type, OV.MaterialType.Phong);
|
||||
});
|
||||
|
||||
it ('Calculate Normal', function () {
|
||||
var mesh = new OV.Mesh ();
|
||||
var v0 = mesh.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0));
|
||||
|
||||
Loading…
Reference in New Issue
Block a user