【问题标题】:Make Image to Particles with Three JS in Next JS在 Next JS 中用三个 JS 将图像制作成粒子
【发布时间】:2022-01-23 17:01:58
【问题描述】:

现在我正在学习三个 JS,现在我在阻止我的期望应该是这样的:

但现在我的代码没有显示任何视图,我的代码是这样的:

import { useEffect } from "react/cjs/react.development";
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";

export default () => {
  useEffect(() => {
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(
      75,
      window.innerWidth / window.innerHeight,
      1,
      1000
    );
    const canvas = document.querySelector("canvas.background");
    let renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
    new OrbitControls(camera, renderer.domElement);

    const img = new Image();
    let texture = new THREE.Texture(img);
    img.onload = () => {
      texture.needsUpdate = true;
      const material = new THREE.PointsMaterial({
        size: 20,
        map: texture,
        blending: THREE.AdditiveBlending,
        transparent: true,
        depthTest: false,
      });
      const particle = new THREE.Sprite(material);
      particle.scale.x = particle.scale.y = 1;
      scene.add(particle);
    };
    img.src = "/flower.png";
    scene.add(camera);

    camera.position.z = 3;
    const animate = () => {
      const elapsedTime = new THREE.Clock().getElapsedTime();

      renderer.setPixelRatio(window.devicePixelRatio);
      renderer.setSize(window.innerWidth, window.innerHeight);
      renderer.render(scene, camera);
      requestAnimationFrame(animate);
    };
    animate();
  }, []);

  return <canvas className="background" />;
};

我尝试了论坛的不同答案,但似乎对我不起作用

你们能帮忙吗?我会感激的

【问题讨论】:

    标签: javascript three.js next.js webgl particles


    【解决方案1】:

    您不能将THREE.PointsMaterialTHREE.Sprite 一起使用。 THREE.PointsMaterial 适用于 THREE.Points。请改用THREE.SpriteMaterial

    【讨论】:

    • 酷!现在它显示出来了,但似乎图像尚未转换为粒子link
    • 转换不会自动发生。这是一个更复杂的过程,您必须自己实施。
    • 好的,那我要去哪里?所以我可以实现/从中学习:)
    • 如果纹理很小,可以考虑为每个纹素创建一个点。然而,这种方法不能很好地扩展。例如,一个 1024x1024 尺寸的纹理已经有超过 100 万个纹素。在生成点云之前,您可能必须先缩小高分辨率纹理。
    • 有什么最佳做法吗?
    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多