diff --git a/Core/src/ots_materials.ts b/Core/src/ots_materials.ts index 230411b..399481e 100644 --- a/Core/src/ots_materials.ts +++ b/Core/src/ots_materials.ts @@ -1,4 +1,4 @@ -import { RGBA } from "./colour"; +import { RGBA, RGBAUtil } from "./colour"; //import { Material, OtS_Util } from "./materials"; import { OtS_Texture } from "./ots_texture"; @@ -7,6 +7,41 @@ export type OtS_MeshSection = { name: string, positionData: Float32Array, normal | { type: 'colour', colourData: Float32Array } | { type: 'textured', texcoordData: Float32Array, texture: OtS_Texture }); +export namespace OtS_MeshUtil { + export function copySection(section: OtS_MeshSection): OtS_MeshSection { + switch(section.type) { + case 'solid': + return { + type: 'solid', + name: section.name, + indexData: section.indexData.slice(0), + positionData: section.positionData.slice(0), + normalData: section.normalData.slice(0), + colour: RGBAUtil.copy(section.colour), + }; + case 'colour': + return { + type: 'colour', + name: section.name, + indexData: section.indexData.slice(0), + positionData: section.positionData.slice(0), + normalData: section.normalData.slice(0), + colourData: section.colourData.slice(0), + }; + case 'textured': + return { + type: 'textured', + name: section.name, + indexData: section.indexData.slice(0), + positionData: section.positionData.slice(0), + normalData: section.normalData.slice(0), + texcoordData: section.texcoordData.slice(0), + texture: section.texture.copy(), + }; + } + } +} + /* export class OtS_MaterialSlots { private _slots: Map; diff --git a/Core/src/ots_mesh.ts b/Core/src/ots_mesh.ts index ca701f8..7f2b485 100644 --- a/Core/src/ots_mesh.ts +++ b/Core/src/ots_mesh.ts @@ -1,7 +1,7 @@ import { Bounds } from './bounds'; import { RGBA } from './colour'; import { degreesToRadians } from './math'; -import { OtS_MeshSection } from './ots_materials'; +import { OtS_MeshSection, OtS_MeshUtil } from './ots_materials'; import { OtS_Texture } from './ots_texture'; import { UV } from "./util"; import { ASSERT } from './util/error_util'; @@ -357,10 +357,10 @@ export class OtS_Mesh { public copy() { const clone = OtS_Mesh.create(); - for (const section of this._sections) { + this.getSectionData().forEach((section) => { const success = clone.addSection(section).ok; ASSERT(success); - } + }) return clone; } @@ -391,9 +391,10 @@ export class OtS_Mesh { .reduce((total, count) => total + count, 0); } - // TODO: Return copy public getSectionData(): OtS_MeshSection[] { - return this._sections; + return this._sections.map((section) => { + return OtS_MeshUtil.copySection(section); + }) } public getSectionMetadata(): OtS_MeshSectionMetadata[] {