-
Notifications
You must be signed in to change notification settings - Fork 336
/
Copy pathscript.js
159 lines (116 loc) · 4.75 KB
/
script.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Import libraries
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import rhino3dm from 'rhino3dm'
import { RhinoCompute } from 'rhinocompute'
let model = {
// main sphere
sphere: null,
sphereRadius: 10,
// spheres array to clash with main sphere
spheres: [],
spheresRadius: 2,
// number of spheres to create
num: 200,
};
let scene, camera, renderer
const rhino = await rhino3dm()
console.log('Loaded rhino3dm.');
RhinoCompute.url = getAuth('RHINO_COMPUTE_URL') // RhinoCompute server url. Use http://localhost:8081/ if debugging locally.
RhinoCompute.apiKey = getAuth('RHINO_COMPUTE_KEY') // RhinoCompute server api key. Leave blank if debugging locally.
init();
compute();
function getAuth( key ) {
let value = localStorage[key]
if ( value === undefined ) {
const prompt = key.includes('URL') ? 'Server URL' : 'Server API Key'
value = window.prompt('RhinoCompute ' + prompt)
if ( value !== null ) {
localStorage.setItem( key, value )
}
}
return value
}
function compute() {
console.log('Creating main sphere');
// create and render 3js sphere
const mainSphere = new THREE.SphereGeometry(model.sphereRadius, 32, 32);
const material = new THREE.MeshStandardMaterial({wireframe:true});
const mainMesh = new THREE.Mesh(mainSphere, material);
mainMesh.rotateOnWorldAxis(new THREE.Vector3(1,0,0), THREE.MathUtils.degToRad(-90));
scene.add(mainMesh);
// create 3dm sphere
let mainRhinoSphere = rhino.Mesh.createFromThreejsJSON( { data: mainSphere } )
model.sphere = mainRhinoSphere;
createClashSpheres();
doMeshClash();
}
function createClashSpheres() {
console.log('Creating clash spheres');
let pointsGeometry = new THREE.BufferGeometry();
let positionBuffer = new Float32Array(3 * model.num);
for (let i = 0; i < model.num; i++) {
let x = Math.random() * (20 - -20) + -20;
let y = Math.random() * (20 - -20) + -20;
let z = Math.random() * (20 - -20) + -20;
//create 3js clash sphere
let clashSphere = new THREE.SphereGeometry( model.spheresRadius, 10, 10 );
clashSphere.translate(x, y, z);
//create 3dm clash sphere
let rhinoClashSphere = rhino.Mesh.createFromThreejsJSON( { data: clashSphere } );
model.spheres.push(rhinoClashSphere);
positionBuffer[i*3] = x;
positionBuffer[i*3+1] = y;
positionBuffer[i*3+2] = z;
}
//vizualize the position of the clash spheres
pointsGeometry.setAttribute( 'position', new THREE.BufferAttribute( positionBuffer, 3 ) );
let points = new THREE.Points(pointsGeometry, new THREE.PointsMaterial( { color: 0xff0000, sizeAttenuation: false, size: 3 } ));
scene.add(points);
}
function doMeshClash() {
console.log('Running mesh clash');
RhinoCompute.computeFetch('rhino/geometry/intersect/meshclash/search', [model.sphere, model.spheres, 0.1, 5])
.then(function (result) {
console.log(result);
// remove spinner
document.getElementById('loader').remove();
//add objects to scene
for (var i = 0; i < result.length; i++) {
let m = rhino.CommonObject.decode(result[i].MeshB);
let material = new THREE.MeshBasicMaterial({ wireframe: true, color: 0x00ff00 });
const loader = new THREE.BufferGeometryLoader();
const geometry = loader.parse((m.toThreejsJSON()));
let threemesh = new THREE.Mesh(geometry, material);
//need to rotate these
threemesh.rotateOnWorldAxis(new THREE.Vector3(1,0,0), THREE.MathUtils.degToRad(-90));
scene.add(threemesh);
m.delete();
}
});
}
// BOILERPLATE //
function init(){
scene = new THREE.Scene();
scene.background = new THREE.Color(1,1,1);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
camera.position.y = 30
camera.position.z = 30
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 );
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()
}