11/* eslint no-undef: "off", no-unused-vars: "off" */
2- import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected] /build/three.module.js' 3- import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected] /examples/jsm/controls/OrbitControls.js' 2+ import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected] /build/three.module.js' 3+ import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected] /examples/jsm/controls/OrbitControls.js' 4+ import { Rhino3dmLoader } from 'https://cdn.jsdelivr.net/npm/[email protected] /examples/jsm/loaders/3DMLoader.js' 45import rhino3dm from 'https://cdn.jsdelivr.net/npm/[email protected] /rhino3dm.module.js' 56
7+ // set up loader for converting the results to threejs
8+ const loader = new Rhino3dmLoader ( )
9+ loader . setLibraryPath ( 'https://cdn.jsdelivr.net/npm/[email protected] /' ) 10+
611const definition = 'srf_kmeans.gh'
712
813// setup input change events
@@ -67,9 +72,7 @@ async function compute(){
6772
6873 // Request finished. Do processing here.
6974
70- // hide spinner
71- document . getElementById ( 'loader' ) . style . display = 'none'
72-
75+ /*
7376 // process mesh
7477 let mesh_data = JSON.parse(responseJson.values[0].InnerTree['{0}'][0].data)
7578 let mesh = rhino.CommonObject.decode(mesh_data)
@@ -80,7 +83,7 @@ async function compute(){
8083 let threeMesh = meshToThreejs(mesh, _threeMaterial)
8184 mesh.delete()
8285 replaceCurrentMesh(threeMesh)
83-
86+ */
8487 //process data
8588 //console.log(responseJson.values[1])
8689 let cluster_data = responseJson . values [ 1 ] . InnerTree [ '{0;0}' ] . map ( d => d . data )
@@ -191,3 +194,101 @@ function meshToThreejs (mesh, material) {
191194 var geometry = loader . parse ( mesh . toThreejsJSON ( ) )
192195 return new THREE . Mesh ( geometry , material )
193196}
197+
198+ /**
199+ * Parse response
200+ */
201+ function collectResults ( responseJson ) {
202+
203+ const values = responseJson . values
204+
205+ // clear doc
206+ if ( doc !== undefined )
207+ doc . delete ( )
208+
209+ //console.log(values)
210+ doc = new rhino . File3dm ( )
211+
212+ // for each output (RH_OUT:*)...
213+ for ( let i = 0 ; i < values . length ; i ++ ) {
214+ // ...iterate through data tree structure...
215+ for ( const path in values [ i ] . InnerTree ) {
216+ const branch = values [ i ] . InnerTree [ path ]
217+ // ...and for each branch...
218+ for ( let j = 0 ; j < branch . length ; j ++ ) {
219+ // ...load rhino geometry into doc
220+ const rhinoObject = decodeItem ( branch [ j ] )
221+ if ( rhinoObject !== null ) {
222+ doc . objects ( ) . add ( rhinoObject , null )
223+ }
224+ }
225+ }
226+ }
227+
228+ if ( doc . objects ( ) . count < 1 ) {
229+ console . error ( 'No rhino objects to load!' )
230+ showSpinner ( false )
231+ return
232+ }
233+
234+ // hack (https://github.com/mcneel/rhino3dm/issues/353)
235+ const sphereAttrs = new rhino . ObjectAttributes ( )
236+ sphereAttrs . mode = rhino . ObjectMode . Hidden
237+ doc . objects ( ) . addSphere ( new rhino . Sphere ( [ 0 , 0 , 0 ] , 0.001 ) , sphereAttrs )
238+
239+ // load rhino doc into three.js scene
240+ const buffer = new Uint8Array ( doc . toByteArray ( ) ) . buffer
241+ loader . parse ( buffer , function ( object )
242+ {
243+ // debug
244+ /*
245+ object.traverse(child => {
246+ if (child.material !== undefined)
247+ child.material = new THREE.MeshNormalMaterial()
248+ }, false)
249+ */
250+
251+ // clear objects from scene. do this here to avoid blink
252+ scene . traverse ( child => {
253+ if ( ! child . isLight && child . name !== 'context' ) {
254+ scene . remove ( child )
255+ }
256+ } )
257+
258+ // add object graph from rhino model to three.js scene
259+ scene . add ( object )
260+
261+ // hide spinner and enable download button
262+ showSpinner ( false )
263+ downloadButton . disabled = false
264+
265+ // zoom to extents
266+ zoomCameraToSelection ( camera , controls , scene . children )
267+ } )
268+ }
269+
270+ /**
271+ * Attempt to decode data tree item to rhino geometry
272+ */
273+ function decodeItem ( item ) {
274+ const data = JSON . parse ( item . data )
275+ if ( item . type === 'System.String' ) {
276+ // hack for draco meshes
277+ try {
278+ return rhino . DracoCompression . decompressBase64String ( data )
279+ } catch { } // ignore errors (maybe the string was just a string...)
280+ } else if ( typeof data === 'object' ) {
281+ return rhino . CommonObject . decode ( data )
282+ }
283+ return null
284+ }
285+
286+ /**
287+ * Shows or hides the loading spinner
288+ */
289+ function showSpinner ( enable ) {
290+ if ( enable )
291+ document . getElementById ( 'loader' ) . style . display = 'block'
292+ else
293+ document . getElementById ( 'loader' ) . style . display = 'none'
294+ }
0 commit comments