I have stumped Bard with this one. Code runs but the drag controls aren’t working. Have tried a few different approaches. Here is the current status on the code:
import * as THREE from 'three';
import './style.css'
import { DragControls } from 'three/addons/controls/DragControls.js';
const scene = new THREE.Scene();
const geometry = new THREE.ConeGeometry( 5, 10, 64 )
const material = new THREE.MeshStandardMaterial( {
color: 0x800080,
roughness: 500,
} )
const cone = new THREE.Mesh(geometry, material )
scene.add( cone )
//create ring
const geometryring = new THREE.RingGeometry( 0, 7, 200 );
const materialring = new THREE.MeshBasicMaterial( {
color: 0x800080,
roughness: 500,
side: THREE.DoubleSide
} )
const ring = new THREE.Mesh( geometryring, materialring );
ring. position. set(0,-6,0)
ring.rotation.x += 86
scene.add( ring );
//join or tie the hat and ring together
const group = new THREE.Object3D();
group.add(cone);
group.add(ring);
group.visible = true;
scene.add(group);
const parentObject = new THREE.Object3D();
parentObject.add(cone);
parentObject.add(ring);
scene.add(parentObject);
const objects = [parentObject];
//Sizes
const sizes = {
width: window.innerWidth,
height: window.innerHeight,
};
const light = new THREE.DirectionalLight( 0xFFFFFF, 20 ); // soft white light
scene.add( light );
//weird ring
//camera
const camera = new THREE.PerspectiveCamera(
45,
sizes.width / sizes.height,
0.1,
100
)
camera.position.z = 20;
scene.add(camera)
//Renderer
const canvas = document.querySelector('.webgl');
const renderer = new THREE.WebGLRenderer({ canvas })
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(window.devicePixelRatio);
renderer.render(scene, camera)
//drag controls
const controls = new DragControls(objects, camera, renderer.domElement);
controls.enable();
controls.update();
controls.on('mousedown', event => {
const object = event.target;
object.children.forEach(child => {
child.material.emissive.set(0xaaaaaa);
});
startX = event.clientX;
startY = event.clientY;
controls.on(event);
});
controls.on('drag', function (event) {
if (event.target.contains(event.clientX, event.clientY) && event.target.visible) {
const object = event.target;
object.children.forEach(child => {
child.material.emissive.set(0xaaaaaa);
});
object.position.x += event.clientX - startX;
}
});
controls.on('dragend', function () {
startX = null;
startY = null;
const object = event.target;
object.children.forEach(child => {
child.material.emissive.set(0x000000);
});
});
// add event listener to highlight dragged objects
controls.on('dragend', function () {
const object = this.object;
object.children.forEach(child => {
child.material.emissive.set(0x000000);
});
});
//controls.enableDamping = true
//resize
window.addEventListener("resize", () => {
//update sizes
sizes.width = window.innerWidth;
sizes.height = window.innerHeight;
//update camera
camera.aspect = sizes.width / sizes.height;
camera.updateProjectionMatrix();
renderer.setSize(sizes.width, sizes.height);
});
const loop = () => {
controls.update();
renderer.render(scene, camera);
window.requestAnimationFrame(loop);
};
loop();