通常要繪製表面法線,您可以設置單獨的緩衝區或幾何着色器來完成工作。爲一個網格物體設置一個單獨的緩衝區以僅僅繪製法線並不重要,並且不需要每個法線的繪製調用,所有的表面法線都將繪製在一個拉線中
既然你會這麼做調試的目的,沒有必要擔心太多的性能,只是堅持更快的方法,讓屏幕上的東西
我個人做的方式取決於網格是否有頂點或面法線,我們可以實例用網格中的每個頂點的線填充一個緩衝區,其中頂點本身的偏移量表示您需要使用以下僞代碼調試的正常值
var normal_buffer = [];
//tweak to your liking
var normal_length = 10.0;
//this assumes your mesh has 2 arrays of the same length
//containing structs of vertices and normals
for(var i = 0; i < mesh.vertices.length; i++) {
//retrieving the normal associated with this vertex
var nx = mesh.normals[i].x;
var ny = mesh.normals[i].y;
var nz = mesh.normals[i].z;
//retrieving the vertex itself, it'll be the first point of our line
var v1x = mesh.vertices[i].x;
var v1y = mesh.vertices[i].y;
var v1z = mesh.vertices[i].z;
//second point of our line representing the normal direction
var v2x = v1x + nx * normal_length;
var v2y = v1y + ny * normal_length;
var v2z = v1z + nz * normal_length;
buffer.push(v1x, v1y, v1z, v2x, v2y, v2z);
}
以後可以正常進行和緩衝器連接到一個頂點緩衝區對象,並使用任何程序,你想發出一個單一的繪圖調用,將提請所有網格法線
vertbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertbuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(buffer), gl.STATIC_DRAW);
/* later on in your program */
gl.drawArrays(gl.LINES, 0, buffer.length/3);
的一個很酷的功能的正常的調試是,你可以使用片段着色器中的正常本身作爲輸出顏色來快速檢查它是否指向預期的方向
是的,我意識到了正常的+位置問題。好吧,我從來沒有做過幾何着色器,我會研究它。謝謝。編輯:我只是意識到,是你的方法發出每一個正常繪製的平局? – Zebrafish