webgl_loader_ldraw.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - LDrawLoader</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. color: #444;
  11. }
  12. a {
  13. color: #08f;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="info">
  19. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - LDrawLoader
  20. </div>
  21. <script type="module">
  22. import * as THREE from '../build/three.module.js';
  23. import { GUI } from './jsm/libs/dat.gui.module.js';
  24. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  25. import { LDrawEnvironment } from './jsm/environments/LDrawEnvironment.js';
  26. import { LDrawLoader } from './jsm/loaders/LDrawLoader.js';
  27. let container, progressBarDiv;
  28. let camera, scene, renderer, controls, gui, guiData;
  29. let model, textureCube;
  30. const ldrawPath = 'models/ldraw/officialLibrary/';
  31. const modelFileList = {
  32. 'Car': 'models/car.ldr_Packed.mpd',
  33. 'Lunar Vehicle': 'models/1621-1-LunarMPVVehicle.mpd_Packed.mpd',
  34. 'Radar Truck': 'models/889-1-RadarTruck.mpd_Packed.mpd',
  35. 'Trailer': 'models/4838-1-MiniVehicles.mpd_Packed.mpd',
  36. 'Bulldozer': 'models/4915-1-MiniConstruction.mpd_Packed.mpd',
  37. 'Helicopter': 'models/4918-1-MiniFlyers.mpd_Packed.mpd',
  38. 'Plane': 'models/5935-1-IslandHopper.mpd_Packed.mpd',
  39. 'Lighthouse': 'models/30023-1-Lighthouse.ldr_Packed.mpd',
  40. 'X-Wing mini': 'models/30051-1-X-wingFighter-Mini.mpd_Packed.mpd',
  41. 'AT-ST mini': 'models/30054-1-AT-ST-Mini.mpd_Packed.mpd',
  42. 'AT-AT mini': 'models/4489-1-AT-AT-Mini.mpd_Packed.mpd',
  43. 'Shuttle': 'models/4494-1-Imperial Shuttle-Mini.mpd_Packed.mpd',
  44. 'TIE Interceptor': 'models/6965-1-TIEIntercep_4h4MXk5.mpd_Packed.mpd',
  45. 'Star fighter': 'models/6966-1-JediStarfighter-Mini.mpd_Packed.mpd',
  46. 'X-Wing': 'models/7140-1-X-wingFighter.mpd_Packed.mpd',
  47. 'AT-ST': 'models/10174-1-ImperialAT-ST-UCS.mpd_Packed.mpd'
  48. };
  49. init();
  50. animate();
  51. function init() {
  52. container = document.createElement( 'div' );
  53. document.body.appendChild( container );
  54. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  55. camera.position.set( 150, 200, 250 );
  56. //
  57. renderer = new THREE.WebGLRenderer( { antialias: true } );
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.outputEncoding = THREE.sRGBEncoding;
  61. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  62. container.appendChild( renderer.domElement );
  63. // scene
  64. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  65. scene = new THREE.Scene();
  66. scene.background = new THREE.Color( 0xdeebed );
  67. scene.environment = pmremGenerator.fromScene( new LDrawEnvironment() ).texture;
  68. controls = new OrbitControls( camera, renderer.domElement );
  69. //
  70. guiData = {
  71. modelFileName: modelFileList[ 'Car' ],
  72. separateObjects: false,
  73. displayLines: true,
  74. conditionalLines: true,
  75. smoothNormals: true,
  76. constructionStep: 0,
  77. noConstructionSteps: "No steps."
  78. };
  79. window.addEventListener( 'resize', onWindowResize );
  80. progressBarDiv = document.createElement( 'div' );
  81. progressBarDiv.innerText = "Loading...";
  82. progressBarDiv.style.fontSize = "3em";
  83. progressBarDiv.style.color = "#888";
  84. progressBarDiv.style.display = "block";
  85. progressBarDiv.style.position = "absolute";
  86. progressBarDiv.style.top = "50%";
  87. progressBarDiv.style.width = "100%";
  88. progressBarDiv.style.textAlign = "center";
  89. // load materials and then the model
  90. reloadObject( true );
  91. }
  92. function updateObjectsVisibility() {
  93. model.traverse( c => {
  94. if ( c.isLineSegments ) {
  95. if ( c.isConditionalLine ) {
  96. c.visible = guiData.conditionalLines;
  97. } else {
  98. c.visible = guiData.displayLines;
  99. }
  100. } else if ( c.isGroup ) {
  101. // Hide objects with construction step > gui setting
  102. c.visible = c.userData.constructionStep <= guiData.constructionStep;
  103. }
  104. } );
  105. }
  106. function reloadObject( resetCamera ) {
  107. if ( model ) {
  108. scene.remove( model );
  109. }
  110. model = null;
  111. updateProgressBar( 0 );
  112. showProgressBar();
  113. const lDrawLoader = new LDrawLoader();
  114. lDrawLoader.separateObjects = guiData.separateObjects;
  115. lDrawLoader.smoothNormals = guiData.smoothNormals;
  116. lDrawLoader
  117. .setPath( ldrawPath )
  118. .load( guiData.modelFileName, function ( group2 ) {
  119. if ( model ) {
  120. scene.remove( model );
  121. }
  122. model = group2;
  123. // Convert from LDraw coordinates: rotate 180 degrees around OX
  124. model.rotation.x = Math.PI;
  125. scene.add( model );
  126. guiData.constructionStep = model.userData.numConstructionSteps - 1;
  127. updateObjectsVisibility();
  128. // Adjust camera and light
  129. const bbox = new THREE.Box3().setFromObject( model );
  130. const size = bbox.getSize( new THREE.Vector3() );
  131. const radius = Math.max( size.x, Math.max( size.y, size.z ) ) * 0.5;
  132. if ( resetCamera ) {
  133. controls.target0.copy( bbox.getCenter( new THREE.Vector3() ) );
  134. controls.position0.set( - 2.3, 1, 2 ).multiplyScalar( radius ).add( controls.target0 );
  135. controls.reset();
  136. }
  137. createGUI();
  138. hideProgressBar();
  139. }, onProgress, onError );
  140. }
  141. function onWindowResize() {
  142. camera.aspect = window.innerWidth / window.innerHeight;
  143. camera.updateProjectionMatrix();
  144. renderer.setSize( window.innerWidth, window.innerHeight );
  145. }
  146. function createGUI() {
  147. if ( gui ) {
  148. gui.destroy();
  149. }
  150. gui = new GUI();
  151. gui.add( guiData, 'modelFileName', modelFileList ).name( 'Model' ).onFinishChange( function () {
  152. reloadObject( true );
  153. } );
  154. gui.add( guiData, 'separateObjects' ).name( 'Separate Objects' ).onChange( function () {
  155. reloadObject( false );
  156. } );
  157. if ( guiData.separateObjects ) {
  158. if ( model.userData.numConstructionSteps > 1 ) {
  159. gui.add( guiData, 'constructionStep', 0, model.userData.numConstructionSteps - 1 ).step( 1 ).name( 'Construction step' ).onChange( updateObjectsVisibility );
  160. } else {
  161. gui.add( guiData, 'noConstructionSteps' ).name( 'Construction step' ).onChange( updateObjectsVisibility );
  162. }
  163. }
  164. gui.add( guiData, 'smoothNormals' ).name( 'Smooth Normals' ).onChange( function changeNormals() {
  165. reloadObject( false );
  166. } );
  167. gui.add( guiData, 'displayLines' ).name( 'Display Lines' ).onChange( updateObjectsVisibility );
  168. gui.add( guiData, 'conditionalLines' ).name( 'Conditional Lines' ).onChange( updateObjectsVisibility );
  169. }
  170. //
  171. function animate() {
  172. requestAnimationFrame( animate );
  173. render();
  174. }
  175. function render() {
  176. renderer.render( scene, camera );
  177. }
  178. function onProgress( xhr ) {
  179. if ( xhr.lengthComputable ) {
  180. updateProgressBar( xhr.loaded / xhr.total );
  181. console.log( Math.round( xhr.loaded / xhr.total * 100, 2 ) + '% downloaded' );
  182. }
  183. }
  184. function onError() {
  185. const message = "Error loading model";
  186. progressBarDiv.innerText = message;
  187. console.log( message );
  188. }
  189. function showProgressBar() {
  190. document.body.appendChild( progressBarDiv );
  191. }
  192. function hideProgressBar() {
  193. document.body.removeChild( progressBarDiv );
  194. }
  195. function updateProgressBar( fraction ) {
  196. progressBarDiv.innerText = 'Loading... ' + Math.round( fraction * 100, 2 ) + '%';
  197. }
  198. </script>
  199. <!-- LDraw.org CC BY 2.0 Parts Library attribution -->
  200. <div style="display: block; position: absolute; bottom: 8px; left: 8px; width: 160px; padding: 10px; background-color: #F3F7F8;">
  201. <center>
  202. <a href="http://www.ldraw.org"><img style="width: 145px" src="models/ldraw/ldraw_org_logo/Stamp145.png"></a>
  203. <br />
  204. <a href="http://www.ldraw.org/">This software uses the LDraw Parts Library</a>
  205. </center>
  206. </div>
  207. </body>
  208. </html>