找回密码
 快速注册
搜索
查看: 29|回复: 2

[Unity]圆内摆线

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2023-3-20 22:19 |阅读模式
来自OSChina开源问答
如何在Unity制作和原帖一样的圆内摆线(Epicycloid)?
原帖截图
192114_PAA9_248241.png
在GeoGebra可以使用
  1. a=20
  2. b=1
  3. Curve(((a + b) cos(θ) - a cos((a + b) θ / b), (a + b) sin(θ) - a sin((a + b) θ / b)), θ, 0, 2π)
复制代码
画出一样的曲线

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-3-20 23:13
菜单栏> Game Object> Create Empty Screenshot 2023-03-20 151258.png
Add Component > Line Renderer Screenshot 2023-03-20 151423.png
取消打勾Use World Space
参看这里
如果打勾就不能使LineRenderer与它的父对象一起旋转
Screenshot 2023-03-20 151540.png

右击Assets区Create> C# script
默认的文件名为NewBehaviourScript
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class NewBehaviourScript : MonoBehaviour
  5. {
  6.     int numPoints=360;
  7.     float a=20,b=1;
  8.     void Start()
  9.     {
  10.         LineRenderer lineRenderer = GetComponent<LineRenderer>();
  11.         Vector3[] points = new Vector3[numPoints];
  12.         for (int i=0; i<numPoints; i++) {
  13.                         float theta = i * Mathf.PI / 180;
  14.                         points [i] = new Vector3 (Mathf.Cos(1*theta) -a/(a + b)*Mathf.Cos((a +b) * theta / b), Mathf.Sin (1*theta)-a/(a + b)* Mathf.Sin ((a +b) * theta / b), 0);
  15.                 }
  16.         lineRenderer.startWidth = 0.1f;
  17.         lineRenderer.endWidth = 0.1f;
  18.         lineRenderer.positionCount = numPoints;
  19.         lineRenderer.SetPositions(points);
  20.         lineRenderer.material.SetColor("_EmissionColor", Color.green);
  21.         lineRenderer.material.SetColor("_Color", Color.green);
  22.     }
  23.     float speed =50f;
  24.     void Update () {
  25.         transform.Rotate(Vector3.up *speed * Time.deltaTime,Space.Self);
  26.     }
  27. }
复制代码
把NewBehaviourScript 拖动到GameObject上
现在出现一个绿色曲线 绕y轴旋转
output.gif

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-3-21 00:19
把MainCamera位置设定为 0 0 -3
为了制作黑色背景, 在Clear Flags选择Solid Color
因为新版的Unity Recorder不再支持导出GIF格式, 需要用Unity Recorder导出PNG序列, 再导入ScreenToGIF制成GIF. 效果如下

output.gif

和原图相比, 线条有点粗, 这个可以在2楼C#代码中修改. 我就不改了.

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 15:36

Powered by Discuz!

× 快速回复 返回顶部 返回列表