index-2.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <div ref="wrap"></div>
  3. </template>
  4. <script setup>
  5. import * as THREE from "three";
  6. import * as SceneUtils from "three/examples/jsm/utils/SceneUtils";
  7. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
  8. import Stats from "three/examples/jsm/libs/stats.module";
  9. import * as dat from "dat.gui";
  10. import { onMounted, ref } from "vue";
  11. import { BoxGeometry, BufferAttribute } from "three";
  12. /**
  13. * @type {import("vue").Ref<HTMLDivElement>}
  14. */
  15. const wrap = ref();
  16. function getCanvasWidth() {
  17. return window.innerWidth - 60;
  18. }
  19. function getCanvasHeight() {
  20. return window.innerHeight - 150;
  21. }
  22. let renderer;
  23. function initRender() {
  24. renderer = new THREE.WebGLRenderer({ antialias: true });
  25. renderer.setSize(getCanvasWidth(), getCanvasHeight());
  26. renderer.setPixelRatio(window.devicePixelRatio);
  27. renderer.setClearColor(0x444444, 1);
  28. //告诉渲染器需要阴影效果
  29. renderer.shadowMap.enabled = true;
  30. renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 默认的是,没有设置的这个清晰 THREE.PCFShadowMap
  31. wrap.value.appendChild(renderer.domElement);
  32. }
  33. let camera;
  34. function initCamera() {
  35. camera = new THREE.PerspectiveCamera(
  36. 50,
  37. getCanvasWidth() / getCanvasHeight(),
  38. 0.1,
  39. 10000
  40. );
  41. camera.position.set(40, 40, 100);
  42. camera.lookAt(new THREE.Vector3(0, 0, 0));
  43. }
  44. let scene;
  45. function initScene() {
  46. scene = new THREE.Scene();
  47. }
  48. //初始化dat.GUI简化试验流程
  49. let gui;
  50. let datGui = new dat.GUI();
  51. let hemiLight, ambientLight, directionalLight, directionalLightHelper;
  52. function initGui() {
  53. //声明一个保存需求修改的相关数据的对象
  54. gui = {
  55. directionalLight: "#ffffff", //点光源
  56. directionalLightIntensity: 1, //灯光强度
  57. visible: true, //是否可见
  58. castShadow: true,
  59. exponent: 30,
  60. target: "plane",
  61. debug: false,
  62. groundColor: "#00ff00",
  63. skyColor: "#0000ff",
  64. hemiLightIntensity: 0.3,
  65. dLightX: -40,
  66. dLightY: 60,
  67. dLightZ: -10,
  68. };
  69. //将设置属性添加到gui当中,gui.add(对象,属性,最小值,最大值)
  70. const ambientFolder = datGui.addFolder("环境光");
  71. ambientFolder.addColor(gui, "skyColor").onChange(function (e) {
  72. hemiLight.color = new THREE.Color(e);
  73. });
  74. ambientFolder.addColor(gui, "groundColor").onChange(function (e) {
  75. hemiLight.groundColor = new THREE.Color(e);
  76. });
  77. ambientFolder.add(gui, "hemiLightIntensity", 0, 1).onChange(function (e) {
  78. hemiLight.intensity = e;
  79. });
  80. ambientFolder.addColor(gui, "directionalLight").onChange(function (e) {
  81. directionalLight.color = new THREE.Color(e);
  82. });
  83. datGui.add(gui, "dLightX", -100, 100).onChange(function (e) {
  84. directionalLight.position.setX(e);
  85. });
  86. datGui.add(gui, "dLightY", -100, 100).onChange(function (e) {
  87. directionalLight.position.setY(e);
  88. });
  89. datGui.add(gui, "dLightZ", -100, 100).onChange(function (e) {
  90. directionalLight.position.setZ(e);
  91. });
  92. datGui.add(gui, "directionalLightIntensity", 0, 5).onChange(function (e) {
  93. directionalLight.intensity = e;
  94. });
  95. datGui.add(gui, "visible").onChange(function (e) {
  96. directionalLight.visible = e;
  97. });
  98. datGui.add(gui, "castShadow").onChange(function (e) {
  99. directionalLight.castShadow = e;
  100. });
  101. datGui.add(gui, "debug").onChange(function (e) {
  102. if (e) {
  103. let debug = new THREE.CameraHelper(directionalLight.shadow.camera);
  104. debug.name = "debug";
  105. scene.add(debug);
  106. } else {
  107. let debug = scene.getObjectByName("debug");
  108. scene.remove(debug);
  109. }
  110. });
  111. }
  112. function initLight() {
  113. hemiLight = new THREE.HemisphereLight("#bcffb1", "#000000", 1);
  114. scene.add(hemiLight);
  115. // ambientLight = new THREE.AmbientLight("#111111");
  116. // scene.add(ambientLight);
  117. directionalLight = new THREE.DirectionalLight("#ffffff");
  118. directionalLight.position.set(-40, 60, -10);
  119. directionalLightHelper = new THREE.DirectionalLightHelper(directionalLight);
  120. directionalLight.shadow.camera.near = 20; //产生阴影的最近距离
  121. directionalLight.shadow.camera.far = 200; //产生阴影的最远距离
  122. directionalLight.shadow.camera.left = -100; //产生阴影距离位置的最左边位置
  123. directionalLight.shadow.camera.right = 100; //最右边
  124. directionalLight.shadow.camera.top = 100; //最上边
  125. directionalLight.shadow.camera.bottom = -100; //最下面
  126. //这两个值决定使用多少像素生成阴影 默认512
  127. directionalLight.shadow.mapSize.height = 1024;
  128. directionalLight.shadow.mapSize.width = 1024;
  129. //告诉平行光需要开启阴影投射
  130. directionalLight.castShadow = true;
  131. // scene.add(directionalLightHelper);
  132. scene.add(directionalLight);
  133. }
  134. let cube, plane;
  135. function initModel() {
  136. //辅助工具
  137. let helper = new THREE.AxesHelper(100);
  138. scene.add(helper);
  139. scene.add(
  140. new THREE.Mesh(new BoxGeometry(10, 10, 10), [
  141. new THREE.MeshBasicMaterial({
  142. color: 0x0000ff,
  143. }),
  144. new THREE.MeshBasicMaterial({
  145. color: 0xff00ff,
  146. }),
  147. new THREE.MeshBasicMaterial({
  148. color: 0x00ffff,
  149. }),
  150. new THREE.MeshBasicMaterial({
  151. color: 0xff0000,
  152. }),
  153. new THREE.MeshBasicMaterial({
  154. color: 0x00ff00,
  155. }),
  156. new THREE.MeshBasicMaterial({
  157. color: 0x0f000f,
  158. }),
  159. ])
  160. );
  161. //底部平面
  162. let planeGeometry = new THREE.PlaneGeometry(5000, 5000, 20, 20);
  163. let planeMaterial = new THREE.MeshLambertMaterial({ color: 0xaaaaaa });
  164. plane = new THREE.Mesh(planeGeometry, planeMaterial);
  165. plane.rotation.x = -0.5 * Math.PI;
  166. plane.position.y = -0;
  167. //告诉底部平面需要接收阴影
  168. plane.receiveShadow = true;
  169. // scene.add(plane);
  170. }
  171. //初始化性能插件
  172. let stats;
  173. function initStats() {
  174. stats = new Stats();
  175. document.body.appendChild(stats.dom);
  176. }
  177. //用户交互插件 鼠标左键按住旋转,右键按住平移,滚轮缩放
  178. let controls;
  179. function initControls() {
  180. controls = new OrbitControls(camera, renderer.domElement);
  181. // 如果使用animate方法时,将此函数删除
  182. //controls.addEventListener( 'change', render );
  183. // 使动画循环使用时阻尼或自转 意思是否有惯性
  184. controls.enableDamping = true;
  185. //动态阻尼系数 就是鼠标拖拽旋转灵敏度
  186. controls.dampingFactor = 0.05;
  187. //是否可以缩放
  188. controls.enableZoom = true;
  189. //是否自动旋转
  190. controls.autoRotate = false;
  191. //设置相机距离原点的最远距离
  192. controls.minDistance = 10;
  193. //设置相机距离原点的最远距离
  194. controls.maxDistance = 300;
  195. //是否开启右键拖拽
  196. controls.enablePan = true;
  197. }
  198. function render() {
  199. renderer.render(scene, camera);
  200. }
  201. //窗口变动触发的函数
  202. function onWindowResize() {
  203. camera.aspect = getCanvasWidth() / getCanvasHeight();
  204. camera.updateProjectionMatrix();
  205. render();
  206. renderer.setSize(getCanvasWidth(), getCanvasHeight());
  207. }
  208. function animate() {
  209. //更新控制器
  210. render();
  211. //更新性能插件
  212. stats.update();
  213. controls.update();
  214. requestAnimationFrame(animate);
  215. }
  216. function draw() {
  217. initRender();
  218. initScene();
  219. initCamera();
  220. initLight();
  221. initModel();
  222. initControls();
  223. initStats();
  224. animate();
  225. window.onresize = onWindowResize;
  226. }
  227. initGui();
  228. onMounted(() => {
  229. draw();
  230. });
  231. </script>