【问题标题】:clipping planes in forge viewer not able to clip objects with ShaderMaterial伪造查看器中的剪切平面无法使用 ShaderMaterial 剪切对象
【发布时间】:2019-02-12 17:38:34
【问题描述】:

我正在尝试在 forge-viewer 中添加带有 THREE.ShaderMaterial 的自定义对象,我可以在 forge-viewer 的 overlayScene 中添加和渲染对象。

我参考了this 博客以添加相同的内容。

我面临的问题是: forge-viewer 的剪裁平面无法剪裁自定义添加的对象。 如果我尝试使用其他材质添加相同的对象,则剪切平面能够剪切它们。

我试过this。但我收到Cannot resolve #include<clipping_planes_pars_vertex.glsl> 的错误(其他着色器源相同)。我尝试在THREE.ShaderChunk 中添加这些着色器,但没有成功。

我已经看到错误来自ShaderChunk.ts,因为它没有在chunks[] 中找到着色器。

  1. 有没有办法使用THREE.ShaderMaterial 的剪切平面?
  2. 我需要在chunks[] 中从ShaderChunk.ts 添加我的自定义着色器吗?如果是,如何?

如果可能,请分享一个演示示例。

【问题讨论】:

    标签: autodesk-forge autodesk-viewer forge


    【解决方案1】:

    与您发现的另一个 Stack Overflow 问题类似,Forge Viewer 中的部分工具使用自定义着色器逻辑,该逻辑仅包含在 Viewer 自己的材质中。尝试在您的材质着色器中包含以下 sn-ps:

    在顶点着色器中:

    ...
    #if NUM_CUTPLANES > 0
        varying vec3 vWorldPosition;
    #endif
    ...
    void main() {
        ...
        #if NUM_CUTPLANES > 0
            vWorldPosition = vec3(/* include your own vertex world position here */);
        #endif
        ...
    }
    ...
    

    在片段着色器中:

    ...
    #if NUM_CUTPLANES > 0
        varying highp vec3 vWorldPosition;
    #endif
    
    #include<cutplanes>
    ...
    void main() {
        ...
        #if NUM_CUTPLANES > 0
            checkCutPlanes(vWorldPosition);
        #endif
        ...
    }
    ...
    

    并且在定义新的THREE.ShaderMaterial 时,您还需要包含一些特定于切片的制服:

    const uniforms = {
        ...
        "cutplanes": { type: "v4v", value: [] },
        "hatchParams": { type: "v2", value: new THREE.Vector2(1.0, 10.0) },
        "hatchTintColor": { type: "c", value: new THREE.Color( 0xFFFFFF ) },
        "hatchTintIntensity": { type: "f", value: 1.0 },
        ...
    }
    

    请参阅此gist,了解向THREE.ShaderMaterial 添加分段支持的完整示例。

    【讨论】:

    • 我已经尝试过了,但没有成功,下面是我的顶点和片段着色器:
    • const vertexShader = #if NUM_CUTPLANES &gt; 0 varying vec3 vWorldPosition; #endif attribute vec4 color; varying vec4 vColor; void main() { vec4 vPosition = modelViewMatrix * vec4(position, 1.0); gl_Position = projectionMatrix * vPosition; vColor = color; #if NUM_CUTPLANES &gt; 0 vWorldPosition = vec3(projectionMatrix * vPosition); #endif };
    • const fragmentShader = #if NUM_CUTPLANES &gt; 0 varying highp vec3 vWorldPosition; #endif #include&lt;cutplanes&gt; #ifdef GL_ES precision highp float; #endif varying vec4 vColor; void main() { gl_FragColor = vColor; #if NUM_CUTPLANES &gt; 0 checkCutPlanes(vWorldPosition); #endif };
    • 不要将 vWorldPosition 与投影矩阵相乘。它应该设置为 vPosition.xyz
    • 不,没用。你能分享一个完整的小例子吗?
    猜你喜欢
    • 2021-10-25
    • 2023-04-11
    • 2023-03-04
    • 2013-05-29
    • 2018-12-26
    • 1970-01-01
    • 2016-02-17
    • 2021-03-14
    • 2020-12-11
    相关资源
    最近更新 更多