mirror of
https://github.com/wahyd4/three.js.git
synced 2026-08-09 04:56:01 +10:00
Noticeable difference: can't anymore assign Material references to faces directly - "face.materials" became "face.materialIndex". Materials have to go to "geometry.materials" array (to which "face.materialIndex" points). This is necessary to allow run-time changing of face materials (this indirection used to be done by arrays). Some casualties: - multi-materials example, this functionality isn't possible anymore - had to simplify LOD Text examples as LODs don't handle groups (used to fake multi-materials) - multi-materials sphere in materials example can't have faces with undefined materials (this may be fixable I guess, when there'll be some real use-case) - scene format still has materials arrays, only the last material will be used (except when there are face materials, then materials from JSON will be used, as before) Other renderers and their corresponding examples were not touched yet. WebGLRenderer still needs some prettification, some things don't make sense anymore without arrays, code can be cleaned up further.
183 lines
4.0 KiB
HTML
183 lines
4.0 KiB
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>
|