Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using Jitter;
- using Jitter.Collision;
- using Jitter.DataStructures;
- using Jitter.Dynamics;
- using Jitter.Collision.Shapes;
- using Jitter.LinearMath;
- public class JitShapeOutliner : MonoBehaviour
- {
- public GameObject go;
- public GameObject pointGO;
- RigidBody body;
- private Vector3 inc;
- public GameObject[, ,] points;
- public float increment = 1;
- public float scale = 1;
- IEnumerator Start()
- {
- while (true)
- {
- yield return null;
- if (!Input.GetKeyDown(KeyCode.T) || go == null)
- continue;
- if (points != null)
- {
- var countD = 0;
- for (int y = 0; y < points.GetLength(1); y++)
- for (int x = 0; x < points.GetLength(0); x++)
- for (int z = 0; z < points.GetLength(2); z++)
- {
- if (points[x, y, z] == null) continue;
- countD++;
- if (countD % 100 == 0) yield return null;
- Destroy(points[x, y, z]);
- }
- points = null;
- continue;
- }
- print("starting outline");
- body = go.GetComponent<JITCapsule>().body;
- if (pointGO == null)
- pointGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- var min = body.BoundingBox.Min;
- var max = body.BoundingBox.Max;
- inc = Vector3.one * increment;
- print(min);
- print(max);
- print("-----");
- pointGO.transform.localScale = Vector3.one* scale;
- points = new GameObject[(int)((max.X - min.X) / inc.x), (int)((max.Y - min.Y) / inc.y), (int)((max.Z - min.Z) / inc.z)];
- var pointTests = new bool[(int)((max.X - min.X) / inc.x), (int)((max.Y - min.Y) / inc.y), (int)((max.Z - min.Z) / inc.z)];
- for (int x = 0; x < pointTests.GetLength(0); x++)
- for (int y = 0; y < pointTests.GetLength(1); y++)
- for (int z = 0; z < pointTests.GetLength(2); z++)
- pointTests[x, y, z] = (PC(body, new JVector(min.X + x * inc.x, min.Y + y * inc.y, min.Z + z * inc.z)));
- var directions = new List<int[]>();
- for (int x = -1; x < 2; x++)
- for (int y = -1; y < 2; y++)
- for (int z = -1; z < 2; z++)
- directions.Add(new int[] { x, y, z });
- var count = 0;
- for (int y = 0; y < pointTests.GetLength(1); y++)
- for (int x = 0; x < pointTests.GetLength(0); x++)
- for (int z = 0; z < pointTests.GetLength(2); z++)
- {
- #region is point hidden?
- if (pointTests[x, y, z] == false) continue;
- var isVisible = false;
- foreach (var dd in directions)
- {
- var d = (int[])dd.Clone();
- // print("a (" + d[0] + "," + d[1] + "," + d[2] + ")");
- d[0] += x;
- d[1] += y;
- d[2] += z;
- if ((d[0] < 0) || (d[1] < 0) || (d[2] < 0)) isVisible = true;
- /* if (!isVisible)
- {
- print("----------------------------");
- print("b (" + d[0] + "," + d[1] + "," + d[2] + ")");
- print("c (" + pointTests.GetLength(0) + "," + pointTests.GetLength(1) + "," + pointTests.GetLength(2) + ")");
- print("d (" + (d[0] > pointTests.GetLength(0)) + "," + (d[1] > pointTests.GetLength(1)) + "," + (d[2] > pointTests.GetLength(2)) + ")");
- }*/
- if ((d[0] >= pointTests.GetLength(0)) || (d[1] >= pointTests.GetLength(1)) || (d[2] >= pointTests.GetLength(2))) isVisible = true;
- //print ("result " + ((d[0] > pointTests.GetLength(0)) || (d[1] > pointTests.GetLength(1)) || (d[2] > pointTests.GetLength(2))));
- // print("DIRECTIONS (" + directions[0][0] + "," + directions[0][1] + "," + directions[0][2] + ")");
- // yield return new WaitForSeconds(0.1f);
- //print("c (" + pointTests.GetLength(0) + "," + pointTests.GetLength(1) + "," + pointTests.GetLength(2) + ")");
- if (isVisible) break;
- //print("SUCCESS (" + d[0] + "," + d[1] + "," + d[2] + ")");
- // print("PointTests (" + pointTests.GetLength(0) + "," + pointTests.GetLength(1) + "," + pointTests.GetLength(2) + ")");
- isVisible = !pointTests[d[0], d[1], d[2]];
- }
- #endregion
- //if visible place point
- if (isVisible)
- {
- var point = (GameObject)GameObject.Instantiate(pointGO);
- point.transform.position = new Vector3(min.X + x * inc.x, min.Y + y * inc.y, min.Z + z * inc.z);
- points[x, y, z] = point;
- count++;
- if (count % 100 == 0) yield return null;
- }
- }
- print("count: " + count);
- }
- }
- static bool PC(RigidBody body, JVector point)
- {
- var or = body.Orientation;
- var pos = body.Position;
- return GJKCollide.Pointcast(body.Shape, ref or, ref pos, ref point);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement