- BabylonJS 教程
- BabylonJS - 主頁
- BabylonJS - 簡介
- BabylonJS - 環境設定
- BabylonJS - 概述
- BabylonJS - 基本元素
- BabylonJS - 材質
- BabylonJS - 動畫
- BabylonJS - 相機
- BabylonJS - 光源
- BabylonJS - 引數形狀
- BabylonJS - 網格
- 向量位置和旋轉
- BabylonJS - 貼花
- BabylonJS - 三維曲線
- BabylonJS - 動態紋理
- BabylonJS - 視差貼圖
- BabylonJS - 鏡頭光暈
- BabylonJS - 建立螢幕截圖
- BabylonJS - 反射探測
- 標準渲染管道
- BabylonJS - 著色器材質
- BabylonJS - 骨骼
- BabylonJS - 物理引擎
- BabylonJS - 播放聲音和音樂
- BabylonJS 有用資源
- BabylonJS - 快速指南
- BabylonJS - 有用資源
- BabylonJS - 討論
BabylonJS - 線段
線段是 3D 遊戲中的基本元素。要繪製線段,需要兩點才能在它們之間繪製線段。
演示
<!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( .5, .5, .5);
var camera = new BABYLON.ArcRotateCamera("camera1", 0, 0, 0, new BABYLON.Vector3(0, 0, -0), scene);
camera.setPosition(new BABYLON.Vector3(0, 0, -100));
camera.attachControl(canvas, true);
var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 0.5, 0), scene);
light.intensity = 0.7;
var pl = new BABYLON.PointLight("pl", new BABYLON.Vector3(0, 0, 0), scene);
pl.diffuse = new BABYLON.Color3(1, 1, 1);
pl.specular = new BABYLON.Color3(1, 0, 0);
pl.intensity = 0.95;
// lines creation
var path = [];
for(var t = 0; t < 40; t++) {
var x = t*Math.cos(5*t);
var y = t*Math.sin(5*t);
var z = 0;
path.push(new BABYLON.Vector3(x, y, z));
}
var mesh = BABYLON.Mesh.CreateLines("lines", path, scene, true);
var k = 0;
scene.registerBeforeRender(function() {
pl.position = camera.position;
});
return scene;
};
var scene = createScene();
engine.runRenderLoop(function() {
scene.render();
});
</script>
</body>
</html>
輸出
以上程式碼行生成以下輸出 −
要獲取沿 x 和 y 軸的座標,請使用以下方程式(數學函式)並使用 x 和 y 值建立路徑。與 babylonjs 的 createline 類相同。使用您選擇的任意方程式,嘗試建立不同的線段。
var mesh = BABYLON.Mesh.CreateLines("lines", path, scene, true);
使用以下方程式建立線段 −
var x = t*Math.cos(5*t); var y = t*Math.sin(5*t); var z = 0;
t 的值從 0 到 40。
執行以下程式碼行時,形狀會發生變化 −
var x = t * Math.cos(6 * t); var y = t * Math.sin(6 * t); var z = 0;
線段的形狀如下所示 −
babylonjs_parametric_shapes.htm
廣告