diff --git a/lib/gltf/geometry.ts b/lib/gltf/geometry.ts index 91df983..e0f821b 100644 --- a/lib/gltf/geometry.ts +++ b/lib/gltf/geometry.ts @@ -418,19 +418,23 @@ export function transformMesh( } // Apply translation - result.positions[i] = x + translation.x - result.positions[i + 1] = y + translation.y - result.positions[i + 2] = z + translation.z + x += translation.x + y += translation.y + z += translation.z + + result.positions[i] = -x + result.positions[i + 1] = y + result.positions[i + 2] = z } - // Also transform normals if there was rotation - if (rotation) { - for (let i = 0; i < result.normals.length; i += 3) { - let nx = result.normals[i]! - let ny = result.normals[i + 1]! - let nz = result.normals[i + 2]! + // Transform normals with rotation and GLTF orientation fix + for (let i = 0; i < result.normals.length; i += 3) { + let nx = result.normals[i]! + let ny = result.normals[i + 1]! + let nz = result.normals[i + 2]! - // Apply same rotations to normals + // Apply same rotations to normals if there was rotation + if (rotation) { // Rotation around Y axis const cosY = Math.cos(rotation.y) const sinY = Math.sin(rotation.y) @@ -454,11 +458,17 @@ export function transformMesh( const rny2 = nx * sinZ + ny * cosZ nx = rnx2 ny = rny2 - - result.normals[i] = nx - result.normals[i + 1] = ny - result.normals[i + 2] = nz } + + result.normals[i] = -nx + result.normals[i + 1] = ny + result.normals[i + 2] = nz + } + + for (let i = 0; i < result.indices.length; i += 3) { + const temp = result.indices[i + 1]! + result.indices[i + 1] = result.indices[i + 2]! + result.indices[i + 2] = temp } return result diff --git a/tests/features/glb-models/__snapshots__/glb-models01.snap.png b/tests/features/glb-models/__snapshots__/glb-models01.snap.png index f390b7e..da31057 100644 Binary files a/tests/features/glb-models/__snapshots__/glb-models01.snap.png and b/tests/features/glb-models/__snapshots__/glb-models01.snap.png differ diff --git a/tests/features/models-from-footprint-strings/__snapshots__/footprint-string-models.snap.png b/tests/features/models-from-footprint-strings/__snapshots__/footprint-string-models.snap.png index f390b7e..da31057 100644 Binary files a/tests/features/models-from-footprint-strings/__snapshots__/footprint-string-models.snap.png and b/tests/features/models-from-footprint-strings/__snapshots__/footprint-string-models.snap.png differ diff --git a/tests/snapshot/__snapshots__/board-center-alignment-3d-view.snap.png b/tests/snapshot/__snapshots__/board-center-alignment-3d-view.snap.png index a869b08..ad8bf51 100644 Binary files a/tests/snapshot/__snapshots__/board-center-alignment-3d-view.snap.png and b/tests/snapshot/__snapshots__/board-center-alignment-3d-view.snap.png differ diff --git a/tests/snapshot/__snapshots__/led-texture-coordinates.snap.png b/tests/snapshot/__snapshots__/led-texture-coordinates.snap.png new file mode 100644 index 0000000..ecd32e4 Binary files /dev/null and b/tests/snapshot/__snapshots__/led-texture-coordinates.snap.png differ diff --git a/tests/snapshot/__snapshots__/silkscreen-text.snap.png b/tests/snapshot/__snapshots__/silkscreen-text.snap.png index 415000d..379cc87 100644 Binary files a/tests/snapshot/__snapshots__/silkscreen-text.snap.png and b/tests/snapshot/__snapshots__/silkscreen-text.snap.png differ diff --git a/tests/snapshot/__snapshots__/simple-circuit.snap.png b/tests/snapshot/__snapshots__/simple-circuit.snap.png index 324df91..c6540fa 100644 Binary files a/tests/snapshot/__snapshots__/simple-circuit.snap.png and b/tests/snapshot/__snapshots__/simple-circuit.snap.png differ diff --git a/tests/snapshot/__snapshots__/usb-c-flashlight.snap.png b/tests/snapshot/__snapshots__/usb-c-flashlight.snap.png index f1ecc21..02d594a 100644 Binary files a/tests/snapshot/__snapshots__/usb-c-flashlight.snap.png and b/tests/snapshot/__snapshots__/usb-c-flashlight.snap.png differ diff --git a/tests/snapshot/jlcpcb-cad-model/__snapshots__/jlcpcb-cad-model.snap.png b/tests/snapshot/jlcpcb-cad-model/__snapshots__/jlcpcb-cad-model.snap.png index 17ddd77..70f6f41 100644 Binary files a/tests/snapshot/jlcpcb-cad-model/__snapshots__/jlcpcb-cad-model.snap.png and b/tests/snapshot/jlcpcb-cad-model/__snapshots__/jlcpcb-cad-model.snap.png differ diff --git a/tests/snapshot/led-texture-coordinates.test.tsx b/tests/snapshot/led-texture-coordinates.test.tsx new file mode 100644 index 0000000..0577f16 --- /dev/null +++ b/tests/snapshot/led-texture-coordinates.test.tsx @@ -0,0 +1,30 @@ +import { Circuit } from "tscircuit" +import { test, expect } from "bun:test" +import { convertCircuitJsonToGltf } from "../../lib" +import { getBestCameraPosition } from "../../lib/utils/camera-position" +import { renderGLTFToPNGBufferFromGLBBuffer } from "poppygl" + +test("led-texture-coordinates-snapshot", async () => { + const circuit = new Circuit() + circuit.add( + + + + + , + ) + + const circuitJson = await circuit.getCircuitJson() + + const glb = await convertCircuitJsonToGltf(circuitJson, { + format: "glb", + }) + + const cameraOptions = getBestCameraPosition(circuitJson) + cameraOptions.camPos = [6, 14, 8] + cameraOptions.lookAt = [0, 0, 1] + + expect( + await renderGLTFToPNGBufferFromGLBBuffer(glb as ArrayBuffer, cameraOptions), + ).toMatchPngSnapshot(import.meta.path) +})