1.多緩沖區(qū)
- 多緩沖區(qū)技術(shù)通常涉及到創(chuàng)建多個(gè)緩沖區(qū)對(duì)象,并將它們用于不同的數(shù)據(jù)集。
- 這種做法可以提高數(shù)據(jù)處理效率,尤其是在處理大量數(shù)據(jù)或需要頻繁更新數(shù)據(jù)時(shí)。
- 通過預(yù)先分配和配置多個(gè)緩沖區(qū),可以在不影響渲染性能的情況下,快速切換數(shù)據(jù)集。
2.數(shù)據(jù)偏移
- 數(shù)據(jù)偏移是指在處理緩沖區(qū)數(shù)據(jù)時(shí),指針跳過一定數(shù)量的字節(jié)來定位特定的數(shù)據(jù)。
- 在WebGL中,gl.vertexAttribPointer函數(shù)的offset參數(shù)用于指定從緩沖區(qū)起始位置開始的偏移量。
- 這允許開發(fā)者將不同類型的數(shù)據(jù)(如頂點(diǎn)位置、法線、顏色等)存儲(chǔ)在同一個(gè)緩沖區(qū)中,并通過偏移量來正確地訪問這些數(shù)據(jù)。
3. 實(shí)現(xiàn)示例
3.1. 聲明aPointSize
attribute float aPointSize;
3.2. 獲取attribute變量aPointSize
const aPointSize = gl.getAttribLocation(program, 'aPointSize');
3.3. 獲取字節(jié)數(shù)
const BYTES = points.BYTES_PER_ELEMENT;
3.4. 頂點(diǎn)大小參數(shù)設(shè)置:
gl.vertexAttribPointer(aPointSize, 1, gl.FLOAT, false, BYTES * 3, BYTES * 2);
4. 代碼實(shí)現(xiàn)
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>* {margin: 0;padding: 0;}canvas {margin: 50px auto;display: block;background: pink;}</style><title>修改點(diǎn)的顏色</title>
</head><body><canvas id="canvas" width="400" height="400">此瀏覽器不支持canvas</canvas><script src="./js/index.js"></script><script>const ctx = document.getElementById('canvas')const gl = ctx.getContext('webgl')// 頂點(diǎn)著色器源碼// 1. 聲明aPointSizeconst vertexShaderSource = `attribute vec4 aPosition;attribute float aPointSize;void main() {gl_Position = aPosition; gl_PointSize = aPointSize;}`// 片源著色器源碼const fragmentShaderSource = `void main() {gl_FragColor = vec4(0.0,0.0,0.0,1.0); // r, g, b, a}`const program = initShader(gl, vertexShaderSource, fragmentShaderSource);const aPosition = gl.getAttribLocation(program, 'aPosition');// 1.獲取attribute變量aPointSizeconst aPointSize = gl.getAttribLocation(program, 'aPointSize');// 2.創(chuàng)建頂點(diǎn)數(shù)據(jù),前兩位是頂點(diǎn)的位置,第三位是頂點(diǎn)的大小const points = new Float32Array([-0.5, -0.5, 10.0,0.5, -0.5, 20.0,0.0, 0.5, 30.0,])const buffer = gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER, buffer);gl.bufferData(gl.ARRAY_BUFFER, points, gl.STATIC_DRAW);// 3.獲取字節(jié)數(shù)const BYTES = points.BYTES_PER_ELEMENT;// 4.頂點(diǎn)位置參數(shù)設(shè)置:兩個(gè)相鄰頂點(diǎn)之間的字節(jié)數(shù)為三個(gè)字節(jié),所以字節(jié)數(shù)為BYTES*3; 頂點(diǎn)數(shù)據(jù)是前兩位,所以偏移量為0;gl.vertexAttribPointer(aPosition, 2, gl.FLOAT, false, BYTES * 3, 0);// 5.頂點(diǎn)大小參數(shù)設(shè)置:兩個(gè)相鄰頂點(diǎn)之間的字節(jié)數(shù)為三個(gè)字節(jié),所以字節(jié)數(shù)為BYTES*3; 頂點(diǎn)數(shù)據(jù)是第三位,所以偏移量為BYTES*2;gl.vertexAttribPointer(aPointSize, 1, gl.FLOAT, false, BYTES * 3, BYTES * 2);// 4.激活變量gl.enableVertexAttribArray(aPosition);gl.enableVertexAttribArray(aPointSize);gl.drawArrays(gl.POINTS, 0, 3);// 著色器 function initShader(gl, vertexShaderSource, fragmentShaderSource) {const vertexShader = gl.createShader(gl.VERTEX_SHADER);const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);gl.shaderSource(vertexShader, vertexShaderSource);gl.shaderSource(fragmentShader, fragmentShaderSource);gl.compileShader(vertexShader);gl.compileShader(fragmentShader);const program = gl.createProgram();gl.attachShader(program, vertexShader);gl.attachShader(program, fragmentShader);gl.linkProgram(program);gl.useProgram(program);return program;}</script>
</body></html>
5. 效果如下?
