反应三个JS不渲染
我一直在尝试遵循本教程: https://www.youtube.com/观看?v = wrmeftrkf -8
但是,当我运行代码时,我只会得到空白(在CSS中设置的黑色背景颜色)画布。我知道帆布是运营的,因为当我在黑色部分上滚动时,我不会在页面上上下滚动。我在视频 / github中使用了相同的代码,只用 @react-three / fiber代替了三纤维。
这是我的代码:
import './Ripple.css';
import * as THREE from 'three';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls';
import { Canvas, extend, useFrame, useLoader, useThree } from '@react-three/fiber';
import circleImg from './assets/circle.png';
import { Suspense, useCallback, useMemo, useRef } from 'react';
extend({OrbitControls})
function CameraControls(){
const {
camera,
gl: {domElement}
} = useThree();
const controlsRef = useRef();
useFrame(() => controlsRef.current.update())
return (
<orbitControls
ref={controlsRef}
args={[camera, domElement]}
autoRotate
autoRotateSpeed={-0.2}
/>
);
}
function Points() {
const imgTex = useLoader(THREE.TextureLoader, circleImg);
const bufferRef = useRef();
let t = 0;
let f = 0.002;
let a = 3;
const graph = useCallback((x, z) => {
return Math.sin(f * (x ** 2 + z ** 2 + t)) * a;
}, [t, f, a])
const count = 100
const sep = 3
let positions = useMemo(() => {
let positions = []
for (let xi = 0; xi < count; xi++) {
for (let zi = 0; zi < count; zi++) {
let x = sep * (xi - count / 2);
let z = sep * (zi - count / 2);
let y = graph(x, z);
positions.push(x, y, z);
}
}
return new Float32Array(positions);
}, [count, sep, graph])
useFrame(() => {
t += 15
const positions = bufferRef.current.array;
let i = 0;
for (let xi = 0; xi < count; xi++) {
for (let zi = 0; zi < count; zi++) {
let x = sep * (xi - count / 2);
let z = sep * (zi - count / 2);
positions[i + 1] = graph(x, z);
i += 3;
}
}
bufferRef.current.needsUpdate = true;
})
return (
<points>
<bufferGeometry attach="geometry">
<bufferAttribute
ref={bufferRef}
attachObject={['attributes', 'position']}
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
colorManagement={false}
camera={{ position: [100, 10, 0], fov: 75 }}
>
<Suspense fallback={null}>
<Points />
</Suspense>
<CameraControls/>
</Canvas>
);
}
function Ripple() {
return (
<div className="anim">
<Suspense fallback={<div>Loading...</div>}>
<AnimationCanvas />
</Suspense>
</div>
);
}
export default Ripple;
谢谢您的帮助!
I've been trying to follow this tutorial: https://www.youtube.com/watch?v=wRmeFtRkF-8
but when I run my code, I only get a blank (black - background color set in the CSS) canvas. I know the canvas is operational because when I scroll on the black portion, I do not scroll up and down the page. I used the same code in the video / github and only replaced react-three-fiber with @react-three/fiber.
Here is my code:
import './Ripple.css';
import * as THREE from 'three';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls';
import { Canvas, extend, useFrame, useLoader, useThree } from '@react-three/fiber';
import circleImg from './assets/circle.png';
import { Suspense, useCallback, useMemo, useRef } from 'react';
extend({OrbitControls})
function CameraControls(){
const {
camera,
gl: {domElement}
} = useThree();
const controlsRef = useRef();
useFrame(() => controlsRef.current.update())
return (
<orbitControls
ref={controlsRef}
args={[camera, domElement]}
autoRotate
autoRotateSpeed={-0.2}
/>
);
}
function Points() {
const imgTex = useLoader(THREE.TextureLoader, circleImg);
const bufferRef = useRef();
let t = 0;
let f = 0.002;
let a = 3;
const graph = useCallback((x, z) => {
return Math.sin(f * (x ** 2 + z ** 2 + t)) * a;
}, [t, f, a])
const count = 100
const sep = 3
let positions = useMemo(() => {
let positions = []
for (let xi = 0; xi < count; xi++) {
for (let zi = 0; zi < count; zi++) {
let x = sep * (xi - count / 2);
let z = sep * (zi - count / 2);
let y = graph(x, z);
positions.push(x, y, z);
}
}
return new Float32Array(positions);
}, [count, sep, graph])
useFrame(() => {
t += 15
const positions = bufferRef.current.array;
let i = 0;
for (let xi = 0; xi < count; xi++) {
for (let zi = 0; zi < count; zi++) {
let x = sep * (xi - count / 2);
let z = sep * (zi - count / 2);
positions[i + 1] = graph(x, z);
i += 3;
}
}
bufferRef.current.needsUpdate = true;
})
return (
<points>
<bufferGeometry attach="geometry">
<bufferAttribute
ref={bufferRef}
attachObject={['attributes', 'position']}
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
colorManagement={false}
camera={{ position: [100, 10, 0], fov: 75 }}
>
<Suspense fallback={null}>
<Points />
</Suspense>
<CameraControls/>
</Canvas>
);
}
function Ripple() {
return (
<div className="anim">
<Suspense fallback={<div>Loading...</div>}>
<AnimationCanvas />
</Suspense>
</div>
);
}
export default Ripple;
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过使用呈现controls和舞台而不是轨道孔子解决了错误,这是一个工作片段
I resolved the error by using PresentationControls and Stage instead of OrbitalControls, here's a working snippet