Buffer Update
Runnable luma.gl example: Buffer Update. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
说明:二百个顶点连成一条波浪线,每帧改写顶点位置并重新上传,形成流动效果
核心逻辑:Buffer.write 覆盖整段顶点数据,Model 的拓扑设为 line-strip
依赖:@luma.gl/core 9.4.2、webgl、shadertools、engine · jsDelivr CDN(UMD)
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>缓冲区数据更新</title>
<style>
html,body{height:100%;margin:0;background:#0d1117}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
.bar{flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
display:flex;gap:12px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar input[type=range]{width:120px;accent-color:#4f9ef8}
.bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:5px;cursor:pointer;
border:1px solid #303845;background:#1d2531;color:#c8d2e0}
.bar button:hover{background:#253040}
.bar .sp{flex:1 1 auto}
.wrap{position:relative;flex:1 1 auto;min-height:0;display:flex}
#cv{flex:1 1 auto;min-height:0;width:100%;display:block}
#err{position:absolute;left:0;right:0;top:0;padding:16px;color:#ff9d85;
font:12px/1.7 ui-monospace,Consolas,monospace;white-space:pre-wrap;display:none}
</style>
</head>
<body>
<div class="bar">
<span>顶点 <b id="n">200</b></span>
<span>上传 <b id="cnt">0</b> 次</span>
<label>振幅 <input type="range" id="amp" min="0.1" max="0.6" step="0.02" value="0.35"></label>
<label>频率 <input type="range" id="freq" min="2" max="12" step="0.5" value="6"></label>
<button id="pause">暂停</button>
<span class="sp"></span>
<span>line-strip · 每帧整体覆盖缓冲区</span>
</div>
<div class="wrap">
<canvas id="cv"></canvas>
<div id="err"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/@luma.gl/core@9.4.2/dist/dist.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@luma.gl/webgl@9.4.2/dist/dist.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@luma.gl/shadertools@9.4.2/dist/dist.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@luma.gl/engine@9.4.2/dist/dist.min.js"></script>
<script>
(function () {
'use strict';
var cv = document.getElementById('cv');
var errBox = document.getElementById('err');
function fail(msg) {
errBox.style.display = 'block';
errBox.textContent = '运行失败:' + msg;
}
if (!window.luma || !luma.Model || !luma.AnimationLoop) {
fail('luma.gl 未加载成功,请检查能否访问 cdn.jsdelivr.net');
return;
}
var COUNT = 200;
var amp = 0.35;
var freq = 6;
var running = true;
var uploads = 0;
var data = new Float32Array(COUNT * 3);
function fillBuffer(t) {
for (var i = 0; i < COUNT; i++) {
var x = (i / (COUNT - 1)) * 2 - 1;
var y = Math.sin(x * freq + t * 1.6) * amp * (1 - 0.35 * Math.abs(x));
data[i * 3] = x * 0.92;
data[i * 3 + 1] = y;
data[i * 3 + 2] = 0;
}
}
fillBuffer(0);
document.getElementById('n').textContent = COUNT;
luma.luma.createDevice({
type: 'webgl',
adapters: [luma.webgl2Adapter],
createCanvasContext: {
canvas: cv,
useDevicePixels: Math.min(window.devicePixelRatio, 2),
autoResize: true
}
}).then(function (device) {
var posBuffer = device.createBuffer({
data: data,
usage: luma.Buffer.VERTEX | luma.Buffer.COPY_DST
});
var model = new luma.Model(device, {
id: 'wave',
topology: 'line-strip',
vertexCount: COUNT,
attributes: { positions: posBuffer },
bufferLayout: [
{ name: 'positions', stepMode: 'vertex', format: 'float32x3' }
],
vs: [
'#version 300 es',
'in vec3 positions;',
'void main() {',
' gl_Position = vec4(positions, 1.0);',
'}'
].join('\n'),
fs: [
'#version 300 es',
'precision highp float;',
'out vec4 fragColor;',
'void main() {',
' fragColor = vec4(0.42, 0.85, 0.98, 1.0);',
'}'
].join('\n')
});
var start = performance.now();
var cntEl = document.getElementById('cnt');
var loop = new luma.AnimationLoop({
device: device,
onRender: function (props) {
var pass = props.device.beginRenderPass({ clearColor: [0.04, 0.06, 0.09, 1] });
if (running) {
fillBuffer((performance.now() - start) / 1000);
posBuffer.write(data);
uploads++;
}
model.draw(pass);
pass.end();
if (uploads % 10 === 0) cntEl.textContent = uploads;
},
onError: function (e) { fail(e.message); }
});
loop.start();
document.getElementById('amp').addEventListener('input', function () {
amp = Number(this.value);
});
document.getElementById('freq').addEventListener('input', function () {
freq = Number(this.value);
});
var pauseBtn = document.getElementById('pause');
pauseBtn.addEventListener('click', function () {
running = !running;
pauseBtn.textContent = running ? '暂停' : '继续';
});
}).catch(function (e) {
fail(e && e.message ? e.message : String(e));
});
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.