简体   繁体   English

A帧A文本蒙版

[英]A-Frame A-Text Mask

i can not find any answer about masking a picture with an a-text primitive or other possibilities with a-frame or threejs behind. 我找不到有关使用a-text原始图片遮盖图片或其他带有a-frame或threejs的可能性的答案。 The bitmap text should be a mask for an underlying image. 位图文本应该是基础图像的遮罩。 Somebody told me, it could be solved via shaders https://aframe.io/docs/0.8.0/primitives/a-text.html#attributes_shader and advised me to read the book of shaders https://thebookofshaders.com with 155 pages foreknowledge! 有人告诉我,可以通过着色器https://aframe.io/docs/0.8.0/primitives/a-text.html#attributes_shader解决,并建议我阅读着色器书https://thebookofshaders.com , 155页的预兆! I am not sure if this is the right hint anyway!? 我不确定这是否是正确的提示!?

Here you can see the illustrated task position as grafic view: 在这里,您可以看到图示的任务位置为局部视图:

https://photos.app.goo.gl/MLZjcucfpuqmtinH7 https://photos.app.goo.gl/MLZjcucfpuqmtinH7

Codepen: http://codepen.w3x.de Codepen: http ://codepen.w3x.de

Copy and paste the A-Frame text shader code (remove the module.exports) and register a text shader. 复制并粘贴A框架文本着色器代码(删除module.exports)并注册一个文本着色器。 Here's the current MSDF shader: 这是当前的MSDF着色器:

AFRAME.registerShader('yourtextshader', {
      schema: {
        alphaTest: {type: 'number', is: 'uniform', default: 0.5},
        color: {type: 'color', is: 'uniform', default: 'white'},
        map: {type: 'map', is: 'uniform'},
        negate: {type: 'boolean', is: 'uniform', default: true},
        opacity: {type: 'number', is: 'uniform', default: 1.0}
      },

      raw: true,

      vertexShader: [
        'attribute vec2 uv;',
        'attribute vec3 position;',
        'uniform mat4 projectionMatrix;',
        'uniform mat4 modelViewMatrix;',
        'varying vec2 vUV;',
        'void main(void) {',
        '  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);',
        '  vUV = uv;',
        '}'
      ].join('\n'),

      fragmentShader: [
        '#ifdef GL_OES_standard_derivatives',
        '#extension GL_OES_standard_derivatives: enable',
        '#endif',

        'precision highp float;',
        'uniform bool negate;',
        'uniform float alphaTest;',
        'uniform float opacity;',
        'uniform sampler2D map;',
        'uniform vec3 color;',
        'varying vec2 vUV;',

        'float median(float r, float g, float b) {',
        '  return max(min(r, g), min(max(r, g), b));',
        '}',

        // FIXME: Experimentally determined constants.
        '#define BIG_ENOUGH 0.001',
        '#define MODIFIED_ALPHATEST (0.02 * isBigEnough / BIG_ENOUGH)',

        'void main() {',
        '  vec3 sample = texture2D(map, vUV).rgb;',
        '  if (negate) { sample = 1.0 - sample; }',

        '  float sigDist = median(sample.r, sample.g, sample.b) - 0.5;',
        '  float alpha = clamp(sigDist / fwidth(sigDist) + 0.5, 0.0, 1.0);',
        '  float dscale = 0.353505;',
        '  vec2 duv = dscale * (dFdx(vUV) + dFdy(vUV));',
        '  float isBigEnough = max(abs(duv.x), abs(duv.y));',

        '  // Do modified alpha test.',
        '  if (alpha < alphaTest * MODIFIED_ALPHATEST) { discard; return; }',
        '  gl_FragColor = vec4(color.xyz, alpha * opacity);',
        '}'
      ].join('\n')
    });

The fragment shader is what computes the color of the text. 片段着色器用于计算文本的颜色。 You get vUV which is the UV map for that fragment, telling where to sample in the texture. 您将获得vUV ,它是该片段的UV贴图,告诉您在纹理中的采样位置。

What you would need to do to modify this shader. 修改此着色器将需要执行的操作。 Don't have time to do a full example yet, but...: 还没有时间做一个完整的例子,但是...:

  1. Accept another parameter of your texture, add to the schema. 接受纹理的另一个参数,将其添加到架构中。
  2. Pass that texture into the shader ( <a-entity text="shader: yourTextShader; yourTexture: #texture ). 将该纹理传递到着色器中( <a-entity text="shader: yourTextShader; yourTexture: #texture )。
  3. Add a uniform for that texture in the fragment shader uniform sampler2D yourTexture ). 在片段着色器中为该纹理添加一个统一的uniform sampler2D yourTextureuniform sampler2D yourTexture )。
  4. Modify the fragment shader to use the texture color instead of the color gl_FragColor = vec4(texture2D(yourTexture, vUv), alpha * opacity) 修改片段着色器以使用纹理颜色而不是颜色gl_FragColor = vec4(texture2D(yourTexture, vUv), alpha * opacity)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM