 # How merge geometry with different textures

Hi! I need merge geometry. Actually, I have geometry with only uv attributes and width uv and uv2.
I think merge geometry with only uv in first common-geometry and with uv and uv2 in second common-geometry.

but, how can I save tranformation and right textures?

I sample do this like this:

``````const reduceGLTF = (obj3d) => {
const group = new THREE.Group()
const geometries = [];
const geometries2 = [];
const materials = [];
const materials2 = [];

obj3d.traverse(child => {

if(child instanceof THREE.Mesh)
{

// child.updateMatrix();

const geom = child.geometry.clone()

geom.applyMatrix4( child.matrixWorld );

if(geom.attributes.hasOwnProperty('uv2')) {
geometries2.push(geom);
materials2.push(child.material);
} else {
geometries.push(geom);
materials.push(child.material);
}

} else if(!(child instanceof THREE.Group)) {
}

})

const mergedGeometry = BufferGeometryUtils.mergeBufferGeometries(geometries, true);
const mergedGeometry2 = BufferGeometryUtils.mergeBufferGeometries(geometries2, true);
mergedGeometry.groupsNeedUpdate = true;
mergedGeometry2.groupsNeedUpdate = true;

const mergedMesh = new THREE.Mesh(mergedGeometry, materials);
const mergedMesh2 = new THREE.Mesh(mergedGeometry2, materials2);

return group
}
``````

but I get wrong result: it wrong position and wrong position textures

You need to recalculate the combined UV, try this method.

``````    const max = mergeGeometry.boundingBox.max;
const min = mergeGeometry.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 uv = [];

for (let i = 0; i < mergeGeometry.attributes.position.array.length; i += 3) {

let u = (mergeGeometry.attributes.position.array[i] + offset.x) / range.x;

let v = (mergeGeometry.attributes.position.array[i + 1] + offset.y) / range.y

uv.push(u, v);

}

mergeGeometry.setAttribute("uv", new THREE.Float32BufferAttribute(uv, 2, true));
``````

I probably misunderstood you. But the position of the textures and geometry is still not correct. And with this method of combining uv textures are not visible at all

``````const mergUV = (mergeGeometry) => {
mergeGeometry.computeBoundingBox()

console.log(mergeGeometry)

const max = mergeGeometry.boundingBox.max;
const min = mergeGeometry.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 uv = [];

for (let i = 0; i < mergeGeometry.attributes.position.array.length; i += 3) {

let u = (mergeGeometry.attributes.position.array[i] + offset.x) / range.x;

let v = (mergeGeometry.attributes.position.array[i + 1] + offset.y) / range.y

uv.push(u, v);

}

mergeGeometry.setAttribute("uv", new THREE.Float32BufferAttribute(uv, 2, true));
}

const reduceGLTF = (obj3d) => {
const group = new THREE.Group()
const geometries = [];
const geometries2 = [];
const materials = [];
const materials2 = [];

obj3d.traverse(child => {

if(child instanceof THREE.Mesh)
{

child.updateMatrix();

const geom = child.geometry.clone()

geom.applyMatrix4( child.matrixWorld );

if(geom.attributes.hasOwnProperty('uv2')) {
geometries2.push(geom);
materials2.push(child.material);
} else {
geometries.push(geom);
materials.push(child.material);
}

} else if(!(child instanceof THREE.Group)) {
}

})

const mergedGeometry = BufferGeometryUtils.mergeBufferGeometries(geometries, true);

const mergedGeometry2 = BufferGeometryUtils.mergeBufferGeometries(geometries2, true);

mergedGeometry.groupsNeedUpdate = true;
mergedGeometry2.groupsNeedUpdate = true;

mergUV(mergedGeometry)
mergUV(mergedGeometry2)

const mergedMesh = new THREE.Mesh(mergedGeometry, materials);
const mergedMesh2 = new THREE.Mesh(mergedGeometry2, materials2);