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

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