generated from iaac-macad/bimsc23-datamgmt-session03
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
251 lines (186 loc) · 7.36 KB
/
script.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Import libraries
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import { Rhino3dmLoader } from 'three/addons/loaders/3DMLoader.js'
import rhino3dm from 'rhino3dm'
import { RhinoCompute } from 'rhinocompute'
const definitionName = "hotdogjpeg.gh";
//const definitionName = document.getElementById("ShowMesh").checked ? "hotdogjpeg.gh" : "hotdogjpegEdges.gh";
// Set up sliders
const length_slider = document.getElementById("length");
length_slider.addEventListener("mouseup", onSliderChange, false);
length_slider.addEventListener("touchend", onSliderChange, false);
const rise_slider = document.getElementById("rise");
rise_slider.addEventListener("mouseup", onSliderChange, false);
rise_slider.addEventListener("touchend", onSliderChange, false);
const radius_slider = document.getElementById("radius");
radius_slider.addEventListener("mouseup", onSliderChange, false);
radius_slider.addEventListener("touchend", onSliderChange, false);
const boolean_slider = document.getElementById("boolean");
boolean_slider.addEventListener("mouseup", onSliderChange, false);
boolean_slider.addEventListener("touchend", onSliderChange, false);
const loader = new Rhino3dmLoader();
loader.setLibraryPath("https://cdn.jsdelivr.net/npm/[email protected]/");
let rhino, definition, doc;
rhino3dm().then(async (m) => {
console.log("Loaded rhino3dm.");
rhino = m; // global
//Local Computer (Turn on / off by uncommenting the line below)
RhinoCompute.url = "http://localhost:8081/"; //if debugging locally.
//Use MaCAD server (Turn on / off by uncommenting the two lines below) **Backslash at end every important
//RhinoCompute.url = 'http://35.157.191.153/' // RhinoCompute server url. Use http://localhost:8081 if debugging locally.
//RhinoCompute.apiKey = 'macad2023' // RhinoCompute server api key. Leave blank if debugging locally.
//Set API key
RhinoCompute.apiKey = 'macad2023'
// Set default value above. If checkbox is checked, change to server. If changed again change back to local.
const computeCheckbox = document.getElementById("computeCheckbox");
computeCheckbox.addEventListener("change", function() {
if (computeCheckbox.checked) {
RhinoCompute.url = 'http://35.157.191.153/'
} else {
RhinoCompute.url = "http://localhost:8081/";
}
compute();
});
// load a grasshopper file!
const url = definitionName;
const res = await fetch(url);
const buffer = await res.arrayBuffer();
const arr = new Uint8Array(buffer);
definition = arr;
init();
compute();
});
async function compute() {
const param1 = new RhinoCompute.Grasshopper.DataTree("length");
param1.append([0], [length_slider.valueAsNumber]);
const param2 = new RhinoCompute.Grasshopper.DataTree("rise");
param2.append([0], [rise_slider.valueAsNumber]);
const param3 = new RhinoCompute.Grasshopper.DataTree("radius");
param3.append([0], [radius_slider.valueAsNumber]);
const param4 = new RhinoCompute.Grasshopper.DataTree("boolean");
param4.append([0], [boolean_slider.valueAsNumber]);
// clear values
const trees = [];
trees.push(param1);
trees.push(param2);
trees.push(param3);
trees.push(param4);
const res = await RhinoCompute.Grasshopper.evaluateDefinition(
definition,
trees
);
//console.log(res);
doc = new rhino.File3dm();
// hide spinner
document.getElementById("loader").style.display = "none";
//decode grasshopper objects and put them into a rhino document
for (let i = 0; i < res.values.length; i++) {
for (const [key, value] of Object.entries(res.values[i].InnerTree)) {
for (const d of value) {
const data = JSON.parse(d.data);
const rhinoObject = rhino.CommonObject.decode(data);
doc.objects().add(rhinoObject, null);
}
}
}
// go through the objects in the Rhino document
let objects = doc.objects();
for ( let i = 0; i < objects.count; i++ ) {
const rhinoObject = objects.get( i );
// asign geometry userstrings to object attributes
if ( rhinoObject.geometry().userStringCount > 0 ) {
const g_userStrings = rhinoObject.geometry().getUserStrings()
rhinoObject.attributes().setUserString(g_userStrings[0][0], g_userStrings[0][1])
}
}
// clear objects from scene
scene.traverse((child) => {
if (!child.isLight) {
scene.remove(child);
}
});
const buffer = new Uint8Array(doc.toByteArray()).buffer;
loader.parse(buffer, function (object) {
// go through all objects, check for userstrings and assing colors
object.traverse((child) => {
if (child.isLine) {
if (child.userData.attributes.geometry.userStringCount > 0) {
//get color from userStrings
const colorData = child.userData.attributes.userStrings[0]
const col = colorData[1];
//convert color from userstring to THREE color and assign it
const threeColor = new THREE.Color("rgb(" + col + ")");
const mat = new THREE.LineBasicMaterial({ color: threeColor });
child.material = mat;
}
}
});
///////////////////////////////////////////////////////////////////////
// add object graph from rhino model to three.js scene
scene.add(object);
});
}
function onSliderChange() {
// show spinner
document.getElementById("loader").style.display = "block";
compute();
}
// THREE BOILERPLATE //
let scene, camera, renderer, controls;
function init() {
// Z Up
THREE.Object3D.DefaultUp = new THREE.Vector3( 0, 0, 1 )
// create a scene and a camera
scene = new THREE.Scene();
scene.background = new THREE.Color( "rgb(255, 212, 212)" );
camera = new THREE.PerspectiveCamera(
15,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.x = -30;
camera.position.y = -30;
camera.position.z = 30;
// create the renderer and add it to the html
renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true });
//renderer.setSize(window.innerWidth, window.innerHeight);
// changed rendered from full screen to 1440
// renderer.setSize(1440, 697);
renderer.setSize(window.innerWidth*0.6, window.innerHeight*0.6);
document.body.appendChild(renderer.domElement);
// add some controls to orbit the camera
controls = new OrbitControls(camera, renderer.domElement);
// add a directional light
const directionalLight = new THREE.DirectionalLight(0xffffff);
directionalLight.intensity = 2;
scene.add(directionalLight);
const ambientLight = new THREE.AmbientLight();
scene.add(ambientLight);
animate();
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
animate();
}
function meshToThreejs(mesh, material) {
const loader = new THREE.BufferGeometryLoader();
const geometry = loader.parse(mesh.toThreejsJSON());
return new THREE.Mesh(geometry, material);
}
// Download a JPG when button Download is clicked
document.getElementById("download").addEventListener("click", function() {
var dataURL = renderer.domElement.toDataURL( "image/jpeg" );
// download the image
var link = document.createElement( "a" );
link.download = "hotdog_JPEG.jpeg";
link.href = dataURL;
link.click();
});