物管理前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.8 KiB

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. html, body, div {
  8. padding: 0;
  9. margin: 0;
  10. height: 100%;
  11. }
  12. </style>
  13. <script src="./three/three.min.js"></script>
  14. <script src="./three/GLTFLoader.js"></script>
  15. <script src="./three/OrbitControls.js"></script>
  16. </head>
  17. <body onload="draw();">
  18. <div></div>
  19. </body>
  20. <script>
  21. let renderer, camera, scene, gui, stats, ambientLight, directionalLight, control;
  22. const w = document.querySelector("div");
  23. function initRender() {
  24. renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
  25. renderer.setSize(w.clientWidth, w.clientHeight);
  26. //告诉渲染器需要阴影效果
  27. renderer.shadowMap.enabled = true;
  28. renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默认的是,没有设置的这个清晰 THREE.PCFShadowMap
  29. w.appendChild(renderer.domElement);
  30. }
  31. function initCamera() {
  32. camera = new THREE.PerspectiveCamera(1, w.clientWidth / w.clientHeight, 0.1, 1000);
  33. camera.position.set(0, 100, 200);
  34. camera.lookAt(new THREE.Vector3(0, 0, 0));
  35. }
  36. function initScene() {
  37. scene = new THREE.Scene();
  38. }
  39. function initGui() {
  40. //声明一个保存需求修改的相关数据的对象
  41. gui = {};
  42. var datGui = new dat.GUI();
  43. //将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)
  44. }
  45. function initLight() {
  46. ambientLight = new THREE.AmbientLight("#111111");
  47. scene.add(ambientLight);
  48. directionalLight = new THREE.DirectionalLight("#ffffff");
  49. directionalLight.position.set(40, 60, 10);
  50. directionalLight.shadow.camera.near = 1; //产生阴影的最近距离
  51. directionalLight.shadow.camera.far = 400; //产生阴影的最远距离
  52. directionalLight.shadow.camera.left = -50; //产生阴影距离位置的最左边位置
  53. directionalLight.shadow.camera.right = 50; //最右边
  54. directionalLight.shadow.camera.top = 50; //最上边
  55. directionalLight.shadow.camera.bottom = -50; //最下面
  56. //这两个值决定生成阴影密度 默认512
  57. directionalLight.shadow.mapSize.height = 1024;
  58. directionalLight.shadow.mapSize.width = 1024;
  59. //告诉平行光需要开启阴影投射
  60. directionalLight.castShadow = true;
  61. scene.add(directionalLight);
  62. }
  63. function initModel() {
  64. //底部平面
  65. // var planeGeometry = new THREE.PlaneGeometry(100, 100);
  66. // var planeMaterial = new THREE.MeshLambertMaterial({color: 0xaaaaaa, side: THREE.DoubleSide});
  67. // var plane = new THREE.Mesh(planeGeometry, planeMaterial);
  68. // plane.rotation.x = -0.5 * Math.PI;
  69. // plane.position.y = -.1;
  70. // plane.receiveShadow = true; //可以接收阴影
  71. // scene.add(plane);
  72. //创建gltf加载器
  73. var loader = new THREE.GLTFLoader();
  74. loader.load("./1.glb", function(gltf) {
  75. gltf.scene.scale.set(20, 20, 20);
  76. scene.add(gltf.scene);
  77. });
  78. }
  79. function initStats() {
  80. stats = new Stats();
  81. document.body.appendChild(stats.dom);
  82. }
  83. function initControl() {
  84. control = new THREE.OrbitControls(camera, renderer.domElement);
  85. }
  86. function render() {
  87. control.update();
  88. control.autoRotate = true;
  89. control.enabled = false;
  90. renderer.render(scene, camera);
  91. }
  92. function onWindowResize() {
  93. camera.aspect = w.clientWidth / w.clientHeight;
  94. camera.updateProjectionMatrix();
  95. renderer.setSize(w.clientWidth, w.clientHeight);
  96. }
  97. function animate() {
  98. //更新控制器
  99. render();
  100. //更新性能插件
  101. //stats.update();
  102. requestAnimationFrame(animate);
  103. }
  104. function draw() {
  105. // initGui();
  106. initRender();
  107. initScene();
  108. initCamera();
  109. initLight();
  110. initModel();
  111. // initStats();
  112. initControl();
  113. animate();
  114. window.onresize = onWindowResize;
  115. }
  116. </script>
  117. </html>