-
Notifications
You must be signed in to change notification settings - Fork 352
Expand file tree
/
Copy pathscript.js
More file actions
118 lines (82 loc) · 2.75 KB
/
script.js
File metadata and controls
118 lines (82 loc) · 2.75 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
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader'
import rhino3dm from 'rhino3dm'
const downloadButton = document.getElementById("downloadButton")
downloadButton.onclick = download
let doc
let scene, camera, renderer
const rhino = await rhino3dm()
console.log('Loaded rhino3dm.')
init()
convert()
function convert () {
doc = new rhino.File3dm()
const loader = new OBJLoader( )
loader.load( 'Rhino_Logo.obj',
//onLoad
( obj ) => {
scene.add( obj )
const mesh = obj.children[ 0 ]
const rhinoMesh = rhino.Mesh.createFromThreejsJSON( { data: mesh.geometry } )
doc.objects().add( rhinoMesh, null )
},
//onProgress
( xhr ) => {
if ( xhr.lengthComputable ) {
const percentComplete = xhr.loaded / xhr.total * 100;
console.log( 'model ' + Math.round( percentComplete, 2 ) + '% loaded' );
}
},
//onError
() => {
console.log( 'Error' )
})
// hide spinner
document.getElementById('loader').style.display = 'none'
// enable download button
downloadButton.disabled = false
console.log(scene)
}
// download button handler
function download () {
const options = new rhino.File3dmWriteOptions()
options.version = 7
let buffer = doc.toByteArrayOptions(options)
saveByteArray( 'rhinoObjects'+options.version+'.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 //
function init () {
// Rhino models are z-up, so set this as the default
THREE.Object3D.DEFAULT_UP = new THREE.Vector3(0,0,1)
scene = new THREE.Scene()
scene.background = new THREE.Color(1,1,1)
camera = new THREE.PerspectiveCamera( 45, window.innerWidth/window.innerHeight, 1, 1000 )
camera.position.z = 50
renderer = new THREE.WebGLRenderer({antialias: true})
renderer.setPixelRatio( window.devicePixelRatio )
renderer.setSize( window.innerWidth, window.innerHeight )
document.body.appendChild( renderer.domElement )
const controls = new OrbitControls( camera, renderer.domElement )
const light = new THREE.DirectionalLight()
scene.add( light )
window.addEventListener( 'resize', onWindowResize, false )
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()
}