webgl_collisions_normal.html 4.6 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

<html> 
 
<head> 
<title>three.js webgl - intersection: ray/mesh readinf normal</title> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"> 
<style type="text/css"> 

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 type="text/javascript" src="../build/Three.js"></script> 
<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
<script type="text/javascript" src="js/Stats.js"></script>

<script type="text/javascript"> 

M
Mr.doob 已提交
48 49 50
var camera, scene, projector, renderer,
info, mouse = { x: 0, y: 0 }, sun, loader, stats, line;

M
Mr.doob 已提交
51 52 53 54 55
var meshes = [];

var theta = 0;
var camdist = 500;

M
Mr.doob 已提交
56
var totalFaces = 0, totalColliders = 0;
M
Mr.doob 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

function init() {

	container = document.createElement( 'div' );
	document.body.appendChild( container );
	
	info = document.getElementById("info");
	
	camera = new THREE.Camera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
	camera.position.z = camdist;
	
	loader = new THREE.JSONLoader(  );

	scene = new THREE.Scene();

M
Mr.doob 已提交
72 73
	projector = new THREE.Projector();

M
Mr.doob 已提交
74 75 76 77 78
	renderer = new THREE.WebGLRenderer();
	renderer.setSize( window.innerWidth, window.innerHeight );
	container.appendChild( renderer.domElement );
	
	var ambientLight = new THREE.AmbientLight( 0x606060 );
79
	scene.add( ambientLight );
M
Mr.doob 已提交
80 81 82

	sun = new THREE.DirectionalLight( 0xffffff );
	sun.position = camera.position.clone();
83
	scene.add( sun );
M
Mr.doob 已提交
84 85 86 87 88 89 90 91 92 93
	
	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);
94
	scene.add( line );
M
Mr.doob 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

	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;

130
	scene.add( mesh );
M
Mr.doob 已提交
131 132 133 134 135 136 137 138 139
	var mc = THREE.CollisionUtils.MeshColliderWBox(mesh);
	THREE.Collisions.colliders.push( mc );
	meshes.push( mesh );

};

function onDocumentMouseMove( event ) {

	event.preventDefault();	
M
Mr.doob 已提交
140 141
	mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
	mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
M
Mr.doob 已提交
142 143 144 145 146 147 148 149

};


function animate() {

	requestAnimationFrame( animate );

M
Mr.doob 已提交
150 151 152 153
	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() );
M
Mr.doob 已提交
154
	
M
Mr.doob 已提交
155
	if ( meshes.length == 0 ) return;
M
Mr.doob 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	
	var i, l = meshes.length;
	
	for ( i = 0; i < l; i++ ) {
		meshes[ i ].materials[ 0 ].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.materials[ 0 ].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) ;

	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>