forked from iaac-macad-s1/lecture3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
194 lines (141 loc) · 5.31 KB
/
Copy pathscript.js
File metadata and controls
194 lines (141 loc) · 5.31 KB
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
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.124.0/build/three.module.js'
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.124.0/examples/jsm/controls/OrbitControls.js'
import { Rhino3dmLoader } from 'https://cdn.jsdelivr.net/npm/three@0.124.0/examples/jsm/loaders/3DMLoader.js'
import rhino3dm from 'https://cdn.jsdelivr.net/npm/rhino3dm@0.15.0-beta/rhino3dm.module.js'
import { RhinoCompute } from 'https://cdn.jsdelivr.net/npm/compute-rhino3d@0.13.0-beta/compute.rhino3d.module.js'
// reference the definition
const definitionName = 'rnd_node.gh'
// listen for slider change events
const count_slider = document.getElementById( 'count' )
count_slider.addEventListener( 'input', onSliderChange, false )
const radius_slider = document.getElementById( 'radius' )
radius_slider.addEventListener( 'input', onSliderChange, false )
const downloadButton = document.getElementById("downloadButton")
downloadButton.onclick = download
// set up loader for converting the results to threejs
const loader = new Rhino3dmLoader()
loader.setLibraryPath( 'https://cdn.jsdelivr.net/npm/rhino3dm@0.15.0-beta/' )
// create a few variables to store a reference to the rhino3dm library and to the loaded definition
let rhino, definition, doc
rhino3dm().then(async m => {
rhino = m
// local
//RhinoCompute.url = 'http://localhost:8081/' // Rhino.Compute server url
// remote
RhinoCompute.url = 'https://macad2021.compute.rhino3d.com/'
RhinoCompute.apiKey = getApiKey() // needed when calling a remote RhinoCompute server
// source a .gh/.ghx file in the same directory
let url = definitionName
let res = await fetch(url)
let buffer = await res.arrayBuffer()
definition = new Uint8Array(buffer)
init()
compute()
animate()
})
async function compute() {
// collect data
// get slider values
let count = document.getElementById('count').valueAsNumber
let radius = document.getElementById('radius').valueAsNumber
// format data
let param1 = new RhinoCompute.Grasshopper.DataTree('RH_IN:radius')
param1.append([0], [radius])
let param2 = new RhinoCompute.Grasshopper.DataTree('RH_IN:count')
param2.append([0], [count])
// Add all params to an array
let trees = []
trees.push(param1)
trees.push(param2)
// Call RhinoCompute
const res = await RhinoCompute.Grasshopper.evaluateDefinition(definition, trees)
console.log(res)
collectResults(res.values)
}
function collectResults(values) {
// clear doc
if( doc !== undefined)
doc.delete()
// clear objects from scene
scene.traverse(child => {
if (!child.isLight) {
scene.remove(child)
}
})
console.log(values)
doc = new rhino.File3dm()
for ( let i = 0; i < values.length; i ++ ) {
const list = values[i].InnerTree['{ 0; }']
for( let j = 0; j < list.length; j ++) {
const data = JSON.parse(values[i].InnerTree['{ 0; }'][j].data)
const rhinoObject = rhino.CommonObject.decode(data)
doc.objects().add(rhinoObject, null)
}
}
const buffer = new Uint8Array(doc.toByteArray()).buffer
loader.parse( buffer, function ( object )
{
scene.add( object )
// hide spinner
document.getElementById('loader').style.display = 'none'
// enable download button
downloadButton.disabled = false
})
}
function onSliderChange() {
// show spinner
document.getElementById('loader').style.display = 'block'
// disable download button
downloadButton.disabled = true
compute()
}
function getApiKey() {
let auth = null
auth = localStorage['compute_api_key']
if (auth == null) {
auth = window.prompt('RhinoCompute Server API Key')
if (auth != null) {
localStorage.setItem('compute_api_key', auth)
}
}
return auth
}
// download button handler
function download () {
let buffer = doc.toByteArray()
saveByteArray("node.3dm", buffer)
}
function saveByteArray ( fileName, byte ) {
let blob = new Blob([byte], {type: "application/octect-stream"})
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = fileName
link.click()
}
// BOILERPLATE //
// declare variables to store scene, camera, and renderer
let scene, camera, renderer
function init() {
// create a scene and a camera
scene = new THREE.Scene()
scene.background = new THREE.Color(1, 1, 1)
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
camera.position.z = - 30
// create the renderer and add it to the html
renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
// add some controls to orbit the camera
const 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 )
}
// function to continuously render the scene
function animate() {
requestAnimationFrame(animate)
renderer.render(scene, camera)
}