Hi Guys,
I am new in three.js and i’am trying to animate one of the children inside my gltf (ring) i’m just wanna to rotate in the x axis just like the sphere behind but i can’t get it to work i hope you can help me please
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
* {
margin: 0;
padding: 0;
border: none;
overflow-x: hidden;
}
</style>
<title>Logo Test</title>
</head>
<body>
<canvas class="webgl"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.js"
integrity="sha512-NLtnLBS9Q2w7GKK9rKxdtgL7rA7CAS85uC/0xd9im4J/yOL4F9ZVlv634NAM7run8hz3wI2GabaA6vv8vJtHiQ=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://82mou.github.io/threejs/js/OrbitControls.js"></script>
<script src="https://cdn.jsdelivr.net/gh/mrdoob/three.js@r92/examples/js/loaders/GLTFLoader.js"></script>
<script>
// Canvas
const canvas = document.querySelector('canvas.webgl')
// Scene
const scene = new THREE.Scene()
const geometry1 = new THREE.IcosahedronGeometry(0.3, 1,);
var obj;
// Objects
const material = new THREE.MeshStandardMaterial({
flatShading: true,
})
material.color = new THREE.Color(0xFFFFFF)
material.metalness = 0.7
material.roughness = 1
//Mesh
const sphere = new THREE.Mesh(geometry1, material)
sphere.position.x = 0;
sphere.scale.set(3, 3, 3)
sphere.position.y = 0;
sphere.position.z = -1.3;
scene.add(sphere)
const loader = new THREE.GLTFLoader();
loader.load('https://uploads-ssl.webflow.com/6061f5bfe4da464153703a20/61483d302c2017c3a89b6725_gym_final.gltf.txt', function (gltf) {
console.log(gltf.scene.children[0].name)
gltf.scene.scale.set(0.5, 0.5, 0.5)
gltf.scene.position.set(0, 0, 0)
gltf.scene.rotation.set(0, 0, 0)
scene.add(gltf.scene);
});
//Frontlight
const pointLight1 = new THREE.PointLight(0xff0015, 1)
pointLight1.position.set(0, 0, 2)
scene.add(pointLight1)
//Rightlight
const pointLight2 = new THREE.PointLight(0xff0015, 0.5)
pointLight2.position.set(2, 0, 0)
scene.add(pointLight2)
//Backlight
const pointLight3 = new THREE.PointLight(0x4A6CFE, 0.5)
pointLight3.position.set(0, 0, -1)
//pointLight3.intensity = 0.5
scene.add(pointLight3)
//Leftlight
const pointLight4 = new THREE.PointLight(0xFFFFFF, 0.5)
pointLight4.position.set(-2, 0, 0)
scene.add(pointLight4)
const ambientLight = new THREE.AmbientLight(0xffffff, 1.3);
scene.add(ambientLight);
//Sizes
const sizes = {
width: window.innerWidth,
height: window.innerHeight
}
window.addEventListener('resize', () => {
// Update sizes
sizes.width = window.innerWidth
sizes.height = window.innerHeight
// Update camera
camera.aspect = sizes.width / sizes.height
camera.updateProjectionMatrix()
// Update renderer
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
})
// Base camera
const camera = new THREE.PerspectiveCamera(50, sizes.width / sizes.height, 0.1, 100)
camera.position.x = 0
camera.position.y = 0
camera.position.z = 2.6
scene.add(camera)
// Controls
const controls = new THREE.OrbitControls(camera, canvas)
controls.enableDamping = true
controls.screenSpacePaning = false;
controls.minDistance = 0.2;
controls.maxDistance = 1.9;
controls.maxPolarAngle = Math.PI / 2;
//Renderer
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
alpha: true,
antialias: true
})
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
const windowX = window.innerWidth / 2;
const windowY = window.innerHeight / 2;
const clock = new THREE.Clock()
const tick = () => {
const elapsedTime = clock.getElapsedTime()
// Update Orbital Controls
controls.update()
// Update objects
sphere.rotation.x -= 0.006;
//gltf.scene.children[0].rotation.x -= 0.006
// Render
renderer.render(scene, camera)
// Call tick again on the next frames
window.requestAnimationFrame(tick)
camera.lookAt(scene.position);
}
tick()
</script>
</body>