11. 建设村庄(旋转,移动,缩放砖头)

  1 <template>
  2   <div class="badylon-canvas2">
  3     <canvas ref="renderCanvas2" id="render-canvas2"></canvas>
  4   </div>
  5 </template>
  6 
  7 <script setup lang="ts">
  8 import * as BABYLON from "@babylonjs/core/Legacy/legacy";
  9 import "@babylonjs/loaders/OBJ";
 10 import "@babylonjs/loaders/glTF";
 11 import Assets from "../../utils/babylon/babylon-asset.js";
 12 import type { Engine, Scene } from "@babylonjs/core/Legacy/legacy";
 13 import { ref, onMounted } from "vue";
 14 
 15 type EngineType = Engine | null;
 16 type SceneType = Scene | null;
 17 
 18 const renderCanvas2 = ref<HTMLCanvasElement | null>(null);
 19 let engine: EngineType = null;
 20 let scene: SceneType = null;
 21 let sceneToRender: SceneType = null;
 22 
 23 function startRenderLoop(engine: Engine) {
 24   engine.runRenderLoop(() => {
 25     if (sceneToRender && sceneToRender.activeCamera) {
 26       sceneToRender.render();
 27     }
 28   });
 29 }
 30 
 31 function createDefaultEngine() {
 32   return new BABYLON.Engine(renderCanvas2.value, true, {
 33     // 这些选项在api完全搜索不到
 34     preserveDrawingBuffer: true,
 35     stencil: true,
 36     disableWebGL2Support: false,
 37   });
 38 }
 39 
 40 function createScene() {
 41   if (engine === null) return null;
 42   scene = new BABYLON.Scene(engine);
 43 
 44   const camera = new BABYLON.ArcRotateCamera(
 45     "camera",
 46     -Math.PI / 2,
 47     Math.PI / 2.5,
 48     10,
 49     new BABYLON.Vector3(0, 0, 0)
 50   );
 51   camera.attachControl(renderCanvas2.value, true);
 52 
 53   const light = new BABYLON.HemisphericLight(
 54     "light",
 55     new BABYLON.Vector3(1, 1, 0),
 56     scene
 57   );
 58 
 59   // 深入了解盒子三维的设定:https://doc.babylonjs.com/features/featuresDeepDive/mesh/transforms/center_origin/position
 60   // 设置x,y,z的三种方式
 61   // 第一种
 62   // const box = BABYLON.MeshBuilder.CreateBox("box", {
 63   //   width: 2,
 64   //   height: 1.5,
 65   //   depth: 3,
 66   // });
 67 
 68   // 第二种
 69   // const box = BABYLON.MeshBuilder.CreateBox("box", {});
 70   // box.scaling.x = 2;
 71   // box.scaling.y = 1.5;
 72   // box.scaling.z = 3;
 73 
 74   // 第三种
 75   const box = BABYLON.MeshBuilder.CreateBox("box", {});
 76   box.scaling = new BABYLON.Vector3(2, 1.5, 3);
 77 
 78   // 设置盒子的位置
 79   // 第一种
 80   // box.position.x = -2;
 81   // box.position.y = 4.2;
 82   // box.position.z = 0.1;
 83 
 84   // 第二种
 85   // box.position = new BABYLON.Vector3(-2, 4.2, 0.1);
 86 
 87   // 旋转盒子
 88   box.rotation.y = Math.PI / 4;
 89 
 90   const ground = BABYLON.MeshBuilder.CreateGround("ground", {
 91     width: 10,
 92     height: 10,
 93   });
 94   // 自动播放声音
 95   // const sound = new BABYLON.Sound("name", "url", scene, null, {
 96   //   loop: true,
 97   //   autoplay: true,
 98   // });
 99 
100   // 手动播放声音
101   const sound = new BABYLON.Sound("name", "url", scene);
102   // sound.play();
103   setInterval(() => sound.play(), 3000);
104 
105   return scene;
106 }
107 
108 async function initFunction() {
109   async function asyncEngineCreation() {
110     try {
111       return createDefaultEngine();
112     } catch (e) {
113       console.log(
114         "the available createEngine function failed. Creating the default engine instead"
115       );
116       return createDefaultEngine();
117     }
118   }
119 
120   engine = await asyncEngineCreation();
121   if (!engine) throw "engine should not be null.";
122   startRenderLoop(engine);
123   scene = createScene();
124 }
125 
126 onMounted(() => {
127   if (renderCanvas2.value) {
128     console.log("ttttt");
129     initFunction().then(() => {
130       sceneToRender = scene;
131     });
132     // Resize
133     window.addEventListener("resize", function () {
134       engine && engine.resize();
135     });
136   }
137 });
138 </script>
139 
140 <style lang="scss" scoped>
141 .badylon-canvas2 {
142   width: 100%;
143   height: 100%;
144 
145   #render-canvas2 {
146     width: 100%;
147     height: 100%;
148     // touch-action 用在触摸屏,缩放
149     touch-action: none;
150   }
151 }
152 </style>

 

posted @ 2022-10-19 15:12  sky-su  阅读(30)  评论(0)    收藏  举报