You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Deck.gl v9自定义片段着色器编译报错,圆角文本图层失效求助

解决deck.gl v9中自定义圆角文本背景图层的问题

以下是适配deck.gl v9的完整可运行代码:

const {Deck, TextLayer, _TextBackgroundLayer} = deck;

const data = [
  {position: [-122.45, 37.78], text: 'San Francisco'},
  {position: [-0.122, 51.51], text: 'London'},
  {position: [-58.59, -34.62], text: 'Buenos Aires'},
  {position: [174.57, -36.86], text: 'Aukland'}
];

class MyBackgroundLayer extends _TextBackgroundLayer {
  static layerName = 'MyBackgroundLayer';

  getShaders() {
    return {
      ...super.getShaders(),
      inject: {
        'fs:declaration': `
uniform float cornerRadius;
        `,
        'fs:final': `
if (cornerRadius > 0.0) {
  vec2 r = (abs(uv - 0.5) - 0.5) * dimensions + cornerRadius;
  if (r.x > 0.0 && r.y > 0.0) {
    float a = smoothstep(cornerRadius + 0.5, max(cornerRadius - 0.5, 0.0), length(r));
    if (a == 0.0) discard;
    gl_FragColor.a *= a;
  }
}`
      }
    };
  }

  getUniforms() {
    return {
      ...super.getUniforms(),
      cornerRadius: this.props.cornerRadius || 0
    };
  }
}

new Deck({
  initialViewState: {
    longitude: 0,
    latitude: 0,
    zoom: 0
  },
  controller: true,
  layers: [
    new TextLayer({
      data,
      getText: d => d.text,
      getPosition: d => d.position,
      getColor: [255, 255, 255],
      getSize: 20,
      background: true,
      getBackgroundColor: [0, 0, 0],
      backgroundPadding: [20, 4],
      
      _subLayerProps: {
        background: {
          type: MyBackgroundLayer,
          cornerRadius: 10
        }
      }
    })
  ]
});

关键改动说明:

  1. Shader注入点更新
    deck.gl v9依赖的luma.gl重构了Shader注入系统,原有的fs:#decl和fs:#main-end标记已被废弃,需替换为标准的fs:declaration(声明段注入)和fs:final(主逻辑末尾注入)才能生效。

  2. Uniform传递方式调整
    v9不再支持在draw方法中直接修改uniform参数,改为通过getUniforms方法统一返回uniform配置,这符合新的图层生命周期管理规范,确保uniforms在渲染流程中正确同步。

  3. 添加图层名称
    显式声明static layerName是deck.gl v9中自定义图层的最佳实践,避免内部图层识别时出现潜在的兼容性问题。

内容的提问来源于stack exchange,提问作者IgnorantUser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 07:43:29