I'm adding objects to a scene for each entry in a database. I had a cube appearing in the scene for entry, but when I tried to add raycasting to click on objects it doesnt work, the objects dont appear and the console reads "Expression unavailable".. I got parts of the code from the three.js website for Raycasting so not sure what I'm doing wrong.
Here is the JS code:
var renderer, scene, container, camera;
var geometry, material;
var controls, group;
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
init()
function onMouseMove( event ) {
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
function init() {
// init renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
// document.body.appendChild( renderer.domElement );
container = document.getElementById('container');
container.appendChild( renderer.domElement );
// init scene
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xffffff );
group = new THREE.Group();
scene.add( group )
//fetch data from database and add object for each entry
getData()
async function getData() {
var response = await fetch('/api/indexvr');
var data = await response.json();
console.log(data)
for (var i=0; i<data.length; i++) {
cube = new THREE.Mesh( geometry, material );
cube.position.x = i;
scene.add(cube);
//group.add(data)
}
}
// init camera
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 15, 15, 15 ); //camera.position.set( 5, 0, 10 );
camera.lookAt( scene.position );
// controls = new OrbitControls( camera, renderer.domElement );
// controls.enableRotate = true;
}
function render() {
// update the picking ray with the camera and mouse position
raycaster.setFromCamera( mouse, camera );
// calculate objects intersecting the picking ray
var intersects = raycaster.intersectObjects( scene.children );
for ( var i = 0; i < intersects.length; i++ ) {
intersects[ i ].object.material.color.set( 0xff0000 );
}
renderer.render( scene, camera );
}
window.addEventListener( 'mousemove', onMouseMove, false );
window.requestAnimationFrame(render);
The HTML just has a div called "container" and this tag:
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/108/three.js"></script>
It doesn't produce any error it only says this in the console:
So it is fetching the data but can't render the scene
var renderer, scene, container, camera;
var geometry, material;
var controls, group;
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
init()
function onMouseMove(event) {
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
function init() {
// init renderer
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
// document.body.appendChild( renderer.domElement );
container = document.getElementById('container');
container.appendChild(renderer.domElement);
// init scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0xffffff);
group = new THREE.Group();
scene.add(group)
//fetch data from database and add object for each entry
getData()
async function getData() {
/**
* @author TheJim01
* Replacing DB call with fake data to make it work here.
* Nancy: Please feel free to add appropriate data.
*/
// var response = await fetch('/api/indexvr');
// var data = await response.json();
var data = [{}, {}, {}, {}, {}]
console.log(data)
for (var i = 0; i < data.length; i++) {
cube = new THREE.Mesh(geometry, material);
cube.position.x = i;
scene.add(cube);
//group.add(data)
}
}
// init camera
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(15, 15, 15); //camera.position.set( 5, 0, 10 );
camera.lookAt(scene.position);
// controls = new OrbitControls( camera, renderer.domElement );
// controls.enableRotate = true;
}
function render() {
// update the picking ray with the camera and mouse position
raycaster.setFromCamera(mouse, camera);
// calculate objects intersecting the picking ray
var intersects = raycaster.intersectObjects(scene.children);
for (var i = 0; i < intersects.length; i++) {
intersects[i].object.material.color.set(0xff0000);
}
renderer.render(scene, camera);
}
window.addEventListener('mousemove', onMouseMove, false);
window.requestAnimationFrame(render);
<script src="//threejs.org/build/three.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/108/three.js"></script>
<script src="https://unpkg.com/spritejs/dist/spritejs.min.js"></script>
<div id="container"></div>
watch
enabled in your debugger? Also, just a note, You only need one<script>
reference to three.js--the one to threejs.org and the other to cdnjs.cloudflare.com currently point to what amounts to the same file. I recommend only using the one from cdnjs because eventually the threejs.org file will be replaced with the latest version. – Rhizome