sgenoud / replicad

The library to build browser based 3D models with code.

Home Page:https://replicad.xyz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Missing UV mapping on the geometry

maelp opened this issue · comments

commented

When I do this

const geometries = syncGeometries([replicadMesh], []);
const geometry = geometries[0].faces;

and use the geometry: BufferGeometry, it seems to miss the uv field in geometry.attributes

Do you know if there's an easy way to obtain those?

commented

Okay I fixed it using this

const updateUVMap = (geometry: THREE.BufferGeometry) => {
geometry.computeBoundingBox();
const max = geometry.boundingBox.max;
const min = geometry.boundingBox.min;
const offset = new THREE.Vector2(0 - min.x, 0 - min.y);
const range = new THREE.Vector2(max.x - min.x, max.y - min.y);
const positions = geometry.attributes.position.array;
const uvs: number[] = [];

for (let i = 0; i < positions.length; i += 3) {
    const x = positions[i];
    const y = positions[i + 1];
    uvs.push((x + offset.x) / range.x, (y + offset.y) / range.y);
}

  geometry.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(uvs), 2));
geometry.uvsNeedUpdate = true;
};

I am not 100% sure I understand how UV and replicad should interact (because I don't really understand them). Is there something you think you be done to improve the situation?

commented

Not exactly sure how it should work, but the snippet above fixed my issue

It looks like this issue is related: #62 (in case you feel like digging deeper).