threejs-cameras-orthographic-canvas-top-left-origin.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 src="resources/threejs/r102/three.min.js"></script>
  36. <script src="resources/threejs/r102/js/controls/OrbitControls.js"></script>
  37. <script src="../3rdparty/dat.gui.min.js"></script>
  38. <script>
  39. 'use strict';
  40. /* global THREE */
  41. function main() {
  42. const canvas = document.querySelector('#c');
  43. const renderer = new THREE.WebGLRenderer({canvas: canvas});
  44. const left = 0;
  45. const right = 300; // default canvas size
  46. const top = 0;
  47. const bottom = 150; // defautl canvas size
  48. const near = -1;
  49. const far = 1;
  50. const camera = new THREE.OrthographicCamera(left, right, top, bottom, near, far);
  51. camera.zoom = 1;
  52. const scene = new THREE.Scene();
  53. scene.background = new THREE.Color('black');
  54. const loader = new THREE.TextureLoader();
  55. const textures = [
  56. loader.load('resources/images/flower-1.jpg'),
  57. loader.load('resources/images/flower-2.jpg'),
  58. loader.load('resources/images/flower-3.jpg'),
  59. loader.load('resources/images/flower-4.jpg'),
  60. loader.load('resources/images/flower-5.jpg'),
  61. loader.load('resources/images/flower-6.jpg'),
  62. ];
  63. const planeSize = 256;
  64. const planeGeo = new THREE.PlaneBufferGeometry(planeSize, planeSize);
  65. const planes = textures.map((texture) => {
  66. const planePivot = new THREE.Object3D();
  67. scene.add(planePivot);
  68. texture.magFilter = THREE.NearestFilter;
  69. const planeMat = new THREE.MeshBasicMaterial({
  70. map: texture,
  71. side: THREE.DoubleSide,
  72. });
  73. const mesh = new THREE.Mesh(planeGeo, planeMat);
  74. planePivot.add(mesh);
  75. // move plane so top left corner is origin
  76. mesh.position.set(planeSize / 2, planeSize / 2, 0);
  77. return planePivot;
  78. });
  79. function resizeRendererToDisplaySize(renderer) {
  80. const canvas = renderer.domElement;
  81. const width = canvas.clientWidth;
  82. const height = canvas.clientHeight;
  83. const needResize = canvas.width !== width || canvas.height !== height;
  84. if (needResize) {
  85. renderer.setSize(width, height, false);
  86. }
  87. return needResize;
  88. }
  89. function render(time) {
  90. time *= 0.001; // convert to seconds;
  91. if (resizeRendererToDisplaySize(renderer)) {
  92. camera.right = canvas.width;
  93. camera.bottom = canvas.height;
  94. camera.updateProjectionMatrix();
  95. }
  96. const xRange = Math.max(20, canvas.width - planeSize) * 2;
  97. const yRange = Math.max(20, canvas.height - planeSize) * 2;
  98. planes.forEach((plane, ndx) => {
  99. const speed = 180;
  100. const t = time * speed + ndx * 300;
  101. const xt = t % xRange;
  102. const yt = t % yRange;
  103. const x = xt < xRange / 2 ? xt : xRange - xt;
  104. const y = yt < yRange / 2 ? yt : yRange - yt;
  105. plane.position.set(x, y, 0);
  106. });
  107. renderer.render(scene, camera);
  108. requestAnimationFrame(render);
  109. }
  110. requestAnimationFrame(render);
  111. }
  112. main();
  113. </script>
  114. </html>