-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlantern.html
167 lines (136 loc) · 6.05 KB
/
lantern.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
<html>
<head>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<script src="three.min.js"></script>
<script src="ThreeCSG.js"></script>
<link rel="stylesheet" href="../etudes.css">
</head>
<body>
<h1 class="white">Lantern <a href="https://boytchev.github.io/etudes/">←</a></h1>
<script>
// create the scene
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animate );
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.shadowMap.mapSize = new THREE.Vector2( 128, 128 );
document.body.appendChild( renderer.domElement );
document.body.style.margin = 0;
document.body.style.overflow = 'hidden';
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 30, window.innerWidth/window.innerHeight, 0.1, 1000 );
camera.position.set( 0, 20, 30 );
camera.lookAt( new THREE.Vector3( 0, -2, 0 ) );
// constructing the die
var body = new THREE.BoxGeometry( 4, 4, 4 ),
bsp = new ThreeBSP(body);
bsp = bsp.subtract(new ThreeBSP(new THREE.BoxGeometry( 3.7, 3.7, 3.7 )));
var d = 0.9; // gaps
// straight cuts
var hole = new THREE.Mesh( new THREE.BoxGeometry(0.15, 10, 10) );
//hole.position.set( -d, 0, 0 ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( 0, 0, 0 ); bsp = bsp.subtract(new ThreeBSP(hole));
//hole.position.set( d, 0, 0 ); bsp = bsp.subtract(new ThreeBSP(hole));
var hole = new THREE.Mesh( new THREE.BoxGeometry(10, 0.15, 10) );
//hole.position.set( 0, -d, 0 ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( 0, 0, 0 ); bsp = bsp.subtract(new ThreeBSP(hole));
//hole.position.set( 0, d, 0 ); bsp = bsp.subtract(new ThreeBSP(hole));
var hole = new THREE.Mesh( new THREE.BoxGeometry(10, 10, 0.15) );
//hole.position.set( 0, 0, -d ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( 0, 0, 0 ); bsp = bsp.subtract(new ThreeBSP(hole));
//hole.position.set( 0, 0, d ); bsp = bsp.subtract(new ThreeBSP(hole));
// circular cuts
var hole = new THREE.Mesh( new THREE.CylinderGeometry(0.4,0.4,1,24) );
hole.geometry.openEnded = true;
// 2
hole.position.set( -d,2,-d ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( d,2,d ); bsp = bsp.subtract(new ThreeBSP(hole));
// 5
hole.position.set( -d,-2,-d ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( -d,-2,d ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( d,-2,d ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( d,-2,-d ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( 0,-2,0 ); bsp = bsp.subtract(new ThreeBSP(hole));
// 1
hole.rotation.z = Math.PI/2;
hole.position.set( 2,0,0 ); bsp = bsp.subtract(new ThreeBSP(hole));
// 6
hole.position.set( -2,-d,-d ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( -2,-d,0 ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( -2,-d,d ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( -2,d,-d ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( -2,d,0 ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( -2,d,d ); bsp = bsp.subtract(new ThreeBSP(hole));
// 3
hole.rotation.y = Math.PI/2;
hole.position.set( -d,-d,2 ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( 0,0,2 ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( d,d,2 ); bsp = bsp.subtract(new ThreeBSP(hole));
// 4
hole.position.set( -d,-d,-2 ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( -d,d,-2 ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( d,-d,-2 ); bsp = bsp.subtract(new ThreeBSP(hole));
hole.position.set( d,d,-2 ); bsp = bsp.subtract(new ThreeBSP(hole));
var lantern = bsp.toMesh(new THREE.MeshPhongMaterial({
emissive: 'navy',
shininess: 100,
emissiveIntensity: 0.3,
polygonOffset: true,
polygonOffsetFactor:1 }));
lantern.castShadow = true;
scene.add( lantern );
var edges = new THREE.EdgesGeometry( body ),
wireframe = new THREE.LineSegments( edges, new THREE.LineBasicMaterial( {
color: 'cornflowerblue',
transparent:true,
opacity:0.5 } ) );
lantern.add( wireframe );
// platform around the lantern
var geometry = new THREE.BoxGeometry( 100, 1, 100),
material = new THREE.MeshPhongMaterial( {color:'cornflowerblue'} );
platform = new THREE.Mesh( geometry, material );
platform.position.set( 0,-5,0 );
platform.receiveShadow = true;
scene.add( platform );
// light inside the lantern
var lanternLight = new THREE.SpotLight( 'lightyellow', 2 );
lanternLight.position.set( 0, 1, 0 );
lanternLight.target = scene;
lanternLight.angle = 1;
lanternLight.penumbra = 0.5;
lanternLight.castShadow = true;
lanternLight.shadow.mapSize.width = 1024;
lanternLight.shadow.mapSize.height = 1024;
scene.add( lanternLight );
// light on top of the lantern
var lanternTopLight = new THREE.SpotLight( 'crimson', 1/3 );
lanternTopLight.position.set( 0, 10, 0 );
lanternTopLight.target = scene;
lanternTopLight.angle = 0.6;
lanternTopLight.penumbra = 1;
scene.add( lanternTopLight );
// light from above
var topLight = new THREE.SpotLight( 'white', 1/3 );
topLight.position.set( 0, 5, 5 );
topLight.angle = 2;
topLight.penumbra = 1;
scene.add( topLight );
var clock = new THREE.Clock( true );
window.addEventListener( 'resize', onWindowResize, false );
onWindowResize();
function onWindowResize( event )
{
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight, true );
}
function animate()
{
var time = clock.getElapsedTime();
lantern.rotation.set( time/2, 2*time, time );
renderer.render( scene, camera );
}
</script>
</body>
</html>