|
11 | 11 | </head>
|
12 | 12 |
|
13 | 13 | <body>
|
14 |
| - <div id="container"></div> |
15 | 14 | <div id="info">
|
16 | 15 | <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a>
|
17 | 16 | -
|
|
26 | 25 |
|
27 | 26 | import { IFCLoader } from './jsm/loaders/IFCLoader.js';
|
28 | 27 |
|
29 |
| - //Scene |
30 |
| - const scene = new THREE.Scene(); |
31 |
| - scene.background = new THREE.Color( 0x8cc7de ); |
32 |
| - |
33 |
| - //Renderer |
34 |
| - const container = document.querySelector( '#container' ); |
35 |
| - const renderer = new THREE.WebGLRenderer( { antialias: true } ); |
36 |
| - renderer.setSize( window.innerWidth, window.innerHeight ); |
37 |
| - renderer.setPixelRatio( Math.min( window.devicePixelRatio, 2 ) ); |
38 |
| - renderer.setAnimationLoop( animation ); |
39 |
| - container.appendChild( renderer.domElement ); |
40 |
| - |
41 |
| - //Camera |
42 |
| - const camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 ); |
43 |
| - camera.position.z = - 70; |
44 |
| - camera.position.y = 25; |
45 |
| - camera.position.x = 90; |
46 |
| - camera.lookAt( 0, 0, 0 ); |
47 |
| - const controls = new OrbitControls( camera, renderer.domElement ); |
48 |
| - |
49 |
| - //Initial cube |
50 |
| - const geometry = new THREE.BoxGeometry(); |
51 |
| - const material = new THREE.MeshPhongMaterial( { color: 0xffffff } ); |
52 |
| - const cube = new THREE.Mesh( geometry, material ); |
53 |
| - scene.add( cube ); |
54 |
| - |
55 |
| - //Lights |
56 |
| - const directionalLight1 = new THREE.DirectionalLight( 0xffeeff, 0.8 ); |
57 |
| - directionalLight1.position.set( 1, 1, 1 ); |
58 |
| - scene.add( directionalLight1 ); |
59 |
| - const directionalLight2 = new THREE.DirectionalLight( 0xffffff, 0.8 ); |
60 |
| - directionalLight2.position.set( - 1, 0.5, - 1 ); |
61 |
| - scene.add( directionalLight2 ); |
62 |
| - const ambientLight = new THREE.AmbientLight( 0xffffee, 0.25 ); |
63 |
| - scene.add( ambientLight ); |
64 |
| - |
65 |
| - //Window resize support |
66 |
| - window.addEventListener( 'resize', () => { |
| 28 | + let scene, camera, renderer; |
| 29 | + |
| 30 | + init(); |
| 31 | + |
| 32 | + function init() { |
| 33 | + |
| 34 | + //Scene |
| 35 | + scene = new THREE.Scene(); |
| 36 | + scene.background = new THREE.Color( 0x8cc7de ); |
| 37 | + |
| 38 | + //Camera |
| 39 | + camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 ); |
| 40 | + camera.position.z = - 70; |
| 41 | + camera.position.y = 25; |
| 42 | + camera.position.x = 90; |
| 43 | + |
| 44 | + //Initial cube |
| 45 | + const geometry = new THREE.BoxGeometry(); |
| 46 | + const material = new THREE.MeshPhongMaterial( { color: 0xffffff } ); |
| 47 | + const cube = new THREE.Mesh( geometry, material ); |
| 48 | + scene.add( cube ); |
| 49 | + |
| 50 | + //Lights |
| 51 | + const directionalLight1 = new THREE.DirectionalLight( 0xffeeff, 0.8 ); |
| 52 | + directionalLight1.position.set( 1, 1, 1 ); |
| 53 | + scene.add( directionalLight1 ); |
| 54 | + |
| 55 | + const directionalLight2 = new THREE.DirectionalLight( 0xffffff, 0.8 ); |
| 56 | + directionalLight2.position.set( - 1, 0.5, - 1 ); |
| 57 | + scene.add( directionalLight2 ); |
| 58 | + |
| 59 | + const ambientLight = new THREE.AmbientLight( 0xffffee, 0.25 ); |
| 60 | + scene.add( ambientLight ); |
| 61 | + |
| 62 | + //Setup IFC Loader |
| 63 | + const ifcLoader = new IFCLoader(); |
| 64 | + ifcLoader.load( 'models/ifc/rac_advanced_sample_project.ifc', function ( model ) { |
| 65 | + |
| 66 | + scene.add( model ); |
| 67 | + render(); |
| 68 | + |
| 69 | + } ); |
| 70 | + |
| 71 | + //Renderer |
| 72 | + renderer = new THREE.WebGLRenderer( { antialias: true } ); |
| 73 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 74 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 75 | + document.body.appendChild( renderer.domElement ); |
| 76 | + |
| 77 | + //Controls |
| 78 | + const controls = new OrbitControls( camera, renderer.domElement ); |
| 79 | + controls.addEventListener( 'change', render ); |
| 80 | + |
| 81 | + window.addEventListener( 'resize', onWindowResize ); |
| 82 | + |
| 83 | + render(); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + function onWindowResize() { |
67 | 88 |
|
68 | 89 | camera.aspect = window.innerWidth / window.innerHeight;
|
69 | 90 | camera.updateProjectionMatrix();
|
70 | 91 | renderer.setSize( window.innerWidth, window.innerHeight );
|
71 | 92 |
|
72 |
| - } ); |
| 93 | + render(); |
73 | 94 |
|
74 |
| - //Setup IFC Loader |
75 |
| - const ifcLoader = new IFCLoader(); |
76 |
| - |
77 |
| - //Load IFC file |
78 |
| - ifcLoader.load( 'models/ifc/rac_advanced_sample_project.ifc', ( geometry ) => scene.add( geometry ) ); |
| 95 | + } |
79 | 96 |
|
80 |
| - //Animation |
81 |
| - function animation() { |
| 97 | + function render() { |
82 | 98 |
|
83 |
| - controls.update(); |
84 | 99 | renderer.render( scene, camera );
|
85 | 100 |
|
86 | 101 | }
|
|
0 commit comments