
- BabylonJS 教程
- BabylonJS - 主頁
- BabylonJS - 簡介
- BabylonJS - 環境設定
- BabylonJS - 概覽
- BabylonJS - 基本元素
- BabylonJS - 材質
- BabylonJS - 動畫
- BabylonJS - 相機
- BabylonJS - 燈光
- BabylonJS - 引數化形狀
- BabylonJS - 網格
- VectorPosition 和 Rotation
- BabylonJS - 貼花
- BabylonJS - Curve3
- BabylonJS - 動態紋理
- BabylonJS - 視差貼圖
- BabylonJS - 鏡頭光暈
- BabylonJS - 建立螢幕截圖
- BabylonJS - 反射探針
- 標準渲染管道
- BabylonJS - ShaderMaterial
- BabylonJS - 骨骼
- BabylonJS - 物理引擎
- BabylonJS - 播放聲音和音樂
- BabylonJS 有用資源
- BabylonJS - 快速指南
- BabylonJS - 有用資源
- BabylonJS - 討論
BabylonJS - 位置
演示
<!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>
輸出

演示
在以上示例中,我們建立了 5 個尺寸為 1 的方塊,即方塊的邊長為 1。我們建立了一個地面並將其放置在中心。
第一個方塊,即方塊 A 放置在地面中央上方。我們可以使用 new BABYLON.Vector3(x, y, z) 或 shape.position.x , shape.position.y 或 shape.position.z 來放置形狀。在以上示例中,我們使用了 new BABYLON.Vector3(x, y, z)。
要將方塊 A 放置在地面中心,我們使用了 x = 0,y = 方塊高度的一半,即 0.5 和 z = 0。
boxa.position = new BABYLON.Vector3(0,0.5,0);
下一個方塊 - 方塊 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);
boxd 沿 z 軸放置,如果放置在 z 軸的相反方向,則值為 3 和 -3。
boxd.position = new BABYLON.Vector3(0,0.5,3); boxe.position = new BABYLON.Vector3(0,0.5,-3);
使用球和地面進行演示
<!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。這些影像儲存在本地 images/ 資料夾中,並貼在下面供參考。您可以在演示連結中下載您選擇的任何影像並使用它。
用於地面的紋理 − images/mat.jpg

用於球體的紋理 − images/rugby.jpg

babylonjs_basic_elements.htm
廣告