|
31 | 31 |
|
32 | 32 | import { IESLoader } from 'three/addons/loaders/IESLoader.js'; |
33 | 33 |
|
| 34 | + import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; |
| 35 | + |
34 | 36 | let renderer, scene, camera; |
35 | 37 | let lights; |
36 | 38 |
|
|
53 | 55 | // |
54 | 56 |
|
55 | 57 | const spotLight = new THREE.IESSpotLight( 0xff0000, 500 ); |
56 | | - spotLight.position.set( 6.5, 1.5, 6.5 ); |
| 58 | + spotLight.position.set( 6.5, 3, 6.5 ); |
57 | 59 | spotLight.angle = Math.PI / 8; |
58 | 60 | spotLight.penumbra = 0.7; |
59 | 61 | spotLight.distance = 20; |
| 62 | + spotLight.castShadow = true; |
60 | 63 | spotLight.iesMap = iesTexture1; |
| 64 | + spotLight.userData.helper = new THREE.SpotLightHelper( spotLight ); |
61 | 65 | scene.add( spotLight ); |
| 66 | + scene.add( spotLight.target ); |
| 67 | + scene.add( spotLight.userData.helper ); |
62 | 68 |
|
63 | 69 | // |
64 | 70 |
|
65 | 71 | const spotLight2 = new THREE.IESSpotLight( 0x00ff00, 500 ); |
66 | | - spotLight2.position.set( 6.5, 1.5, - 6.5 ); |
| 72 | + spotLight2.position.set( - 6.5, 3, 6.5 ); |
67 | 73 | spotLight2.angle = Math.PI / 8; |
68 | 74 | spotLight2.penumbra = 0.7; |
69 | 75 | spotLight2.distance = 20; |
| 76 | + spotLight2.castShadow = true; |
70 | 77 | spotLight2.iesMap = iesTexture2; |
| 78 | + spotLight2.userData.helper = new THREE.SpotLightHelper( spotLight2 ); |
71 | 79 | scene.add( spotLight2 ); |
| 80 | + scene.add( spotLight2.target ); |
| 81 | + scene.add( spotLight2.userData.helper ); |
72 | 82 |
|
73 | 83 | // |
74 | 84 |
|
75 | 85 | const spotLight3 = new THREE.IESSpotLight( 0x0000ff, 500 ); |
76 | | - spotLight3.position.set( - 6.5, 1.5, - 6.5 ); |
| 86 | + spotLight3.position.set( - 6.5, 3, - 6.5 ); |
77 | 87 | spotLight3.angle = Math.PI / 8; |
78 | 88 | spotLight3.penumbra = 0.7; |
79 | 89 | spotLight3.distance = 20; |
| 90 | + spotLight3.castShadow = true; |
80 | 91 | spotLight3.iesMap = iesTexture3; |
| 92 | + spotLight3.userData.helper = new THREE.SpotLightHelper( spotLight3 ); |
81 | 93 | scene.add( spotLight3 ); |
| 94 | + scene.add( spotLight3.target ); |
| 95 | + scene.add( spotLight3.userData.helper ); |
82 | 96 |
|
83 | 97 | // |
84 | 98 |
|
85 | 99 | const spotLight4 = new THREE.IESSpotLight( 0xffffff, 500 ); |
86 | | - spotLight4.position.set( - 6.5, 1.5, 6.5 ); |
| 100 | + spotLight4.position.set( 6.5, 3, - 6.5 ); |
87 | 101 | spotLight4.angle = Math.PI / 8; |
88 | 102 | spotLight4.penumbra = 0.7; |
89 | 103 | spotLight4.distance = 20; |
| 104 | + spotLight4.castShadow = true; |
90 | 105 | spotLight4.iesMap = iesTexture4; |
| 106 | + spotLight4.userData.helper = new THREE.SpotLightHelper( spotLight4 ); |
91 | 107 | scene.add( spotLight4 ); |
| 108 | + scene.add( spotLight4.target ); |
| 109 | + scene.add( spotLight4.userData.helper ); |
92 | 110 |
|
93 | 111 | // |
94 | 112 |
|
95 | 113 | lights = [ spotLight, spotLight2, spotLight3, spotLight4 ]; |
96 | 114 |
|
97 | 115 | // |
98 | 116 |
|
99 | | - const material = new THREE.MeshPhongMaterial( { color: 0x808080/*, dithering: true*/ } ); |
| 117 | + const material = new THREE.MeshPhongMaterial( { color: 0x999999/*, dithering: true*/ } ); |
100 | 118 |
|
101 | 119 | const geometry = new THREE.PlaneGeometry( 200, 200 ); |
102 | 120 |
|
103 | 121 | const mesh = new THREE.Mesh( geometry, material ); |
104 | 122 | mesh.rotation.x = - Math.PI * 0.5; |
| 123 | + mesh.receiveShadow = true; |
105 | 124 | scene.add( mesh ); |
106 | 125 |
|
| 126 | + const geometry2 = new THREE.BoxGeometry( 2, 2, 2 ); |
| 127 | + //const geometry2 = new THREE.IcosahedronGeometry( 1, 5 ); |
| 128 | + |
| 129 | + const mesh2 = new THREE.Mesh( geometry2, material ); |
| 130 | + mesh2.position.y = 1; |
| 131 | + mesh2.castShadow = true; |
| 132 | + scene.add( mesh2 ); |
| 133 | + |
107 | 134 | // |
108 | 135 |
|
109 | 136 | renderer = new THREE.WebGPURenderer( { antialias: true } ); |
110 | 137 | renderer.setPixelRatio( window.devicePixelRatio ); |
111 | 138 | renderer.setSize( window.innerWidth, window.innerHeight ); |
112 | 139 | renderer.setAnimationLoop( render ); |
| 140 | + renderer.shadowMap.enabled = true; |
113 | 141 | document.body.appendChild( renderer.domElement ); |
114 | 142 |
|
115 | 143 | camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, .1, 100 ); |
|
122 | 150 |
|
123 | 151 | // |
124 | 152 |
|
| 153 | + function setHelperVisible( value ) { |
| 154 | + |
| 155 | + for ( let i = 0; i < lights.length; i ++ ) { |
| 156 | + |
| 157 | + lights[ i ].userData.helper.visible = value; |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + setHelperVisible( false ); |
| 164 | + |
| 165 | + // |
| 166 | + |
| 167 | + const gui = new GUI(); |
| 168 | + |
| 169 | + gui.add( { helper: false }, 'helper' ).onChange( ( v ) => setHelperVisible( v ) ); |
| 170 | + |
| 171 | + gui.open(); |
| 172 | + |
| 173 | + // |
| 174 | + |
125 | 175 | window.addEventListener( 'resize', onWindowResize ); |
126 | 176 |
|
127 | 177 | } |
|
137 | 187 |
|
138 | 188 | function render( time ) { |
139 | 189 |
|
140 | | - time = ( time / 1000 ) * 2.0; |
| 190 | + time = time / 1000; |
141 | 191 |
|
142 | 192 | for ( let i = 0; i < lights.length; i ++ ) { |
143 | 193 |
|
144 | | - lights[ i ].position.y = Math.sin( time + i ) + 0.97; |
| 194 | + const t = ( Math.sin( ( time + i ) * ( Math.PI / 2 ) ) + 1 ) / 2; |
| 195 | + |
| 196 | + const x = THREE.MathUtils.lerp( lights[ i ].position.x, 0, t ); |
| 197 | + const z = THREE.MathUtils.lerp( lights[ i ].position.z, 0, t ); |
| 198 | + |
| 199 | + lights[ i ].target.position.x = x; |
| 200 | + lights[ i ].target.position.z = z; |
| 201 | + if ( lights[ i ].userData.helper ) lights[ i ].userData.helper.update(); |
145 | 202 |
|
146 | 203 | } |
147 | 204 |
|
|
0 commit comments