简体   繁体   English

如何在three.js或WebGL中模拟“overflow:hidden”?

[英]How do I simulate “overflow: hidden” in three.js or WebGL?

I'm currently working on a WebGL GUI for my games and I really want to dig deep into GPU graphics, since it's a lot smoother than WebKit CSS rendering. 我目前正在为我的游戏开发WebGL GUI,我真的想深入研究GPU图形,因为它比WebKit CSS渲染要平滑得多。

Is it possible to make a scrollview where the inside meshes are following overflow rule to hide when going outside the boundaries of the parent mesh? 是否可以制作滚动视图,其中内部网格遵循溢出规则以在超出父网格边界时隐藏?

Perhaps a shader could work, any suggestions? 也许着色器可以工作,任何建议?

Thanks! 谢谢!

If you only want to clip by rectangles you can use the scissor test. 如果您只想按矩形剪辑,可以使用剪刀测试。

gl.enable(gl.SCISSOR_TEST);
gl.scissor(x, y, width, height);

Now WebGL will only render between x, y, width, and height. 现在,WebGL将仅在x,y,width和height之间呈现。

THREE.js also has scissor settings WebGLRenderer.setScissor and WebGLRenderer.setScissorTest THREE.js还有剪刀设置WebGLRenderer.setScissorWebGLRenderer.setScissorTest

You could use a "stencil test" to achieve this. 您可以使用“模板测试”来实现此目的。 The stencil test allows you to mask subsequent rendering of geometry against pixels that you have denoted as the "stencil". 模板测试允许您屏蔽几何体的后续渲染与您表示为“模板”的像素。

In terms of what you are doing, you might use the stencil technique to: 就您正在做的事情而言,您可以使用模板技术:

  1. specify the interior (rectangle?) of your scroll area as the "stencil". 将滚动区域的内部(矩形?)指定为“模板”。 At this point, you would "draw" a rectangle that represents the boundary of your scroll-able area into your "stencil buffer". 此时,您将“绘制”一个矩形,表示可滚动区域的边界到“模板缓冲区”中。
  2. Now, with the stencilled area defined and rendered to your stencil buffer, you'd then render the actual contents of the scroll-able area. 现在,通过定义模板区域并将其渲染到模板缓冲区,然后渲染可滚动区域的实际内容。 Because the stencil buffer is active, the contents of the scrollbar area are clipped by the rectangle you drew into the "stencil buffer". 由于模板缓冲区处于活动状态,滚动条区域的内容将被您绘制到“模板缓冲区”中的矩形剪切。 Any contents rendered outside of the "stencilled pixels" would not be rendered. 在“模板化像素”之外渲染的任何内容都不会被渲染。

To give you an idea of how to achieve this, you could define your rendering sequence as follows: 为了让您了解如何实现此目的,您可以按如下方式定义渲染序列:

    // Clearing the stencil buffer
    gl.clearStencil(0);
    gl.clear(gl.STENCIL_BUFFER_BIT);


    // Tell webgl how to render into the stencil buffer
    gl.stencilFunc(gl.ALWAYS, 1, 1);
    gl.stencilOp(gl.REPLACE, gl.REPLACE, gl.REPLACE);
    gl.colorMask(false, false, false, false);

    gl.enable(gl.STENCIL_TEST);

    // Renders the inner rectangle of scroll area
    drawInnerRectangleOfScrollArea();

    // Tell webgl how to clip rendering of the scroll area content
    gl.stencilFunc(gl.EQUAL, 1, 1);
    gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
    gl.colorMask(true, true, true, true);

    // Renders the inner contents of scroll area (ie the list of items, etc)
    drawInnerContentsOfScrollArea();

    // Reset the stenicl test state so to not affect any other rendering
    gl.disable(gl.STENCIL_TEST);

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

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