11/* eslint no-undef: "off", no-unused-vars: "off" */
22import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected] /build/three.module.js' 33import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected] /examples/jsm/controls/OrbitControls.js' 4+ import { TransformControls } from 'https://cdn.jsdelivr.net/npm/[email protected] /examples/jsm/controls/TransformControls.js' 45import { Rhino3dmLoader } from 'https://cdn.jsdelivr.net/npm/[email protected] /examples/jsm/loaders/3DMLoader.js' 56import rhino3dm from 'https://cdn.jsdelivr.net/npm/[email protected] /rhino3dm.module.js' 67
@@ -19,9 +20,6 @@ height_slider.addEventListener( 'mouseup', onSliderChange, false )
1920height_slider . addEventListener ( 'touchend' , onSliderChange , false )
2021
2122let points = [ ]
22- points . push ( "{\"X\":" + 279 + ",\"Y\":" + 218 + ",\"Z\":" + 0 + "}" )
23- points . push ( "{\"X\":" + - 181 + ",\"Y\":" + 218 + ",\"Z\":" + 0 + "}" )
24- points . push ( "{\"X\":" + 49 + ",\"Y\":" + - 212 + ",\"Z\":" + 0 + "}" )
2523
2624let rhino , doc
2725
@@ -30,13 +28,77 @@ rhino3dm().then(async m => {
3028 rhino = m // global
3129
3230 init ( )
31+ rndPts ( )
3332 compute ( )
3433} )
3534
35+ function rndPts ( ) {
36+ // generate random points
37+
38+ const cntPts = 3
39+ const bndX = dimension_slider . valueAsNumber / 2
40+ const bndY = dimension_slider . valueAsNumber / 2
41+
42+ for ( let i = 0 ; i < cntPts ; i ++ ) {
43+ const x = Math . random ( ) * ( bndX - - bndX ) + - bndX
44+ const y = Math . random ( ) * ( bndY - - bndY ) + - bndY
45+ const z = 0
46+
47+ const pt = "{\"X\":" + x + ",\"Y\":" + y + ",\"Z\":" + z + "}"
48+
49+ console . log ( `x ${ x } y ${ y } ` )
50+
51+ points . push ( pt )
52+
53+ //viz in three
54+ const icoGeo = new THREE . IcosahedronGeometry ( 25 )
55+ const icoMat = new THREE . MeshNormalMaterial ( )
56+ const ico = new THREE . Mesh ( icoGeo , icoMat )
57+ ico . name = 'ico'
58+ ico . position . set ( x , y , z )
59+ scene . add ( ico )
60+
61+ let tcontrols = new TransformControls ( camera , renderer . domElement )
62+ tcontrols . enabled = true
63+ tcontrols . attach ( ico )
64+ tcontrols . showZ = false
65+ tcontrols . addEventListener ( 'dragging-changed' , onChange )
66+ scene . add ( tcontrols )
67+
68+ }
69+
70+ }
71+
72+ let dragging = false
73+ function onChange ( ) {
74+ dragging = ! dragging
75+ if ( ! dragging ) {
76+ // update points position
77+ points = [ ]
78+ scene . traverse ( child => {
79+ if ( child . name === 'ico' ) {
80+ const pt = "{\"X\":" + child . position . x + ",\"Y\":" + child . position . y + ",\"Z\":" + child . position . z + "}"
81+ points . push ( pt )
82+ console . log ( pt )
83+ }
84+ } , false )
85+
86+ compute ( )
87+
88+ controls . enabled = true
89+ return
90+ }
91+
92+ controls . enabled = false
93+
94+ }
95+
3696/**
3797 * Call appserver
3898 */
39- async function compute ( ) {
99+ async function compute ( ) {
100+
101+ showSpinner ( true )
40102
41103 // initialise 'data' object that will be used by compute()
42104 const data = {
@@ -81,8 +143,8 @@ async function compute(){
81143
82144 // clear doc
83145 try {
84- if ( doc !== undefined )
85- doc . delete ( )
146+ if ( doc !== undefined )
147+ doc . delete ( )
86148 } catch { }
87149
88150 //console.log(values)
@@ -111,23 +173,21 @@ async function compute(){
111173 return
112174 }
113175
114- // hack (https://github.com/mcneel/rhino3dm/issues/353)
115- //doc.objects().addSphere(new rhino.Sphere([0,0,0], 0.001), null)
116-
117176 // load rhino doc into three.js scene
118177 const buffer = new Uint8Array ( doc . toByteArray ( ) ) . buffer
119178 loader . parse ( buffer , function ( object )
120179 {
180+
121181 // clear objects from scene
122182 scene . traverse ( child => {
123- if ( ! child . isLight ) {
124- scene . remove ( child )
183+ if ( child . userData . hasOwnProperty ( 'objectType' ) && child . userData . objectType === 'File3dm' ) {
184+ scene . remove ( child )
125185 }
126186 } )
127187
128188 ///////////////////////////////////////////////////////////////////////
129189
130- // render wireframe mesh
190+ // color crvs
131191 object . traverse ( child => {
132192 if ( child . isLine ) {
133193 if ( child . userData . attributes . geometry . userStringCount > 0 ) {
@@ -138,22 +198,16 @@ async function compute(){
138198 child . material = mat
139199 }
140200 }
141- } , false )
201+ } )
142202
143203 ///////////////////////////////////////////////////////////////////////
144-
145-
146-
147-
148204 // add object graph from rhino model to three.js scene
149205 scene . add ( object )
150206
151207 // hide spinner and enable download button
152208 showSpinner ( false )
153209 //downloadButton.disabled = false
154210
155- // zoom to extents
156- zoomCameraToSelection ( camera , controls , scene . children )
157211 } )
158212}
159213
@@ -204,10 +258,10 @@ function init () {
204258
205259 scene = new THREE . Scene ( )
206260 scene . background = new THREE . Color ( 1 , 1 , 1 )
207- camera = new THREE . PerspectiveCamera ( 45 , window . innerWidth / window . innerHeight , 1 , 1000 )
208- camera . position . x = 50
209- camera . position . y = 50
210- camera . position . z = 50
261+ camera = new THREE . PerspectiveCamera ( 45 , window . innerWidth / window . innerHeight , 1 , 10000 )
262+ camera . position . x = 1000
263+ camera . position . y = 1000
264+ camera . position . z = 1000
211265
212266 renderer = new THREE . WebGLRenderer ( { antialias : true } )
213267 renderer . setPixelRatio ( window . devicePixelRatio )
0 commit comments