Я создаю трубчатую сетку из точек лайнера. Он должен представлять собой обычную ногу/щупальце. Все работает нормально, однако, когда я приостанавливаю игру (устанавливаю временную шкалу на 0), она ломается, и моя сетка исчезает. Я получаю некоторые ошибки с недопустимыми AABB и неверными ошибками сетки. Может быть, где-то есть распространение NaN? Я пытался это проверить, но не помогает.
Вот код:
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(LineRenderer), typeof(MeshFilter), typeof(MeshRenderer))]
public class CylinderMesh1 : MonoBehaviour
{
private LineRenderer line;
private MeshFilter filter;
private Mesh mesh;
[Min(3)]
public int verticeCount = 16;
public AnimationCurve radiusCurve = AnimationCurve.Constant(0, 1, 0.5f);
private readonly List vertices = new();
private readonly List normals = new();
private readonly List triangles = new();
private void Awake()
{
line = GetComponent();
filter = GetComponent();
mesh = new Mesh { name = "CylinderMesh" };
mesh.MarkDynamic();
filter.sharedMesh = mesh;
}
private void LateUpdate()
{
BuildMesh();
}
private void BuildMesh()
{
int pointCount = line.positionCount;
if (pointCount < 2)
{
mesh.Clear(); return;
}
vertices.Clear();
normals.Clear();
triangles.Clear();
float angleStep = Mathf.PI * 2f / verticeCount;
Vector3 prevNormal = Vector3.zero;
for (int i = 0; i < pointCount; i++)
{
Vector3 p = transform.InverseTransformPoint(line.GetPosition(i));
Vector3 tangent =
(i < pointCount - 1)
? (transform.InverseTransformPoint(line.GetPosition(i + 1)) - p).normalized
: (p - transform.InverseTransformPoint(line.GetPosition(i - 1))).normalized;
Vector3 normal;
Vector3 binormal;
if (i == 0)
{
normal = Vector3.up;
if (Mathf.Abs(Vector3.Dot(tangent, normal)) > 0.99f)
normal = Vector3.right;
binormal = Vector3.Cross(tangent, normal).normalized;
normal = Vector3.Cross(binormal, tangent).normalized;
}
else
{
Vector3 proj = prevNormal - Vector3.Dot(prevNormal, tangent) * tangent;
if (proj.sqrMagnitude < 1e-6f)
{
normal = Vector3.up;
if (Mathf.Abs(Vector3.Dot(tangent, normal)) > 0.99f)
normal = Vector3.right;
normal = Vector3.Cross(Vector3.Cross(tangent, normal), tangent).normalized;
}
else
{
normal = proj.normalized;
}
binormal = Vector3.Cross(tangent, normal);
}
prevNormal = normal;
float t = (float)i / (pointCount - 1);
float radius = radiusCurve.Evaluate(t);
for (int j = 0; j < verticeCount; j++)
{
float theta = angleStep * j;
Vector3 dir = Mathf.Cos(theta) * normal + Mathf.Sin(theta) * binormal;
vertices.Add(p + dir * radius);
normals.Add(dir);
}
}
for (int i = 0; i < pointCount - 1; i++)
{
int ringA = i * verticeCount;
int ringB = (i + 1) * verticeCount;
for (int j = 0; j < verticeCount; j++)
{
int next = (j + 1) % verticeCount;
triangles.Add(ringA + j);
triangles.Add(ringA + next);
triangles.Add(ringB + j);
triangles.Add(ringB + j);
triangles.Add(ringA + next);
triangles.Add(ringB + next);
}
}
AddCap(
transform.InverseTransformPoint(line.GetPosition(0)),
-(transform.InverseTransformPoint(line.GetPosition(1)) - transform.InverseTransformPoint(line.GetPosition(0))).normalized,
0,
false);
AddCap(
transform.InverseTransformPoint(line.GetPosition(pointCount - 1)),
(transform.InverseTransformPoint(line.GetPosition(pointCount - 1)) - transform.InverseTransformPoint(line.GetPosition(pointCount - 2))).normalized,
(pointCount - 1) * verticeCount,
true);
mesh.Clear(false);
mesh.SetVertices(vertices);
mesh.SetNormals(normals);
mesh.SetTriangles(triangles, 0);
mesh.RecalculateBounds();
}
private void AddCap(Vector3 center, Vector3 normal, int ringStart, bool reverse)
{
int centerIndex = vertices.Count;
vertices.Add(center);
normals.Add(normal);
for (int i = 0; i < verticeCount; i++)
{
int next = (i + 1) % verticeCount;
if (!reverse)
{
triangles.Add(centerIndex);
triangles.Add(ringStart + next);
triangles.Add(ringStart + i);
}
else
{
triangles.Add(centerIndex);
triangles.Add(ringStart + i);
triangles.Add(ringStart + next);
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... ration-bug
Unity - ошибка генерации сетки ⇐ C#
Место общения программистов C#
1770636313
Anonymous
Я создаю трубчатую сетку из точек лайнера. Он должен представлять собой обычную ногу/щупальце. Все работает нормально, однако, когда я приостанавливаю игру (устанавливаю временную шкалу на 0), она ломается, и моя сетка исчезает. Я получаю некоторые ошибки с недопустимыми AABB и неверными ошибками сетки. Может быть, где-то есть распространение NaN? Я пытался это проверить, но не помогает.
Вот код:
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(LineRenderer), typeof(MeshFilter), typeof(MeshRenderer))]
public class CylinderMesh1 : MonoBehaviour
{
private LineRenderer line;
private MeshFilter filter;
private Mesh mesh;
[Min(3)]
public int verticeCount = 16;
public AnimationCurve radiusCurve = AnimationCurve.Constant(0, 1, 0.5f);
private readonly List vertices = new();
private readonly List normals = new();
private readonly List triangles = new();
private void Awake()
{
line = GetComponent();
filter = GetComponent();
mesh = new Mesh { name = "CylinderMesh" };
mesh.MarkDynamic();
filter.sharedMesh = mesh;
}
private void LateUpdate()
{
BuildMesh();
}
private void BuildMesh()
{
int pointCount = line.positionCount;
if (pointCount < 2)
{
mesh.Clear(); return;
}
vertices.Clear();
normals.Clear();
triangles.Clear();
float angleStep = Mathf.PI * 2f / verticeCount;
Vector3 prevNormal = Vector3.zero;
for (int i = 0; i < pointCount; i++)
{
Vector3 p = transform.InverseTransformPoint(line.GetPosition(i));
Vector3 tangent =
(i < pointCount - 1)
? (transform.InverseTransformPoint(line.GetPosition(i + 1)) - p).normalized
: (p - transform.InverseTransformPoint(line.GetPosition(i - 1))).normalized;
Vector3 normal;
Vector3 binormal;
if (i == 0)
{
normal = Vector3.up;
if (Mathf.Abs(Vector3.Dot(tangent, normal)) > 0.99f)
normal = Vector3.right;
binormal = Vector3.Cross(tangent, normal).normalized;
normal = Vector3.Cross(binormal, tangent).normalized;
}
else
{
Vector3 proj = prevNormal - Vector3.Dot(prevNormal, tangent) * tangent;
if (proj.sqrMagnitude < 1e-6f)
{
normal = Vector3.up;
if (Mathf.Abs(Vector3.Dot(tangent, normal)) > 0.99f)
normal = Vector3.right;
normal = Vector3.Cross(Vector3.Cross(tangent, normal), tangent).normalized;
}
else
{
normal = proj.normalized;
}
binormal = Vector3.Cross(tangent, normal);
}
prevNormal = normal;
float t = (float)i / (pointCount - 1);
float radius = radiusCurve.Evaluate(t);
for (int j = 0; j < verticeCount; j++)
{
float theta = angleStep * j;
Vector3 dir = Mathf.Cos(theta) * normal + Mathf.Sin(theta) * binormal;
vertices.Add(p + dir * radius);
normals.Add(dir);
}
}
for (int i = 0; i < pointCount - 1; i++)
{
int ringA = i * verticeCount;
int ringB = (i + 1) * verticeCount;
for (int j = 0; j < verticeCount; j++)
{
int next = (j + 1) % verticeCount;
triangles.Add(ringA + j);
triangles.Add(ringA + next);
triangles.Add(ringB + j);
triangles.Add(ringB + j);
triangles.Add(ringA + next);
triangles.Add(ringB + next);
}
}
AddCap(
transform.InverseTransformPoint(line.GetPosition(0)),
-(transform.InverseTransformPoint(line.GetPosition(1)) - transform.InverseTransformPoint(line.GetPosition(0))).normalized,
0,
false);
AddCap(
transform.InverseTransformPoint(line.GetPosition(pointCount - 1)),
(transform.InverseTransformPoint(line.GetPosition(pointCount - 1)) - transform.InverseTransformPoint(line.GetPosition(pointCount - 2))).normalized,
(pointCount - 1) * verticeCount,
true);
mesh.Clear(false);
mesh.SetVertices(vertices);
mesh.SetNormals(normals);
mesh.SetTriangles(triangles, 0);
mesh.RecalculateBounds();
}
private void AddCap(Vector3 center, Vector3 normal, int ringStart, bool reverse)
{
int centerIndex = vertices.Count;
vertices.Add(center);
normals.Add(normal);
for (int i = 0; i < verticeCount; i++)
{
int next = (i + 1) % verticeCount;
if (!reverse)
{
triangles.Add(centerIndex);
triangles.Add(ringStart + next);
triangles.Add(ringStart + i);
}
else
{
triangles.Add(centerIndex);
triangles.Add(ringStart + i);
triangles.Add(ringStart + next);
}
}
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79885391/unity-mesh-generation-bug[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия