106 lines
1.8 KiB
C#
106 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
[AddComponentMenu("Modifiers/Push")]
|
|
public class MegaPush : MegaModifier
|
|
{
|
|
public float amount;
|
|
|
|
public MegaNormType method;
|
|
|
|
private Vector3[] normals;
|
|
|
|
public override string ModName()
|
|
{
|
|
return "Push";
|
|
}
|
|
|
|
public override string GetHelpURL()
|
|
{
|
|
return "?page_id=282";
|
|
}
|
|
|
|
public override Vector3 Map(int i, Vector3 p)
|
|
{
|
|
if (i >= 0)
|
|
{
|
|
p += normals[i] * amount;
|
|
}
|
|
return p;
|
|
}
|
|
|
|
private void CalcNormals(Mesh mesh)
|
|
{
|
|
if (!(mesh != null))
|
|
{
|
|
return;
|
|
}
|
|
switch (method)
|
|
{
|
|
case MegaNormType.Normals:
|
|
normals = mesh.normals;
|
|
break;
|
|
case MegaNormType.Vertices:
|
|
{
|
|
normals = new Vector3[mesh.normals.Length];
|
|
for (int k = 0; k < mesh.vertexCount; k++)
|
|
{
|
|
normals[k] = Vector3.Normalize(mesh.vertices[k]);
|
|
}
|
|
break;
|
|
}
|
|
case MegaNormType.Average:
|
|
{
|
|
normals = mesh.normals;
|
|
for (int i = 0; i < mesh.vertexCount; i++)
|
|
{
|
|
for (int j = 0; j < mesh.vertexCount; j++)
|
|
{
|
|
if (mesh.vertices[i] == mesh.vertices[j])
|
|
{
|
|
normals[i] = (normals[i] + normals[j]) / 2f;
|
|
normals[j] = (normals[i] + normals[j]) / 2f;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void ModStart(MegaModifiers mc)
|
|
{
|
|
CalcNormals(mc.mesh);
|
|
}
|
|
|
|
public override bool ModLateUpdate(MegaModContext mc)
|
|
{
|
|
return Prepare(mc);
|
|
}
|
|
|
|
public override bool Prepare(MegaModContext mc)
|
|
{
|
|
if (normals != null)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void Reset()
|
|
{
|
|
Renderer component = GetComponent<Renderer>();
|
|
if (component != null)
|
|
{
|
|
Mesh sharedMesh = MegaUtils.GetSharedMesh(base.gameObject);
|
|
if (sharedMesh != null)
|
|
{
|
|
CalcNormals(sharedMesh);
|
|
Bounds bounds = sharedMesh.bounds;
|
|
Offset = -bounds.center;
|
|
bbox.min = bounds.center - bounds.extents;
|
|
bbox.max = bounds.center + bounds.extents;
|
|
}
|
|
}
|
|
}
|
|
}
|