提交 6ad7cbb4 编写于 作者: M Mr.doob

Removed Collisions code.

No time right now to re-create the examples using Ray. Will do for r47...
上级 c99ff7f5
<!doctype html>
<html lang="en">
<head>
<title>three.js webgl - intersection: ray with box</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
#oldie { background-color: #ddd !important }
#info {
position: absolute;
top: 30px; left: 150px; width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index:100;
}
#options {
position: absolute;
top: 10px; left: 10px; width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index:100;
}
</style>
<script src="../build/Three.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script src="js/Stats.js"></script>
<script>
var camera, scene, projector, renderer,
info, mouse = { x: 0, y: 0 }, sun, cube;
var bounce = 0;
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
info = document.getElementById("info");
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = - 500;
scene = new THREE.Scene();
projector = new THREE.Projector();
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
var ambientLight = new THREE.AmbientLight( 0x606060 );
scene.add( ambientLight );
sun = new THREE.DirectionalLight( 0xffffff );
sun.position = camera.position.clone();
scene.add( sun );
createCube( 200, new THREE.Vector3( 0,0,0 ) );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
container.onmousemove = onDocumentMouseMove;
animate();
}
function createCube( s, p ) {
cube = new THREE.Mesh (
new THREE.CubeGeometry( s, s, s ),
new THREE.MeshLambertMaterial( { color: 0x003300 } )
);
cube.position = p;
scene.add( cube );
THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshOBB( cube ) );
};
function onDocumentMouseMove( event ) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
};
function animate() {
requestAnimationFrame( animate );
info.innerHTML = "";
var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
var c = THREE.Collisions.rayCastNearest( ray );
if ( c ) {
info.innerHTML += "Found @ distance " + c.distance.toFixed(2);
c.mesh.material.color.setHex( 0xaa0000 );
} else {
info.innerHTML += "No intersection";
cube.material.color.setHex( 0x003300 );
}
cube.rotation.x += 0.01;
cube.rotation.y += 0.02;
cube.position.x = Math.sin(bounce) * 100;
bounce += 0.01;
camera.lookAt( scene.position );
renderer.render( scene, camera );
stats.update();
};
function vts(v) {
if(!v) return "undefined<br>";
else return v.x + " , " + v.y + " , " + v.z + "<br>";
};
</script>
</head>
<body onload="init();">
<div id="info"></div>
<div id="options"></div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<title>three.js webgl - intersection: ray with mesh (through box)</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
#oldie { background-color: #ddd !important }
#info {
position: absolute;
top: 30px; left: 10px; width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index:100;
}
#options {
position: absolute;
top: 10px; left: 10px; width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index:100;
}
</style>
<script src="../build/Three.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script src="js/Stats.js"></script>
<script>
var camera, scene, projector, renderer,
info, mouse = { x: 0, y: 0 }, sun, loader, stats;
var meshes = [];
var theta = 0;
var camdist = 1500;
var totalFaces = 0, totalColliders = 0;
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
info = document.getElementById("info");
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = camdist;
loader = new THREE.JSONLoader();
scene = new THREE.Scene();
projector = new THREE.Projector();
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
var ambientLight = new THREE.AmbientLight( 0x606060 );
scene.add( ambientLight );
sun = new THREE.DirectionalLight( 0xffffff );
sun.position = camera.position.clone();
scene.add( sun );
loadCube();
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
container.onmousemove = onDocumentMouseMove;
animate();
}
function loadCube(p) {
var onGeometry = function( geometry ) {
var sx = 300;
var sy = 240;
var sz = 300;
addCube( new THREE.Vector3(-sx, 0, 0), geometry );
addCube( new THREE.Vector3( 0, 0, 0), geometry );
addCube( new THREE.Vector3( sx, 0, 0), geometry );
addCube( new THREE.Vector3(-sx, sy, 0), geometry );
addCube( new THREE.Vector3( 0, sy, 0), geometry );
addCube( new THREE.Vector3( sx, sy, 0), geometry );
addCube( new THREE.Vector3(-sx,-sy, 0), geometry );
addCube( new THREE.Vector3( 0, -sy, 0), geometry );
addCube( new THREE.Vector3( sx,-sy, 0), geometry );
addCube( new THREE.Vector3(-sx, 0, sz), geometry );
addCube( new THREE.Vector3( 0, 0, sz), geometry );
addCube( new THREE.Vector3( sx, 0, sz), geometry );
addCube( new THREE.Vector3(-sx, sy, sz), geometry );
addCube( new THREE.Vector3( 0, sy, sz), geometry );
addCube( new THREE.Vector3( sx, sy, sz), geometry );
addCube( new THREE.Vector3(-sx,-sy, sz), geometry );
addCube( new THREE.Vector3( 0, -sy, sz), geometry );
addCube( new THREE.Vector3( sx,-sy, sz), geometry );
addCube( new THREE.Vector3(-sx, 0, -sz), geometry );
addCube( new THREE.Vector3( 0, 0, -sz), geometry );
addCube( new THREE.Vector3( sx, 0, -sz), geometry );
addCube( new THREE.Vector3(-sx, sy, -sz), geometry );
addCube( new THREE.Vector3( 0, sy, -sz), geometry );
addCube( new THREE.Vector3( sx, sy, -sz), geometry );
addCube( new THREE.Vector3(-sx,-sy, -sz), geometry );
addCube( new THREE.Vector3( 0, -sy, -sz), geometry );
addCube( new THREE.Vector3( sx,-sy, -sz), geometry );
addCube( new THREE.Vector3(-sx, 0, -sz*2), geometry );
addCube( new THREE.Vector3( 0, 0, -sz*2), geometry );
addCube( new THREE.Vector3( sx, 0, -sz*2), geometry );
addCube( new THREE.Vector3(-sx, sy, -sz*2), geometry );
addCube( new THREE.Vector3( 0, sy, -sz*2), geometry );
addCube( new THREE.Vector3( sx, sy, -sz*2), geometry );
addCube( new THREE.Vector3(-sx,-sy, -sz*2), geometry );
addCube( new THREE.Vector3( 0, -sy, -sz*2), geometry );
addCube( new THREE.Vector3( sx,-sy, -sz*2), geometry );
};
loader.load( "obj/suzanne/suzanneHi.js", onGeometry );
}
function addCube( p, g) {
totalFaces += g.faces.length;
totalColliders++;
var mesh = new THREE.Mesh( g, new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
mesh.position = p;
scene.add( mesh );
var mc = THREE.CollisionUtils.MeshColliderWBox( mesh) ;
THREE.Collisions.colliders.push( mc );
meshes.push( mesh );
};
function onDocumentMouseMove( event ) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
};
function animate() {
requestAnimationFrame( animate );
if( meshes.length == 0 ) return;
var i, l = meshes.length;
for ( i = 0; i < l; i ++ ) {
meshes[ i ].material.color.setHex( 0x003300 );
}
var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
var c = THREE.Collisions.rayCastNearest( ray );
if( c ) {
//info.innerHTML += "Found @ distance " + c.distance;
c.mesh.material.color.setHex( 0xbb0000 );
} else {
//info.innerHTML += "No intersection";
}
camera.position.x = camdist * Math.cos( theta );
camera.position.z = camdist * Math.sin( theta );
camera.position.y = camdist/2 * Math.sin( theta * 2 );
camera.lookAt( scene.position );
sun.position.copy( camera.position );
sun.position.normalize();
theta += 0.005;
renderer.render( scene, camera );
stats.update();
};
function vts(v) {
if(!v) return "undefined<br>";
else return v.x + " , " + v.y + " , " + v.z + "<br>";
};
</script>
</head>
<body onload="init();">
<div id="info"></div>
<div id="options"></div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<title>three.js webgl - intersection: ray/mesh readinf normal</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
#oldie { background-color: #ddd !important }
#info {
position: absolute;
top: 30px; left: 10px; width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index:100;
}
#options {
position: absolute;
top: 10px; left: 10px; width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index:100;
}
</style>
<script src="../build/Three.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script src="js/Stats.js"></script>
<script>
var camera, scene, projector, renderer,
info, mouse = { x: 0, y: 0 }, sun, loader, stats, line;
var meshes = [];
var theta = 0;
var camdist = 500;
var totalFaces = 0, totalColliders = 0;
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
info = document.getElementById("info");
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = camdist;
loader = new THREE.JSONLoader( );
scene = new THREE.Scene();
projector = new THREE.Projector();
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
var ambientLight = new THREE.AmbientLight( 0x606060 );
scene.add( ambientLight );
sun = new THREE.DirectionalLight( 0xffffff );
sun.position = camera.position.clone();
scene.add( sun );
loadCube();
var lineMat = new THREE.LineBasicMaterial( { color: 0xff0000, opacity: 1, linewidth: 3 } );
var geom = new THREE.Geometry();
geom.vertices.push( new THREE.Vertex( new THREE.Vector3(-100, 0, 0) ) );
geom.vertices.push( new THREE.Vertex( new THREE.Vector3( 100, 0, 0) ) );
line = new THREE.Line(geom, lineMat);
scene.add( line );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
container.onmousemove = onDocumentMouseMove;
animate();
}
function loadCube( p ) {
var onGeometry = function( geometry ) {
var sx = 300;
var sy = 240;
var sz = 300;
addCube( new THREE.Vector3( 0, 0, 0 ), geometry );
};
loader.load( { model: "obj/suzanne/suzanneHi.js", callback: onGeometry } );
}
function addCube( p, g) {
totalFaces += g.faces.length;
totalColliders++;
var mesh = new THREE.Mesh( g, new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
mesh.position = p;
scene.add( mesh );
var mc = THREE.CollisionUtils.MeshColliderWBox( mesh );
THREE.Collisions.colliders.push( mc );
meshes.push( mesh );
};
function onDocumentMouseMove( event ) {
event.preventDefault();
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
};
function animate() {
requestAnimationFrame( animate );
var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
if ( meshes.length == 0 ) return;
var i, l = meshes.length;
for ( i = 0; i < l; i ++ ) {
meshes[ i ].material.color.setHex( 0x003300 );
}
info.innerHTML = "";
var c = THREE.Collisions.rayCastNearest( ray );
if( c ) {
info.innerHTML += "Found @ normal " + vts( c.normal );
var poi = ray.origin.clone().addSelf( ray.direction.clone().multiplyScalar(c.distance) );
line.geometry.vertices[0].position = poi;
line.geometry.vertices[1].position = poi.clone().addSelf(c.normal.multiplyScalar(100));
line.geometry.__dirtyVertices = true;
line.geometry.__dirtyElements = true;
c.mesh.material.color.setHex( 0xbb0000 );
} else {
info.innerHTML += "No intersection";
}
camera.position.x = camdist * Math.cos( theta );
camera.position.z = camdist * Math.sin( theta );
camera.position.y = camdist/2 * Math.sin( theta * 2 );
camera.lookAt( scene.position );
sun.position.copy( camera.position );
sun.position.normalize();
theta += 0.005;
renderer.render( scene, camera );
stats.update();
};
function vts(v) {
if(!v) return "undefined<br>";
else return v.x.toFixed(2) + " , " + v.y.toFixed(2) + " , " + v.z.toFixed(2) + "<br>";
};
</script>
</head>
<body onload="init();">
<div id="info"></div>
<div id="options"></div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<title>three.js webgl - intersection: ray with sphere/AABB/plane</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
#oldie {
background-color: #ddd !important
}
#info {
position: absolute;
top: 30px;
left: 150px;
width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index: 100;
}
#options {
position: absolute;
top: 10px;
left: 150px;
width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index: 100;
}
</style>
<script src="../build/Three.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script src="js/Stats.js"></script>
<script>
var camera, scene, projector, renderer,
info, mouse = { x: 0, y: 0 }, sun;
var theta = 0;
var camdist = 1500;
var geoms = [];
function init () {
container = document.createElement( 'div' );
document.body.appendChild( container );
info = document.getElementById( "info" );
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
scene = new THREE.Scene();
projector = new THREE.Projector();
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
var ambientLight = new THREE.AmbientLight( 0x606060 );
scene.add( ambientLight );
sun = new THREE.DirectionalLight( 0xffffff );
scene.add( sun );
makeWall(240);
makeWall(120);
makeWall(0);
makeWall(-120);
makeWall(-240);
plane = new THREE.Mesh( new THREE.PlaneGeometry( 30000, 30000, 10, 10 ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
plane.position.y = - 480;
plane.rotation.x = Math.PI / - 2;
scene.add( plane );
geoms.push( plane );
var cplane = new THREE.PlaneCollider( plane.position, new THREE.Vector3( 0, 1, 0 ) );
cplane.mesh = plane;
THREE.Collisions.colliders.push( cplane );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
container.onmousemove = onDocumentMouseMove;
animate();
}
function makeWall(z){
var mx = 120 * 2;
for ( var i = -mx; i <= mx; i += 120 ) {
for ( var j = -mx; j <= mx; j += 120 ) {
if ( Math.random() > 0.5 )
createCube( 100, new THREE.Vector3( j, i, z ) );
else
createSphere( 50, new THREE.Vector3( j, i, z ) );
}
}
}
function createCube( s, p ) {
var cube = new THREE.Mesh(new THREE.CubeGeometry( s, s, s ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
cube.position = p;
scene.add(cube);
geoms.push(cube);
THREE.Collisions.colliders.push(THREE.CollisionUtils.MeshAABB(cube, p));
}
function createSphere( rad, p ) {
var sphere = new THREE.Mesh( new THREE.SphereGeometry( rad, 10, 10 ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
sphere.position = p;
scene.add(sphere);
geoms.push(sphere);
var sc = new THREE.SphereCollider(p, rad);
sc.mesh = sphere;
THREE.Collisions.colliders.push(sc);
}
function onDocumentMouseMove(event){
event.preventDefault();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
function animate(){
requestAnimationFrame( animate );
var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
for ( var i = 0; i < geoms.length; i ++ ) {
geoms[ i ].material.color.setHex( 0x007700 );
}
if ( !document.getElementById( "nearest" ).checked ) {
// Raycast all
ts = new Date().getTime();
var cs = THREE.Collisions.rayCastAll( ray );
tt = new Date().getTime() - ts;
if ( cs.length > 0 ) {
info.innerHTML = cs.length + " colliders found in " + tt;
for ( var i = 0; i < cs.length; i ++ ) {
cs[ i ].mesh.material.color.setHex( 0xaa0000 );
}
} else {
info.innerHTML = "No intersection";
}
} else {
// Raycast nearest
ts = new Date().getTime();
var c = THREE.Collisions.rayCastNearest( ray );
tt = new Date().getTime() - ts;
if ( c ) {
info.innerHTML = "Found in " + tt + " @ distance " + c.distance;
c.mesh.material.color.setHex( 0xaa0000 );
} else {
info.innerHTML = "No intersection";
}
}
camera.position.x = camdist * Math.cos(theta);
camera.position.z = camdist * Math.sin(theta);
camera.position.y = camdist / 2 * Math.sin(theta * 2);
camera.lookAt( scene.position );
sun.position.copy( camera.position );
sun.position.normalize();
theta += 0.005;
renderer.render( scene, camera );
stats.update();
}
function vts(v){
if (!v)
return "undefined<br>";
else
return v.x + " , " + v.y + " , " + v.z + "<br>";
}
</script>
</head>
<body onload="init();">
<div id="info">
</div>
<div id="options">
<input type="checkbox" id="nearest" checked/> Nearest collider only
<br/>
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<title>three.js webgl - collision reaction</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
#oldie {
background-color: #ddd !important
}
#info {
position: absolute;
top: 30px;
left: 10px;
width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index: 100;
}
#options {
position: absolute;
top: 10px;
left: 10px;
width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index: 100;
}
</style>
<script src="../build/Three.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script>
var camera, scene, renderer, info, mouse2d, sun, loader, sphere, debugNormal;
var range = 400;
var speed = 1;
var sphereSize = 4;
var cubes = [];
function init(){
container = document.createElement('div');
document.body.appendChild(container);
info = document.getElementById("info");
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 300, 120, 0 );
mouse2d = new THREE.Vector3( 0, 0, 1 );
loader = new THREE.Loader(true);
scene = new THREE.Scene();
sphere = new THREE.Mesh( new THREE.SphereGeometry( sphereSize, 10, 10 ), new THREE.MeshLambertMaterial( { color: 0xff0000 } ) );
scene.add( sphere );
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
var ambientLight = new THREE.AmbientLight(0xdddddd);
scene.add(ambientLight);
sun = new THREE.DirectionalLight(0xffffff);
sun.position = new THREE.Vector3(1, -1, 1).normalize();
scene.add(sun);
createObstacles();
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vertex( new THREE.Vector3(0, 0, 0) ) );
geometry.vertices.push( new THREE.Vertex( new THREE.Vector3(10, 0, 0) ) );
debugNormal = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xff0000 } ) );
scene.add( debugNormal );
container.onmousemove = onDocumentMouseMove;
animate();
}
function createObstacles(){
createCube(100, 50, 10, new THREE.Vector3( 20, 0, 100), Math.PI / 4);
camera.target = createCube(100, 50, 10, new THREE.Vector3(-20, 0, 200), -Math.PI / 4).position;
createCube(100, 50, 10, new THREE.Vector3( 20, 0, 300), Math.PI / 4);
}
function createCube(sx, sy, sz, p, ry){
var cube = new THREE.Mesh( new THREE.CubeGeometry( sx, sy, sz ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
cube.position = p;
cube.rotation.y = ry;
scene.add(cube);
THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshOBB(cube) );
cubes.push(cube);
return cube;
}
function onDocumentMouseMove(event){
event.preventDefault();
mouse2d.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse2d.y = -(event.clientY / window.innerHeight) * 2 + 1;
mouse2d.z = 1;
}
function animate() {
requestAnimationFrame( animate );
var ray = new THREE.Ray( sphere.position, new THREE.Vector3( 0, 0, 1 ) );
//debugNormal.position = sphere.position;
var c = THREE.Collisions.rayCastNearest( ray );
if ( !c || c.distance > speed ) {
sphere.position.z += speed;
}
if( c && c.normal ) {
info.innerHTML = vts( c.normal );
var poi = ray.origin.clone().addSelf( ray.direction.clone().multiplyScalar( c.distance ) );
debugNormal.geometry.vertices[0].position = poi.clone().addSelf( new THREE.Vector3( 2, 2, 2 ) );
debugNormal.geometry.vertices[1].position = poi.clone().addSelf( c.normal.clone() );
}
if ( sphere.position.z > range ) sphere.position.set( 0, 0, 0);
camera.position.x = Math.cos(mouse2d.x * Math.PI) * 300;
camera.position.y = Math.cos(mouse2d.y * Math.PI) * 300;
camera.position.z = 200 + Math.sin(mouse2d.x * Math.PI) * 300 + Math.sin(mouse2d.y * Math.PI) * 300;
camera.lookAt( camera.target );
renderer.render( scene, camera );
}
function vts(v) {
if (!v)
return "undefined<br>";
else
return v.x + " , " + v.y + " , " + v.z + "<br>";
}
</script>
</head>
<body onload="init();">
<div id="info">
</div>
<div id="options">
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<title>three.js webgl - intersection: ray with terrain mesh (through box)</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
#oldie { background-color: #ddd !important }
#info {
position: absolute;
top: 30px; left: 10px; width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index:100;
}
#options {
position: absolute;
top: 10px; left: 10px; width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index:100;
}
</style>
<script src="../build/Three.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script>
var camera, scene, renderer,
info, mouse2d, sun, loader, sphere, ray;
var theta = 0, radius = 250, speed = 0.002, sphereSize = 4;
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
info = document.getElementById("info");
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.y = 120;
mouse2d = new THREE.Vector3( 0, 0, 1 );
loader = new THREE.JSONLoader();
scene = new THREE.Scene();
ray = new THREE.Ray();
ray.origin.y = 10000;
ray.direction = new THREE.Vector3(0, -1, 0);
sphere = new THREE.Mesh( new THREE.SphereGeometry( sphereSize, 10, 10 ), new THREE.MeshLambertMaterial( { color: 0xff0000 } ) );
scene.add(sphere);
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild(renderer.domElement);
var ambientLight = new THREE.AmbientLight( 0x444444 );
scene.add( ambientLight );
sun = new THREE.DirectionalLight( 0xaaaaaa );
sun.position = new THREE.Vector3(-1,1, -1).normalize();
scene.add( sun );
loadCube();
container.onmousemove = onDocumentMouseMove;
animate();
}
function loadCube(p) {
loader.load( "obj/terrain.js", function( geometry ) {
var mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: 0xf3e4b8 } ) );
scene.add( mesh );
THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshColliderWBox( mesh ) );
} );
}
function onDocumentMouseMove( event ) {
event.preventDefault();
mouse2d.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse2d.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
mouse2d.z = 1;
}
function animate() {
requestAnimationFrame( animate );
ray.origin.x = radius * Math.cos(theta);
ray.origin.z = radius * Math.sin(theta);
var c = THREE.Collisions.rayCastNearest(ray);
if(c) {
//info.innerHTML = "Found @ distance " + c.distance;
sphere.position = ray.origin.clone().subSelf( new THREE.Vector3(0, c.distance - sphereSize/2, 0) );
} else {
//info.innerHTML = "No intersection";
}
theta += speed;
camera.lookAt( sphere.position );
renderer.render( scene, camera );
}
function vts(v) {
if(!v) return "undefined<br>";
else return v.x + " , " + v.y + " , " + v.z + "<br>";
}
</script>
</head>
<body onload="init();">
<div id="info"></div>
<div id="options"></div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<title>three.js webgl - collision detection</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #f0f0f0;
margin: 0px;
overflow: hidden;
}
#oldie {
background-color: #ddd !important
}
#info {
position: absolute;
top: 30px;
left: 10px;
width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index: 100;
}
#options {
position: absolute;
top: 10px;
left: 10px;
width: 800px;
color: #000000;
padding: 5px;
font-family: Monospace;
font-size: 13px;
text-align: left;
z-index: 100;
}
</style>
<script src="../build/Three.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script>
var camera, cameraTarget, scene, renderer, info, mouse2d, sun, sphere;
var range = 400, speed = 1, sphereSize = 4;
var cubes = [];
function init() {
container = document.createElement('div');
document.body.appendChild(container);
info = document.getElementById("info");
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 300, 120, 0 );
mouse2d = new THREE.Vector3( 0, 0, 1 );
scene = new THREE.Scene();
sphere = new THREE.Mesh( new THREE.SphereGeometry( sphereSize, 10, 10 ), new THREE.MeshLambertMaterial( { color: 0xff0000 } ) );
scene.add( sphere );
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
var ambientLight = new THREE.AmbientLight( 0xdddddd );
scene.add( ambientLight );
sun = new THREE.DirectionalLight( 0xffffff );
sun.position.set( 1, -1, 1 ).normalize();
scene.add( sun );
createObstacles();
container.onmousemove = onDocumentMouseMove;
animate();
}
function createObstacles(){
createCube( 100, 50, 10, new THREE.Vector3(0, 0, 100) );
cameraTarget = createCube( 100, 50, 10, new THREE.Vector3( 0, 0, 200 )) ;
createCube(100, 50, 10, new THREE.Vector3(0, 0, 300));
}
function createCube( sx, sy, sz, p ) {
var cube = new THREE.Mesh( new THREE.CubeGeometry( sx, sy, sz ), new THREE.MeshLambertMaterial( { color: 0x003300 } ) );
cube.position = p;
scene.add( cube );
THREE.Collisions.colliders.push( THREE.CollisionUtils.MeshOBB( cube ) );
cubes.push( cube );
return cube;
}
function onDocumentMouseMove( event ){
event.preventDefault();
mouse2d.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse2d.y = -(event.clientY / window.innerHeight) * 2 + 1;
mouse2d.z = 1;
}
function animate() {
requestAnimationFrame( animate );
sphere.position.z += speed;
if( sphere.position.z > range ) sphere.position.z = 0;
for ( var i = 0; i < cubes.length; i++ ) {
cubes[ i ].material.color.setHex( 0x003300 );
}
var ray = new THREE.Ray( sphere.position, new THREE.Vector3( 0, 0, 1 ) );
var c = THREE.Collisions.rayCastNearest( ray );
if ( c && c.distance == -1 ) {
info.innerHTML = "Colliding!";
c.mesh.material.color.setHex( 0xff0000 );
} else if( c && c.distance >= 0 ) {
info.innerHTML = "Approaching @ " + c.distance;
} else {
info.innerHTML = "No collider in sight.";
}
camera.position.x = Math.cos(mouse2d.x * Math.PI) * 300;
camera.position.z = 200 + Math.sin(mouse2d.x * Math.PI) * 300;
camera.lookAt( cameraTarget.position );
renderer.render( scene, camera );
}
function vts(v){
if (!v)
return "undefined<br>";
else
return v.x + " , " + v.y + " , " + v.z + "<br>";
}
</script>
</head>
<body onload="init();">
<div id="info">
</div>
<div id="options">
</div>
</body>
</html>
/**
* @author bartek drozdz / http://everyday3d.com/
*/
THREE.CollisionUtils = {};
// @params m THREE.Mesh
// @returns CBox dynamic Object Bounding Box
THREE.CollisionUtils.MeshOBB = function( m ) {
m.geometry.computeBoundingBox();
var b = m.geometry.boundingBox;
var min = new THREE.Vector3( b.x[0], b.y[0], b.z[0] );
var max = new THREE.Vector3( b.x[1], b.y[1], b.z[1] );
var box = new THREE.BoxCollider( min, max );
box.mesh = m;
return box;
}
// @params m THREE.Mesh
// @returns CBox static Axis-Aligned Bounding Box
//
// The AABB is calculated based on current
// position of the object (assumes it won't move)
THREE.CollisionUtils.MeshAABB = function( m ) {
var box = THREE.CollisionUtils.MeshOBB( m );
box.min.addSelf( m.position );
box.max.addSelf( m.position );
box.dynamic = false;
return box;
};
// @params m THREE.Mesh
// @returns CMesh with aOOB attached (that speeds up intersection tests)
THREE.CollisionUtils.MeshColliderWBox = function( m ) {
var mc = new THREE.MeshCollider( m, THREE.CollisionUtils.MeshOBB( m ) );
return mc;
};
/**
* @author bartek drozdz / http://everyday3d.com/
*/
THREE.PlaneCollider = function( point, normal ) {
this.point = point;
this.normal = normal;
};
THREE.SphereCollider = function( center, radius ) {
this.center = center;
this.radius = radius;
this.radiusSq = radius * radius;
};
THREE.BoxCollider = function( min, max ) {
this.min = min;
this.max = max;
this.dynamic = true;
this.normal = new THREE.Vector3();
};
THREE.MeshCollider = function( mesh, box ) {
this.mesh = mesh;
this.box = box;
this.numFaces = this.mesh.geometry.faces.length;
this.normal = new THREE.Vector3();
};
THREE.CollisionSystem = function() {
this.collisionNormal = new THREE.Vector3();
this.colliders = [];
this.hits = [];
// console.log("Collision system init / 004");
};
THREE.Collisions = new THREE.CollisionSystem();
THREE.CollisionSystem.prototype.merge = function( collisionSystem ) {
Array.prototype.push.apply( this.colliders, collisionSystem.colliders );
Array.prototype.push.apply( this.hits, collisionSystem.hits );
};
THREE.CollisionSystem.prototype.rayCastAll = function( ray ) {
ray.direction.normalize();
this.hits.length = 0;
var i, l, d, collider,
ld = 0;
for ( i = 0, l = this.colliders.length; i < l; i++ ) {
collider = this.colliders[ i ];
d = this.rayCast( ray, collider );
if ( d < Number.MAX_VALUE ) {
collider.distance = d;
if ( d > ld )
this.hits.push( collider );
else
this.hits.unshift( collider );
ld = d;
}
}
return this.hits;
};
THREE.CollisionSystem.prototype.rayCastNearest = function( ray ) {
var cs = this.rayCastAll( ray );
if( cs.length == 0 ) return null;
var i = 0;
while( cs[ i ] instanceof THREE.MeshCollider ) {
var dist_index = this.rayMesh ( ray, cs[ i ] );
if( dist_index.dist < Number.MAX_VALUE ) {
cs[ i ].distance = dist_index.dist;
cs[ i ].faceIndex = dist_index.faceIndex;
break;
}
i++;
}
if ( i > cs.length ) return null;
return cs[ i ];
};
THREE.CollisionSystem.prototype.rayCast = function( ray, collider ) {
if ( collider instanceof THREE.PlaneCollider )
return this.rayPlane( ray, collider );
else if ( collider instanceof THREE.SphereCollider )
return this.raySphere( ray, collider );
else if ( collider instanceof THREE.BoxCollider )
return this.rayBox( ray, collider );
else if ( collider instanceof THREE.MeshCollider && collider.box )
return this.rayBox( ray, collider.box );
};
THREE.CollisionSystem.prototype.rayMesh = function( r, me ) {
var rt = this.makeRayLocal( r, me.mesh );
var d = Number.MAX_VALUE;
var nearestface;
for( var i = 0; i < me.numFaces; i++ ) {
var face = me.mesh.geometry.faces[i];
var p0 = me.mesh.geometry.vertices[ face.a ].position;
var p1 = me.mesh.geometry.vertices[ face.b ].position;
var p2 = me.mesh.geometry.vertices[ face.c ].position;
var p3 = face instanceof THREE.Face4 ? me.mesh.geometry.vertices[ face.d ].position : null;
if (face instanceof THREE.Face3) {
var nd = this.rayTriangle( rt, p0, p1, p2, d, this.collisionNormal, me.mesh );
if( nd < d ) {
d = nd;
nearestface = i;
me.normal.copy( this.collisionNormal );
me.normal.normalize();
}
}
else if (face instanceof THREE.Face4) {
var nd = this.rayTriangle( rt, p0, p1, p3, d, this.collisionNormal, me.mesh );
if( nd < d ) {
d = nd;
nearestface = i;
me.normal.copy( this.collisionNormal );
me.normal.normalize();
}
nd = this.rayTriangle( rt, p1, p2, p3, d, this.collisionNormal, me.mesh );
if( nd < d ) {
d = nd;
nearestface = i;
me.normal.copy( this.collisionNormal );
me.normal.normalize();
}
}
}
return {dist: d, faceIndex: nearestface};
};
THREE.CollisionSystem.prototype.rayTriangle = function( ray, p0, p1, p2, mind, n, mesh ) {
var e1 = THREE.CollisionSystem.__v1,
e2 = THREE.CollisionSystem.__v2;
n.set( 0, 0, 0 );
// do not crash on quads, fail instead
e1.sub( p1, p0 );
e2.sub( p2, p1 );
n.cross( e1, e2 )
var dot = n.dot( ray.direction );
if ( !( dot < 0 ) ) {
if ( mesh.doubleSided || mesh.flipSided ) {
n.multiplyScalar (-1.0);
dot *= -1.0;
} else {
return Number.MAX_VALUE;
}
}
var d = n.dot( p0 );
var t = d - n.dot( ray.origin );
if ( !( t <= 0 ) ) return Number.MAX_VALUE;
if ( !( t >= dot * mind ) ) return Number.MAX_VALUE;
t = t / dot;
var p = THREE.CollisionSystem.__v3;
p.copy( ray.direction );
p.multiplyScalar( t );
p.addSelf( ray.origin );
var u0, u1, u2, v0, v1, v2;
if ( Math.abs( n.x ) > Math.abs( n.y ) ) {
if ( Math.abs( n.x ) > Math.abs( n.z ) ) {
u0 = p.y - p0.y;
u1 = p1.y - p0.y;
u2 = p2.y - p0.y;
v0 = p.z - p0.z;
v1 = p1.z - p0.z;
v2 = p2.z - p0.z;
} else {
u0 = p.x - p0.x;
u1 = p1.x - p0.x;
u2 = p2.x - p0.x;
v0 = p.y - p0.y;
v1 = p1.y - p0.y;
v2 = p2.y - p0.y;
}
} else {
if( Math.abs( n.y ) > Math.abs( n.z ) ) {
u0 = p.x - p0.x;
u1 = p1.x - p0.x;
u2 = p2.x - p0.x;
v0 = p.z - p0.z;
v1 = p1.z - p0.z;
v2 = p2.z - p0.z;
} else {
u0 = p.x - p0.x;
u1 = p1.x - p0.x;
u2 = p2.x - p0.x;
v0 = p.y - p0.y;
v1 = p1.y - p0.y;
v2 = p2.y - p0.y;
}
}
var temp = u1 * v2 - v1 * u2;
if( !(temp != 0) ) return Number.MAX_VALUE;
//console.log("temp: " + temp);
temp = 1 / temp;
var alpha = ( u0 * v2 - v0 * u2 ) * temp;
if( !(alpha >= 0) ) return Number.MAX_VALUE;
//console.log("alpha: " + alpha);
var beta = ( u1 * v0 - v1 * u0 ) * temp;
if( !(beta >= 0) ) return Number.MAX_VALUE;
//console.log("beta: " + beta);
var gamma = 1 - alpha - beta;
if( !(gamma >= 0) ) return Number.MAX_VALUE;
//console.log("gamma: " + gamma);
return t;
};
THREE.CollisionSystem.prototype.makeRayLocal = function( ray, m ) {
var mt = THREE.CollisionSystem.__m;
mt.getInverse( m.matrixWorld );
var rt = THREE.CollisionSystem.__r;
rt.origin.copy( ray.origin );
rt.direction.copy( ray.direction );
mt.multiplyVector3( rt.origin );
mt.rotateAxis( rt.direction );
rt.direction.normalize();
//m.localRay = rt;
return rt;
};
THREE.CollisionSystem.prototype.rayBox = function( ray, ab ) {
var rt;
if ( ab.dynamic && ab.mesh && ab.mesh.matrixWorld ) {
rt = this.makeRayLocal( ray, ab.mesh );
} else {
rt = THREE.CollisionSystem.__r;
rt.origin.copy( ray.origin );
rt.direction.copy( ray.direction );
}
var xt = 0, yt = 0, zt = 0;
var xn = 0, yn = 0, zn = 0;
var ins = true;
if( rt.origin.x < ab.min.x ) {
xt = ab.min.x - rt.origin.x;
//if(xt > ray.direction.x) return return Number.MAX_VALUE;
xt /= rt.direction.x;
ins = false;
xn = -1;
} else if( rt.origin.x > ab.max.x ) {
xt = ab.max.x - rt.origin.x;
//if(xt < ray.direction.x) return return Number.MAX_VALUE;
xt /= rt.direction.x;
ins = false;
xn = 1;
}
if( rt.origin.y < ab.min.y ) {
yt = ab.min.y - rt.origin.y;
//if(yt > ray.direction.y) return return Number.MAX_VALUE;
yt /= rt.direction.y;
ins = false;
yn = -1;
} else if( rt.origin.y > ab.max.y ) {
yt = ab.max.y - rt.origin.y;
//if(yt < ray.direction.y) return return Number.MAX_VALUE;
yt /= rt.direction.y;
ins = false;
yn = 1;
}
if( rt.origin.z < ab.min.z ) {
zt = ab.min.z - rt.origin.z;
//if(zt > ray.direction.z) return return Number.MAX_VALUE;
zt /= rt.direction.z;
ins = false;
zn = -1;
} else if( rt.origin.z > ab.max.z ) {
zt = ab.max.z - rt.origin.z;
//if(zt < ray.direction.z) return return Number.MAX_VALUE;
zt /= rt.direction.z;
ins = false;
zn = 1;
}
if( ins ) return -1;
var which = 0;
var t = xt;
if( yt > t ) {
which = 1;
t = yt;
}
if ( zt > t ) {
which = 2;
t = zt;
}
switch( which ) {
case 0:
var y = rt.origin.y + rt.direction.y * t;
if ( y < ab.min.y || y > ab.max.y ) return Number.MAX_VALUE;
var z = rt.origin.z + rt.direction.z * t;
if ( z < ab.min.z || z > ab.max.z ) return Number.MAX_VALUE;
ab.normal.set( xn, 0, 0 );
break;
case 1:
var x = rt.origin.x + rt.direction.x * t;
if ( x < ab.min.x || x > ab.max.x ) return Number.MAX_VALUE;
var z = rt.origin.z + rt.direction.z * t;
if ( z < ab.min.z || z > ab.max.z ) return Number.MAX_VALUE;
ab.normal.set( 0, yn, 0) ;
break;
case 2:
var x = rt.origin.x + rt.direction.x * t;
if ( x < ab.min.x || x > ab.max.x ) return Number.MAX_VALUE;
var y = rt.origin.y + rt.direction.y * t;
if ( y < ab.min.y || y > ab.max.y ) return Number.MAX_VALUE;
ab.normal.set( 0, 0, zn );
break;
}
return t;
};
THREE.CollisionSystem.prototype.rayPlane = function( r, p ) {
var t = r.direction.dot( p.normal );
var d = p.point.dot( p.normal );
var ds;
if( t < 0 ) ds = ( d - r.origin.dot( p.normal ) ) / t;
else return Number.MAX_VALUE;
if( ds > 0 ) return ds;
else return Number.MAX_VALUE;
};
THREE.CollisionSystem.prototype.raySphere = function( r, s ) {
var e = s.center.clone().subSelf( r.origin );
if ( e.lengthSq < s.radiusSq ) return -1;
var a = e.dot( r.direction.clone() );
if ( a <= 0 ) return Number.MAX_VALUE;
var t = s.radiusSq - ( e.lengthSq() - a * a );
if ( t >= 0 ) return Math.abs( a ) - Math.sqrt( t );
return Number.MAX_VALUE;
};
THREE.CollisionSystem.__v1 = new THREE.Vector3();
THREE.CollisionSystem.__v2 = new THREE.Vector3();
THREE.CollisionSystem.__v3 = new THREE.Vector3();
THREE.CollisionSystem.__nr = new THREE.Vector3();
THREE.CollisionSystem.__m = new THREE.Matrix4();
THREE.CollisionSystem.__r = new THREE.Ray();
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册