先看看效果图:
大多数形状都支持,但仍存在不少会生成奇怪形状的情形,到后面也没继续扩展维护了,只当看看好玩
在编辑器模式便能看到预览效果(Enable,Disable此脚本 Editor便会更新)
不算上起始点,总共有三个控制点,目前上传版本不支持高度y上的平滑插值
W:宽度, R:第一个节点的控制圆半径,R2:第二个控制圆的半径,Div:拐点圆弧平均切割数
代码如下(写的比较乱),一个简易的自动mesh生成工具
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
// 大于90度非直角转弯
[ExecuteInEditMode]
public class G90D_2 : MonoBehaviour
{
[SerializeField]
Material material;
Vector3 c1 = new Vector3(0f, 0f, 0f);
public Vector3 c2 = new Vector3(0f, 0f, 20f); // 控制点2
public Vector3 c3 = new Vector3(20f, 0f, 30f); // 控制点3
public Vector3 c4 = new Vector3(40f, 0f, 25f); // 控制点4
public float w = 1f; // 画的线宽 = 2w
public float r = 1.5f; // 内径圆半径
public float r2 = 1.5f;
public int div = 4; // 拐角弧切成多少份(2三角面)
public string path = "Assets/mymesh.asset"; // 保存路径,相对于项目
// Use this for initialization
void OnEnable()
{
GenerateHeightmap();
}
void GenerateHeightmap()
{
// Create the game object containing the renderer
gameObject.AddComponent<MeshFilter>();
gameObject.AddComponent<MeshRenderer>();
if (material)
GetComponent<Renderer>().material = material;
else
GetComponent<Renderer>().material.color = Color.white;
// Retrieve a mesh instance
Mesh mesh = GetComponent<MeshFilter>().mesh;
List<Vector3> points = new List<Vector3>();
int[] indecies = new int[4];
Color[] colors = new Color[4];
List<Vector2> uvs = new List<Vector2>();
Debug.Log("CosQ: " + Mathf.Cos(60f * Mathf.Deg2Rad));
// 目前只做了 c2 右拐, c2 左拐用 z轴对称回去
bool invert = false;
if (c3.x < 0)
{
invert = true;
c3.x *= -1;
c4.x *= -1;
}
points.Add(new Vector3(c1.x - w, c1.y, c1.z));
points.Add(new Vector3(c1.x + w, c1.y, c1.z));
Vector3 c2l = (c1 - c2).normalized;
Vector3 c2r = (c3 - c2).normalized;
float Q = 90 * Mathf.Deg2Rad - Mathf.Acos(Vector3.Dot(c2l, c2r)) / 2;
//float Q2 = Mathf.Atan((c3.z - c2.z) / (c3.x - c2.x));
Debug.Log("Q1: " + Q / Mathf.Deg2Rad);
Vector3 c22 = c2;
Vector3 c33 = c3;
float D = 2 * Q;
if (c3.x > c2.x) // else情况先用这种模式求, 对结果 Z 取反
{
c22 = new Vector3(c2.x + w + r, c2.y, c2.z - (w + r) * Mathf.Tan(Q));
points.Add(new Vector3(c22.x - 2 * w - r, c22.y, c22.z));
points.Add(new Vector3(c22.x - r, c22.y, c22.z));
for (int i = 1; i <= div; ++i)
{
float d = D * i / div;
points.Add(new Vector3(c22.x - (r + 2 * w) * Mathf.Cos(d), c22.y, c22.z + (r + 2 * w) * Mathf.Sin(d)))