File size: 6,796 Bytes
a8f64d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!DOCTYPE html>
<html lang="en">
	<head>
		<!-- <title>three.js webgl - multiple elements</title> -->
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="./src/App.css">
		<style>
			* {
				box-sizing: border-box;
				-moz-box-sizing: border-box;
                
                /* top: -10%;
                left: 50%;
                transform: translate(-50%, 50%); */
			}

			body {
				background-color: #fff;
				color: #444;
			}

			a {
				color: #08f;
			}

			#content {
				position: absolute;
				top: 0; width: 100%;
				z-index: 1;
				padding: 3em 3rem 0 3rem ;
			}

			#c {
				position: absolute;
				left: 0;
				width: 100%;
				height: 100%;
			}

			.list-item {
				display: inline-block;
				margin: 1em;
				padding: 1em;
				box-shadow: 1px 2px 4px 0px rgba(0,0,0,0.25);
			}

			.list-item > div:nth-child(1) {
				width: 350px;
				height: 220px;
			}

			.list-item > div:nth-child(2) {
				color: #888;
				font-family: sans-serif;
				font-size: large;
				width: 200px;
				margin-top: 0.5em;
			}
		</style>
	</head>
	<body>
        
		<canvas id="c"></canvas>
		<div id="content">
			<!-- <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple elements - webgl</div> -->
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.module.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three';
			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
		
			let canvas, renderer;
			const scenes = [];
		
			async function fetchImages() {
				const response = await fetch('./images/names.json');
				console.log(response)
				const imageFiles = await response.json();
		
				const imagePairs = pairImages(imageFiles);
				init(imagePairs);
				animate();
			}
		
			function pairImages(imageFiles) {
				const resultImages = imageFiles.filter(file => file.startsWith('result'));
				console.log(resultImages)
				const imagePairs = [];
		
				resultImages.forEach(resultImage => {
					const resultNumber = resultImage.match(/\d+/)[0];
					const depthImage = `depth${resultNumber}.png`;
		
					if (imageFiles.includes(depthImage)) {
						imagePairs.push({
							map: `./images/${resultImage}`,
							depthMap: `./images/${depthImage}`
						});
					}
				});
		
				return imagePairs;
			}
		
			function init(imagePairs) {
		
				canvas = document.getElementById( 'c' );
				let amap = new THREE.TextureLoader().load("result.png")
				let dmap = new THREE.TextureLoader().load("depth.png")
				const settings = {
					metalness: 0.0,
					roughness: 1,
					//ambientIntensity: 0,
					displacementScale: 5, 
					displacementBias: -0.5,
				};
		
				const geometries = [
					new THREE.PlaneGeometry( 10, 10, 512, 512 ),
				];
		
				const content = document.getElementById( 'content' );
				
				for (let i = 0; i < imagePairs.length; i++) {
					const scene = new THREE.Scene();
		
					// make a list item
					const element = document.createElement( 'div' );
					element.className = 'list-item';
		
					const sceneElement = document.createElement( 'div' );
					element.appendChild( sceneElement );
		
					scene.userData.element = sceneElement;
					content.appendChild( element );
		
					const camera = new THREE.PerspectiveCamera( 40, 1, 1, 10 );
					camera.position.z = 12.5;
					scene.userData.camera = camera;
		
					const controls = new OrbitControls( scene.userData.camera, scene.userData.element );
					controls.minDistance = 2;
					controls.maxDistance = 5;
					controls.enablePan = false;
					controls.enableZoom = false;
					scene.userData.controls = controls;
		
					window.addEventListener("mousemove", onmousemove, false);
					var plane = new THREE.Plane(new THREE.Vector3(1, 1, 0), 2); 
					var raycaster = new THREE.Raycaster(); 
					var mouse = new THREE.Vector2();       
					var intersectPoint = new THREE.Vector3();
		
					function onmousemove(event) {
						mouse.x = (event.clientX / window.innerWidth)  - 1;
						mouse.y = -(event.clientY / window.innerHeight)- 1;
						raycaster.setFromCamera(mouse, camera);
						raycaster.ray.intersectPlane(plane, intersectPoint);
						mesh.lookAt(intersectPoint);
					}
		
					const geometry = geometries[ geometries.length * Math.random() | 0 ];
				
					const material = new THREE.MeshStandardMaterial( {
						map: new THREE.TextureLoader().load(imagePairs[i].map),
						displacementMap: new THREE.TextureLoader().load(imagePairs[i].depthMap),
						emissive: 0xffffff,
						emissiveIntensity: 0.9, 
						emissiveMap :  new THREE.TextureLoader().load(imagePairs[i].map),
						roughness: settings.roughness,
						metalness: settings.metalness,
						displacementScale: settings.displacementScale,
						displacementBias: settings.displacementBias,
						side: THREE.DoubleSide
					} );
		
					const mesh = new THREE.Mesh( geometry, material );
					mesh.scale.set(0.6,0.6,0.6)
					mesh.frustrumCulled = false
					mesh.position.set(0,0,3)
					scene.add( mesh );
					scenes.push( scene );
				}
		
				renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
				renderer.setClearColor( 0x000000, 0 );
				renderer.setPixelRatio( window.devicePixelRatio );
			}
		
			function updateSize() {
				const width = canvas.clientWidth;
				const height = canvas.clientHeight;
		
				if ( canvas.width !== width || canvas.height !== height ) {
					renderer.setSize( width, height, false );
				}
			}
		
			function animate() {
				render();
				requestAnimationFrame( animate );
			}
		
			function render() {
				updateSize();
		
				canvas.style.transform = `translateY(${window.scrollY}px)`;
		
				renderer.setClearColor( 0xffffff );
				renderer.setScissorTest( false );
				renderer.clear();
		
				renderer.setClearColor( 0xe0e0e0 );
				renderer.setScissorTest( true );
		
				scenes.forEach( function ( scene ) {
					const element = scene.userData.element;
					const rect = element.getBoundingClientRect();
		
					if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
						 rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
						return;
					}
		
					const width = rect.right - rect.left;
					const height = rect.bottom - rect.top;
					const left = rect.left;
					const bottom = renderer.domElement.clientHeight - rect.bottom;
		
					renderer.setViewport( left, bottom, width, height );
					renderer.setScissor( left, bottom, width, height );
		
					const camera = scene.userData.camera;
					renderer.render( scene, camera );
				});
			}
		
			fetchImages();
		
		</script>

	</body>
</html>