webgl_loader_vrml.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - VRML loader</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. <meta property="og:title" content="three.js webgl - loaders - VRML loader">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_loader_vrml.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_loader_vrml.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - VRML loader
  16. </div>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { VRMLLoader } from 'three/addons/loaders/VRMLLoader.js';
  30. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. let camera, scene, renderer, stats, controls, loader;
  32. const params = {
  33. asset: 'house'
  34. };
  35. const assets = [
  36. 'creaseAngle',
  37. 'crystal',
  38. 'house',
  39. 'elevationGrid1',
  40. 'elevationGrid2',
  41. 'extrusion1',
  42. 'extrusion2',
  43. 'extrusion3',
  44. 'lines',
  45. 'linesTransparent',
  46. 'meshWithLines',
  47. 'meshWithTexture',
  48. 'pixelTexture',
  49. 'points',
  50. 'camera',
  51. 'multilineString'
  52. ];
  53. let vrmlScene;
  54. init();
  55. function init() {
  56. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1e10 );
  57. camera.position.set( - 10, 5, 10 );
  58. scene = new THREE.Scene();
  59. scene.add( camera );
  60. // light
  61. const ambientLight = new THREE.AmbientLight( 0xffffff, 1.2 );
  62. scene.add( ambientLight );
  63. const dirLight = new THREE.DirectionalLight( 0xffffff, 2.0 );
  64. dirLight.position.set( 200, 200, 200 );
  65. scene.add( dirLight );
  66. loader = new VRMLLoader();
  67. loadAsset( params.asset );
  68. // renderer
  69. renderer = new THREE.WebGLRenderer();
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.setAnimationLoop( animate );
  73. document.body.appendChild( renderer.domElement );
  74. // controls
  75. controls = new OrbitControls( camera, renderer.domElement );
  76. controls.minDistance = 1;
  77. controls.maxDistance = 200;
  78. controls.enableDamping = true;
  79. //
  80. stats = new Stats();
  81. document.body.appendChild( stats.dom );
  82. //
  83. window.addEventListener( 'resize', onWindowResize );
  84. //
  85. const gui = new GUI();
  86. gui.add( params, 'asset', assets ).onChange( function ( value ) {
  87. if ( vrmlScene ) {
  88. vrmlScene.traverse( function ( object ) {
  89. if ( object.material ) object.material.dispose();
  90. if ( object.material && object.material.map ) object.material.map.dispose();
  91. if ( object.geometry ) object.geometry.dispose();
  92. } );
  93. scene.remove( vrmlScene );
  94. }
  95. loadAsset( value );
  96. } );
  97. }
  98. function loadAsset( asset ) {
  99. loader.load( 'models/vrml/' + asset + '.wrl', function ( object ) {
  100. vrmlScene = object;
  101. scene.add( object );
  102. controls.reset();
  103. } );
  104. }
  105. function onWindowResize() {
  106. camera.aspect = window.innerWidth / window.innerHeight;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. function animate() {
  111. controls.update(); // to support damping
  112. renderer.render( scene, camera );
  113. stats.update();
  114. }
  115. </script>
  116. </body>
  117. </html>
粤ICP备19079148号