webgl_loader_ldraw.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 { LDrawLoader } from './jsm/loaders/LDrawLoader.js';
  26. var container, progressBarDiv;
  27. var camera, scene, renderer, controls, gui, guiData;
  28. var modelFileList, model, textureCube;
  29. var envMapActivated = false;
  30. var ldrawPath = 'models/ldraw/officialLibrary/';
  31. var 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. // scene
  57. scene = new THREE.Scene();
  58. scene.background = new THREE.Color( 0xdeebed );
  59. var ambientLight = new THREE.AmbientLight( 0xdeebed, 0.4 );
  60. scene.add( ambientLight );
  61. var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
  62. directionalLight.position.set( - 1000, 1200, 1500 );
  63. scene.add( directionalLight );
  64. //
  65. renderer = new THREE.WebGLRenderer( { antialias: true } );
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. container.appendChild( renderer.domElement );
  69. controls = new OrbitControls( camera, renderer.domElement );
  70. //
  71. guiData = {
  72. modelFileName: modelFileList[ 'Car' ],
  73. envMapActivated: false,
  74. separateObjects: false,
  75. displayLines: true,
  76. conditionalLines: true,
  77. smoothNormals: true,
  78. constructionStep: 0,
  79. noConstructionSteps: "No steps."
  80. };
  81. window.addEventListener( 'resize', onWindowResize, false );
  82. progressBarDiv = document.createElement( 'div' );
  83. progressBarDiv.innerText = "Loading...";
  84. progressBarDiv.style.fontSize = "3em";
  85. progressBarDiv.style.color = "#888";
  86. progressBarDiv.style.display = "block";
  87. progressBarDiv.style.position = "absolute";
  88. progressBarDiv.style.top = "50%";
  89. progressBarDiv.style.width = "100%";
  90. progressBarDiv.style.textAlign = "center";
  91. // load materials and then the model
  92. reloadObject( true );
  93. }
  94. function updateObjectsVisibility() {
  95. model.traverse( c => {
  96. if ( c.isLineSegments ) {
  97. if ( c.isConditionalLine ) {
  98. c.visible = guiData.conditionalLines;
  99. } else {
  100. c.visible = guiData.displayLines;
  101. }
  102. }
  103. else if ( c.isGroup ) {
  104. // Hide objects with construction step > gui setting
  105. c.visible = c.userData.constructionStep <= guiData.constructionStep;
  106. }
  107. } );
  108. }
  109. function reloadObject( resetCamera ) {
  110. if ( model ) {
  111. scene.remove( model );
  112. }
  113. model = null;
  114. updateProgressBar( 0 );
  115. showProgressBar();
  116. var lDrawLoader = new LDrawLoader();
  117. lDrawLoader.separateObjects = guiData.separateObjects;
  118. lDrawLoader.smoothNormals = guiData.smoothNormals;
  119. lDrawLoader
  120. .setPath( ldrawPath )
  121. .load( guiData.modelFileName, function ( group2 ) {
  122. if ( model ) {
  123. scene.remove( model );
  124. }
  125. model = group2;
  126. // Convert from LDraw coordinates: rotate 180 degrees around OX
  127. model.rotation.x = Math.PI;
  128. scene.add( model );
  129. // Adjust materials
  130. var materials = lDrawLoader.materials;
  131. if ( envMapActivated ) {
  132. if ( ! textureCube ) {
  133. // Envmap texture
  134. var r = "textures/cube/Bridge2/";
  135. var urls = [ r + "posx.jpg", r + "negx.jpg",
  136. r + "posy.jpg", r + "negy.jpg",
  137. r + "posz.jpg", r + "negz.jpg" ];
  138. textureCube = new THREE.CubeTextureLoader().load( urls );
  139. textureCube.format = THREE.RGBFormat;
  140. textureCube.mapping = THREE.CubeReflectionMapping;
  141. }
  142. for ( var i = 0, n = materials.length; i < n; i ++ ) {
  143. var material = materials[ i ];
  144. if ( material.userData.canHaveEnvMap ) {
  145. material.envMap = textureCube;
  146. }
  147. }
  148. }
  149. guiData.constructionStep = model.userData.numConstructionSteps - 1;
  150. updateObjectsVisibility();
  151. // Adjust camera and light
  152. var bbox = new THREE.Box3().setFromObject( model );
  153. var size = bbox.getSize( new THREE.Vector3() );
  154. var radius = Math.max( size.x, Math.max( size.y, size.z ) ) * 0.5;
  155. if ( resetCamera ) {
  156. controls.target0.copy( bbox.getCenter( new THREE.Vector3() ) );
  157. controls.position0.set( - 2.3, 2, 2 ).multiplyScalar( radius ).add( controls.target0 );
  158. controls.reset();
  159. }
  160. createGUI();
  161. hideProgressBar();
  162. }, onProgress, onError );
  163. }
  164. function onWindowResize() {
  165. camera.aspect = window.innerWidth / window.innerHeight;
  166. camera.updateProjectionMatrix();
  167. renderer.setSize( window.innerWidth, window.innerHeight );
  168. }
  169. function createGUI() {
  170. if ( gui ) {
  171. gui.destroy();
  172. }
  173. gui = new GUI();
  174. gui.add( guiData, 'modelFileName', modelFileList ).name( 'Model' ).onFinishChange( function () {
  175. reloadObject( true );
  176. } );
  177. gui.add( guiData, 'separateObjects' ).name( 'Separate Objects' ).onChange( function ( value ) {
  178. reloadObject( false );
  179. } );
  180. if ( guiData.separateObjects ) {
  181. if ( model.userData.numConstructionSteps > 1 ) {
  182. gui.add( guiData, 'constructionStep', 0, model.userData.numConstructionSteps - 1 ).step( 1 ).name( 'Construction step' ).onChange( updateObjectsVisibility );
  183. }
  184. else {
  185. gui.add( guiData, 'noConstructionSteps' ).name( 'Construction step' ).onChange( updateObjectsVisibility );
  186. }
  187. }
  188. gui.add( guiData, 'envMapActivated' ).name( 'Env. map' ).onChange( function changeEnvMap ( value ) {
  189. envMapActivated = value;
  190. reloadObject( false );
  191. } );
  192. gui.add( guiData, 'smoothNormals' ).name( 'Smooth Normals' ).onChange( function changeNormals ( value ) {
  193. reloadObject( false );
  194. } );
  195. gui.add( guiData, 'displayLines' ).name( 'Display Lines' ).onChange( updateObjectsVisibility );
  196. gui.add( guiData, 'conditionalLines' ).name( 'Conditional Lines' ).onChange( updateObjectsVisibility );
  197. }
  198. //
  199. function animate() {
  200. requestAnimationFrame( animate );
  201. render();
  202. }
  203. function render() {
  204. renderer.render( scene, camera );
  205. }
  206. function onProgress( xhr ) {
  207. if ( xhr.lengthComputable ) {
  208. updateProgressBar( xhr.loaded / xhr.total );
  209. console.log( Math.round( xhr.loaded / xhr.total * 100, 2 ) + '% downloaded' );
  210. }
  211. }
  212. function onError() {
  213. var message = "Error loading model";
  214. progressBarDiv.innerText = message;
  215. console.log( message );
  216. }
  217. function showProgressBar() {
  218. document.body.appendChild( progressBarDiv );
  219. }
  220. function hideProgressBar() {
  221. document.body.removeChild( progressBarDiv );
  222. }
  223. function updateProgressBar( fraction ) {
  224. progressBarDiv.innerText = 'Loading... ' + Math.round( fraction * 100, 2 ) + '%';
  225. }
  226. </script>
  227. <!-- LDraw.org CC BY 2.0 Parts Library attribution -->
  228. <div style="display: block; position: absolute; bottom: 8px; left: 8px; width: 160px; padding: 10px; background-color: #F3F7F8;">
  229. <center>
  230. <a href="http://www.ldraw.org"><img style="width: 145px" src="models/ldraw/ldraw_org_logo/Stamp145.png"></a>
  231. <br />
  232. <a href="http://www.ldraw.org/">This software uses the LDraw Parts Library</a>
  233. </center>
  234. </div>
  235. </body>
  236. </html>