文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
摄像机 和 网格 对象
让我们开始编码吧!首先,我们需要定义一些有关于摄像机和网格的细节。网格这个名称用来代指三维物体。
我们的摄像机有 2 个属性:它在 3D 世界的位置以及它所看向的方位。两者都是 Vector3 类型。在 C#中用 SharpDX.Vector3,在 TypeScript & JavaScript 中使用 BABYLON.Vector3。
我们的网格有顶点数组 (一些三维点),这将用来构建我们的三维物体,它在 3D 世界中的位置和它旋转的状态。
继续,我们需要下面的代码:
【译者注:C# 代码】
// Camera.cs & Mesh.cs
using SharpDX;
namespace SoftEngine
{
public class Camera
{
public Vector3 Position { get; set; }
public Vector3 Target { get; set; }
}
public class Mesh
{
public string Name { get; set; }
public Vector3[] Vertices { get; private set; }
public Vector3 Position { get; set; }
public Vector3 Rotation { get; set; }
public Mesh(string name, int verticesCount)
{
Vertices = new Vector3[verticesCount];
Name = name;
}
}
}
【译者注:TypeScript 代码】
//<reference path="babylon.math.ts"/>
module SoftEngine {
export class Camera {
Position: BABYLON.Vector3;
Target: BABYLON.Vector3;
constructor() {
this.Position = BABYLON.Vector3.Zero();
this.Target = BABYLON.Vector3.Zero();
}
}
export class Mesh {
Position: BABYLON.Vector3;
Rotation: BABYLON.Vector3;
Vertices: BABYLON.Vector3[];
constructor(public name: string, verticesCount: number) {
this.Vertices = new Array(verticesCount);
this.Rotation = BABYLON.Vector3.Zero();
this.Position = BABYLON.Vector3.Zero();
}
}
}
【译者注:JavaScript 代码】
var SoftEngine;
(function (SoftEngine) {
var Camera = (function () {
function Camera() {
this.Position = BABYLON.Vector3.Zero();
this.Target = BABYLON.Vector3.Zero();
}
return Camera;
})();
SoftEngine.Camera = Camera;
var Mesh = (function () {
function Mesh(name, verticesCount) {
this.name = name;
this.Vertices = new Array(verticesCount);
this.Rotation = BABYLON.Vector3.Zero();
this.Position = BABYLON.Vector3.Zero();
}
return Mesh;
})();
SoftEngine.Mesh = Mesh;
})(SoftEngine || (SoftEngine = {}));
举例来说,如果你想使用我们的网格对象来描述一个立方体,你需要创建 8 个顶点(vetices) 来关联到 8 个点(points)。下面是在 Blender 中显示的立方体坐标。
用的是左手坐标系。请别忘了,当你创建一个网格,坐标系开始点再网格的中心。因此,X=0, Y=0, Z=0 是立方体的中心点。
这个立方体可以通过这样的代码来创建:
var mesh = new Mesh("Cube", 8);
mesh.Vertices[0] = new Vector3(-1, 1, 1);
mesh.Vertices[1] = new Vector3(1, 1, 1);
mesh.Vertices[2] = new Vector3(-1, -1, 1);
mesh.Vertices[3] = new Vector3(-1, -1, -1);
mesh.Vertices[4] = new Vector3(-1, 1, -1);
mesh.Vertices[5] = new Vector3(1, 1, -1);
mesh.Vertices[6] = new Vector3(1, -1, 1);
mesh.Vertices[7] = new Vector3(1, -1, -1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论