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

React Three Fiber中波纹(Ripple)效果无法显示的问题排查求助

React Three Fiber中波纹(Ripple)效果无法显示的问题排查求助

我帮你排查了这段代码,发现几个可能导致波纹效果无法渲染的关键点,咱们一步步来解决:

1. 相机位置与视野问题

你的相机设置为position: [100, 10, 0],但你创建的点网格范围是从-750到750(count=500,sep=3,500/2*3=750),这个位置的相机只能看到很小一部分区域,甚至可能完全看不到点网格。建议调整相机位置到能俯视整个网格的视角,比如:

camera={{position: [0, 100, 800], fov: 90}}

2. BufferAttribute的Attach方式有误

你用了attachObject={['attributes','position']}这种旧写法,在React Three Fiber的新版本中,正确的属性绑定方式应该是:

attach="attributes.position"

这个修改能确保位置属性正确挂载到几何体上。

3. 点数量过大导致性能问题

一次性创建500*500=250000个点,对浏览器渲染压力很大,可能会导致渲染卡顿甚至不显示。建议先把count改成50,测试基础渲染是否正常,确认没问题后再逐步增大数量。

4. Canvas容器尺寸问题

如果你的.anim容器没有设置宽高,Canvas会被压缩成0高度,自然看不到内容。记得在App.css里添加:

.anim {
  width: 100vw;
  height: 100vh;
}

调整后的测试代码

我把这些修改整合到了你的代码里,你可以先测试这个版本:

import './App.css'; 
import { useMemo } from 'react';
import * as THREE from 'three'; 
import { Canvas, useLoader} from '@react-three/fiber';
import circleImg from './assets/circle.png'; 
import { Suspense } from 'react';

function Points(){
  
  const imgTex = useLoader(THREE.TextureLoader, circleImg); 

  const count = 50; // 先减小数量测试
  const sep = 3;  

  const positions = useMemo(() => {
     let coords = [];

     for(let i = 0; i < count; i++){
      for(let j = 0; j < count; j++){
        let x = sep * (i - count/2);
        let z = sep * (j - count/2);
        let y = 0;
        coords.push(x,y,z);
      }
     }

     let arr = new Float32Array(coords); 
     console.log(arr); 

     return arr; 

  }, [count, sep]);

  return(
    <points>
      <bufferGeometry attach="geometry">
        <bufferAttribute 
          attach="attributes.position" // 修正attach方式
          array={positions}
          count={positions.length/3}
          itemSize={3}
        />
      </bufferGeometry>
      
      <pointsMaterial 
        attach="material"
        //map={imgTex}
        color={0x00AAFF}
        size={0.5}
        sizeAttenuation
        transparent={false}
        alphaTest={0.5}
        opacity={1.0} 
      />
    </points>
  )
}

function AnimationCanvas(){
  return (
    <Canvas 
        camera={{position: [0, 100, 800], fov: 90}}> // 调整相机位置
        <Points />
    </Canvas>
  )
}


function App() { 

  return (
      <div className="anim">
        <Suspense fallback={<div>loading...</div>}>
          <AnimationCanvas />
        </Suspense>
      </div>
  )
}

export default App

等测试出点网格后,你就可以通过useFrame钩子来更新点的y坐标,实现波纹动画效果啦。

备注:内容来源于stack exchange,提问作者Ande Caleb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 13:32:57