This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
forked from chandlerprall/ThreeCSG
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexamples.html
133 lines (94 loc) · 3.74 KB
/
examples.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
<!DOCTYPE html>
<html>
<head>
<title>ThreeBSP Examples</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF8">
<style type="text/css">
html, body {
margin: 0;
padding: 0;
overflow: hidden;
background:black;
}
</style>
<script type="text/javascript" src="require.js"></script>
<script type="text/javascript" src="require.config.js"></script>
<script type="text/javascript">
require(["THREE","ThreeBSP"]); // Preload...
window.onload = function () {
require(["THREE","ThreeBSP"],function(THREE,ThreeBSP){
var renderer, scene, camera, light;
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById('viewport').appendChild(renderer.domElement);
scene = new THREE.Scene();
light = new THREE.DirectionalLight(0xffffff);
light.position.set(1, 1, 1).normalize();
scene.add(light);
camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
1,
1000
);
camera.position.set(5, 5, 15);
camera.lookAt(scene.position);
scene.add(camera);
// Example #1 - Cube (mesh) sub Sphere (mesh)
(function () {
var start_time = (new Date()).getTime();
var cube_geometry = new THREE.CubeGeometry(3, 3, 3);
var cube_mesh = new THREE.Mesh(cube_geometry);
cube_mesh.position.x = -7;
var cube_bsp = new ThreeBSP(cube_mesh);
var sphere_geometry = new THREE.SphereGeometry(1.8, 32, 32);
var sphere_mesh = new THREE.Mesh(sphere_geometry);
sphere_mesh.position.x = -7;
var sphere_bsp = new ThreeBSP(sphere_mesh);
var subtract_bsp = cube_bsp.subtract(sphere_bsp);
var result = subtract_bsp.toMesh(new THREE.MeshLambertMaterial({ shading: THREE.SmoothShading, map: THREE.ImageUtils.loadTexture('texture.png') }));
result.geometry.computeVertexNormals();
scene.add(result);
console.log('Example 1: ' + ((new Date()).getTime() - start_time) + 'ms');
})();
// Example #2 - Sphere (geometry) union Cube (geometry)
(function () {
var start_time = (new Date()).getTime();
var sphere_geometry = new THREE.SphereGeometry(2, 16, 16);
var sphere_bsp = new ThreeBSP(sphere_geometry);
var cube_geometry = new THREE.CubeGeometry(7, .5, 3);
var cube_bsp = new ThreeBSP(cube_geometry);
var union_bsp = sphere_bsp.union(cube_bsp);
var result = union_bsp.toMesh(new THREE.MeshLambertMaterial({ shading: THREE.SmoothShading, map: THREE.ImageUtils.loadTexture('texture.png') }));
result.geometry.computeVertexNormals();
scene.add(result);
console.log('Example 2: ' + ((new Date()).getTime() - start_time) + 'ms');
})();
// Example #3 - Sphere (geometry) intersect Sphere (mesh)
(function () {
var start_time = (new Date()).getTime();
var sphere_geometry_1 = new THREE.SphereGeometry(2, 64, 8);
var sphere_bsp_1 = new ThreeBSP(sphere_geometry_1);
var sphere_geometry_2 = new THREE.SphereGeometry(2, 8, 32);
var sphere_mesh_2 = new THREE.Mesh(sphere_geometry_2);
sphere_mesh_2.position.x = 2;
var sphere_bsp_2 = new ThreeBSP(sphere_mesh_2);
var intersect_bsp = sphere_bsp_1.intersect(sphere_bsp_2);
var result = intersect_bsp.toMesh(new THREE.MeshLambertMaterial({ shading: THREE.SmoothShading, map: THREE.ImageUtils.loadTexture('texture.png') }));
result.position.x = 6;
result.geometry.computeVertexNormals();
scene.add(result);
console.log('Example 3: ' + ((new Date()).getTime() - start_time) + 'ms');
})();
(function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
})();
});
}
</script>
</head>
<body>
<div id="viewport"></div>
</body>
</html>