threejs-postprocessing-3dlut-prep.html 4.6 KB

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