Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
Three.js - How to pre-position objects to merge?

I've successfully merged my cubes, although they seem to merge together reverting back to their original position/rotation, ignoring the declared positioning/rotation

var geometries = [];

cube1.position.set( 0, 0, 0 );
geometries.push( cube1 );

cube2.position.set( 1, 0, 0 );
geometries.push( cube2 );

cube3.position.set( 0, 1, 0 );
geometries.push( cube3 );

const cubes = BufferGeometryUtils.mergeBufferGeometries( geometries );

scene.add( cubes );//All the cubes get merged on 0, 0, 0 - overlapping one another

How do I position the cubes so they can be merged in their declared positions?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here is a working example of using BufferGeometryUtils and .applyMatrix4():

body{
  overflow: hidden;
  margin: 0;
}
<script type="module">
import * as THREE from "https://cdn.skypack.dev/three@0.136.0";
import {
  OrbitControls
} from "https://cdn.skypack.dev/three@0.136.0/examples/jsm/controls/OrbitControls.js";
import * as BufferGeometryUtils from "https://cdn.skypack.dev/three@0.136.0/examples/jsm/utils/BufferGeometryUtils";

let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 1, 1000);
camera.position.setScalar(1).setLength(6);
let renderer = new THREE.WebGLRenderer({
  antialias: true
});
renderer.setSize(innerWidth, innerHeight);
renderer.setClearColor(0x404040);
document.body.appendChild(renderer.domElement);

let controls = new OrbitControls(camera, renderer.domElement);

let light = new THREE.DirectionalLight(0xffffff, 1);
light.position.setScalar(1);
scene.add(light, new THREE.AmbientLight(0xffffff, 0.5));

let boxes = [
  makeBox(2, 0, 0, 0xff0000),
  makeBox(0, 2, 0, 0x00ff00),
  makeBox(0, 0, 2, 0x0000ff)
];
let materials = [];

let g = BufferGeometryUtils.mergeBufferGeometries(boxes.map(b => {
  materials.push(b.material);
  b.updateMatrixWorld(); // needs to be done to be sure that all transformations are applied
  return b.geometry.applyMatrix4(b.matrixWorld);
}), true);

let o = new THREE.Mesh(g, materials);
scene.add(o);

window.addEventListener("resize", onWindowResize);

animate();

function animate() {

  requestAnimationFrame(animate);
  renderer.render(scene, camera);

}

function onWindowResize() {

  camera.aspect = innerWidth / innerHeight;
  camera.updateProjectionMatrix();

  renderer.setSize(innerWidth, innerHeight);

}

function makeBox(x, y, z, color) {
  let g = new THREE.BoxGeometry();
  let m = new THREE.MeshLambertMaterial({
    color: color
  });
  let box = new THREE.Mesh(g, m);
  box.position.set(x, y, z);
  return box;
}

</script>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda