2
0

webgl_loader_sea3d_bvh.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - sea3d / bvh</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <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/>
  12. Runtime convertion of BVH Animation to SEA3D Skeleton Animation
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  18. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  19. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  20. import { CopyShader } from './jsm/shaders/CopyShader.js';
  21. import { ColorCorrectionShader } from './jsm/shaders/ColorCorrectionShader.js';
  22. import { VignetteShader } from './jsm/shaders/VignetteShader.js';
  23. import { BVHLoader } from './jsm/loaders/BVHLoader.js';
  24. import { SEA3D } from './jsm/loaders/sea3d/SEA3DLoader.js';
  25. import './jsm/loaders/sea3d/SEA3DLZMA.js'; // sea3d lzma extension
  26. import { SkeletonUtils } from './jsm/utils/SkeletonUtils.js';
  27. import Stats from './jsm/libs/stats.module.js';
  28. console.log( "Visit https://github.com/sunag/sea3d to all codes and builds under development." );
  29. var container, stats;
  30. var camera, scene, renderer, composer, player, hat;
  31. var loader;
  32. var bvhSkeletonHelper, bvhMixer;
  33. // Initialize Three.JS
  34. init();
  35. //
  36. // SEA3D Loader
  37. //
  38. loader = new SEA3D( {
  39. autoPlay: true, // Auto play animations
  40. container: scene, // Container to add models
  41. multiplier: .6 // Light multiplier
  42. } );
  43. loader.onComplete = function ( ) {
  44. // Get the first camera from SEA3D Studio
  45. // use loader.get... to get others objects
  46. var cam = loader.cameras[ 0 ];
  47. camera.position.copy( cam.position );
  48. camera.rotation.copy( cam.rotation );
  49. new OrbitControls( camera, renderer.domElement );
  50. // get meshes
  51. player = loader.getMesh( "Player" );
  52. hat = loader.getMesh( "Hat" );
  53. hat.visible = false;
  54. loadBVH();
  55. animate();
  56. };
  57. loader.load( './models/sea3d/skin.tjs.sea' );
  58. //
  59. function bvhToSEA3D( result ) {
  60. var options = {
  61. useFirstFramePosition: true,
  62. preserveHipPosition: false,
  63. hip: "hip",
  64. // left is SEA3D bone names and right BVH bone names
  65. names: {
  66. "Base HumanPelvis": "hip",
  67. "Base HumanSpine3": "abdomen",
  68. "Base HumanRibcage": "chest",
  69. "Base HumanHead": "head",
  70. "Base HumanRUpperarm": "rShldr",
  71. "Base HumanRForearm1": "rForeArm",
  72. "Base HumanRPalm": "rHand",
  73. "Base HumanLUpperarm": "lShldr",
  74. "Base HumanLForearm1": "lForeArm",
  75. "Base HumanLPalm": "lHand",
  76. "Base HumanRThigh": "rThigh",
  77. "Base HumanRCalf1": "rShin",
  78. "Base HumanRFoot": "rFoot",
  79. "Base HumanLThigh": "lThigh",
  80. "Base HumanLCalf1": "lShin",
  81. "Base HumanLFoot": "lFoot"
  82. },
  83. };
  84. // Automatic offset: get offsets when it is in T-Pose
  85. options.offsets = SkeletonUtils.getSkeletonOffsets( player, bvhSkeletonHelper, options );
  86. /*
  87. // Manual offsets: compensates the difference in skeletons ( T-Pose )
  88. options.offsets = {
  89. "lShldr": new Matrix4().makeRotationFromEuler(
  90. new Euler(
  91. 0,
  92. Math.degToRad( - 45 ),
  93. Math.degToRad( - 80 )
  94. )
  95. ),
  96. "rShldr": new Matrix4().makeRotationFromEuler(
  97. new Euler(
  98. 0,
  99. Math.degToRad( 45 ),
  100. Math.degToRad( 80 )
  101. )
  102. ),
  103. "lFoot": new Matrix4().makeRotationFromEuler(
  104. new Euler(
  105. 0,
  106. Math.degToRad( 15 ),
  107. 0
  108. )
  109. ),
  110. "rFoot": new Matrix4().makeRotationFromEuler(
  111. new Euler(
  112. 0,
  113. Math.degToRad( 15 ),
  114. 0
  115. )
  116. )
  117. };
  118. */
  119. var clip = SkeletonUtils.retargetClip( player, result.skeleton, result.clip, options );
  120. clip.name = "dance";
  121. clip = SEA3D.AnimationClip.fromClip( clip );
  122. player.addAnimation( new SEA3D.Animation( clip ) );
  123. player.play( "dance" );
  124. }
  125. function loadBVH() {
  126. var loader = new BVHLoader();
  127. loader.load( "models/bvh/pirouette.bvh", function ( result ) {
  128. bvhSkeletonHelper = new THREE.SkeletonHelper( result.skeleton.bones[ 0 ] );
  129. bvhSkeletonHelper.skeleton = result.skeleton; // allow animation mixer to bind to THREE.SkeletonHelper directly
  130. var boneContainer = new THREE.Group();
  131. boneContainer.add( result.skeleton.bones[ 0 ] );
  132. boneContainer.position.y = - 100;
  133. scene.add( bvhSkeletonHelper );
  134. scene.add( boneContainer );
  135. // play animation
  136. bvhMixer = new THREE.AnimationMixer( bvhSkeletonHelper );
  137. bvhMixer.clipAction( result.clip ).setEffectiveWeight( 1.0 ).play();
  138. bvhToSEA3D( result );
  139. } );
  140. }
  141. function init() {
  142. scene = new THREE.Scene();
  143. scene.background = new THREE.Color( 0x333333 );
  144. container = document.createElement( 'div' );
  145. document.body.appendChild( container );
  146. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 20000 );
  147. camera.position.set( 1000, - 300, 1000 );
  148. renderer = new THREE.WebGLRenderer();
  149. renderer.setPixelRatio( window.devicePixelRatio );
  150. renderer.setSize( window.innerWidth, window.innerHeight );
  151. container.appendChild( renderer.domElement );
  152. stats = new Stats();
  153. container.appendChild( stats.dom );
  154. // post-processing
  155. composer = new EffectComposer( renderer );
  156. var renderPass = new RenderPass( scene, camera );
  157. var copyPass = new ShaderPass( CopyShader );
  158. composer.addPass( renderPass );
  159. var vh = 1.4, vl = 1.2;
  160. var colorCorrectionPass = new ShaderPass( ColorCorrectionShader );
  161. colorCorrectionPass.uniforms[ "powRGB" ].value = new THREE.Vector3( vh, vh, vh );
  162. colorCorrectionPass.uniforms[ "mulRGB" ].value = new THREE.Vector3( vl, vl, vl );
  163. composer.addPass( colorCorrectionPass );
  164. var vignettePass = new ShaderPass( VignetteShader );
  165. vignettePass.uniforms[ "darkness" ].value = 1.0;
  166. composer.addPass( vignettePass );
  167. composer.addPass( copyPass );
  168. // events
  169. window.addEventListener( 'resize', onWindowResize, false );
  170. }
  171. function onWindowResize() {
  172. camera.aspect = window.innerWidth / window.innerHeight;
  173. camera.updateProjectionMatrix();
  174. composer.setSize( window.innerWidth, window.innerHeight );
  175. renderer.setSize( window.innerWidth, window.innerHeight );
  176. }
  177. //
  178. var clock = new THREE.Clock();
  179. function animate() {
  180. var delta = clock.getDelta();
  181. requestAnimationFrame( animate );
  182. // Update SEA3D Animations
  183. SEA3D.AnimationHandler.update( delta );
  184. if ( bvhMixer ) bvhMixer.update( delta );
  185. render( delta );
  186. stats.update();
  187. }
  188. function render( dlt ) {
  189. //renderer.render( scene, camera );
  190. composer.render( dlt );
  191. }
  192. </script>
  193. </body>
  194. </html>