Atlaslog
Atlaslog / Examples / luma.gl / Color Triangle
luma.gl · Runnable Example

Color Triangle

A minimal WebGL2 pipeline with shaders and per-vertex color attributes.

Open standalone → Chinese notes All luma.gl examples
/demos/lumagl-color-triangle.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/lumagl-color-triangle.html
<!--
  说明:每个顶点带一种颜色,片元着色器按重心插值,得到平滑的渐变三角
  核心逻辑:位置与颜色各建一个缓冲区,顶点着色器用 varying 传给片元阶段
  依赖:@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:10px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
  .bar b{color:#58a6ff;font-weight:600}
  .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>positions · colors</b></span>
  <button id="palette">换一组配色</button>
  <span class="sp"></span>
  <span>三个顶点各一个颜色,中间由 GPU 插值</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 palettes = [
    [[0.95, 0.36, 0.32], [0.98, 0.78, 0.35], [0.35, 0.80, 0.95]],
    [[0.42, 0.86, 0.66], [0.32, 0.55, 0.98], [0.86, 0.45, 0.92]]
  ];
  var paletteIndex = 0;

  var positions = new Float32Array([
    -0.6, -0.45, 0,
     0.6, -0.45, 0,
     0.0,  0.55, 0
  ]);

  var colors = new Float32Array(palettes[0][0].concat(palettes[0][1], palettes[0][2]));

  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: positions,
      usage: luma.Buffer.VERTEX | luma.Buffer.COPY_DST
    });

    var colBuffer = device.createBuffer({
      data: colors,
      usage: luma.Buffer.VERTEX | luma.Buffer.COPY_DST
    });

    /* 两个 vec3 属性分别声明布局,Model 才能找到着色器里的同名输入 */
    var model = new luma.Model(device, {
      id: 'color-triangle',
      topology: 'triangle-list',
      vertexCount: 3,
      attributes: {
        positions: posBuffer,
        colors: colBuffer
      },
      bufferLayout: [
        { name: 'positions', stepMode: 'vertex', format: 'float32x3' },
        { name: 'colors', stepMode: 'vertex', format: 'float32x3' }
      ],
      vs: [
        '#version 300 es',
        'in vec3 positions;',
        'in vec3 colors;',
        'out vec3 vColor;',
        'void main() {',
        '  vColor = colors;',
        '  gl_Position = vec4(positions, 1.0);',
        '}'
      ].join('\n'),
      fs: [
        '#version 300 es',
        'precision highp float;',
        'in vec3 vColor;',
        'out vec4 fragColor;',
        'void main() {',
        '  fragColor = vec4(vColor, 1.0);',
        '}'
      ].join('\n')
    });

    var loop = new luma.AnimationLoop({
      device: device,
      onRender: function (props) {
        var pass = props.device.beginRenderPass({ clearColor: [0.04, 0.06, 0.09, 1] });
        model.draw(pass);
        pass.end();
      },
      onError: function (e) { fail(e.message); }
    });

    loop.start();

    document.getElementById('palette').addEventListener('click', function () {
      paletteIndex = (paletteIndex + 1) % palettes.length;
      var p = palettes[paletteIndex];
      colors.set(p[0], 0);
      colors.set(p[1], 3);
      colors.set(p[2], 6);
      /* 只改颜色缓冲区,位置与着色器都不动 */
      colBuffer.write(colors);
      loop.setNeedsRedraw('palette');
    });
  }).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.

More luma.gl examples

luma.gl · EXAMPLEDepth Test luma.gl · EXAMPLEDraw Rect luma.gl · EXAMPLEInstancing