forked from robertoranon/int3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17-shadowMapping.html
156 lines (148 loc) · 4.62 KB
/
17-shadowMapping.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>interactive 3D graphics - shadow mapping example </title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
</style>
</head>
<body>
<script src="lib/three.min.js"></script>
<script src="lib/stats.min.js"></script>
<script src="lib/OrbitControls.js"></script>
<script src='lib/dat.gui.min.js'></script>
<script>
var camera, scene, renderer, clock, stats;
var dirLight, spotLight;
var torusKnot, cube;
var gui;
var params = {
shadowBias: 0.0,
shadowResolution: 1024,
};
init();
animate();
function init() {
initScene();
initMisc();
document.body.appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize, false );
}
function initScene() {
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 15, 35 );
scene = new THREE.Scene();
// Lights
scene.add( new THREE.AmbientLight( 0x404040 ) );
spotLight = new THREE.SpotLight( 0xffffff );
spotLight.name = 'Spot Light';
spotLight.angle = Math.PI / 5;
spotLight.penumbra = 0.3;
spotLight.position.set( 10, 10, 5 );
spotLight.castShadow = true;
spotLight.shadow.camera.near = 8;
spotLight.shadow.camera.far = 30;
// try changing the size
spotLight.shadow.mapSize.width = 1024;
spotLight.shadow.mapSize.height = 1024;
scene.add( spotLight );
scene.add( new THREE.CameraHelper( spotLight.shadow.camera ) );
// Geometry
var geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 20 );
var material = new THREE.MeshStandardMaterial( {
color: 0xff0000,
roughness: 0.1,
metalness: 0,
} );
torusKnot = new THREE.Mesh( geometry, material );
torusKnot.scale.multiplyScalar( 1 / 18 );
torusKnot.position.y = 3;
torusKnot.castShadow = true;
torusKnot.receiveShadow = true;
scene.add( torusKnot );
var geometry = new THREE.BoxGeometry( 3, 3, 3 );
cube = new THREE.Mesh( geometry, material );
cube.position.set( -6, 1.5, 1.5 );
cube.castShadow = true;
cube.receiveShadow = true;
scene.add( cube );
var geometry = new THREE.BoxGeometry( 10, 0.15, 10 );
var material = new THREE.MeshStandardMaterial( {
color: 0xaaaaaa,
roughness: 0.6,
metalness: 0,
} );
var ground = new THREE.Mesh( geometry, material );
ground.scale.multiplyScalar( 3 );
ground.castShadow = false;
ground.receiveShadow = true;
scene.add( ground );
}
function initMisc() {
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setClearColor( 0x000000 );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
// try change the shadow map filter
//renderer.shadowMap.type = THREE.BasicShadowMap;
//renderer.shadowMap.type = THREE.PCFShadowMap;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// Mouse control
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.target.set( 0, 2, 0 );
controls.update();
clock = new THREE.Clock();
stats = new Stats();
document.body.appendChild( stats.dom );
gui = new dat.GUI();
gui.add( params, 'shadowResolution', [64, 128, 256, 512, 1024] ).onChange(
function(value) {
spotLight.shadow.mapSize.width = value;
spotLight.shadow.mapSize.height = value;
});
gui.add( params, 'shadowBias', -0.01, 0.01 ).onChange( function() {
spotLight.shadow.bias = params.shadowBias;
});
gui.open();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function renderScene() {
renderer.render( scene, camera );
}
function render() {
var delta = clock.getDelta();
renderScene();
torusKnot.rotation.x += 0.15 * delta;
torusKnot.rotation.y += 1 * delta;
torusKnot.rotation.z += 0.5 * delta;
}
</script>
</body>
</html>