Commit 1
This commit is contained in:
71
Assets/Scripts/VFX/SlashMeshGenerator.cs
Normal file
71
Assets/Scripts/VFX/SlashMeshGenerator.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
|
||||
public class SlashMeshGenerator : MonoBehaviour
|
||||
{
|
||||
[Header("Mesh Settings")]
|
||||
public int segments = 10; // Số lượng phân đoạn (càng cao càng mượt)
|
||||
public float length = 5f; // Chiều dài vệt chém
|
||||
public float width = 0.5f; // Chiều rộng ở giữa
|
||||
public float curviness = 1f; // Độ cong của nhát chém
|
||||
|
||||
[ContextMenu("Generate Slash Mesh")]
|
||||
public void GenerateMesh()
|
||||
{
|
||||
Mesh mesh = new Mesh();
|
||||
mesh.name = "SukunaSlashMesh";
|
||||
|
||||
int vertexCount = (segments + 1) * 2;
|
||||
Vector3[] vertices = new Vector3[vertexCount];
|
||||
Vector2[] uvs = new Vector2[vertexCount];
|
||||
int[] triangles = new int[segments * 6];
|
||||
|
||||
for (int i = 0; i <= segments; i++)
|
||||
{
|
||||
float t = (float)i / segments; // Tiến trình từ 0 đến 1
|
||||
|
||||
// Tính toán vị trí X (chiều dài)
|
||||
float x = (t - 0.5f) * length;
|
||||
|
||||
// Tính toán độ nhọn (Width Taper): Nhỏ ở 2 đầu, to ở giữa
|
||||
// Dùng hàm Sin để tạo độ mượt hoặc (1 - |2t-1|)
|
||||
float currentWidth = Mathf.Sin(t * Mathf.PI) * width;
|
||||
|
||||
// Tính toán độ cong (Y Offset)
|
||||
float yOffset = Mathf.Pow((t - 0.5f) * 2f, 2f) * curviness;
|
||||
|
||||
// Tạo 2 đỉnh (trên và dưới) cho mỗi phân đoạn
|
||||
vertices[i * 2] = new Vector3(x, yOffset + currentWidth / 2f, 0);
|
||||
vertices[i * 2 + 1] = new Vector3(x, yOffset - currentWidth / 2f, 0);
|
||||
|
||||
// Gán UV (để Shader chạy đúng)
|
||||
uvs[i * 2] = new Vector2(t, 1);
|
||||
uvs[i * 2 + 1] = new Vector2(t, 0);
|
||||
|
||||
// Tạo tam giác (trừ phân đoạn cuối)
|
||||
if (i < segments)
|
||||
{
|
||||
int start = i * 2;
|
||||
triangles[i * 6] = start;
|
||||
triangles[i * 6 + 1] = start + 2;
|
||||
triangles[i * 6 + 2] = start + 1;
|
||||
triangles[i * 6 + 3] = start + 1;
|
||||
triangles[i * 6 + 4] = start + 2;
|
||||
triangles[i * 6 + 5] = start + 3;
|
||||
}
|
||||
}
|
||||
|
||||
mesh.vertices = vertices;
|
||||
mesh.uv = uvs;
|
||||
mesh.triangles = triangles;
|
||||
mesh.RecalculateNormals();
|
||||
mesh.RecalculateBounds();
|
||||
|
||||
GetComponent<MeshFilter>().mesh = mesh;
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
GenerateMesh();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user