|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js webgpu - stereo</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> |
| 7 | + <link type="text/css" rel="stylesheet" href="main.css"> |
| 8 | + </head> |
| 9 | + |
| 10 | + <body> |
| 11 | + <div id="info"> |
| 12 | + <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - stereo. skybox by <a href="http://www.zfight.com/" target="_blank" rel="noopener">Jochum Skoglund</a> |
| 13 | + </div> |
| 14 | + |
| 15 | + <script type="importmap"> |
| 16 | + { |
| 17 | + "imports": { |
| 18 | + "three": "../build/three.webgpu.js", |
| 19 | + "three/tsl": "../build/three.webgpu.js", |
| 20 | + "three/addons/": "./jsm/" |
| 21 | + } |
| 22 | + } |
| 23 | + </script> |
| 24 | + |
| 25 | + <script type="module"> |
| 26 | + |
| 27 | + import * as THREE from 'three'; |
| 28 | + |
| 29 | + import { stereoPass } from 'three/tsl'; |
| 30 | + |
| 31 | + let camera, scene, renderer, postProcessing; |
| 32 | + |
| 33 | + let mesh, dummy; |
| 34 | + |
| 35 | + let mouseX = 0, mouseY = 0; |
| 36 | + |
| 37 | + let windowHalfX = window.innerWidth / 2; |
| 38 | + let windowHalfY = window.innerHeight / 2; |
| 39 | + |
| 40 | + const position = new THREE.Vector3(); |
| 41 | + |
| 42 | + init(); |
| 43 | + |
| 44 | + function init() { |
| 45 | + |
| 46 | + camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 ); |
| 47 | + camera.position.z = 3; |
| 48 | + |
| 49 | + scene = new THREE.Scene(); |
| 50 | + scene.background = new THREE.CubeTextureLoader() |
| 51 | + .setPath( 'textures/cube/Park3Med/' ) |
| 52 | + .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] ); |
| 53 | + |
| 54 | + const geometry = new THREE.SphereGeometry( 0.1, 32, 16 ); |
| 55 | + |
| 56 | + const textureCube = new THREE.CubeTextureLoader() |
| 57 | + .setPath( 'textures/cube/Park3Med/' ) |
| 58 | + .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] ); |
| 59 | + textureCube.mapping = THREE.CubeRefractionMapping; |
| 60 | + |
| 61 | + const material = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube, refractionRatio: 0.95 } ); |
| 62 | + |
| 63 | + mesh = new THREE.InstancedMesh( geometry, material, 500 ); |
| 64 | + mesh.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); |
| 65 | + |
| 66 | + dummy = new THREE.Mesh(); |
| 67 | + |
| 68 | + for ( let i = 0; i < 500; i ++ ) { |
| 69 | + |
| 70 | + dummy.position.x = Math.random() * 10 - 5; |
| 71 | + dummy.position.y = Math.random() * 10 - 5; |
| 72 | + dummy.position.z = Math.random() * 10 - 5; |
| 73 | + dummy.scale.x = dummy.scale.y = dummy.scale.z = Math.random() * 3 + 1; |
| 74 | + |
| 75 | + dummy.updateMatrix(); |
| 76 | + |
| 77 | + mesh.setMatrixAt( i, dummy.matrix ); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + scene.add( mesh ); |
| 82 | + |
| 83 | + // |
| 84 | + |
| 85 | + renderer = new THREE.WebGPURenderer(); |
| 86 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 87 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 88 | + renderer.setAnimationLoop( animate ); |
| 89 | + document.body.appendChild( renderer.domElement ); |
| 90 | + |
| 91 | + postProcessing = new THREE.PostProcessing( renderer ); |
| 92 | + const pass = stereoPass( scene, camera ); |
| 93 | + postProcessing.outputNode = pass; |
| 94 | + |
| 95 | + // |
| 96 | + |
| 97 | + window.addEventListener( 'resize', onWindowResize ); |
| 98 | + |
| 99 | + document.addEventListener( 'mousemove', onDocumentMouseMove ); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + function onWindowResize() { |
| 104 | + |
| 105 | + windowHalfX = window.innerWidth / 2; |
| 106 | + windowHalfY = window.innerHeight / 2; |
| 107 | + |
| 108 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 109 | + camera.updateProjectionMatrix(); |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + function onDocumentMouseMove( event ) { |
| 114 | + |
| 115 | + mouseX = ( event.clientX - windowHalfX ) * 0.01; |
| 116 | + mouseY = ( event.clientY - windowHalfY ) * 0.01; |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + function extractPosition( matrix, position ) { |
| 121 | + |
| 122 | + position.x = matrix.elements[ 12 ]; |
| 123 | + position.y = matrix.elements[ 13 ]; |
| 124 | + position.z = matrix.elements[ 14 ]; |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + // |
| 129 | + |
| 130 | + function animate() { |
| 131 | + |
| 132 | + const timer = 0.0001 * Date.now(); |
| 133 | + |
| 134 | + camera.position.x += ( mouseX - camera.position.x ) * .05; |
| 135 | + camera.position.y += ( - mouseY - camera.position.y ) * .05; |
| 136 | + camera.lookAt( scene.position ); |
| 137 | + |
| 138 | + for ( let i = 0; i < mesh.count; i ++ ) { |
| 139 | + |
| 140 | + mesh.getMatrixAt( i, dummy.matrix ); |
| 141 | + |
| 142 | + extractPosition( dummy.matrix, position ); |
| 143 | + |
| 144 | + position.x = 5 * Math.cos( timer + i ); |
| 145 | + position.y = 5 * Math.sin( timer + i * 1.1 ); |
| 146 | + |
| 147 | + dummy.matrix.setPosition( position ); |
| 148 | + |
| 149 | + mesh.setMatrixAt( i, dummy.matrix ); |
| 150 | + |
| 151 | + mesh.instanceMatrix.needsUpdate = true; |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + postProcessing.render(); |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + </script> |
| 160 | + |
| 161 | + </body> |
| 162 | +</html> |
0 commit comments