145 lines
4.3 KiB
C#
145 lines
4.3 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using uNature.Core.FoliageClasses;
|
|
|
|
namespace uNature.Core
|
|
{
|
|
public struct UNPhysicsObject
|
|
{
|
|
private static UNPhysicsHit_Grass hit = default(UNPhysicsHit_Grass);
|
|
|
|
[SerializeField]
|
|
private bool _enabled;
|
|
|
|
private Matrix4x4 transform;
|
|
|
|
[SerializeField]
|
|
private Bounds Bounds;
|
|
|
|
public bool enabled
|
|
{
|
|
get
|
|
{
|
|
return _enabled;
|
|
}
|
|
set
|
|
{
|
|
if (_enabled != value)
|
|
{
|
|
_enabled = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
internal UNPhysicsObject(UNPhysicsTemplate template, FoliageMeshInstance meshInstance)
|
|
{
|
|
Bounds = default(Bounds);
|
|
_enabled = true;
|
|
transform = Matrix4x4.identity;
|
|
CalculateTransform(0f, 0f, 0f, template, meshInstance);
|
|
}
|
|
|
|
internal void Destroy()
|
|
{
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
internal void CalculateTransform(float x, float y, float z, UNPhysicsTemplate template, FoliageMeshInstance meshInstance)
|
|
{
|
|
Vector3 pos = default(Vector3);
|
|
pos.x = template.position.x + template.spread.x * template.prototype.spread + x + meshInstance.position.x;
|
|
pos.y = template.position.y + y;
|
|
pos.z = template.position.z + template.spread.y * template.prototype.spread + z + meshInstance.position.z;
|
|
Quaternion q = Quaternion.Euler(0f, template.spread.x * 360f, 0f);
|
|
float num = template.prototype.maximumWidth - template.prototype.minimumWidth;
|
|
num *= template.spread.x;
|
|
float num2 = template.prototype.minimumWidth + num;
|
|
float num3 = template.prototype.maximumHeight - template.prototype.minimumHeight;
|
|
num *= template.spread.x;
|
|
float y2 = template.prototype.minimumHeight + num3;
|
|
Vector3 scale = new Vector3(num2, y2, num2);
|
|
Vector3 worldScale = template.prototype.FoliageInstancedMeshData.worldScale;
|
|
worldScale.Scale(scale);
|
|
transform = Matrix4x4.TRS(pos, q, worldScale);
|
|
UpdateBounds();
|
|
}
|
|
|
|
private void UpdateBounds()
|
|
{
|
|
Vector3 zero = Vector3.zero;
|
|
Vector3 one = Vector3.one;
|
|
Vector3 vector = new Vector3(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
|
|
Vector3 vector2 = new Vector3(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);
|
|
Vector3[] array = new Vector3[8];
|
|
Vector3 vector3 = one / 2f;
|
|
array[0] = transform.MultiplyPoint3x4(new Vector3(zero.x - vector3.x, zero.y + vector3.y, zero.z - vector3.z));
|
|
array[1] = transform.MultiplyPoint3x4(new Vector3(zero.x + vector3.x, zero.y + vector3.y, zero.z - vector3.z));
|
|
array[2] = transform.MultiplyPoint3x4(new Vector3(zero.x - vector3.x, zero.y - vector3.y, zero.z - vector3.z));
|
|
array[3] = transform.MultiplyPoint3x4(new Vector3(zero.x + vector3.x, zero.y - vector3.y, zero.z - vector3.z));
|
|
array[4] = transform.MultiplyPoint3x4(new Vector3(zero.x - vector3.x, zero.y + vector3.y, zero.z + vector3.z));
|
|
array[5] = transform.MultiplyPoint3x4(new Vector3(zero.x + vector3.x, zero.y + vector3.y, zero.z + vector3.z));
|
|
array[6] = transform.MultiplyPoint3x4(new Vector3(zero.x - vector3.x, zero.y - vector3.y, zero.z + vector3.z));
|
|
array[7] = transform.MultiplyPoint3x4(new Vector3(zero.x + vector3.x, zero.y - vector3.y, zero.z + vector3.z));
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
Vector3 vector4 = array[i];
|
|
if (vector4.x < vector.x)
|
|
{
|
|
vector.x = vector4.x;
|
|
}
|
|
if (vector4.x > vector2.x)
|
|
{
|
|
vector2.x = vector4.x;
|
|
}
|
|
if (vector4.y < vector.y)
|
|
{
|
|
vector.y = vector4.y;
|
|
}
|
|
if (vector4.y > vector2.y)
|
|
{
|
|
vector2.y = vector4.y;
|
|
}
|
|
if (vector4.z < vector.z)
|
|
{
|
|
vector.z = vector4.z;
|
|
}
|
|
if (vector4.z > vector2.z)
|
|
{
|
|
vector2.z = vector4.z;
|
|
}
|
|
}
|
|
Vector3 vector5 = new Vector3(vector2.x - vector.x, vector2.y - vector.y, vector2.z - vector.z);
|
|
Bounds = new Bounds(new Vector3(vector.x + vector5.x / 2f, vector.y + vector5.y / 2f, vector.z + vector5.z / 2f), new Vector3(vector2.x - vector.x, vector2.y - vector.y, vector2.z - vector.z));
|
|
}
|
|
|
|
public void OnDrawGizmos()
|
|
{
|
|
if (enabled)
|
|
{
|
|
}
|
|
}
|
|
|
|
private void DrawShape(Matrix4x4 matrix)
|
|
{
|
|
Gizmos.color = Color.green;
|
|
Gizmos.matrix = matrix;
|
|
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
|
|
}
|
|
|
|
public bool Raycast(Ray ray, out UNPhysicsHit_Grass _hit, LayerMask mask)
|
|
{
|
|
_hit = hit;
|
|
if (!enabled)
|
|
{
|
|
return false;
|
|
}
|
|
if (Bounds.IntersectRay(ray, out _hit.distance))
|
|
{
|
|
_hit.point = ray.GetPoint(_hit.distance);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|