postprocessing-3dlut-prep.html 4.7 KB

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