87 lines
2.7 KiB
C#
87 lines
2.7 KiB
C#
using Obi;
|
|
using UnityEngine;
|
|
|
|
public class RodLine : MonoBehaviour
|
|
{
|
|
private ObiRope obiRope;
|
|
|
|
private LineRenderer lineRenderer;
|
|
|
|
private Transform[] points;
|
|
|
|
public void GenerateLineRendererRope(Transform[] points, float thickness)
|
|
{
|
|
if (points.Length < 2)
|
|
{
|
|
Debug.LogError("LineRenderer requires at least two points.");
|
|
return;
|
|
}
|
|
this.points = points;
|
|
if (lineRenderer == null)
|
|
{
|
|
lineRenderer = base.gameObject.GetComponent<LineRenderer>();
|
|
if (lineRenderer == null)
|
|
{
|
|
lineRenderer = base.gameObject.AddComponent<LineRenderer>();
|
|
}
|
|
}
|
|
lineRenderer.positionCount = points.Length;
|
|
lineRenderer.startWidth = thickness;
|
|
lineRenderer.endWidth = thickness;
|
|
for (int i = 0; i < points.Length; i++)
|
|
{
|
|
lineRenderer.SetPosition(i, points[i].position);
|
|
}
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (lineRenderer == null)
|
|
{
|
|
return;
|
|
}
|
|
for (int i = 0; i < points.Length; i++)
|
|
{
|
|
Transform transform = points[i];
|
|
if ((bool)transform)
|
|
{
|
|
lineRenderer.SetPosition(i, transform.position);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void GenerateObiRope(Transform[] points, float thickness)
|
|
{
|
|
if (points.Length < 2)
|
|
{
|
|
Debug.LogError("Rope line Must have more than two points");
|
|
return;
|
|
}
|
|
obiRope = GetComponent<ObiRope>();
|
|
Transform transform = points[0];
|
|
Transform transform2 = points[^1];
|
|
base.transform.position = (transform.position + transform2.position) / 2f;
|
|
base.transform.rotation = Quaternion.FromToRotation(Vector3.right, transform2.position - transform.position);
|
|
ObiRopeBlueprint obiRopeBlueprint = ScriptableObject.CreateInstance<ObiRopeBlueprint>();
|
|
int filter = ObiUtils.MakeFilter(65535, 0);
|
|
for (int i = 0; i < points.Length; i++)
|
|
{
|
|
Transform transform3 = points[Mathf.Clamp(i + 1, 0, points.Length - 1)];
|
|
Vector3 vector = base.transform.InverseTransformPoint(points[i].position);
|
|
Vector3 normalized = (base.transform.InverseTransformPoint(transform3.position) - vector).normalized;
|
|
obiRopeBlueprint.path.AddControlPoint(vector, -normalized, normalized, Vector3.up, 0.001f, 0.01f, thickness, filter, Color.white, points[i].name);
|
|
}
|
|
obiRopeBlueprint.path.FlushEvents();
|
|
obiRopeBlueprint.GenerateImmediate();
|
|
ObiRopeExtrudedRenderer obiRopeExtrudedRenderer = base.gameObject.AddComponent<ObiRopeExtrudedRenderer>();
|
|
for (int j = 0; j < points.Length; j++)
|
|
{
|
|
ObiParticleAttachment obiParticleAttachment = base.gameObject.AddComponent<ObiParticleAttachment>();
|
|
obiParticleAttachment.target = points[j];
|
|
obiParticleAttachment.particleGroup = obiRopeBlueprint.groups[j];
|
|
}
|
|
obiRopeExtrudedRenderer.section = Resources.Load<ObiRopeSection>("DefaultRopeSection");
|
|
obiRope.ropeBlueprint = obiRopeBlueprint;
|
|
}
|
|
}
|