postprocessing-3dlut-prep.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Post Processing 3DLUT (not) - prep</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js",
  27. "three/addons/": "../../examples/jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  35. function main() {
  36. const canvas = document.querySelector( '#c' );
  37. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  38. renderer.autoClearColor = false;
  39. const fov = 45;
  40. const aspect = 2; // the canvas default
  41. const near = 0.1;
  42. const far = 100;
  43. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  44. camera.position.set( 0, 10, 20 );
  45. const controls = new OrbitControls( camera, canvas );
  46. controls.target.set( 0, 5, 0 );
  47. controls.update();
  48. const scene = new THREE.Scene();
  49. const sceneBG = new THREE.Scene();
  50. const cameraBG = new THREE.OrthographicCamera( - 1, 1, 1, - 1, - 1, 1 );
  51. let bgMesh;
  52. let bgTexture;
  53. {
  54. const loader = new THREE.TextureLoader();
  55. bgTexture = loader.load( 'resources/images/beach.jpg' );
  56. bgTexture.colorSpace = THREE.SRGBColorSpace;
  57. const planeGeo = new THREE.PlaneGeometry( 2, 2 );
  58. const planeMat = new THREE.MeshBasicMaterial( {
  59. map: bgTexture,
  60. depthTest: false,
  61. } );
  62. bgMesh = new THREE.Mesh( planeGeo, planeMat );
  63. sceneBG.add( bgMesh );
  64. }
  65. function frameArea( sizeToFitOnScreen, boxSize, boxCenter, camera ) {
  66. const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
  67. const halfFovY = THREE.MathUtils.degToRad( camera.fov * .5 );
  68. const distance = halfSizeToFitOnScreen / Math.tan( halfFovY );
  69. // compute a unit vector that points in the direction the camera is now
  70. // in the xz plane from the center of the box
  71. const direction = ( new THREE.Vector3() )
  72. .subVectors( camera.position, boxCenter )
  73. .multiply( new THREE.Vector3( 1, 0, 1 ) )
  74. .normalize();
  75. // move the camera to a position distance units way from the center
  76. // in whatever direction the camera was from the center already
  77. camera.position.copy( direction.multiplyScalar( distance ).add( boxCenter ) );
  78. // pick some near and far values for the frustum that
  79. // will contain the box.
  80. camera.near = boxSize / 100;
  81. camera.far = boxSize * 100;
  82. camera.updateProjectionMatrix();
  83. // point the camera to look at the center of the box
  84. camera.lookAt( boxCenter.x, boxCenter.y, boxCenter.z );
  85. }
  86. {
  87. const gltfLoader = new GLTFLoader();
  88. gltfLoader.load( 'resources/models/3dbustchallange_submission/scene.gltf', ( gltf ) => {
  89. const root = gltf.scene;
  90. scene.add( root );
  91. // fix materials from r114
  92. root.traverse( ( { material } ) => {
  93. if ( material ) {
  94. material.depthWrite = true;
  95. }
  96. } );
  97. // compute the box that contains all the stuff
  98. // from root and below
  99. const box = new THREE.Box3().setFromObject( root );
  100. const boxSize = box.getSize( new THREE.Vector3() ).length();
  101. const boxCenter = box.getCenter( new THREE.Vector3() );
  102. // set the camera to frame the box
  103. frameArea( boxSize * 0.4, boxSize, boxCenter, camera );
  104. // update the Trackball controls to handle the new size
  105. controls.maxDistance = boxSize * 10;
  106. controls.target.copy( boxCenter );
  107. controls.update();
  108. } );
  109. }
  110. function resizeRendererToDisplaySize( renderer ) {
  111. const canvas = renderer.domElement;
  112. const width = canvas.clientWidth;
  113. const height = canvas.clientHeight;
  114. const needResize = canvas.width !== width || canvas.height !== height;
  115. if ( needResize ) {
  116. renderer.setSize( width, height, false );
  117. }
  118. return needResize;
  119. }
  120. function render() {
  121. if ( resizeRendererToDisplaySize( renderer ) ) {
  122. const canvas = renderer.domElement;
  123. const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  124. camera.aspect = canvasAspect;
  125. camera.updateProjectionMatrix();
  126. // scale the background plane to keep the image's
  127. // aspect correct.
  128. // Note the image may not have loaded yet.
  129. const imageAspect = bgTexture.image ? bgTexture.image.width / bgTexture.image.height : 1;
  130. const aspect = imageAspect / canvasAspect;
  131. bgMesh.scale.x = aspect > 1 ? aspect : 1;
  132. bgMesh.scale.y = aspect > 1 ? 1 : 1 / aspect;
  133. }
  134. renderer.render( sceneBG, cameraBG );
  135. renderer.render( scene, camera );
  136. requestAnimationFrame( render );
  137. }
  138. requestAnimationFrame( render );
  139. }
  140. main();
  141. </script>
  142. </html>