You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					148 lines
				
				3.9 KiB
			
		
		
			
		
	
	
					148 lines
				
				3.9 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								<!-- Licensed under a BSD license. See license.html for license -->
							 | 
						||
| 
								 | 
							
								<!DOCTYPE html>
							 | 
						||
| 
								 | 
							
								<html>
							 | 
						||
| 
								 | 
							
								  <head>
							 | 
						||
| 
								 | 
							
								    <meta charset="utf-8">
							 | 
						||
| 
								 | 
							
								    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
							 | 
						||
| 
								 | 
							
								    <title>Three.js - Cameras - Orthographic Canvas Top Left Origin</title>
							 | 
						||
| 
								 | 
							
								    <style>
							 | 
						||
| 
								 | 
							
								    html, body {
							 | 
						||
| 
								 | 
							
								        margin: 0;
							 | 
						||
| 
								 | 
							
								        height: 100%;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    #c {
							 | 
						||
| 
								 | 
							
								        width: 100%;
							 | 
						||
| 
								 | 
							
								        height: 100%;
							 | 
						||
| 
								 | 
							
								        display: block;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    .split {
							 | 
						||
| 
								 | 
							
								      position: absolute;
							 | 
						||
| 
								 | 
							
								      left: 0;
							 | 
						||
| 
								 | 
							
								      top: 0;
							 | 
						||
| 
								 | 
							
								      width: 100%;
							 | 
						||
| 
								 | 
							
								      height: 100%;
							 | 
						||
| 
								 | 
							
								      display: flex;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    .split>div {
							 | 
						||
| 
								 | 
							
								      width: 100%;
							 | 
						||
| 
								 | 
							
								      height: 100%;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    </style>
							 | 
						||
| 
								 | 
							
								  </head>
							 | 
						||
| 
								 | 
							
								  <body>
							 | 
						||
| 
								 | 
							
								    <canvas id="c"></canvas>
							 | 
						||
| 
								 | 
							
								  </body>
							 | 
						||
| 
								 | 
							
								<!-- Import maps polyfill -->
							 | 
						||
| 
								 | 
							
								<!-- Remove this when import maps will be widely supported -->
							 | 
						||
| 
								 | 
							
								<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								<script type="importmap">
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								  "imports": {
							 | 
						||
| 
								 | 
							
								    "three": "../../build/three.module.js"
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								</script>
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								<script type="module">
							 | 
						||
| 
								 | 
							
								import * as THREE from 'three';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								function main() {
							 | 
						||
| 
								 | 
							
								  const canvas = document.querySelector('#c');
							 | 
						||
| 
								 | 
							
								  const renderer = new THREE.WebGLRenderer({canvas});
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  const left = 0;
							 | 
						||
| 
								 | 
							
								  const right = 300;  // default canvas size
							 | 
						||
| 
								 | 
							
								  const top = 0;
							 | 
						||
| 
								 | 
							
								  const bottom = 150;  // defautl canvas size
							 | 
						||
| 
								 | 
							
								  const near = -1;
							 | 
						||
| 
								 | 
							
								  const far = 1;
							 | 
						||
| 
								 | 
							
								  const camera = new THREE.OrthographicCamera(left, right, top, bottom, near, far);
							 | 
						||
| 
								 | 
							
								  camera.zoom = 1;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  const scene = new THREE.Scene();
							 | 
						||
| 
								 | 
							
								  scene.background = new THREE.Color('black');
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  const loader = new THREE.TextureLoader();
							 | 
						||
| 
								 | 
							
								  const textures = [
							 | 
						||
| 
								 | 
							
								    loader.load('resources/images/flower-1.jpg'),
							 | 
						||
| 
								 | 
							
								    loader.load('resources/images/flower-2.jpg'),
							 | 
						||
| 
								 | 
							
								    loader.load('resources/images/flower-3.jpg'),
							 | 
						||
| 
								 | 
							
								    loader.load('resources/images/flower-4.jpg'),
							 | 
						||
| 
								 | 
							
								    loader.load('resources/images/flower-5.jpg'),
							 | 
						||
| 
								 | 
							
								    loader.load('resources/images/flower-6.jpg'),
							 | 
						||
| 
								 | 
							
								  ];
							 | 
						||
| 
								 | 
							
								  const planeSize = 256;
							 | 
						||
| 
								 | 
							
								  const planeGeo = new THREE.PlaneGeometry(planeSize, planeSize);
							 | 
						||
| 
								 | 
							
								  const planes = textures.map((texture) => {
							 | 
						||
| 
								 | 
							
								    const planePivot = new THREE.Object3D();
							 | 
						||
| 
								 | 
							
								    scene.add(planePivot);
							 | 
						||
| 
								 | 
							
								    texture.magFilter = THREE.NearestFilter;
							 | 
						||
| 
								 | 
							
								    const planeMat = new THREE.MeshBasicMaterial({
							 | 
						||
| 
								 | 
							
								      map: texture,
							 | 
						||
| 
								 | 
							
								      side: THREE.DoubleSide,
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								    const mesh = new THREE.Mesh(planeGeo, planeMat);
							 | 
						||
| 
								 | 
							
								    planePivot.add(mesh);
							 | 
						||
| 
								 | 
							
								    // move plane so top left corner is origin
							 | 
						||
| 
								 | 
							
								    mesh.position.set(planeSize / 2, planeSize / 2, 0);
							 | 
						||
| 
								 | 
							
								    return planePivot;
							 | 
						||
| 
								 | 
							
								  });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  function resizeRendererToDisplaySize(renderer) {
							 | 
						||
| 
								 | 
							
								    const canvas = renderer.domElement;
							 | 
						||
| 
								 | 
							
								    const width = canvas.clientWidth;
							 | 
						||
| 
								 | 
							
								    const height = canvas.clientHeight;
							 | 
						||
| 
								 | 
							
								    const needResize = canvas.width !== width || canvas.height !== height;
							 | 
						||
| 
								 | 
							
								    if (needResize) {
							 | 
						||
| 
								 | 
							
								      renderer.setSize(width, height, false);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    return needResize;
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  function render(time) {
							 | 
						||
| 
								 | 
							
								    time *= 0.001;  // convert to seconds;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (resizeRendererToDisplaySize(renderer)) {
							 | 
						||
| 
								 | 
							
								      camera.right = canvas.width;
							 | 
						||
| 
								 | 
							
								      camera.bottom = canvas.height;
							 | 
						||
| 
								 | 
							
								      camera.updateProjectionMatrix();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    const distAcross = Math.max(20, canvas.width - planeSize);
							 | 
						||
| 
								 | 
							
								    const distDown = Math.max(20, canvas.height - planeSize);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    // total distance to move across and back
							 | 
						||
| 
								 | 
							
								    const xRange = distAcross * 2;
							 | 
						||
| 
								 | 
							
								    const yRange = distDown * 2;
							 | 
						||
| 
								 | 
							
								    const speed = 180;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    planes.forEach((plane, ndx) => {
							 | 
						||
| 
								 | 
							
								      // compute a unique time for each plane
							 | 
						||
| 
								 | 
							
								      const t = time * speed + ndx * 300;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      // get a value between 0 and range
							 | 
						||
| 
								 | 
							
								      const xt = t % xRange;
							 | 
						||
| 
								 | 
							
								      const yt = t % yRange;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      // set our position going forward if 0 to half of range
							 | 
						||
| 
								 | 
							
								      // and backward if half of range to range
							 | 
						||
| 
								 | 
							
								      const x = xt < distAcross ? xt : xRange - xt;
							 | 
						||
| 
								 | 
							
								      const y = yt < distDown   ? yt : yRange - yt;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      plane.position.set(x, y, 0);
							 | 
						||
| 
								 | 
							
								    });
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    renderer.render(scene, camera);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    requestAnimationFrame(render);
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  requestAnimationFrame(render);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								main();
							 | 
						||
| 
								 | 
							
								</script>
							 | 
						||
| 
								 | 
							
								</html>
							 | 
						||
| 
								 | 
							
								
							 |