forked from mrdoob/three.js
/
webgl_shadowmap_pointlight.html
182 lines (127 loc) · 4.63 KB
/
webgl_shadowmap_pointlight.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - THREE.PointLight ShadowMap</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="main.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - THREE.PointLight ShadowMap by <a href="https://github.com/mkkellogg">mkkellogg</a>
</div>
<script type="module">
import * as THREE from '../build/three.module.js';
import Stats from './jsm/libs/stats.module.js';
import { OrbitControls } from './jsm/controls/OrbitControls.js';
let camera, scene, renderer, stats;
let pointLight, pointLight2;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 10, 40 );
scene = new THREE.Scene();
scene.add( new THREE.AmbientLight( 0x111122 ) );
// lights
function createLight( color ) {
const intensity = 1.5;
const light = new THREE.PointLight( color, intensity, 20 );
light.castShadow = true;
light.shadow.bias = - 0.005; // reduces self-shadowing on double-sided objects
let geometry = new THREE.SphereGeometry( 0.3, 12, 6 );
let material = new THREE.MeshBasicMaterial( { color: color } );
material.color.multiplyScalar( intensity );
let sphere = new THREE.Mesh( geometry, material );
light.add( sphere );
const texture = new THREE.CanvasTexture( generateTexture() );
texture.magFilter = THREE.NearestFilter;
texture.wrapT = THREE.RepeatWrapping;
texture.wrapS = THREE.RepeatWrapping;
texture.repeat.set( 1, 4.5 );
geometry = new THREE.SphereGeometry( 2, 32, 8 );
material = new THREE.MeshPhongMaterial( {
side: THREE.DoubleSide,
alphaMap: texture,
alphaTest: 0.5
} );
sphere = new THREE.Mesh( geometry, material );
sphere.castShadow = true;
sphere.receiveShadow = true;
light.add( sphere );
// custom distance material
const distanceMaterial = new THREE.MeshDistanceMaterial( {
alphaMap: material.alphaMap,
alphaTest: material.alphaTest
} );
sphere.customDistanceMaterial = distanceMaterial;
return light;
}
pointLight = createLight( 0x0088ff );
scene.add( pointLight );
pointLight2 = createLight( 0xff8888 );
scene.add( pointLight2 );
//
const geometry = new THREE.BoxGeometry( 30, 30, 30 );
const material = new THREE.MeshPhongMaterial( {
color: 0xa0adaf,
shininess: 10,
specular: 0x111111,
side: THREE.BackSide
} );
const mesh = new THREE.Mesh( geometry, material );
mesh.position.y = 10;
mesh.receiveShadow = true;
scene.add( mesh );
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.BasicShadowMap;
document.body.appendChild( renderer.domElement );
const controls = new OrbitControls( camera, renderer.domElement );
controls.target.set( 0, 10, 0 );
controls.update();
stats = new Stats();
document.body.appendChild( stats.dom );
//
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function generateTexture() {
const canvas = document.createElement( 'canvas' );
canvas.width = 2;
canvas.height = 2;
const context = canvas.getContext( '2d' );
context.fillStyle = 'white';
context.fillRect( 0, 1, 2, 1 );
return canvas;
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
let time = performance.now() * 0.001;
pointLight.position.x = Math.sin( time * 0.6 ) * 9;
pointLight.position.y = Math.sin( time * 0.7 ) * 9 + 6;
pointLight.position.z = Math.sin( time * 0.8 ) * 9;
pointLight.rotation.x = time;
pointLight.rotation.z = time;
time += 10000;
pointLight2.position.x = Math.sin( time * 0.6 ) * 9;
pointLight2.position.y = Math.sin( time * 0.7 ) * 9 + 6;
pointLight2.position.z = Math.sin( time * 0.8 ) * 9;
pointLight2.rotation.x = time;
pointLight2.rotation.z = time;
renderer.render( scene, camera );
stats.update();
}
</script>
</body>
</html>