<script src="https://rawgit.com/mrdoob/three.js/dev/build/three.js"></script>
<script src="https://rawgit.com/mrdoob/three.js/dev/examples/js/controls/OrbitControls.js"></script>
<!--
A ray works at world space coordinates. You cannot add a ray inside another mesh, although you can indirectly if you create a setup for maintaing and updating the ray.
When you transform your shape in any way, note that until the render kicks in, the matrix of those objects are not yet truly transformed. This may play a critical role in when you should perofrm a ray cast. Either before or after the transoforms and keep it consistent. Performing raycast before the render kicks in, requires you call updateMatrixWorld on the object to truly update its matrix.
-->
var scene = new THREE.Scene();
var width = window.innerWidth;
// don't use the full window height
// allow bottom space room for UI button
var height = window.innerHeight * .75;
// renderer and canvas size setup
var renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
renderer.setClearColor( 0xcccccc, 1 );
document.body.appendChild( renderer.domElement );
// perspective camera - toggle only one on
camera = new THREE.PerspectiveCamera(45, window.innerWidth / height, 1, 10000);
camera.position.set(0, 0, 200);
// ortho camera
// camera = new THREE.OrthographicCamera(-width/2, width/2, height/2, -height/2, 0, 1200);
orthoOrbit = new THREE.OrbitControls(camera, renderer.domElement);
orthoOrbit.screenSpacePanning = true;
scene.add(camera);
// create the scene
faceNormalMesh = new THREE.Mesh();
scene.add(faceNormalMesh);
shapeMesh = createScene();
divHitStatusBox = null;
isWireframe = false;
createUI();
// global raycaster refs
raySphere = null;
raySphereSize = 1;
rayLine = null;
rayLength = 10;
raycaster = new THREE.Raycaster();
var start = new THREE.Vector3(0,0,5);
rayDir = new THREE.Vector3(0,0,-1);
raycaster.set(start, rayDir);
drawRay();
castRay();
function castRay(){
var intersections = raycaster.intersectObject(shapeMesh);
if (intersections.length){
divHitStatusBox.innerHTML = 'hit';
} else {
divHitStatusBox.innerHTML = 'miss';
}
}
function drawRay(){
// clean out old meshes
if (rayLine) { scene.remove(rayLine); }
var ray = raycaster.ray;
createSphereAtPoint(ray.origin, raySphereSize);
var lineGeo = new THREE.Geometry();
var end = ray.origin.clone();
var shift = ray.direction.clone();
shift.setLength(rayLength);
end.add(shift);
lineGeo.vertices.push(ray.origin.clone(), end);
rayLine = new THREE.Line(lineGeo, new THREE.LineBasicMaterial({color:0x0000ff}));
scene.add(rayLine);
}
function createLine(start, end){
var geo = new THREE.Geometry();
geo.vertices.push(start, end);
var line =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.