|
26 | 26 |
|
27 | 27 | import * as THREE from 'three';
|
28 | 28 |
|
29 |
| - import Stats from 'three/addons/libs/stats.module.js'; |
| 29 | + import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; |
30 | 30 |
|
| 31 | + import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
31 | 32 | import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
|
32 | 33 | import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
|
33 | 34 | import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
|
34 | 35 | import { ShaderPass } from 'three/addons/postprocessing/ShaderPass.js';
|
35 | 36 | import { BleachBypassShader } from 'three/addons/shaders/BleachBypassShader.js';
|
36 | 37 | import { ColorCorrectionShader } from 'three/addons/shaders/ColorCorrectionShader.js';
|
37 |
| - import { FXAAShader } from 'three/addons/shaders/FXAAShader.js'; |
| 38 | + import { FXAAPass } from 'three/addons/postprocessing/FXAAPass.js'; |
38 | 39 | import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
|
39 | 40 |
|
40 |
| - let container, stats, loader; |
| 41 | + let container, loader; |
41 | 42 |
|
42 |
| - let camera, scene, renderer; |
| 43 | + let camera, scene, renderer, controls; |
43 | 44 |
|
44 | 45 | let mesh;
|
45 | 46 |
|
46 | 47 | let directionalLight, pointLight, ambientLight;
|
47 | 48 |
|
48 |
| - let mouseX = 0; |
49 |
| - let mouseY = 0; |
| 49 | + let composer; |
50 | 50 |
|
51 |
| - let targetX = 0; |
52 |
| - let targetY = 0; |
53 |
| - |
54 |
| - const windowHalfX = window.innerWidth / 2; |
55 |
| - const windowHalfY = window.innerHeight / 2; |
56 |
| - |
57 |
| - let composer, effectFXAA; |
| 51 | + const params = { |
| 52 | + enableNormalMap: true, |
| 53 | + normalScale: 1 |
| 54 | + }; |
58 | 55 |
|
59 | 56 | init();
|
60 | 57 |
|
|
63 | 60 | container = document.createElement( 'div' );
|
64 | 61 | document.body.appendChild( container );
|
65 | 62 |
|
66 |
| - camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 10000 ); |
| 63 | + camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 0.1, 100 ); |
67 | 64 | camera.position.z = 12;
|
68 | 65 |
|
69 | 66 | scene = new THREE.Scene();
|
|
100 | 97 | map: diffuseMap,
|
101 | 98 | specularMap: specularMap,
|
102 | 99 | normalMap: normalMap,
|
103 |
| - normalScale: new THREE.Vector2( 0.8, 0.8 ) |
| 100 | + normalScale: new THREE.Vector2( params.normalScale, params.normalScale ) |
104 | 101 | } );
|
105 | 102 |
|
106 | 103 | loader = new GLTFLoader();
|
|
122 | 119 | renderer.setAnimationLoop( animate );
|
123 | 120 | container.appendChild( renderer.domElement );
|
124 | 121 |
|
125 |
| - // |
126 |
| - |
127 |
| - stats = new Stats(); |
128 |
| - container.appendChild( stats.dom ); |
129 |
| - |
130 |
| - |
131 | 122 | // COMPOSER
|
132 | 123 |
|
133 | 124 | renderer.autoClear = false;
|
|
137 | 128 | const effectBleach = new ShaderPass( BleachBypassShader );
|
138 | 129 | const effectColor = new ShaderPass( ColorCorrectionShader );
|
139 | 130 | const outputPass = new OutputPass();
|
140 |
| - effectFXAA = new ShaderPass( FXAAShader ); |
141 |
| - |
142 |
| - effectFXAA.uniforms[ 'resolution' ].value.set( 1 / window.innerWidth, 1 / window.innerHeight ); |
| 131 | + const effectFXAA = new FXAAPass(); |
143 | 132 |
|
144 | 133 | effectBleach.uniforms[ 'opacity' ].value = 0.2;
|
145 | 134 |
|
|
158 | 147 |
|
159 | 148 | // EVENTS
|
160 | 149 |
|
161 |
| - document.addEventListener( 'mousemove', onDocumentMouseMove ); |
162 | 150 | window.addEventListener( 'resize', onWindowResize );
|
163 | 151 |
|
| 152 | + // GUI |
| 153 | + |
| 154 | + const gui = new GUI(); |
| 155 | + |
| 156 | + gui.add( params, 'enableNormalMap' ).name( 'enable normal map' ).onChange( ( value ) => { |
| 157 | + |
| 158 | + material.normalMap = ( value === true ) ? normalMap : null; |
| 159 | + material.needsUpdate = true; |
| 160 | + |
| 161 | + } ); |
| 162 | + gui.add( params, 'normalScale', 0, 2 ).name( 'normal scale' ).onChange( ( value )=> material.normalScale.setScalar( value ) ); |
| 163 | + gui.open(); |
| 164 | + |
| 165 | + // CONTROLS |
| 166 | + |
| 167 | + controls = new OrbitControls( camera, renderer.domElement ); |
| 168 | + controls.minDistance = 8; |
| 169 | + controls.maxDistance = 50; |
| 170 | + controls.enablePan = false; |
| 171 | + controls.enableDamping = true; |
| 172 | + |
164 | 173 | }
|
165 | 174 |
|
166 | 175 | //
|
|
176 | 185 | renderer.setSize( width, height );
|
177 | 186 | composer.setSize( width, height );
|
178 | 187 |
|
179 |
| - effectFXAA.uniforms[ 'resolution' ].value.set( 1 / width, 1 / height ); |
180 |
| - |
181 |
| - } |
182 |
| - |
183 |
| - function onDocumentMouseMove( event ) { |
184 |
| - |
185 |
| - mouseX = ( event.clientX - windowHalfX ); |
186 |
| - mouseY = ( event.clientY - windowHalfY ); |
187 |
| - |
188 | 188 | }
|
189 | 189 |
|
190 | 190 | //
|
191 | 191 |
|
192 | 192 | function animate() {
|
193 | 193 |
|
194 |
| - render(); |
195 |
| - |
196 |
| - stats.update(); |
197 |
| - |
198 |
| - } |
199 |
| - |
200 |
| - function render() { |
201 |
| - |
202 |
| - targetX = mouseX * .001; |
203 |
| - targetY = mouseY * .001; |
204 |
| - |
205 |
| - if ( mesh ) { |
206 |
| - |
207 |
| - mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y ); |
208 |
| - mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x ); |
209 |
| - |
210 |
| - } |
| 194 | + controls.update(); |
211 | 195 |
|
212 | 196 | composer.render();
|
213 | 197 |
|
|
0 commit comments