webgl_loader_sea3d_physics.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - sea3d / physics</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> - Ammo Physics Example<br/>
  12. Exported by <a href="https://github.com/sunag/sea3d" target="_blank" rel="noopener">SEA3D Exporter</a> edited by <a href="https://github.com/sunag/sea3d" target="_blank" rel="noopener">SEA3D Studio</a><br/>
  13. <div id="description">Right click to clone</div>
  14. </div>
  15. <script src="js/libs/ammo.js"></script>
  16. <script type="module">
  17. import {
  18. Vector3,
  19. Color,
  20. Clock,
  21. PerspectiveCamera,
  22. Scene,
  23. WebGLRenderer
  24. } from "../build/three.module.js";
  25. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  26. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  27. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  28. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  29. import { CopyShader } from './jsm/shaders/CopyShader.js';
  30. import { ColorCorrectionShader } from './jsm/shaders/ColorCorrectionShader.js';
  31. import { VignetteShader } from './jsm/shaders/VignetteShader.js';
  32. import { SEA3D } from './jsm/loaders/sea3d/SEA3DLoader.js';
  33. import { AMMO } from './jsm/loaders/sea3d/physics/SEA3DAmmoLoader.js'; // sea3d ammo.js extension
  34. import './jsm/loaders/sea3d/SEA3DLZMA.js'; // sea3d lzma extension
  35. import './jsm/loaders/sea3d/physics/SEA3DSDKRigidBody.js'; // sea3d physics extension
  36. import Stats from './jsm/libs/stats.module.js';
  37. console.log( "Visit https://github.com/sunag/sea3d to all codes and builds under development." );
  38. var container, stats;
  39. var camera, scene, renderer, composer;
  40. var loader;
  41. Ammo().then( function ( AmmoLib ) {
  42. // Initialize Three.JS
  43. init();
  44. // Initialize Physics Engine
  45. Ammo = AmmoLib;
  46. AMMO.init();
  47. //
  48. // SEA3D Loader
  49. //
  50. loader = new SEA3D( {
  51. container: scene // Container to add models
  52. } );
  53. loader.onComplete = function () {
  54. new OrbitControls( camera, renderer.domElement );
  55. // events
  56. window.addEventListener( 'contextmenu', function ( e ) {
  57. e.preventDefault();
  58. cloneAsset();
  59. } );
  60. // prevent material compilation in render loop
  61. renderer.compile( scene, camera );
  62. animate();
  63. };
  64. loader.load( './models/sea3d/car.tjs.sea' );
  65. var cloneAsset = function () {
  66. var offset = 0;
  67. return function () {
  68. var domain = loader.clone( { lights: false, runScripts: false, autoPlay: false, enabledPhysics: false } );
  69. offset -= 180;
  70. domain.container.position.x += offset;
  71. domain.applyContainerTransform();
  72. domain.enabledPhysics( true );
  73. domain.runScripts();
  74. scene.add( domain.container );
  75. };
  76. }();
  77. } );
  78. //
  79. function init() {
  80. scene = new Scene();
  81. scene.background = new Color( 0x333333 );
  82. container = document.createElement( 'div' );
  83. document.body.appendChild( container );
  84. camera = new PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 15000 );
  85. camera.position.set( 300, 200, - 300 );
  86. renderer = new WebGLRenderer();
  87. renderer.setPixelRatio( window.devicePixelRatio );
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. container.appendChild( renderer.domElement );
  90. stats = new Stats();
  91. container.appendChild( stats.dom );
  92. // post-processing
  93. composer = new EffectComposer( renderer );
  94. var renderPass = new RenderPass( scene, camera );
  95. var copyPass = new ShaderPass( CopyShader );
  96. composer.addPass( renderPass );
  97. var vh = 1.4, vl = 1.2;
  98. var colorCorrectionPass = new ShaderPass( ColorCorrectionShader );
  99. colorCorrectionPass.uniforms[ "powRGB" ].value = new Vector3( vh, vh, vh );
  100. colorCorrectionPass.uniforms[ "mulRGB" ].value = new Vector3( vl, vl, vl );
  101. composer.addPass( colorCorrectionPass );
  102. var vignettePass = new ShaderPass( VignetteShader );
  103. vignettePass.uniforms[ "darkness" ].value = 1.0;
  104. composer.addPass( vignettePass );
  105. composer.addPass( copyPass );
  106. // events
  107. window.addEventListener( 'resize', onWindowResize, false );
  108. }
  109. function onWindowResize() {
  110. camera.aspect = window.innerWidth / window.innerHeight;
  111. camera.updateProjectionMatrix();
  112. composer.setSize( window.innerWidth, window.innerHeight );
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. //
  116. var clock = new Clock();
  117. function animate() {
  118. var delta = clock.getDelta();
  119. requestAnimationFrame( animate );
  120. // Update Physics Engine ( fix delta-time in 60fps for more stability )
  121. AMMO.update( 1 / 60 );
  122. // Update SEA3D Animations
  123. SEA3D.AnimationHandler.update( delta );
  124. render( delta );
  125. stats.update();
  126. }
  127. function render( dlt ) {
  128. //renderer.render( scene, camera );
  129. composer.render( dlt );
  130. }
  131. </script>
  132. </body>
  133. </html>