Hi all,
I’m still working on my globe asset, long may the journey be but I’m sure it’ll be worth it.
With my previous version, thanks to the insight of the community, the issue of a too-high poly count was recognised. I’m trying to rayCast to select the current object hovered under the mouse, however the high poly count is breaking the rayCast. The issue is, this is for my landing page so I need the resolution so I was recommended three-mesh-bvh. I’ve been trying to get this library to work for the past three days to no avail, I’ve gone through the examples and tried making it work in my project with no luck.
I’m sure its something stupid I’m missing, however in new to this so I’m just totally unaware.
Heres the code, if anyone can point to my mistakes it would be greatly appreciated.
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import {
computeBoundsTree,
disposeBoundsTree,
acceleratedRaycast,
} from "three-mesh-bvh";
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
THREE.Mesh.prototype.raycast = acceleratedRaycast;
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var pointer, raycaster;
pointer = new THREE.Vector2();
raycaster = new THREE.Raycaster();
pointer.x = 1;
pointer.y = 1;
const scene = new THREE.Scene();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
1,
10000
);
const controls = new OrbitControls(camera, renderer.domElement);
camera.position.set(0, 400, 1000);
controls.update();
renderer.render(scene, camera);
window.addEventListener("pointermove", onPointerMove);
/* lighting */
const pointLight = new THREE.PointLight(0xffffff);
pointLight.position.set(-500, 500, 1000);
scene.add(pointLight);
//
//
// main
//
//
/* world */
const worldTexture = new THREE.TextureLoader().load("./assets/world-map.jpg");
const worldGeometry = new THREE.SphereGeometry(100, 64, 32);
const world = new THREE.Mesh(
worldGeometry,
new THREE.MeshStandardMaterial({
map: worldTexture,
// wireframe: true,
})
);
scene.add(world);
/* ellipse const */
const ellipseMaterial = new THREE.LineBasicMaterial({ color: 0x000000 });
/* ellipse two */
const curveTwo = new THREE.EllipseCurve(
0,
0, // ax, aY
150,
150, // xRadius, yRadius
0,
2 * Math.PI, // aStartAngle, aEndAngle
false, // aClockwise
0 // aRotation
);
const pointsTwo = curveTwo.getPoints(100);
const geometryTwo = new THREE.BufferGeometry().setFromPoints(pointsTwo);
// Create the final object to add to the scene
const ellipseTwo = new THREE.Line(geometryTwo, ellipseMaterial);
ellipseTwo.rotation.x = Math.PI * 0.5;
ellipseTwo.rotation.z = Math.PI * 1.5;
ellipseTwo.rotation.y = Math.PI * 0.05;
scene.add(ellipseTwo);
// ellipseTwo.computeBoundsTree();
/* comet */
const cometGeometry = new THREE.SphereGeometry(10, 10, 5);
const cometTwo = new THREE.Mesh(
cometGeometry,
new THREE.MeshStandardMaterial({
wireframe: true,
})
);
scene.add(cometTwo);
/* ellipse three */
const curveThree = new THREE.EllipseCurve(
50,
0, // ax, aY
250,
150, // xRadius, yRadius
0,
2 * Math.PI, // aStartAngle, aEndAngle
false, // aClockwise
0 // aRotation
);
const pointsThree = curveThree.getPoints(100);
const geometryThree = new THREE.BufferGeometry().setFromPoints(pointsThree);
// Create the final object to add to the scene
const ellipseThree = new THREE.Line(geometryThree, ellipseMaterial);
ellipseThree.rotation.x = Math.PI * 0.6;
ellipseThree.rotation.y = Math.PI * 0.15;
scene.add(ellipseThree);
// ellipseThree.computeBoundsTree();
/* comet */
const cometThree = new THREE.Mesh(
cometGeometry,
new THREE.MeshStandardMaterial({
wireframe: true,
})
);
cometThree.position.x = 200;
scene.add(cometThree);
// cometThree.computeBoundsTree();
/* clock */
let clock = new THREE.Clock();
let v = new THREE.Vector3();
let w = new THREE.Vector3();
/* compute bounds */
worldGeometry.computeBoundsTree();
cometGeometry.computeBoundsTree();
/* function */
console.log("scene children", scene.children);
animate();
function animate() {
requestAnimationFrame(animate);
let s = (clock.getElapsedTime() * 0.08) % 1;
let t = (clock.getElapsedTime() * 0.05) % 1;
curveTwo.getPointAt(s, v);
cometTwo.position.copy(v);
cometTwo.position.applyMatrix4(ellipseTwo.matrixWorld);
curveThree.getPointAt(t, w);
cometThree.position.copy(w);
cometThree.position.applyMatrix4(ellipseThree.matrixWorld);
hoverElement();
world.rotation.y += -0.001;
renderer.render(scene, camera);
}
function onPointerMove(event) {
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
function hoverElement() {
raycaster.setFromCamera(pointer, camera);
// raycaster.raycaster.firstHitOnly = true;
const intersects = raycaster.intersectObjects(scene.children);
if (intersects.length > 0) {
console.log("current intersects", intersects);
}
}
As you can see the code runs. However, I’m running into the same issues as before highlighted here, where rayCaster is not registering the hits due to the slow speed. So, I have a feeling I’m just not running it correctly, since some of the projects made with this wonderful library are way more advanced.
Here’s the link to the repo if its easier.
Once again Thank you!!