bones-browser.html 6.6 KB
Newer Older
G
Greg Tatum 已提交
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
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Three.js Bones Browser</title>
		<style>
			@font-face {
				font-family: 'inconsolata';
				src: url('../files/inconsolata.woff') format('woff');
				font-weight: normal;
				font-style: normal;
			}
			
			body {
				margin:0;
				font-family: 'inconsolata';
				font-size: 15px;
				line-height: 18px;
				overflow: hidden;
			}
			
			canvas { width: 100%; height: 100% }
			
			#newWindow {
				display: block;
				position: absolute;
				bottom: 0.3em;
				left: 0.5em;
				color: #fff;
			}
		</style>
	</head>
	<body>
		
		<a id='newWindow' href='./bones-browser.html' target='_blank'>Open in New Window</a>
		
		<script src="../../build/three.min.js"></script>
		<script src='../../examples/js/libs/dat.gui.min.js'></script>
		<script src="../../examples/js/controls/OrbitControls.js"></script>
		
		<script>
G
Greg Tatum 已提交
42

G
Greg Tatum 已提交
43
			var gui, scene, camera, renderer, orbit, ambientLight, lights, mesh, bones, skeletonHelper;
G
Greg Tatum 已提交
44

G
Greg Tatum 已提交
45
			var state = {
G
Greg Tatum 已提交
46

G
Greg Tatum 已提交
47
				animateBones : false
G
Greg Tatum 已提交
48

G
Greg Tatum 已提交
49
			};
G
Greg Tatum 已提交
50

G
Greg Tatum 已提交
51
			function initScene () {
G
Greg Tatum 已提交
52

G
Greg Tatum 已提交
53 54
				gui = new dat.GUI();
				scene = new THREE.Scene();
G
Greg Tatum 已提交
55
				camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 200 );
G
Greg Tatum 已提交
56 57
				camera.position.z = 30;
				camera.position.y = 30;
G
Greg Tatum 已提交
58 59

				renderer = new THREE.WebGLRenderer( { antialias: true } );
G
Greg Tatum 已提交
60 61
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );
G
Greg Tatum 已提交
62

G
Greg Tatum 已提交
63 64 65 66 67 68 69
				orbit = new THREE.OrbitControls( camera, renderer.domElement );
				orbit.noZoom = true;

				ambientLight = new THREE.AmbientLight( 0x000000 );
				scene.add( ambientLight );

				lights = [];
G
Greg Tatum 已提交
70 71 72 73 74 75 76 77 78 79 80
				lights[ 0 ] = new THREE.PointLight( 0xffffff, 1, 0 );
				lights[ 1 ] = new THREE.PointLight( 0xffffff, 1, 0 );
				lights[ 2 ] = new THREE.PointLight( 0xffffff, 1, 0 );

				lights[ 0 ].position.set( 0, 200, 0 );
				lights[ 1 ].position.set( 100, 200, 100 );
				lights[ 2 ].position.set( -100, -200, -100 );

				scene.add( lights[ 0 ] );
				scene.add( lights[ 1 ] );
				scene.add( lights[ 2 ] );
G
Greg Tatum 已提交
81 82

				window.addEventListener( 'resize', function () {
G
Greg Tatum 已提交
83

G
Greg Tatum 已提交
84 85 86 87
					camera.aspect = window.innerWidth / window.innerHeight;
					camera.updateProjectionMatrix();

					renderer.setSize( window.innerWidth, window.innerHeight );
G
Greg Tatum 已提交
88

G
Greg Tatum 已提交
89
				}, false );
G
Greg Tatum 已提交
90

G
Greg Tatum 已提交
91 92
				initBones();
				setupDatGui();
G
Greg Tatum 已提交
93

G
Greg Tatum 已提交
94 95 96
			}

			function createGeometry ( sizing ) {
G
Greg Tatum 已提交
97

G
Greg Tatum 已提交
98 99 100 101 102 103 104 105
				var geometry = new THREE.CylinderGeometry(
					5,                       // radiusTop
					5,                       // radiusBottom
					sizing.height,           // height
					8,                       // radiusSegments
					sizing.segmentCount * 3, // heightSegments
					true                     // openEnded
				);
G
Greg Tatum 已提交
106 107 108 109 110 111

				for ( var i = 0; i < geometry.vertices.length; i ++ ) {

					var vertex = geometry.vertices[ i ];
					var y = ( vertex.y + sizing.halfHeight );

G
Greg Tatum 已提交
112
					var skinIndex = Math.floor( y / sizing.segmentHeight );
G
Greg Tatum 已提交
113 114 115 116 117
					var skinWeight = ( y % sizing.segmentHeight ) / sizing.segmentHeight;

					geometry.skinIndices.push( new THREE.Vector4( skinIndex, skinIndex + 1, 0, 0 ) );
					geometry.skinWeights.push( new THREE.Vector4( 1 - skinWeight, skinWeight, 0, 0 ) );

G
Greg Tatum 已提交
118
				}
G
Greg Tatum 已提交
119

G
Greg Tatum 已提交
120
				return geometry;
G
Greg Tatum 已提交
121

G
Greg Tatum 已提交
122
			};
G
Greg Tatum 已提交
123

G
Greg Tatum 已提交
124
			function createBones ( sizing ) {
G
Greg Tatum 已提交
125

G
Greg Tatum 已提交
126
				bones = [];
G
Greg Tatum 已提交
127

G
Greg Tatum 已提交
128 129 130
				var prevBone = new THREE.Bone();
				bones.push( prevBone );
				prevBone.position.y = -sizing.halfHeight;
G
Greg Tatum 已提交
131 132 133

				for ( var i = 0; i < sizing.segmentCount; i ++ ) {

G
Greg Tatum 已提交
134 135 136 137 138
					var bone = new THREE.Bone();
					bone.position.y = sizing.segmentHeight;
					bones.push( bone );
					prevBone.add( bone );
					prevBone = bone;
G
Greg Tatum 已提交
139

G
Greg Tatum 已提交
140
				}
G
Greg Tatum 已提交
141

G
Greg Tatum 已提交
142
				return bones;
G
Greg Tatum 已提交
143

G
Greg Tatum 已提交
144 145 146
			};

			function createMesh ( geometry, bones ) {
G
Greg Tatum 已提交
147 148

				var material = new THREE.MeshPhongMaterial( {
G
Greg Tatum 已提交
149 150 151 152 153
					skinning : true,
					color: 0x156289,
					emissive: 0x072534,
					side: THREE.DoubleSide,
					shading: THREE.FlatShading
G
Greg Tatum 已提交
154 155
				} );

G
Greg Tatum 已提交
156
				var mesh = new THREE.SkinnedMesh( geometry,	material );
G
Greg Tatum 已提交
157
				var skeleton = new THREE.Skeleton( bones );
G
Greg Tatum 已提交
158 159 160
	
				mesh.add( bones[ 0 ] );
	
G
Greg Tatum 已提交
161
				mesh.bind( skeleton );
G
Greg Tatum 已提交
162

G
Greg Tatum 已提交
163 164 165
				skeletonHelper = new THREE.SkeletonHelper( mesh );
				skeletonHelper.material.linewidth = 2;
				scene.add( skeletonHelper );
G
Greg Tatum 已提交
166

G
Greg Tatum 已提交
167
				return mesh;
G
Greg Tatum 已提交
168
	
G
Greg Tatum 已提交
169
			};
G
Greg Tatum 已提交
170

G
Greg Tatum 已提交
171
			function setupDatGui () {
G
Greg Tatum 已提交
172
	
G
Greg Tatum 已提交
173
				var folder = gui.addFolder( "General Options" );
G
Greg Tatum 已提交
174
	
G
Greg Tatum 已提交
175
				folder.add( state, "animateBones" );
G
Greg Tatum 已提交
176
				folder.__controllers[ 0 ].name( "Animate Bones" );
G
Greg Tatum 已提交
177 178

				folder.add( mesh, "pose" );
G
Greg Tatum 已提交
179 180
				folder.__controllers[ 1 ].name( ".pose()" );
	
G
Greg Tatum 已提交
181
				var bones = mesh.skeleton.bones;
G
Greg Tatum 已提交
182 183 184 185 186
	
				for ( var i = 0; i < bones.length; i ++ ) {
		
					var bone = bones[ i ];
		
G
Greg Tatum 已提交
187
					folder = gui.addFolder( "Bone " + i );
G
Greg Tatum 已提交
188
	
G
Greg Tatum 已提交
189 190 191 192 193 194 195
					folder.add( bone.position, 'x', -10 + bone.position.x, 10 + bone.position.x );
					folder.add( bone.position, 'y', -10 + bone.position.y, 10 + bone.position.y );
					folder.add( bone.position, 'z', -10 + bone.position.z, 10 + bone.position.z );

					folder.add( bone.rotation, 'x', -Math.PI * 0.5, Math.PI * 0.5 );
					folder.add( bone.rotation, 'y', -Math.PI * 0.5, Math.PI * 0.5 );
					folder.add( bone.rotation, 'z', -Math.PI * 0.5, Math.PI * 0.5 );
G
Greg Tatum 已提交
196
		
G
Greg Tatum 已提交
197 198 199 200
					folder.add( bone.scale, 'x', 0, 2 );
					folder.add( bone.scale, 'y', 0, 2 );
					folder.add( bone.scale, 'z', 0, 2 );

G
Greg Tatum 已提交
201 202 203 204 205 206 207 208 209 210 211 212
					folder.__controllers[ 0 ].name( "position.x" );
					folder.__controllers[ 1 ].name( "position.y" );
					folder.__controllers[ 2 ].name( "position.z" );
		
					folder.__controllers[ 3 ].name( "rotation.x" );
					folder.__controllers[ 4 ].name( "rotation.y" );
					folder.__controllers[ 5 ].name( "rotation.z" );
		
					folder.__controllers[ 6 ].name( "scale.x" );
					folder.__controllers[ 7 ].name( "scale.y" );
					folder.__controllers[ 8 ].name( "scale.z" );
		
G
Greg Tatum 已提交
213
				}
G
Greg Tatum 已提交
214
				
G
Greg Tatum 已提交
215 216 217
			}

			function initBones () {
G
Greg Tatum 已提交
218
	
G
Greg Tatum 已提交
219 220
				var segmentHeight = 8;
				var segmentCount = 4;
G
Greg Tatum 已提交
221 222
				var height = segmentHeight * segmentCount;
				var halfHeight = height * 0.5;
G
Greg Tatum 已提交
223

G
Greg Tatum 已提交
224 225 226 227 228 229
				var sizing = {
					segmentHeight : segmentHeight,
					segmentCount : segmentCount,
					height : height,
					halfHeight : halfHeight
				};
G
Greg Tatum 已提交
230

G
Greg Tatum 已提交
231 232 233
				var geometry = createGeometry( sizing );
				var bones = createBones( sizing );
				mesh = createMesh( geometry, bones );
G
Greg Tatum 已提交
234

G
Greg Tatum 已提交
235 236
				mesh.scale.multiplyScalar( 1 );
				scene.add( mesh );
G
Greg Tatum 已提交
237

G
Greg Tatum 已提交
238
			};
G
Greg Tatum 已提交
239

G
Greg Tatum 已提交
240
			function render () {
G
Greg Tatum 已提交
241
	
G
Greg Tatum 已提交
242 243 244
				requestAnimationFrame( render );

				var time = Date.now() * 0.001;
G
Greg Tatum 已提交
245
	
G
Greg Tatum 已提交
246 247
				var bone = mesh;

G
Greg Tatum 已提交
248
	
G
Greg Tatum 已提交
249
				//Wiggle the bones
G
Greg Tatum 已提交
250 251 252 253 254 255
				if ( state.animateBones ) {
		
					for ( var i = 0; i < mesh.skeleton.bones.length; i ++ ) {
		
						mesh.skeleton.bones[ i ].rotation.z = Math.sin( time ) * 2 / mesh.skeleton.bones.length;
		
G
Greg Tatum 已提交
256
					}
G
Greg Tatum 已提交
257
		
G
Greg Tatum 已提交
258 259 260
				}

				skeletonHelper.update();
G
Greg Tatum 已提交
261
	
G
Greg Tatum 已提交
262
				renderer.render( scene, camera );
G
Greg Tatum 已提交
263
	
G
Greg Tatum 已提交
264
			};
G
Greg Tatum 已提交
265

G
Greg Tatum 已提交
266 267 268 269 270 271
			initScene();
			render();
			
		</script>
	</body>
</html>