我正在开发一个带有场景图的快速 WebGL 引擎,以便在 reddit ( https://www.reddit.com/r/gameideas/comments/3dsy8m/revolt/ ) 上快速制作我的游戏创意原型。现在,在我完成了一些基本渲染之后,我无法确定正确的顺序,以及对大多数人来说看起来正确的顺序,我应该使用它来转换场景图中的节点。
很难解释正在发生的事情,但我希望你能理解它并没有像大多数人期望它在任何其他引擎中发生的那样旋转。
这是我目前正在做的事情的简化版本。
- Mat4 = glMatrix 0.9.5
- 实用程序 = 自定义实用程序
节点(渲染):
@param {父矩阵}
// Create Local Matrix
self.lMatrix = mat4.create();
mat4.identity(self.lMatrix);
mat4.translate(self.lMatrix, self.position);
mat4.rotate(self.lMatrix, self.rotation[0], [1, 0, 0]);
mat4.rotate(self.lMatrix, self.rotation[1], [0, 1, 0]);
mat4.rotate(self.lMatrix, self.rotation[2], [0, 0, 1]);
var wMatrix = mat4.create();
mat4.identity(wMatrix);
if(parentMatrix){
mat4.multiply(self.lMatrix, parentMatrix, wMatrix);
}
else{
mat4.set(self.lMatrix, wMatrix);
}
// Render
var children = this.children,
numChildren = children.length,
child;
for(var i = 0; i < numChildren; i++){
child = children[i];
child.render(wMatrix);
}
实体(渲染):
@param {父矩阵}
// Set Transformation matrix
var tMatrix = mat4.create();
mat4.identity(tMatrix);
mat4.translate(tMatrix, self.position);
mat4.rotate(tMatrix, self.rotation[0], [1, 0, 0]);
mat4.rotate(tMatrix, self.rotation[1], [0, 1, 0]);
mat4.rotate(tMatrix, self.rotation[2], [0, 0, 1]);
mat4.scale(tMatrix, self.scale || [1, 1, 1]);
var wMatrix = mat4.create();
mat4.identity(wMatrix);
mat4.multiply(tMatrix, parentMatrix, wMatrix);
Utils.loadTMatrix(wMatrix);
this.texture.bind();
this.mesh.render();