postprocessing-3dlut-prep.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import {OrbitControls} from '../../examples/jsm/controls/OrbitControls.js';
  36. import {GLTFLoader} from '../../examples/jsm/loaders/GLTFLoader.js';
  37. function main() {
  38. const canvas = document.querySelector('#c');
  39. const renderer = new THREE.WebGLRenderer({canvas});
  40. renderer.outputEncoding = THREE.sRGBEncoding;
  41. renderer.autoClearColor = false;
  42. const fov = 45;
  43. const aspect = 2; // the canvas default
  44. const near = 0.1;
  45. const far = 100;
  46. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  47. camera.position.set(0, 10, 20);
  48. const controls = new OrbitControls(camera, canvas);
  49. controls.target.set(0, 5, 0);
  50. controls.update();
  51. const scene = new THREE.Scene();
  52. const sceneBG = new THREE.Scene();
  53. const cameraBG = new THREE.OrthographicCamera(-1, 1, 1, -1, -1, 1);
  54. let bgMesh;
  55. let bgTexture;
  56. {
  57. const loader = new THREE.TextureLoader();
  58. bgTexture = loader.load('resources/images/beach.jpg');
  59. bgTexture.encoding = THREE.sRGBEncoding;
  60. const planeGeo = new THREE.PlaneGeometry(2, 2);
  61. const planeMat = new THREE.MeshBasicMaterial({
  62. map: bgTexture,
  63. depthTest: false,
  64. });
  65. bgMesh = new THREE.Mesh(planeGeo, planeMat);
  66. sceneBG.add(bgMesh);
  67. }
  68. function frameArea(sizeToFitOnScreen, boxSize, boxCenter, camera) {
  69. const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
  70. const halfFovY = THREE.MathUtils.degToRad(camera.fov * .5);
  71. const distance = halfSizeToFitOnScreen / Math.tan(halfFovY);
  72. // compute a unit vector that points in the direction the camera is now
  73. // in the xz plane from the center of the box
  74. const direction = (new THREE.Vector3())
  75. .subVectors(camera.position, boxCenter)
  76. .multiply(new THREE.Vector3(1, 0, 1))
  77. .normalize();
  78. // move the camera to a position distance units way from the center
  79. // in whatever direction the camera was from the center already
  80. camera.position.copy(direction.multiplyScalar(distance).add(boxCenter));
  81. // pick some near and far values for the frustum that
  82. // will contain the box.
  83. camera.near = boxSize / 100;
  84. camera.far = boxSize * 100;
  85. camera.updateProjectionMatrix();
  86. // point the camera to look at the center of the box
  87. camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z);
  88. }
  89. {
  90. const gltfLoader = new GLTFLoader();
  91. gltfLoader.load('resources/models/3dbustchallange_submission/scene.gltf', (gltf) => {
  92. const root = gltf.scene;
  93. scene.add(root);
  94. // fix materials from r114
  95. root.traverse(({material}) => {
  96. if (material) {
  97. material.depthWrite = true;
  98. }
  99. });
  100. // compute the box that contains all the stuff
  101. // from root and below
  102. const box = new THREE.Box3().setFromObject(root);
  103. const boxSize = box.getSize(new THREE.Vector3()).length();
  104. const boxCenter = box.getCenter(new THREE.Vector3());
  105. // set the camera to frame the box
  106. frameArea(boxSize * 0.4, boxSize, boxCenter, camera);
  107. // update the Trackball controls to handle the new size
  108. controls.maxDistance = boxSize * 10;
  109. controls.target.copy(boxCenter);
  110. controls.update();
  111. });
  112. }
  113. function resizeRendererToDisplaySize(renderer) {
  114. const canvas = renderer.domElement;
  115. const width = canvas.clientWidth;
  116. const height = canvas.clientHeight;
  117. const needResize = canvas.width !== width || canvas.height !== height;
  118. if (needResize) {
  119. renderer.setSize(width, height, false);
  120. }
  121. return needResize;
  122. }
  123. function render() {
  124. if (resizeRendererToDisplaySize(renderer)) {
  125. const canvas = renderer.domElement;
  126. const canvasAspect = canvas.clientWidth / canvas.clientHeight;
  127. camera.aspect = canvasAspect;
  128. camera.updateProjectionMatrix();
  129. // scale the background plane to keep the image's
  130. // aspect correct.
  131. // Note the image may not have loaded yet.
  132. const imageAspect = bgTexture.image ? bgTexture.image.width / bgTexture.image.height : 1;
  133. const aspect = imageAspect / canvasAspect;
  134. bgMesh.scale.x = aspect > 1 ? aspect : 1;
  135. bgMesh.scale.y = aspect > 1 ? 1 : 1 / aspect;
  136. }
  137. renderer.render(sceneBG, cameraBG);
  138. renderer.render(scene, camera);
  139. requestAnimationFrame(render);
  140. }
  141. requestAnimationFrame(render);
  142. }
  143. main();
  144. </script>
  145. </html>