-
Notifications
You must be signed in to change notification settings - Fork 0
/
cubedrawer.js
189 lines (172 loc) · 5.36 KB
/
cubedrawer.js
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
const canvasSketch = require("canvas-sketch");
// Ensure ThreeJS is in global scope for the 'examples/'
global.THREE = require("three");
function save(blob, filename) {
var link = document.createElement("a");
link.style.display = "none";
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
// URL.revokeObjectURL( url ); breaks Firefox...
}
function saveString(text, filename) {
save(new Blob([text], { type: "text/plain" }), filename);
}
function saveArrayBuffer(buffer, filename) {
save(new Blob([buffer], { type: "application/octet-stream" }), filename);
}
// Include any additional ThreeJS examples below
require("three/examples/js/controls/OrbitControls");
require("three/examples/js/exporters/GLTFExporter");
const settings = {
// Make the loop animated
animate: true,
// Get a WebGL canvas rather than 2D
context: "webgl",
// Turn on MSAA
attributes: { antialias: true },
};
const sketch = ({ context }) => {
// Create a renderer
const renderer = new THREE.WebGLRenderer({
context,
});
// WebGL background color
renderer.setClearColor("#000", 1);
// Setup a camera
const camera = new THREE.PerspectiveCamera(25, 1, 0.01, 100);
camera.position.set(-1, -3, -2);
camera.lookAt(new THREE.Vector3());
// Setup camera controller
const controls = new THREE.OrbitControls(camera, context.canvas);
// Setup your scene
const scene = new THREE.Scene();
const empty = new THREE.Object3D();
scene.add(empty);
empty.position.set(-0.5, -0.5, 0);
const mat = new THREE.MeshPhysicalMaterial({
roughness: 0.75,
flatShading: true,
color: "white",
});
const addCube = (x, y, s, zSize) => {
const mesh = new THREE.Mesh(new THREE.BoxBufferGeometry(s, s, zSize), mat);
mesh.position.set(x + s / 2, y + s / 2, -zSize / 2);
empty.add(mesh);
};
const addArc = (x, y, s, zSize) => {
const shapes = [
{ thetaStart: 0, xOff: 0, yOff: s },
{ thetaStart: Math.PI / 2, xOff: 0, yOff: 0 },
{ thetaStart: Math.PI, xOff: s, yOff: 0 },
{ thetaStart: (Math.PI * 3) / 2, xOff: s, yOff: s },
];
const shape = shapes[Math.floor(shapes.length * Math.random())];
const geom = new THREE.CylinderGeometry(
s,
s,
zSize,
32,
1,
false,
shape.thetaStart,
Math.PI / 2
);
geom.faces.push(
new THREE.Face3(0, 67, 33),
new THREE.Face3(0, 66, 67),
new THREE.Face3(66, 65, 67),
new THREE.Face3(66, 32, 65)
);
const mesh = new THREE.Mesh(geom, mat);
mesh.position.set(x + shape.xOff, y + shape.yOff, -zSize / 2);
mesh.rotateOnAxis(new THREE.Vector3(1, 0, 0), Math.PI / 2);
empty.add(mesh);
};
const addCyl = (x, y, s, zSize) => {
const mesh = new THREE.Mesh(
new THREE.CylinderBufferGeometry(
s / 2,
s / 2,
zSize,
32,
1,
false,
0,
Math.PI * 2
),
mat
);
mesh.position.set(x + s / 2, y + s / 2, -zSize / 2);
mesh.rotateOnAxis(new THREE.Vector3(1, 0, 0), Math.PI / 2);
empty.add(mesh);
};
const doSquare = (x, y, s) => {
const shouldSplit = Math.random() < s * 5;
if (shouldSplit) {
const hs = s / 2;
doSquare(x, y, hs);
doSquare(x + hs, y, hs);
doSquare(x, y + hs, hs);
doSquare(x + hs, y + hs, hs);
} else {
const zSize = 0.1 + 0.1 * Math.random();
const shapes = [addCube];
const shape = shapes[Math.floor(shapes.length * Math.random())];
shape(x, y, s, zSize);
}
};
doSquare(0, 0, 1);
var axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
const gltfExporter = new THREE.GLTFExporter();
var options = {
// trs: document.getElementById( 'option_trs' ).checked,
// onlyVisible: document.getElementById( 'option_visible' ).checked,
// truncateDrawRange: document.getElementById( 'option_drawrange' ).checked,
binary: true,
// forceIndices: document.getElementById( 'option_forceindices' ).checked,
// forcePowerOfTwoTextures: document.getElementById( 'option_forcepot' ).checked,
// maxTextureSize: Number( document.getElementById( 'option_maxsize' ).value ) || Infinity // To prevent NaN value
};
// gltfExporter.parse(
// empty,
// function (result) {
// if (result instanceof ArrayBuffer) {
// saveArrayBuffer(result, "scene.glb");
// } else {
// var output = JSON.stringify(result, null, 2);
// console.log(output);
// saveString(output, "scene.gltf");
// }
// },
// options
// );
// Specify an ambient/unlit colour
scene.add(new THREE.AmbientLight("#59314f"));
// Add some light
const light = new THREE.PointLight("#45caf7", 3, 15.5);
light.position.set(2, 2, -4).multiplyScalar(1.5);
scene.add(light);
// draw each frame
return {
// Handle resize events here
resize({ pixelRatio, viewportWidth, viewportHeight }) {
renderer.setPixelRatio(pixelRatio);
renderer.setSize(viewportWidth, viewportHeight);
camera.aspect = viewportWidth / viewportHeight;
camera.updateProjectionMatrix();
},
// Update & render your scene here
render({ time }) {
controls.update();
renderer.render(scene, camera);
},
// Dispose of events & renderer for cleaner hot-reloading
unload() {
controls.dispose();
renderer.dispose();
},
};
};
canvasSketch(sketch, settings);