Babylonjs-basic-elements-position

提供:Dev Guides
移動先:案内検索

BabylonJS-ポジション

Demo

<!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);
            scene.activeCamera.attachControl(canvas);

            var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(0, 100, 100), scene);

            var boxa = BABYLON.Mesh.CreateBox("BoxA", 1.0, scene);
            boxa.position = new BABYLON.Vector3(0,0.5,0);

            var boxb = BABYLON.Mesh.CreateBox("BoxB", 1.0, scene);
            boxb.position = new BABYLON.Vector3(3,0.5,0);

            var boxc = BABYLON.Mesh.CreateBox("BoxC", 1.0, scene);
            boxc.position = new BABYLON.Vector3(-3,0.5,0);

            var boxd = BABYLON.Mesh.CreateBox("BoxD", 1.0, scene);
            boxd.position = new BABYLON.Vector3(0,0.5,3);

            var boxe = BABYLON.Mesh.CreateBox("BoxE", 1.0, scene);
            boxe.position = new BABYLON.Vector3(0,0.5,-3);

            var ground = BABYLON.Mesh.CreateGround("ground1", 10, 6, 2, scene);
            ground.position = new BABYLON.Vector3(0,0,0);
            return scene;
         };
         var scene = createScene();
         engine.runRenderLoop(function() {
            scene.render();
         });
      </script>
   </body>
</html>

出力

基本的な要素の位置、回転、スケーリング

Demo

上記の例では、サイズ1のボックスを5つ作成しました。つまり、ボックスの辺は1になります。 地面を作成し、地面を中央に配置しました。

最初のボックス、つまり、boxAは地面の上の中央に配置されます。 * new BABYLON.Vector3(x、y、z)または *shape.position.x、shape.position.y または shape.position.z を使用して形状を配置できます。 上記の例では、* new BABYLON.Vector3(x、y、z)*を使用しました。

boxAを地面の中央に配置するために、x = 0、y =ボックスの高さの半分、つまり0.5とz = 0を使用しました。

boxa.position = new BABYLON.Vector3(0,0.5,0);

次のボックス-box * b は *x 軸に向かって配置されます。 x方向の値3。

boxb.position = new BABYLON.Vector3(3,0.5,0);

boxcはx方向の反対側に配置されます。 xには-3値が与えられます。

boxc.position = new BABYLON.Vector3(-3,0.5,0);

box * d *はz軸に沿って配置され、z軸の反対方向に配置されると値3および-3が与えられます。

boxd.position = new BABYLON.Vector3(0,0.5,3);
boxe.position = new BABYLON.Vector3(0,0.5,-3);

SphereとGroundを使用したデモ

<!doctype html>
<html>
   <head>
      <meta charset = "utf-8">
      <title>BabylonJs - Ball/Ground Demo</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( .5, .5, .5);

            var camera = new BABYLON.ArcRotateCamera("camera1",  0, 0, 0, new BABYLON.Vector3(0, 0, 0), scene);
            camera.setPosition(new BABYLON.Vector3(-100, 0, -100));
            camera.attachControl(canvas, true);

            var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 0.5, 0), scene);

            var pl = new BABYLON.PointLight("pl", new BABYLON.Vector3(0, 0, 0), scene);

            var gmat = new BABYLON.StandardMaterial("mat1", scene);
            gmat.alpha = 1.0;
            var texture = new BABYLON.Texture("images/mat.jpg", scene);
            gmat.diffuseTexture = texture;

            var ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 150, height:15}, scene);
            ground.material = gmat;

            var mat = new BABYLON.StandardMaterial("mat1", scene);
            mat.alpha = 1.0;
            mat.diffuseColor = new BABYLON.Color3(1, 0, 0);
            var texture = new BABYLON.Texture("images/rugby.jpg", scene);
            mat.diffuseTexture = texture;

            var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 5, diameterX:5}, scene);
            sphere.position= new BABYLON.Vector3(-75,2.5,0);
            sphere.material = mat;
            console.log(sphere.position.x);
            scene.registerBeforeRender(function () {
               if (sphere.position.x <=75) {
                  console.log(sphere.position.x);
                  if (sphere.position.x <= -75) sphere.position.x=75;
                  sphere.position.x -= 0.25;
               } else if (sphere.position.x <= -15) {
                  console.log('B');
                  sphere.position.x += 1;
               }
            });
            return scene;
         };
         var scene = createScene();
         engine.runRenderLoop(function() {
            scene.render();
         });
      </script>
   </body>
</html>

出力

上記のコード行は、次の出力を生成します-

基本要素の球体

このデモでは、mat.jpg_と_rugby.jpg_の2つの画像を使用しました。 画像は_images/フォルダーにローカルに保存され、参照用に以下に貼り付けられます。 任意の画像をダウンロードして、デモリンクで使用できます。

地面に使用されるテクスチャ- images/mat.jpg

マット画像

球に使用されるテクスチャ- images/rugby.jpg

ラグビー画像