-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
/
webgl_geometry_csg.html
234 lines (182 loc) · 6.1 KB
/
webgl_geometry_csg.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js geometry - csg</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">
<style>
body {
background-color: #eeeeee;
color: #333;
}
a {
color: #009688;
text-decoration: underline;
}
</style>
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> bvh csg - <a href="https://github.com/gkjohnson/three-bvh-csg" target="_blank" rel="noopener">three-bvh-csg</a><br/>
See <a href="https://github.com/gkjohnson/three-bvh-csg" target="_blank" rel="noopener">main project repository</a> for more information and examples on constructive solid geometry.
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/",
"three-mesh-bvh": "https://unpkg.com/three-mesh-bvh@0.6.0/build/index.module.js",
"three-bvh-csg": "https://unpkg.com/three-bvh-csg@0.0.7/build/index.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
import { SUBTRACTION, INTERSECTION, ADDITION, Brush, Evaluator } from 'three-bvh-csg';
let stats;
let camera, scene, renderer;
let baseBrush, brush;
let core;
let result, evaluator, wireframe;
const params = {
operation: SUBTRACTION,
useGroups: true,
wireframe: false,
};
init();
animate();
function init() {
// environment
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 100 );
camera.position.set( - 1, 1, 1 ).normalize().multiplyScalar( 10 );
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xfce4ec );
// lights
const ambient = new THREE.HemisphereLight( 0xffffff, 0xbfd4d2, 0.9 );
scene.add( ambient );
const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.1 );
directionalLight.position.set( 1, 4, 3 ).multiplyScalar( 3 );
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.setScalar( 2048 );
directionalLight.shadow.bias = - 1e-4;
directionalLight.shadow.normalBias = 1e-4;
scene.add( directionalLight );
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.appendChild( renderer.domElement );
stats = new Stats();
document.body.appendChild( stats.dom );
// add shadow plane
const plane = new THREE.Mesh(
new THREE.PlaneGeometry(),
new THREE.ShadowMaterial( {
color: 0xd81b60,
transparent: true,
opacity: 0.075,
side: THREE.DoubleSide,
} ),
);
plane.position.y = - 3;
plane.rotation.x = - Math.PI / 2;
plane.scale.setScalar( 10 );
plane.receiveShadow = true;
scene.add( plane );
// create brushes
evaluator = new Evaluator();
baseBrush = new Brush(
new THREE.IcosahedronGeometry( 2, 3 ),
new THREE.MeshStandardMaterial( {
flatShading: true,
polygonOffset: true,
polygonOffsetUnits: 1,
polygonOffsetFactor: 1,
} ),
);
brush = new Brush(
new THREE.CylinderGeometry( 1, 1, 5, 45 ),
new THREE.MeshStandardMaterial( {
color: 0x80cbc4,
polygonOffset: true,
polygonOffsetUnits: 1,
polygonOffsetFactor: 1,
} ),
);
core = new Brush(
new THREE.IcosahedronGeometry( 0.15, 1 ),
new THREE.MeshStandardMaterial( {
flatShading: true,
color: 0xff9800,
emissive: 0xff9800,
emissiveIntensity: 0.35,
polygonOffset: true,
polygonOffsetUnits: 1,
polygonOffsetFactor: 1,
} ),
);
core.castShadow = true;
scene.add( core );
// create wireframe
wireframe = new THREE.Mesh(
undefined,
new THREE.MeshBasicMaterial( { color: 0x009688, wireframe: true } ),
);
scene.add( wireframe );
// controls
const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 5;
controls.maxDistance = 75;
// set up gui
const gui = new GUI();
gui.add( params, 'operation', { SUBTRACTION, INTERSECTION, ADDITION } );
gui.add( params, 'wireframe' );
gui.add( params, 'useGroups' );
window.addEventListener( 'resize', onWindowResize );
onWindowResize();
}
function updateCSG() {
evaluator.useGroups = params.useGroups;
result = evaluator.evaluate( baseBrush, brush, params.operation, result );
result.castShadow = true;
result.receiveShadow = true;
scene.add( result );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
// update the transforms
const t = window.performance.now() + 9000;
baseBrush.rotation.x = t * 0.0001;
baseBrush.rotation.y = t * 0.00025;
baseBrush.rotation.z = t * 0.0005;
baseBrush.updateMatrixWorld();
brush.rotation.x = t * - 0.0002;
brush.rotation.y = t * - 0.0005;
brush.rotation.z = t * - 0.001;
const s = 0.5 + 0.5 * ( 1 + Math.sin( t * 0.001 ) );
brush.scale.set( s, 1, s );
brush.updateMatrixWorld();
// update the csg
updateCSG();
wireframe.geometry = result.geometry;
wireframe.visible = params.wireframe;
renderer.render( scene, camera );
stats.update();
}
</script>
</body>
</html>