postprocessing-3dlut-prep.html 5.1 KB

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