generated from iaac-macad/bimsc23-datamgmt-session02
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
123 lines (85 loc) · 3.43 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
// Import libraries
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import { Rhino3dmLoader } from 'three/addons/loaders/3DMLoader.js'
let camera, scene, raycaster, renderer
const mouse = new THREE.Vector2()
window.addEventListener('click', onClick);
init()
animate()
function init() {
THREE.Object3D.DefaultUp = new THREE.Vector3( 0, 0, 1 )
// create a scene and a camera
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 = - 100
// create the renderer and add it to the html
renderer = new THREE.WebGLRenderer( { antialias: true } )
renderer.setSize( window.innerWidth, window.innerHeight )
document.body.appendChild( renderer.domElement )
const controls = new OrbitControls( camera, renderer.domElement )
const directionalLight = new THREE.DirectionalLight( 0xffffff )
directionalLight.position.set( 0, 0, 2 )
directionalLight.castShadow = true
directionalLight.intensity = 2
scene.add( directionalLight )
raycaster = new THREE.Raycaster()
const loader = new Rhino3dmLoader()
loader.setLibraryPath( 'https://cdn.jsdelivr.net/npm/[email protected]/' )
loader.load( 'sphere.3dm', function ( object ) {
document.getElementById('loader').remove()
scene.add( object )
console.log( object )
} )
}
function onClick( event ) {
console.log( `click! (${event.clientX}, ${event.clientY})`)
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1
raycaster.setFromCamera( mouse, camera )
// calculate objects intersecting the picking ray
const intersects = raycaster.intersectObjects( scene.children, true )
let container = document.getElementById( 'container' )
if (container) container.remove()
// reset object colours
scene.traverse((child, i) => {
if (child.isMesh) {
child.material.color.set( 'white' )
}
});
if (intersects.length > 0) {
// get closest object
const object = intersects[0].object
console.log(object) // debug
object.material.color.set( 'yellow' )
// get user strings
let data, count
if (object.userData.attributes !== undefined) {
data = object.userData.attributes.userStrings
} else {
// breps store user strings differently...
data = object.parent.userData.attributes.userStrings
}
// do nothing if no user strings
if ( data === undefined ) return
console.log( data )
// create container div with table inside
container = document.createElement( 'div' )
container.id = 'container'
const table = document.createElement( 'table' )
container.appendChild( table )
for ( let i = 0; i < data.length; i ++ ) {
const row = document.createElement( 'tr' )
row.innerHTML = `<td>${data[ i ][ 0 ]}</td><td>${data[ i ][ 1 ]}</td>`
table.appendChild( row )
}
document.body.appendChild( container )
}
}
function animate() {
requestAnimationFrame( animate )
renderer.render( scene, camera )
}