cameras-orthographic-canvas-top-left-origin.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 - Cameras - Orthographic Canvas Top Left Origin</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. .split {
  19. position: absolute;
  20. left: 0;
  21. top: 0;
  22. width: 100%;
  23. height: 100%;
  24. display: flex;
  25. }
  26. .split>div {
  27. width: 100%;
  28. height: 100%;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <canvas id="c"></canvas>
  34. </body>
  35. <script type="module">
  36. import * as THREE from './resources/threejs/r132/build/three.module.js';
  37. function main() {
  38. const canvas = document.querySelector('#c');
  39. const renderer = new THREE.WebGLRenderer({canvas});
  40. const left = 0;
  41. const right = 300; // default canvas size
  42. const top = 0;
  43. const bottom = 150; // defautl canvas size
  44. const near = -1;
  45. const far = 1;
  46. const camera = new THREE.OrthographicCamera(left, right, top, bottom, near, far);
  47. camera.zoom = 1;
  48. const scene = new THREE.Scene();
  49. scene.background = new THREE.Color('black');
  50. const loader = new THREE.TextureLoader();
  51. const textures = [
  52. loader.load('resources/images/flower-1.jpg'),
  53. loader.load('resources/images/flower-2.jpg'),
  54. loader.load('resources/images/flower-3.jpg'),
  55. loader.load('resources/images/flower-4.jpg'),
  56. loader.load('resources/images/flower-5.jpg'),
  57. loader.load('resources/images/flower-6.jpg'),
  58. ];
  59. const planeSize = 256;
  60. const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
  61. const planes = textures.map((texture) => {
  62. const planePivot = new THREE.Object3D();
  63. scene.add(planePivot);
  64. texture.magFilter = THREE.NearestFilter;
  65. const planeMat = new THREE.MeshBasicMaterial({
  66. map: texture,
  67. side: THREE.DoubleSide,
  68. });
  69. const mesh = new THREE.Mesh(planeGeo, planeMat);
  70. planePivot.add(mesh);
  71. // move plane so top left corner is origin
  72. mesh.position.set(planeSize / 2, planeSize / 2, 0);
  73. return planePivot;
  74. });
  75. function resizeRendererToDisplaySize(renderer) {
  76. const canvas = renderer.domElement;
  77. const width = canvas.clientWidth;
  78. const height = canvas.clientHeight;
  79. const needResize = canvas.width !== width || canvas.height !== height;
  80. if (needResize) {
  81. renderer.setSize(width, height, false);
  82. }
  83. return needResize;
  84. }
  85. function render(time) {
  86. time *= 0.001; // convert to seconds;
  87. if (resizeRendererToDisplaySize(renderer)) {
  88. camera.right = canvas.width;
  89. camera.bottom = canvas.height;
  90. camera.updateProjectionMatrix();
  91. }
  92. const distAcross = Math.max(20, canvas.width - planeSize);
  93. const distDown = Math.max(20, canvas.height - planeSize);
  94. // total distance to move across and back
  95. const xRange = distAcross * 2;
  96. const yRange = distDown * 2;
  97. const speed = 180;
  98. planes.forEach((plane, ndx) => {
  99. // compute a unique time for each plane
  100. const t = time * speed + ndx * 300;
  101. // get a value between 0 and range
  102. const xt = t % xRange;
  103. const yt = t % yRange;
  104. // set our position going forward if 0 to half of range
  105. // and backward if half of range to range
  106. const x = xt < distAcross ? xt : xRange - xt;
  107. const y = yt < distDown ? yt : yRange - yt;
  108. plane.position.set(x, y, 0);
  109. });
  110. renderer.render(scene, camera);
  111. requestAnimationFrame(render);
  112. }
  113. requestAnimationFrame(render);
  114. }
  115. main();
  116. </script>
  117. </html>