From 5aca2d2cda0f65eb4e4d29a734613d504fa7477b Mon Sep 17 00:00:00 2001 From: Lucas Dower Date: Fri, 15 Sep 2023 19:43:48 +0100 Subject: [PATCH] Convert `UV` from class to type --- src/runtime/atlas.ts | 8 ++++---- src/runtime/importers/gltf_loader.ts | 8 ++++---- src/runtime/importers/obj_importer.ts | 2 +- src/runtime/mesh.ts | 12 ++++++------ src/runtime/ots_voxel_mesh_converter.ts | 8 ++++---- src/runtime/texture.ts | 2 +- src/runtime/util.ts | 15 ++------------- 7 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/runtime/atlas.ts b/src/runtime/atlas.ts index 20e5c6e..4e41740 100644 --- a/src/runtime/atlas.ts +++ b/src/runtime/atlas.ts @@ -60,10 +60,10 @@ export class Atlas { const getTextureUV = (name: string) => { const tex = atlasData.textures[name]; - return new UV( - (3 * tex.atlasColumn + 1) / (atlas._atlasSize * 3), - (3 * tex.atlasRow + 1) / (atlas._atlasSize * 3), - ); + return { + u: (3 * tex.atlasColumn + 1) / (atlas._atlasSize * 3), + v: (3 * tex.atlasRow + 1) / (atlas._atlasSize * 3), + }; }; for (const block of atlasData.blocks) { diff --git a/src/runtime/importers/gltf_loader.ts b/src/runtime/importers/gltf_loader.ts index e38421f..f7a9dac 100644 --- a/src/runtime/importers/gltf_loader.ts +++ b/src/runtime/importers/gltf_loader.ts @@ -76,10 +76,10 @@ export class GltfLoader extends IImporter { if (attributes.TEXCOORD_0 !== undefined) { const texcoords = attributes.TEXCOORD_0.value as Float32Array; for (let i = 0; i < texcoords.length; i += 2) { - meshTexcoords.push(new UV( - texcoords[i + 0], - 1.0 - texcoords[i + 1], - )); + meshTexcoords.push({ + u: texcoords[i + 0], + v: 1.0 - texcoords[i + 1], + }); } } // Material diff --git a/src/runtime/importers/obj_importer.ts b/src/runtime/importers/obj_importer.ts index 5dd912e..e7e9bc2 100644 --- a/src/runtime/importers/obj_importer.ts +++ b/src/runtime/importers/obj_importer.ts @@ -119,7 +119,7 @@ export class ObjImporter extends IImporter { return err; } - this._uvs.push(new UV(u, v)); + this._uvs.push({ u: u, v: v }); return null; }, }, diff --git a/src/runtime/mesh.ts b/src/runtime/mesh.ts index 144a5d2..66d1d82 100644 --- a/src/runtime/mesh.ts +++ b/src/runtime/mesh.ts @@ -364,15 +364,15 @@ export class Mesh { const tri = this._tris[triIndex]; if (tri.texcoordIndices) { return { - uv0: this._uvs[tri.texcoordIndices.x] || new UV(0.0, 0.0), - uv1: this._uvs[tri.texcoordIndices.y] || new UV(0.0, 0.0), - uv2: this._uvs[tri.texcoordIndices.z] || new UV(0.0, 0.0), + uv0: this._uvs[tri.texcoordIndices.x] || { u: 0.0, v: 0.0 }, + uv1: this._uvs[tri.texcoordIndices.y] || { u: 0.0, v: 0.0 }, + uv2: this._uvs[tri.texcoordIndices.z] || { u: 0.0, v: 0.0 }, }; } return { - uv0: new UV(0.0, 0.0), - uv1: new UV(0.0, 0.0), - uv2: new UV(0.0, 0.0), + uv0: { u: 0.0, v: 0.0 }, + uv1: { u: 0.0, v: 0.0 }, + uv2: { u: 0.0, v: 0.0 }, }; } diff --git a/src/runtime/ots_voxel_mesh_converter.ts b/src/runtime/ots_voxel_mesh_converter.ts index 114bf50..36c83dd 100644 --- a/src/runtime/ots_voxel_mesh_converter.ts +++ b/src/runtime/ots_voxel_mesh_converter.ts @@ -146,10 +146,10 @@ export class OtS_VoxelMesh_Converter { const w1 = area20 / total; const w2 = area01 / total; - const uv = new UV( - triangle.uv0.u * w0 + triangle.uv1.u * w1 + triangle.uv2.u * w2, - triangle.uv0.v * w0 + triangle.uv1.v * w1 + triangle.uv2.v * w2, - ); + const uv = { + u: triangle.uv0.u * w0 + triangle.uv1.u * w1 + triangle.uv2.u * w2, + v: triangle.uv0.v * w0 + triangle.uv1.v * w1 + triangle.uv2.v * w2, + }; if (isNaN(uv.u) || isNaN(uv.v)) { RGBAUtil.copy(RGBAColours.MAGENTA); diff --git a/src/runtime/texture.ts b/src/runtime/texture.ts index cb84143..b952445 100644 --- a/src/runtime/texture.ts +++ b/src/runtime/texture.ts @@ -95,7 +95,7 @@ export class Texture { * UV can be in any range and is not limited to [0, 1] */ public getRGBA(inUV: UV, interpolation: TTexelInterpolation, extension: TTexelExtension): RGBA { - const uv = new UV(0.0, 0.0); + const uv = { u: 0.0, v: 0.0 }; if (extension === 'clamp') { uv.u = clamp(inUV.u, 0.0, 1.0); diff --git a/src/runtime/util.ts b/src/runtime/util.ts index fd121d9..3816ba3 100644 --- a/src/runtime/util.ts +++ b/src/runtime/util.ts @@ -1,4 +1,5 @@ import { AppMath } from "./math"; +import { TBrand } from "./util/type_util"; export namespace AppUtil { export namespace Text { @@ -57,19 +58,7 @@ export namespace AppTypes { export type TNamespacedBlockName = string; } -export class UV { - public u: number; - public v: number; - - constructor(u: number, v: number) { - this.u = u; - this.v = v; - } - - public copy() { - return new UV(this.u, this.v); - } -} +export type UV = { u: number, v: number }; export type TOptional = T | undefined;