My current project involves generating a world filled with blocks on the ground upon mouse click (using mouse click coordinates that I've calculated). To ensure that each new block doesn't intersect with existing ones, I implemented a check using THREE.Raycaster. Essentially, I cast rays from the center of the block in all directions towards its vertices.
var ln = preview.geometry.vertices.length;
//for each vertices we throw a ray
for(var i = 0; i < ln; i++){
var pr_vertex = preview.geometry.vertices[i].clone();
var gl_vertex = pr_vertex.applyMatrix4(preview.matrix);
var dr_vector = gl_vertex.sub(preview.position);
var ray = new THREE.Raycaster(preview.position, dr_vector.clone().normalize());
var intersects = ray.intersectObjects(objmanager.getAllObject(), true);
//if there is intersection
if(intersects.length > 0 && intersects[0].distance < dr_vector.length()){
//hide preview material
preview.material.opacity = 0;
//exit checking
break;
}
}
The object being referred to as "preview" is crucial for this functionality and the code works flawlessly. However, I encountered an issue with Raycaster.intersectObjects only detecting intersections with the front face of the tested object. When the ray started from inside the object, it failed to recognize collisions with the surface. This led me to believe that intersectObjects only functions with the front surface of objects. Is this assumption correct?
In an attempt to address this limitation, I explored using THREE.DoubleSide to create double-sided objects. Unfortunately, this resulted in a shader initialization error:
Could not initialise shader
VALIDATE_STATUS: false, gl error [0] three.js:25254
buildProgram three.js:25254
initMaterial three.js:23760
setProgram three.js:23830
renderBuffer three.js:22371
renderObjects three.js:23061
render three.js:22935
animLoop
I am currently utilizing THREE.MeshLambertMaterial but switching to MeshBasicMaterial rendered Raycaster.intersectObjects ineffective altogether.
This roadblock has left me seeking solutions. If anyone has any ideas on how to address this issue, I would be immensely grateful. Otherwise, I might have to resort to manually testing collisions against individual faces of objects, which could significantly impact performance.
Thank you in advance.
===============
Edit: It's worth noting that I'm working with the latest version of three.js, estimated to be around r63.