Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
100 lines (75 loc) · 2.43 KB
/
Copy pathapp.js
File metadata and controls
100 lines (75 loc) · 2.43 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
const edge = require('electron-edge-js')
// construct the path to the .NET dll
const namespace = 'SampleRhinoInsideJavascriptTaskManagerLib'
const baseAppPath = '../' + namespace + '/bin/Debug/'
const baseDll = baseAppPath + namespace + '.dll'
console.log(baseDll)
const rhinoTypeName = namespace + '.RhinoMethods'
const startGrasshopper = edge.func({
assemblyFile: baseDll,
typeName: rhinoTypeName,
methodName: 'StartGrasshopper'
})
// wait for the rhino3dm web assembly to load asynchronously
let rhino
rhino3dm().then((m) => {
rhino = m // global
console.log('rhino3dm loaded.')
init()
var payload = {
cb: function (data, callback) {
callback(null, onGhObjectAdded(data))
}
}
startGrasshopper(payload, function(error, result) {
if (error) throw JSON.stringify(error)
console.log(result)
// hide spinner
document.getElementById('loader').style.display = 'none'
})
})
function onGhObjectAdded(data)
{
scene.traverseVisible(function(child) {
if (child.type !== 'Scene') {
scene.remove(child);
}
});
//convert this to object
var obj = JSON.parse(data);
var rhinoMesh = rhino.CommonObject.decode(obj);
let material = new THREE.MeshNormalMaterial();
var threeMesh = meshToThreejs(rhinoMesh, material);
scene.add(threeMesh);
}
function meshToThreejs (mesh, material) {
let loader = new THREE.BufferGeometryLoader()
let geometry = loader.parse(mesh.toThreejsJSON())
return new THREE.Mesh(geometry, material)
}
let scene, camera, renderer, controls
function init () {
scene = new THREE.Scene()
scene.background = new THREE.Color(1,1,1)
camera = new THREE.PerspectiveCamera( 45, window.innerWidth/window.innerHeight, 1, 1000 )
renderer = new THREE.WebGLRenderer({antialias: true})
renderer.setPixelRatio( window.devicePixelRatio )
renderer.setSize( window.innerWidth, window.innerHeight )
let canvas = document.getElementById('canvas')
canvas.appendChild( renderer.domElement )
controls = new THREE.OrbitControls( camera, renderer.domElement )
camera.position.z = 50
window.addEventListener( 'resize', onWindowResize, false )
animate()
}
const animate = function () {
requestAnimationFrame( animate )
controls.update()
renderer.render( scene, camera )
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize( window.innerWidth, window.innerHeight )
animate()
}