webgl_loader_sea3d_bvh.html 7.2 KB
Newer Older
1 2 3
<!DOCTYPE html>
<html lang="en">
	<head>
S
sunag 已提交
4
		<title>three.js webgl - sea3d / bvh</title>
5 6
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
M
Mr.doob 已提交
7
		<link type="text/css" rel="stylesheet" href="main.css">
8 9 10
	</head>
	<body>
		<div id="info">
M
Mr.doob 已提交
11 12
			<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Exported by <a href="https://github.com/sunag/sea3d" target="_blank" rel="noopener">SEA3D Exporter</a>. Asset by <a href="http://www.turbosquid.com/3d-models/soccer-player-max-free/307330" target="_blank" rel="noopener">Trivision</a><br/>
			Runtime convertion of BVH Animation to SEA3D Skeleton Animation
13 14
		</div>

S
sunag 已提交
15 16
		<script type="module">

M
Mr.doob 已提交
17
			import * as THREE from '../build/three.module.js';
S
sunag 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

			import { OrbitControls } from './jsm/controls/OrbitControls.js';

			import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
			import { RenderPass } from './jsm/postprocessing/RenderPass.js';
			import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
			import { CopyShader } from './jsm/shaders/CopyShader.js';
			import { ColorCorrectionShader } from './jsm/shaders/ColorCorrectionShader.js';
			import { VignetteShader } from './jsm/shaders/VignetteShader.js';

			import { BVHLoader } from './jsm/loaders/BVHLoader.js';

			import { SEA3D } from './jsm/loaders/sea3d/SEA3DLoader.js';
			import './jsm/loaders/sea3d/SEA3DLZMA.js'; // sea3d lzma extension

			import { SkeletonUtils } from './jsm/utils/SkeletonUtils.js';

			import Stats from './jsm/libs/stats.module.js';
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

			console.log( "Visit https://github.com/sunag/sea3d to all codes and builds under development." );

			var container, stats;

			var camera, scene, renderer, composer, player, hat;

			var loader;

			var bvhSkeletonHelper, bvhMixer;

			// Initialize Three.JS

			init();

			//
			// SEA3D Loader
			//

S
sunag 已提交
55
			loader = new SEA3D( {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

				autoPlay: true, // Auto play animations
				container: scene, // Container to add models
				multiplier: .6 // Light multiplier

			} );

			loader.onComplete = function ( ) {

				// Get the first camera from SEA3D Studio
				// use loader.get... to get others objects

				var cam = loader.cameras[ 0 ];
				camera.position.copy( cam.position );
				camera.rotation.copy( cam.rotation );

S
sunag 已提交
72
				new OrbitControls( camera, renderer.domElement );
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

				// get meshes
				player = loader.getMesh( "Player" );
				hat = loader.getMesh( "Hat" );

				hat.visible = false;

				loadBVH();

				animate();

			};

			loader.load( './models/sea3d/skin.tjs.sea' );

			//

			function bvhToSEA3D( result ) {

92
				var options = {
93
					useFirstFramePosition: true,
94 95
					preserveHipPosition: false,
					hip: "hip",
S
sunag 已提交
96
					// left is SEA3D bone names and right BVH bone names
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
					names: {
						"Base HumanPelvis": "hip",
						"Base HumanSpine3": "abdomen",
						"Base HumanRibcage": "chest",
						"Base HumanHead": "head",

						"Base HumanRUpperarm": "rShldr",
						"Base HumanRForearm1": "rForeArm",
						"Base HumanRPalm": "rHand",

						"Base HumanLUpperarm": "lShldr",
						"Base HumanLForearm1": "lForeArm",
						"Base HumanLPalm": "lHand",

						"Base HumanRThigh": "rThigh",
						"Base HumanRCalf1": "rShin",
						"Base HumanRFoot": "rFoot",

						"Base HumanLThigh": "lThigh",
						"Base HumanLCalf1": "lShin",
						"Base HumanLFoot": "lFoot"
					},
S
sunag 已提交
119

120
				};
S
sunag 已提交
121

122
				// Automatic offset: get offsets when it is in T-Pose
S
sunag 已提交
123
				options.offsets = SkeletonUtils.getSkeletonOffsets( player, bvhSkeletonHelper, options );
S
sunag 已提交
124

125
				/*
S
sunag 已提交
126 127
				// Manual offsets: compensates the difference in skeletons ( T-Pose )
				options.offsets = {
S
sunag 已提交
128 129
					"lShldr": new Matrix4().makeRotationFromEuler(
						new Euler(
S
sunag 已提交
130
							0,
S
sunag 已提交
131 132
							Math.degToRad( - 45 ),
							Math.degToRad( - 80 )
S
sunag 已提交
133 134
						)
					),
S
sunag 已提交
135 136
					"rShldr": new Matrix4().makeRotationFromEuler(
						new Euler(
S
sunag 已提交
137
							0,
S
sunag 已提交
138 139
							Math.degToRad( 45 ),
							Math.degToRad( 80 )
S
sunag 已提交
140 141
						)
					),
S
sunag 已提交
142 143
					"lFoot": new Matrix4().makeRotationFromEuler(
						new Euler(
S
sunag 已提交
144
							0,
S
sunag 已提交
145
							Math.degToRad( 15 ),
S
sunag 已提交
146 147 148
							0
						)
					),
S
sunag 已提交
149 150
					"rFoot": new Matrix4().makeRotationFromEuler(
						new Euler(
S
sunag 已提交
151
							0,
S
sunag 已提交
152
							Math.degToRad( 15 ),
S
sunag 已提交
153 154 155 156 157
							0
						)
					)
				};
				*/
S
sunag 已提交
158

S
sunag 已提交
159
				var clip = SkeletonUtils.retargetClip( player, result.skeleton, result.clip, options );
160

S
sunag 已提交
161 162
				clip.name = "dance";

S
sunag 已提交
163
				clip = SEA3D.AnimationClip.fromClip( clip );
164

S
sunag 已提交
165
				player.addAnimation( new SEA3D.Animation( clip ) );
166

S
sunag 已提交
167
				player.play( "dance" );
168 169 170 171 172 173


			}

			function loadBVH() {

S
sunag 已提交
174
				var loader = new BVHLoader();
175 176
				loader.load( "models/bvh/pirouette.bvh", function ( result ) {

M
Mr.doob 已提交
177 178
					bvhSkeletonHelper = new THREE.SkeletonHelper( result.skeleton.bones[ 0 ] );
					bvhSkeletonHelper.skeleton = result.skeleton; // allow animation mixer to bind to THREE.SkeletonHelper directly
179

M
Mr.doob 已提交
180
					var boneContainer = new THREE.Group();
181 182 183 184 185 186 187
					boneContainer.add( result.skeleton.bones[ 0 ] );
					boneContainer.position.y = - 100;

					scene.add( bvhSkeletonHelper );
					scene.add( boneContainer );

					// play animation
M
Mr.doob 已提交
188
					bvhMixer = new THREE.AnimationMixer( bvhSkeletonHelper );
189 190 191 192 193 194 195 196 197 198
					bvhMixer.clipAction( result.clip ).setEffectiveWeight( 1.0 ).play();

					bvhToSEA3D( result );

				} );

			}

			function init() {

M
Mr.doob 已提交
199 200
				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0x333333 );
201 202 203 204

				container = document.createElement( 'div' );
				document.body.appendChild( container );

M
Mr.doob 已提交
205
				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 20000 );
206 207
				camera.position.set( 1000, - 300, 1000 );

M
Mr.doob 已提交
208
				renderer = new THREE.WebGLRenderer();
209 210 211 212 213 214 215 216 217
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );

				stats = new Stats();
				container.appendChild( stats.dom );

				// post-processing

S
sunag 已提交
218
				composer = new EffectComposer( renderer );
219

S
sunag 已提交
220 221
				var renderPass = new RenderPass( scene, camera );
				var copyPass = new ShaderPass( CopyShader );
222 223 224 225
				composer.addPass( renderPass );

				var vh = 1.4, vl = 1.2;

S
sunag 已提交
226
				var colorCorrectionPass = new ShaderPass( ColorCorrectionShader );
M
Mr.doob 已提交
227 228
				colorCorrectionPass.uniforms[ "powRGB" ].value = new THREE.Vector3( vh, vh, vh );
				colorCorrectionPass.uniforms[ "mulRGB" ].value = new THREE.Vector3( vl, vl, vl );
229 230
				composer.addPass( colorCorrectionPass );

S
sunag 已提交
231
				var vignettePass = new ShaderPass( VignetteShader );
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
				vignettePass.uniforms[ "darkness" ].value = 1.0;
				composer.addPass( vignettePass );

				composer.addPass( copyPass );

				// events

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				composer.setSize( window.innerWidth, window.innerHeight );
				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

M
Mr.doob 已提交
255
			var clock = new THREE.Clock();
256 257 258 259 260 261 262 263

			function animate() {

				var delta = clock.getDelta();

				requestAnimationFrame( animate );

				// Update SEA3D Animations
S
sunag 已提交
264
				SEA3D.AnimationHandler.update( delta );
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

				if ( bvhMixer ) bvhMixer.update( delta );

				render( delta );

				stats.update();

			}

			function render( dlt ) {

				//renderer.render( scene, camera );
				composer.render( dlt );

			}

		</script>

	</body>
</html>