webgl_loader_3mf_materials.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 3MF</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. background-color: #a0a0a0;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  17. <a href="http://3mf.io" target="_blank" rel="noopener">3MF</a> file with materials
  18. </div>
  19. <script src="js/libs/jszip.min.js"></script>
  20. <script type="module">
  21. import * as THREE from '../build/three.module.js';
  22. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  23. import { ThreeMFLoader } from './jsm/loaders/3MFLoader.js';
  24. var camera, scene, renderer;
  25. init();
  26. function init() {
  27. scene = new THREE.Scene();
  28. scene.background = new THREE.Color( 0xa0a0a0 );
  29. scene.fog = new THREE.Fog( 0xa0a0a0, 10, 500 );
  30. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 500 );
  31. camera.position.set( - 50, 40, 50 );
  32. scene.add( camera );
  33. //
  34. var hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  35. hemiLight.position.set( 0, 100, 0 );
  36. scene.add( hemiLight );
  37. var dirLight = new THREE.DirectionalLight( 0xffffff );
  38. dirLight.position.set( - 0, 40, 50 );
  39. dirLight.castShadow = true;
  40. dirLight.shadow.camera.top = 50;
  41. dirLight.shadow.camera.bottom = - 25;
  42. dirLight.shadow.camera.left = - 25;
  43. dirLight.shadow.camera.right = 25;
  44. dirLight.shadow.camera.near = 0.1;
  45. dirLight.shadow.camera.far = 200;
  46. dirLight.shadow.mapSize.set( 1024, 1024 );
  47. scene.add( dirLight );
  48. // scene.add( new CameraHelper( dirLight.shadow.camera ) );
  49. //
  50. var manager = new THREE.LoadingManager();
  51. var loader = new ThreeMFLoader( manager );
  52. loader.load( './models/3mf/truck.3mf', function ( object ) {
  53. object.quaternion.setFromEuler( new THREE.Euler( - Math.PI / 2, 0, 0 ) ); // z-up conversion
  54. object.traverse( function ( child ) {
  55. child.castShadow = true;
  56. } );
  57. scene.add( object );
  58. } );
  59. //
  60. manager.onLoad = function () {
  61. render();
  62. };
  63. //
  64. var ground = new THREE.Mesh( new THREE.PlaneBufferGeometry( 1000, 1000 ), new THREE.MeshPhongMaterial( { color: 0x999999, depthWrite: false } ) );
  65. ground.rotation.x = - Math.PI / 2;
  66. ground.position.y = 11;
  67. ground.receiveShadow = true;
  68. scene.add( ground );
  69. //
  70. renderer = new THREE.WebGLRenderer( { antialias: true } );
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. renderer.outputEncoding = THREE.sRGBEncoding;
  74. renderer.shadowMap.enabled = true;
  75. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  76. document.body.appendChild( renderer.domElement );
  77. //
  78. var controls = new OrbitControls( camera, renderer.domElement );
  79. controls.addEventListener( 'change', render );
  80. controls.minDistance = 50;
  81. controls.maxDistance = 200;
  82. controls.enablePan = false;
  83. controls.target.set( 0, 20, 0 );
  84. controls.update();
  85. window.addEventListener( 'resize', onWindowResize, false );
  86. render();
  87. }
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. render();
  93. }
  94. function render() {
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>