// Create a Three.js scene
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x999999);
// Create a directional light
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(0, 20, 10);
scene.add(directionalLight);// Set light position
// Create an ambient light
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); // Soft white light
scene.add(ambientLight);
// Create a renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight); // Set renderer size
document.body.appendChild(renderer.domElement); // Append renderer to the document body
// Define camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 0, 50);
// Define camera controls
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Add damping for smoother movements
controls.dampingFactor = 0.25; // Adjust damping factor
controls.rotateSpeed = 0.5; // Adjust rotation speed
controls.zoomSpeed = 1.2; // Adjust zoom speed
controls.enableZoom = true;
// Load the parent GLB object
const parentLoader = new THREE.GLTFLoader();
parentLoader.load('scan.glb', parentGltf => {
const parentObject = parentGltf.scene;
// Adjust model scale
parentObject.scale.set(7, 7, 7); // Scale parent object down by a factor of 10
// Load the child GLB object
const childLoader = new THREE.GLTFLoader();
childLoader.load('stylized_chairs.glb', childGltf => {
const childObject = childGltf.scene;
childObject.scale.set(0.1, 0.1, 0.1);
// // Get the position of the parent object
// const parentPosition = new THREE.Vector3();
// parentObject.getWorldPosition(parentPosition);
// // Set the position of the child object relative to the parent
// childObject.position.sub(parentPosition);
// Get the parent object's position in world coordinates
const parentPosition = parentObject.position;
childObject.position.set(parentPosition.x , parentPosition.y, parentPosition.z);
parentObject.add(childObject);
// Traverse through all children of the parent object
parentObject.traverse(child => {
if (child.isMesh) {
// Apply a new material with the desired color
child.material = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.5, metalness: 0.1 }); // Red color
}
});
// Add the parent object to the scene
scene.add(parentObject);
// Start the animation loop
animate();
});
});
// Define the animate function
function animate() {
requestAnimationFrame(animate); // Request the browser to call animate() again before the next repaint
controls.update(); // Update controls
renderer.render(scene, camera); // Render the scene
}
This is my app js file, if i try to increate childObject.position.set(parentPosition.x , parentPosition.y, parentPosition.z); x axis length is is getting out of parent model, how can i achieve this, make child relative to parent.