BabylonJS - 立方體



在本節中,我們將學習如何向建立的場景新增立方體。

要建立立方體,語法如下。

語法

var box = BABYLON.Mesh.CreateBox("box", 6.0, scene, false, BABYLON.Mesh.DEFAULTSIDE);

引數

以下是新增立方體的不同引數:

  • 名稱 - 要賦予立方體的名稱;例如,“box”。

  • 立方體大小 - 立方體的大小。

  • 場景 - 立方體將附加到的場景。

  • 布林值 - 預設值為 False。

  • BABYLON.Mesh.DEFAULTSIDE - 用於定位。

最後兩個引數是可選的。

演示 - 立方體

<!doctype html>
<html>
   <head>
      <meta charset = "utf-8">
      <title>BabylonJs - Basic Element-Creating Scene</title>
      <script src = "babylon.js"></script>
      <style>
         canvas {width: 100%; height: 100%;}
      </style>
   </head>

   <body>
      <canvas id = "renderCanvas"></canvas>
      <script type = "text/javascript">
         var canvas = document.getElementById("renderCanvas");
         var engine = new BABYLON.Engine(canvas, true);
         var createScene  = function() {
            var scene = new BABYLON.Scene(engine);
            scene.clearColor = new BABYLON.Color3(0, 1, 0);
         
            var camera = new BABYLON.ArcRotateCamera("Camera", 1, 0.8, 10, new BABYLON.Vector3(0, 0, 0), scene);
            camera.attachControl(canvas, true);
         
            var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
            light.intensity = 0.7;	
         
            var pl = new BABYLON.PointLight("pl", BABYLON.Vector3.Zero(), scene);
            pl.diffuse = new BABYLON.Color3(1, 1, 1);
            pl.specular = new BABYLON.Color3(1, 1, 1);
            pl.intensity = 0.8;
         
            var box = BABYLON.Mesh.CreateBox("box", '3', scene);	
            scene.registerBeforeRender(function() { 
               pl.position = camera.position;
            });

            return scene;
         };
         var scene = createScene();
         engine.runRenderLoop(function() {
            scene.render();
         });
      </script>
   </body>
</html>

輸出

執行後,上述程式碼將生成以下輸出:

Basic Elements Demo Box

大小是指立方體所有側面的高度。100 的大小基本上將是一個佔據全屏的立方體。賦予背景場景的顏色為綠色。我們使用相機和光效將螢幕移至滑鼠游標上。這也有助於光效。

babylonjs_basic_elements.htm
廣告