我是threejs的新手,我在尝试在屏幕上显示星星模型时遇到了问题。点可以正常工作,但我收到了以下消息:
THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.
在查看我的代码并发现出现问题的部分是stars canvas之后。
const StarsCanvas = () => {
return (
<div className="w-full h-auto absolute inset-0 z-[-1]">
<Canvas camera={{ position: [0, 0, 1] }}>
<Suspense fallback={null}>
<Stars />
</Suspense>
<Preload all />
</Canvas>
</div>
);
};
这是我正在使用的画布,以下是星星组件:
星星:
import * as random from "maath/random/dist/maath-random.esm";
...
const Stars = (props) => {
const ref = useRef();
const [sphere] = useState(() =>
random.inSphere(new Float32Array(5000), { radius: 1.2 })
);
useFrame((state, delta) => {
ref.current.rotation.x -= delta / 10;
ref.current.rotation.y -= delta / 15;
});
return (
<group rotation={[0, 0, Math.PI / 4]}>
<Points ref={ref} positions={sphere} stride={3} frustumCulled {...props}>
<PointMaterial
transparent
color="#f272c8"
size={0.002}
sizeAttenuation={true}
depthWrite={false}
/>
</Points>
</group>
);
};
使用useFrames让星星移动,它正在工作。
导致问题的部分很可能是
random.inSphere(new Float32Array(5000), { radius: 1.2 })
它显然会给出使半径变为NaN的值(也许是在加载后?)。
我正在使用"@react-three/fiber"和"@react-three/drei"库。
我阅读了文档,但没有找到解决方案,希望能得到一些帮助。 谢谢!
重新编写随机位置代码仍然不起作用。 尝试另一种方式来显示星星。
random
从哪里来? - kcanull
有关?“默认情况下不会计算边界框。必须明确计算,否则它们将为null
。” - kca