webgl_loader_sea3d_physics.html 5.2 KB

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