main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
  2. import {OrbitControls} from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js';
  3. class BasicWorldDemo {
  4. constructor() {
  5. this._Initialize();
  6. }
  7. _Initialize() {
  8. this._threejs = new THREE.WebGLRenderer({
  9. antialias: true,
  10. });
  11. this._threejs.shadowMap.enabled = true;
  12. this._threejs.shadowMap.type = THREE.PCFSoftShadowMap;
  13. this._threejs.setPixelRatio(window.devicePixelRatio);
  14. this._threejs.setSize(window.innerWidth, window.innerHeight);
  15. document.body.appendChild(this._threejs.domElement);
  16. window.addEventListener('resize', () => {
  17. this._OnWindowResize();
  18. }, false);
  19. const fov = 60;
  20. const aspect = 1920 / 1080;
  21. const near = 1.0;
  22. const far = 1000.0;
  23. this._camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  24. this._camera.position.set(75, 20, 0);
  25. this._scene = new THREE.Scene();
  26. let light = new THREE.DirectionalLight(0xFFFFFF, 1.0);
  27. light.position.set(20, 100, 10);
  28. light.target.position.set(0, 0, 0);
  29. light.castShadow = true;
  30. light.shadow.bias = -0.001;
  31. light.shadow.mapSize.width = 2048;
  32. light.shadow.mapSize.height = 2048;
  33. light.shadow.camera.near = 0.1;
  34. light.shadow.camera.far = 500.0;
  35. light.shadow.camera.near = 0.5;
  36. light.shadow.camera.far = 500.0;
  37. light.shadow.camera.left = 100;
  38. light.shadow.camera.right = -100;
  39. light.shadow.camera.top = 100;
  40. light.shadow.camera.bottom = -100;
  41. this._scene.add(light);
  42. light = new THREE.AmbientLight(0x101010);
  43. this._scene.add(light);
  44. const controls = new OrbitControls(
  45. this._camera, this._threejs.domElement);
  46. controls.target.set(0, 20, 0);
  47. controls.update();
  48. const loader = new THREE.CubeTextureLoader();
  49. const texture = loader.load([
  50. './resources/posx.jpg',
  51. './resources/negx.jpg',
  52. './resources/posy.jpg',
  53. './resources/negy.jpg',
  54. './resources/posz.jpg',
  55. './resources/negz.jpg',
  56. ]);
  57. this._scene.background = texture;
  58. const plane = new THREE.Mesh(
  59. new THREE.PlaneGeometry(100, 100, 10, 10),
  60. new THREE.MeshStandardMaterial({
  61. color: 0xFFFFFF,
  62. }));
  63. plane.castShadow = false;
  64. plane.receiveShadow = true;
  65. plane.rotation.x = -Math.PI / 2;
  66. this._scene.add(plane);
  67. const box = new THREE.Mesh(
  68. new THREE.BoxGeometry(2, 2, 2),
  69. new THREE.MeshStandardMaterial({
  70. color: 0xFFFFFF,
  71. }));
  72. box.position.set(0, 1, 0);
  73. box.castShadow = true;
  74. box.receiveShadow = true;
  75. this._scene.add(box);
  76. for (let x = -8; x < 8; x++) {
  77. for (let y = -8; y < 8; y++) {
  78. const box = new THREE.Mesh(
  79. new THREE.BoxGeometry(2, 2, 2),
  80. new THREE.MeshStandardMaterial({
  81. color: 0x808080,
  82. }));
  83. box.position.set(Math.random() + x * 5, Math.random() * 4.0 + 2.0, Math.random() + y * 5);
  84. box.castShadow = true;
  85. box.receiveShadow = true;
  86. this._scene.add(box);
  87. }
  88. }
  89. // const box = new THREE.Mesh(
  90. // new THREE.SphereGeometry(2, 32, 32),
  91. // new THREE.MeshStandardMaterial({
  92. // color: 0xFFFFFF,
  93. // wireframe: true,
  94. // wireframeLinewidth: 4,
  95. // }));
  96. // box.position.set(0, 0, 0);
  97. // box.castShadow = true;
  98. // box.receiveShadow = true;
  99. // this._scene.add(box);
  100. this._RAF();
  101. }
  102. _OnWindowResize() {
  103. this._camera.aspect = window.innerWidth / window.innerHeight;
  104. this._camera.updateProjectionMatrix();
  105. this._threejs.setSize(window.innerWidth, window.innerHeight);
  106. }
  107. _RAF() {
  108. requestAnimationFrame(() => {
  109. this._threejs.render(this._scene, this._camera);
  110. this._RAF();
  111. });
  112. }
  113. }
  114. let _APP = null;
  115. window.addEventListener('DOMContentLoaded', () => {
  116. _APP = new BasicWorldDemo();
  117. });