File size: 6,271 Bytes
71d12ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <title>depthmap-viewer-three</title>
  <style>

    body {

      font-family: sans-serif;

      margin: 0;

    }



    .dropzone {

      box-sizing: border-box;

      display: none;

      position: fixed;

      width: 100%;

      height: 100%;

      left: 0;

      top: 0;

      z-index: 99999;

      background: rgba(#60a7dc, .8);

      border: 11px dashed #60a7dc;

    }

  </style>
  <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  <script type="importmap">

{

"imports": {

  "three": "https://unpkg.com/[email protected]/build/three.module.js",

  "three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"

}

}

</script>
  <script type="module">

    import * as THREE from 'three';

    import { OrbitControls } from "three/addons/controls/OrbitControls";

    import { GUI } from 'three/addons/libs/lil-gui.module.min.js';



    var rgbBase64Img = window.frameElement?.getAttribute('data-rgb') || "public/images/rgb.png"

    var depthBase64Img = window.frameElement?.getAttribute('data-depth') || "public/images/depth.png"







    let mesh;

    let material;

    let stopAnimation = false;



    const settings = {

      metalness: 0.0,

      roughness: 0.5,

      ambientIntensity: 0.85,

      displacementScale: 100,

      displacementBias: -0.5,

    };

    const meshSettings = {

      rotation: {

        x: 0,

        y: 0,

        z: 0

      }

    }



    // init

    const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);

    camera.position.z = 3;



    const scene = new THREE.Scene();



    const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);

    scene.add(ambientLight);



    const pointLight = new THREE.PointLight(0xff0000, 0.5);

    pointLight.position.z = 2500;

    scene.add(pointLight);





    const renderer = new THREE.WebGLRenderer({ antialias: true });

    renderer.setSize(window.innerWidth, window.innerHeight);

    renderer.setAnimationLoop(animation);

    // renderer.xr.enabled = true;

    renderer.toneMapping = THREE.ACESFilmicToneMapping;

    renderer.toneMappingExposure = 1;

    renderer.outputEncoding = THREE.sRGBEncoding;

    document.body.appendChild(renderer.domElement);



    // animation

    function animation(time) {

      if (mesh && !stopAnimation) {

        mesh.rotation.x = 0.5 * Math.sin(time / 2000);

        mesh.rotation.y = 0.5 * Math.sin(time / 2000);

        meshSettings.rotation.x = mesh.rotation.x;

        meshSettings.rotation.y = mesh.rotation.y;

      }

      renderer.render(scene, camera);



    }



    function onWindowResize() {



      const aspect = window.innerWidth / window.innerHeight;

      camera.aspect = aspect;

      camera.updateProjectionMatrix();



      renderer.setSize(window.innerWidth, window.innerHeight);



    }

    window.addEventListener('resize', onWindowResize);





    // orbit controls

    const controls = new OrbitControls(camera, renderer.domElement);

    controls.enableZoom = true;

    controls.enableDamping = true;



    async function getCanvasTexture(imageSrc) {

      return new Promise((resolve, reject) => {

        const image = new Image();

        image.src = imageSrc;

        image.onload = () => {

          const ctx = document.createElement('canvas').getContext('2d');

          ctx.canvas.width = image.width;

          ctx.canvas.height = image.height;

          ctx.drawImage(image, 0, 0, image.width, image.height);

          const texture = new THREE.CanvasTexture(ctx.canvas);

          resolve(texture);

        }

      })



    }

    (async () => {

      const rgbTexture = await getCanvasTexture(rgbBase64Img);

      const depthTexture = await getCanvasTexture(depthBase64Img);



      if (mesh) {

        mesh.geometry.dispose();

        mesh.material.dispose();

        scene.remove(mesh);

      }

      // material

      material = new THREE.MeshStandardMaterial({



        color: 0xaaaaaa,

        roughness: settings.roughness,

        metalness: settings.metalness,



        map: rgbTexture,



        displacementMap: depthTexture,

        displacementScale: settings.displacementScale,

        displacementBias: settings.displacementBias,



        side: THREE.DoubleSide



      });

      // generating geometry and add mesh to scene

      const geometry = new THREE.PlaneGeometry(rgbTexture.image.width, rgbTexture.image.height, 512, 512);

      mesh = new THREE.Mesh(geometry, material);

      const scale = 1 / Math.max(rgbTexture.image.width, rgbTexture.image.height);

      mesh.scale.set(scale, scale, scale);

      scene.add(mesh);



    })()







    // setup gui

    const gui = new GUI();

    gui.close();

    const sceneGUI = gui.addFolder('Scene');

    sceneGUI.add(settings, 'metalness').min(0).max(1).onChange(function (value) {

      material.metalness = value;

    });

    sceneGUI.add(settings, 'roughness').min(0).max(1).onChange(function (value) {

      material.roughness = value;

    });

    sceneGUI.add(settings, 'ambientIntensity').min(0).max(1).onChange(function (value) {

      ambientLight.intensity = value;

    });

    sceneGUI.add(settings, 'displacementScale').min(0).max(500.0).onChange(function (value) {

      material.displacementScale = value;

    });

    sceneGUI.add(settings, 'displacementBias').min(-500).max(500).onChange(function (value) {

      material.displacementBias = value;

    });

    const meshGUI = gui.addFolder('Mesh');

    meshGUI.add(meshSettings.rotation, 'x').min(-Math.PI).max(Math.PI).step(0.0001).onChange(function (value) {

      mesh.rotation.x = value;

      stopAnimation = true;

    }).listen()





    meshGUI.add(meshSettings.rotation, 'y').min(-Math.PI).max(Math.PI).step(0.0001).onChange(function (value) {

      mesh.rotation.y = value;

      stopAnimation = true;

    }).listen()





    meshGUI.add(meshSettings.rotation, 'z').min(-Math.PI).max(Math.PI).step(0.0001).onChange(function (value) {

      mesh.rotation.z = value;

      stopAnimation = true;

    }).listen()





  </script>
</head>

<body>
</body>

</html>