我如何在反应三纤维中创建一个3D平面椭圆

发布于 2025-01-25 12:06:48 字数 134 浏览 4 评论 0原文

我正在进行一个使用React-three-Fiber制作3D对象的项目, 我正在尝试制作各种形状,并且在制作平坦的椭圆方面遇到了麻烦。 注意:我知道r3f和three.js基本上是相同的,但是在R3F中,我可以在标签中使用它,因此更容易。 感谢您的帮助。

I'm doing a project using react-three-fiber to make 3d objects,
I'm trying to make sorts of shapes and I'm having trouble with making a flat ellipse.
Note: I know r3f and three.js are basically the same but in r3f I can use it in tags so it's easier.
Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2025-02-01 12:06:48

这就是我在React函数组件中进行的方式:

import React from 'react';
import PropTypes from 'prop-types';

import { EllipseCurve } from 'three';

import { Line2 } from 'three/examples/jsm/lines/Line2';
import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial';
import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry';

import { extend } from '@react-three/fiber';

extend({ Line2 });

function Ellipse(props) {

  // geometry
  const geometry = new LineGeometry();
  const material = new LineMaterial({ color: props.color, linewidth: props.lineWidth });

  const curve = new EllipseCurve(
    0, 0, // xCenter, yCenter
    props.rx, props.ry, // xRadius, yRadius
    0, 2 * Math.PI, // aStartAngle, aEndAngle
    false, // aClockwise
    0 // aRotation
  );

  const points = curve.getPoints( 50 );

  const positions = points.reduce((acc, p) => {
    acc.push(p.x);
    acc.push(p.y);
    acc.push(0);

    return acc;
  }, []);

  // to get a closed curve, add first point at the end again
  positions.push(points[0].x);
  positions.push(points[0].y);
  positions.push(0);

  geometry.setPositions( positions);
  material.resolution.set(window.innerWidth, window.innerHeight);

  const position = [props.position.x, props.position.y, props.position.z];
  const quaternion = [props.orientation.x, props.orientation.y, props.orientation.z, props.orientation.w];

  return (
    <mesh
      { ...props }
      position={ position }
      quaternion={ quaternion }
    >
      <line2 geometry={ geometry } material={ material } />
    </mesh>
  );
}

That's how I did it in a React function component:

import React from 'react';
import PropTypes from 'prop-types';

import { EllipseCurve } from 'three';

import { Line2 } from 'three/examples/jsm/lines/Line2';
import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial';
import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry';

import { extend } from '@react-three/fiber';

extend({ Line2 });

function Ellipse(props) {

  // geometry
  const geometry = new LineGeometry();
  const material = new LineMaterial({ color: props.color, linewidth: props.lineWidth });

  const curve = new EllipseCurve(
    0, 0, // xCenter, yCenter
    props.rx, props.ry, // xRadius, yRadius
    0, 2 * Math.PI, // aStartAngle, aEndAngle
    false, // aClockwise
    0 // aRotation
  );

  const points = curve.getPoints( 50 );

  const positions = points.reduce((acc, p) => {
    acc.push(p.x);
    acc.push(p.y);
    acc.push(0);

    return acc;
  }, []);

  // to get a closed curve, add first point at the end again
  positions.push(points[0].x);
  positions.push(points[0].y);
  positions.push(0);

  geometry.setPositions( positions);
  material.resolution.set(window.innerWidth, window.innerHeight);

  const position = [props.position.x, props.position.y, props.position.z];
  const quaternion = [props.orientation.x, props.orientation.y, props.orientation.z, props.orientation.w];

  return (
    <mesh
      { ...props }
      position={ position }
      quaternion={ quaternion }
    >
      <line2 geometry={ geometry } material={ material } />
    </mesh>
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文