Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,960 changes: 924 additions & 1,036 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@
"node": ">=25"
},
"devDependencies": {
"@biomejs/biome": "2.2.7",
"@types/node": "^24.9.1",
"@types/three": "^0.180.0",
"three": "^0.180.0",
"@biomejs/biome": "2.4.8",
"@types/node": "^25.5.0",
"@types/three": "^0.183.1",
"three": "^0.183.2",
"typescript": "^5.9.3",
"vite": "^7.1.12",
"vite": "^8.0.1",
"vite-plugin-dts": "^4.5.4",
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^4.0.2"
"vitest": "^4.1.0"
},
"peerDependencies": {
"three": "^0.143.0"
Expand Down
99 changes: 99 additions & 0 deletions src/ensureUV.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { describe, it, expect } from "vitest";
import { BufferAttribute, BufferGeometry, Float32BufferAttribute } from "three";
import { ensureUV } from "./ensureUV";

describe("ensureUV", () => {
it("should add UV attribute when geometry has no UVs", () => {
const geometry = new BufferGeometry();
const positions = new Float32BufferAttribute([0, 0, 0, 1, 1, 1, 2, 2, 2], 3);
geometry.setAttribute("position", positions);

ensureUV(geometry);

expect(geometry.attributes.uv).toBeDefined();
expect(geometry.attributes.uv).toBeInstanceOf(BufferAttribute);
});

it("should create UVs with item size of 2", () => {
const geometry = new BufferGeometry();
const positions = new Float32BufferAttribute([0, 0, 0, 1, 1, 1], 3);
geometry.setAttribute("position", positions);

ensureUV(geometry);

expect(geometry.attributes.uv.itemSize).toBe(2);
});

it("should create UV count matching position count", () => {
const geometry = new BufferGeometry();
const vertexCount = 5;
const positions = new Float32BufferAttribute(new Float32Array(vertexCount * 3), 3);
geometry.setAttribute("position", positions);

ensureUV(geometry);

expect(geometry.attributes.uv.count).toBe(vertexCount);
});

it("should initialize all UV values to 0", () => {
const geometry = new BufferGeometry();
const positions = new Float32BufferAttribute([0, 0, 0, 1, 1, 1, 2, 2, 2], 3);
geometry.setAttribute("position", positions);

ensureUV(geometry);

const uvArray = geometry.attributes.uv.array;
for (let i = 0; i < uvArray.length; i++) {
expect(uvArray[i]).toBe(0);
}
});

it("should use Float32Array for UV data", () => {
const geometry = new BufferGeometry();
const positions = new Float32BufferAttribute([0, 0, 0], 3);
geometry.setAttribute("position", positions);

ensureUV(geometry);

expect(geometry.attributes.uv.array).toBeInstanceOf(Float32Array);
});

it("should not overwrite existing UV attribute", () => {
const geometry = new BufferGeometry();
const positions = new Float32BufferAttribute([0, 0, 0, 1, 1, 1], 3);
geometry.setAttribute("position", positions);

const existingUVs = new Float32BufferAttribute([0.5, 0.5, 1.0, 1.0], 2);
geometry.setAttribute("uv", existingUVs);

ensureUV(geometry);

expect(geometry.attributes.uv.array[0]).toBe(0.5);
expect(geometry.attributes.uv.array[1]).toBe(0.5);
expect(geometry.attributes.uv.array[2]).toBe(1.0);
expect(geometry.attributes.uv.array[3]).toBe(1.0);
});

it("should handle geometry with a single vertex", () => {
const geometry = new BufferGeometry();
const positions = new Float32BufferAttribute([0, 0, 0], 3);
geometry.setAttribute("position", positions);

ensureUV(geometry);

expect(geometry.attributes.uv.count).toBe(1);
expect(geometry.attributes.uv.array.length).toBe(2);
});

it("should handle geometry with many vertices", () => {
const geometry = new BufferGeometry();
const vertexCount = 1000;
const positions = new Float32BufferAttribute(new Float32Array(vertexCount * 3), 3);
geometry.setAttribute("position", positions);

ensureUV(geometry);

expect(geometry.attributes.uv.count).toBe(vertexCount);
expect(geometry.attributes.uv.array.length).toBe(vertexCount * 2);
});
});
17 changes: 17 additions & 0 deletions src/ensureUV.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BufferAttribute, type BufferGeometry } from "three";

// Function to add UVs if missing
export function ensureUV(geometry: BufferGeometry) {
if (!geometry.attributes.uv) {
const count = geometry.attributes.position.count;
const uvs = new Float32Array(count * 2);

// Assign default UVs (you can adjust logic as needed)
for (let i = 0; i < count; i++) {
uvs[i * 2] = 0; // u-coordinate
uvs[i * 2 + 1] = 0; // v-coordinate
}

geometry.setAttribute("uv", new BufferAttribute(uvs, 2));
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ensureUV } from "./ensureUV";
export { applyOffset, processGeometry } from "./offset";
export type { InitialObject, VertexUsageInfo } from "./types";
export { createMeshFromObject } from "./utils/createMeshFromObject";
export { createOffsetMesh } from "./utils/offsetObjectHash";
Loading