1- import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected] /build/three.module.js' 2- import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected] /examples/jsm/controls/OrbitControls.js' 1+ import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected] /build/three.module.js' 2+ import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected] /examples/jsm/controls/OrbitControls.js' 3+ import { Rhino3dmLoader } from 'https://cdn.jsdelivr.net/npm/[email protected] /examples/jsm/loaders/3DMLoader.js' 34import rhino3dm from 'https://cdn.jsdelivr.net/npm/[email protected] /rhino3dm.module.js' 45
56/* eslint no-undef: "off", no-unused-vars: "off" */
67
8+ const loader = new Rhino3dmLoader ( )
9+ loader . setLibraryPath ( 'https://cdn.jsdelivr.net/npm/[email protected] /' ) 10+
711const definition = 'BranchNodeRnd.gh'
812
913// setup input change events
@@ -18,7 +22,7 @@ length_slider.addEventListener( 'mouseup', onSliderChange, false )
1822length_slider . addEventListener ( 'touchend' , onSliderChange , false )
1923
2024// load the rhino3dm library
21- let rhino
25+ let rhino , doc
2226rhino3dm ( ) . then ( async m => {
2327 console . log ( 'Loaded rhino3dm.' )
2428 rhino = m // global
@@ -66,28 +70,32 @@ async function compute(){
6670 headers = response . headers . get ( 'server-timing' )
6771 const responseJson = await response . json ( )
6872
73+ collectResults ( responseJson )
74+
6975 // Request finished. Do processing here.
7076 let t1 = performance . now ( )
7177 const computeSolveTime = t1 - timeComputeStart
7278 t0 = t1
7379
7480 // hide spinner
75- document . getElementById ( 'loader' ) . style . display = 'none'
81+ //document.getElementById('loader').style.display = 'none'
82+ //showSpinner(false)
7683 //console.log(responseJson.values[0])
77- let data = JSON . parse ( responseJson . values [ 0 ] . InnerTree [ '{0}' ] [ 0 ] . data )
78- let mesh = rhino . DracoCompression . decompressBase64String ( data )
84+ // let data = JSON.parse(responseJson.values[0].InnerTree['{0}'][0].data)
85+ // let mesh = rhino.DracoCompression.decompressBase64String(data)
7986
8087 t1 = performance . now ( )
8188 const decodeMeshTime = t1 - t0
8289 t0 = t1
83-
90+ /*
8491 if (!_threeMaterial) {
8592 _threeMaterial = new THREE.MeshNormalMaterial()
8693 }
94+
8795 let threeMesh = meshToThreejs(mesh, _threeMaterial)
8896 mesh.delete()
8997 replaceCurrentMesh(threeMesh)
90-
98+ */
9199 t1 = performance . now ( )
92100 const rebuildSceneTime = t1 - t0
93101
@@ -117,13 +125,106 @@ async function compute(){
117125
118126}
119127
128+ /**
129+ * Parse response
130+ */
131+ function collectResults ( responseJson ) {
132+
133+ const values = responseJson . values
134+
135+ // clear doc
136+ if ( doc !== undefined )
137+ doc . delete ( )
138+
139+ //console.log(values)
140+ doc = new rhino . File3dm ( )
141+
142+ // for each output (RH_OUT:*)...
143+ for ( let i = 0 ; i < values . length ; i ++ ) {
144+ // ...iterate through data tree structure...
145+ for ( const path in values [ i ] . InnerTree ) {
146+ const branch = values [ i ] . InnerTree [ path ]
147+ // ...and for each branch...
148+ for ( let j = 0 ; j < branch . length ; j ++ ) {
149+ // ...load rhino geometry into doc
150+ const rhinoObject = decodeItem ( branch [ j ] )
151+ if ( rhinoObject !== null ) {
152+ doc . objects ( ) . add ( rhinoObject , null )
153+ }
154+ }
155+ }
156+ }
157+
158+ if ( doc . objects ( ) . count < 1 ) {
159+ console . error ( 'No rhino objects to load!' )
160+ showSpinner ( false )
161+ return
162+ }
163+
164+ // load rhino doc into three.js scene
165+ const buffer = new Uint8Array ( doc . toByteArray ( ) ) . buffer
166+ loader . parse ( buffer , function ( object )
167+ {
168+ // debug
169+
170+ object . traverse ( child => {
171+ if ( child . material )
172+ child . material = new THREE . MeshNormalMaterial ( )
173+ } , false )
174+
175+
176+ // clear objects from scene. do this here to avoid blink
177+ scene . traverse ( child => {
178+ if ( ! child . isLight && child . name !== 'context' ) {
179+ scene . remove ( child )
180+ }
181+ } )
182+
183+ // add object graph from rhino model to three.js scene
184+ scene . add ( object )
185+
186+ // hide spinner and enable download button
187+ showSpinner ( false )
188+ //downloadButton.disabled = false
189+
190+ // zoom to extents
191+ //zoomCameraToSelection(camera, controls, scene.children)
192+ } )
193+ }
194+
195+ /**
196+ * Shows or hides the loading spinner
197+ */
198+ function showSpinner ( enable ) {
199+ if ( enable )
200+ document . getElementById ( 'loader' ) . style . display = 'block'
201+ else
202+ document . getElementById ( 'loader' ) . style . display = 'none'
203+ }
204+
205+ /**
206+ * Attempt to decode data tree item to rhino geometry
207+ */
208+ function decodeItem ( item ) {
209+ const data = JSON . parse ( item . data )
210+ if ( item . type === 'System.String' ) {
211+ // hack for draco meshes
212+ try {
213+ return rhino . DracoCompression . decompressBase64String ( data )
214+ } catch { } // ignore errors (maybe the string was just a string...)
215+ } else if ( typeof data === 'object' ) {
216+ return rhino . CommonObject . decode ( data )
217+ }
218+ return null
219+ }
220+
120221/**
121222 * Called when a slider value changes in the UI. Collect all of the
122223 * slider values and call compute to solve for a new scene
123224 */
124225function onSliderChange ( ) {
125226 // show spinner
126- document . getElementById ( 'loader' ) . style . display = 'block'
227+ showSpinner ( true )
127228 compute ( )
128229}
129230
@@ -165,18 +266,3 @@ function onWindowResize() {
165266 renderer . setSize ( window . innerWidth , window . innerHeight )
166267 animate ( )
167268}
168-
169- function replaceCurrentMesh ( threeMesh ) {
170- if ( _threeMesh ) {
171- scene . remove ( _threeMesh )
172- _threeMesh . geometry . dispose ( )
173- }
174- _threeMesh = threeMesh
175- scene . add ( _threeMesh )
176- }
177-
178- function meshToThreejs ( mesh , material ) {
179- let loader = new THREE . BufferGeometryLoader ( )
180- var geometry = loader . parse ( mesh . toThreejsJSON ( ) )
181- return new THREE . Mesh ( geometry , material )
182- }
0 commit comments