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

Three.js:使用裁剪平面裁剪troika-three-text失效问题咨询

如何用Three.js裁剪平面裁剪troika-three-text组件

可以用Three.js的裁剪平面裁剪troika-three-text组件,但你的代码存在两个关键问题导致功能失效:

  1. 裁剪平面重复:你定义的两个平面方向和参数完全一致,无法形成有效的裁剪区域,需要设置方向相反的平面。
  2. troika材质的特殊性:troika-three-text的Text组件使用自定义ShaderMaterial,默认不支持裁剪平面逻辑,需要手动注入着色器代码来实现裁剪。

修正后的代码示例

import { Text } from 'troika-three-text';
import * as THREE from 'three';

// 定义上下两个方向相反的裁剪平面,控制文本的显示区域
const bottomClipPlane = new THREE.Plane(new THREE.Vector3(0, -1, 0), 0.5); // 裁剪文本下方超出y=0.5的部分
const topClipPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0.5); // 裁剪文本上方超出y=-0.5的部分

const text = new Text({
  text: "Hello",
  fontSize: 0.5,
  position: new THREE.Vector3(0, 0, 0)
});

// 为troika的材质注入裁剪逻辑
text.material.onBeforeCompile = (shader) => {
  // 添加裁剪相关的uniform变量
  shader.uniforms.clippingPlanes = { value: [bottomClipPlane, topClipPlane] };
  shader.uniforms.clipIntersection = { value: true };

  // 顶点着色器中传递裁剪空间位置
  shader.vertexShader = `
    varying vec4 vClipPos;
    ${shader.vertexShader}
  `.replace(
    '#include <project_vertex>',
    `#include <project_vertex>
    vClipPos = gl_Position;
    `
  );

  // 片元着色器中添加裁剪判断逻辑
  shader.fragmentShader = `
    uniform vec4 clippingPlanes[2];
    uniform bool clipIntersection;
    varying vec4 vClipPos;
    ${shader.fragmentShader}
  `.replace(
    'void main() {',
    `void main() {
      bool clipped = false;
      for (int i = 0; i < 2; i++) {
        float dist = dot(clippingPlanes[i].xyz, vClipPos.xyz) + clippingPlanes[i].w;
        if (dist < 0.0) {
          clipped = !clipped;
          if (!clipIntersection) break;
        }
      }
      // 根据clipIntersection决定是否丢弃像素
      if ((clipIntersection && !clipped) || (!clipIntersection && clipped)) {
        discard;
      }
    `
  );
};

// 将文本添加到场景
scene.add(text);

关键说明

  • 裁剪平面参数:THREE.Plane的第二个参数是平面到原点的距离,根据文本的大小和位置调整数值,确保裁剪区域符合预期。
  • clipIntersection属性:设为true时,只显示两个裁剪平面的交集区域;设为false时,显示两个平面之外的区域,按需调整。
  • 着色器注入:troika的自定义材质需要手动添加裁剪的着色器逻辑,通过onBeforeCompile钩子修改顶点和片元着色器,实现裁剪效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 09:02:54