Advertisement
NPSF3000

JitShapeOutliner

Dec 17th, 2011
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.93 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Jitter;
  5. using Jitter.Collision;
  6. using Jitter.DataStructures;
  7. using Jitter.Dynamics;
  8. using Jitter.Collision.Shapes;
  9. using Jitter.LinearMath;
  10.  
  11. public class JitShapeOutliner : MonoBehaviour
  12. {
  13.     public GameObject go;
  14.     public GameObject pointGO;
  15.     RigidBody body;
  16.     private Vector3 inc;
  17.     public GameObject[, ,] points;
  18.     public float increment = 1;
  19.     public float scale = 1;
  20.  
  21.     IEnumerator Start()
  22.     {
  23.         while (true)
  24.         {
  25.             yield return null;
  26.  
  27.             if (!Input.GetKeyDown(KeyCode.T) || go == null)
  28.                 continue;
  29.  
  30.             if (points != null)
  31.             {
  32.                 var countD = 0;
  33.                 for (int y = 0; y < points.GetLength(1); y++)
  34.                     for (int x = 0; x < points.GetLength(0); x++)
  35.                         for (int z = 0; z < points.GetLength(2); z++)
  36.                         {
  37.                             if (points[x, y, z] == null) continue;
  38.                             countD++;
  39.                             if (countD % 100 == 0) yield return null;
  40.                             Destroy(points[x, y, z]);
  41.                         }
  42.                 points = null;
  43.                 continue;
  44.             }
  45.  
  46.             print("starting outline");
  47.             body = go.GetComponent<JITCapsule>().body;
  48.  
  49.             if (pointGO == null)
  50.                 pointGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  51.  
  52.  
  53.             var min = body.BoundingBox.Min;
  54.             var max = body.BoundingBox.Max;
  55.  
  56.             inc = Vector3.one * increment;
  57.  
  58.  
  59.             print(min);
  60.             print(max);
  61.             print("-----");
  62.  
  63.             pointGO.transform.localScale = Vector3.one* scale;
  64.  
  65.             points = new GameObject[(int)((max.X - min.X) / inc.x), (int)((max.Y - min.Y) / inc.y), (int)((max.Z - min.Z) / inc.z)];
  66.  
  67.             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)];
  68.  
  69.  
  70.             for (int x = 0; x < pointTests.GetLength(0); x++)
  71.                 for (int y = 0; y < pointTests.GetLength(1); y++)
  72.                     for (int z = 0; z < pointTests.GetLength(2); z++)
  73.                         pointTests[x, y, z] = (PC(body, new JVector(min.X + x * inc.x, min.Y + y * inc.y, min.Z + z * inc.z)));
  74.  
  75.  
  76.             var directions = new List<int[]>();
  77.  
  78.             for (int x = -1; x < 2; x++)
  79.                 for (int y = -1; y < 2; y++)
  80.                     for (int z = -1; z < 2; z++)
  81.                         directions.Add(new int[] { x, y, z });
  82.  
  83.             var count = 0;
  84.  
  85.             for (int y = 0; y < pointTests.GetLength(1); y++)
  86.                 for (int x = 0; x < pointTests.GetLength(0); x++)
  87.  
  88.                     for (int z = 0; z < pointTests.GetLength(2); z++)
  89.                     {
  90.                         #region is point hidden?
  91.                         if (pointTests[x, y, z] == false) continue;
  92.  
  93.                         var isVisible = false;
  94.  
  95.                         foreach (var dd in directions)
  96.                         {
  97.                             var d = (int[])dd.Clone();
  98.                             // print("a (" + d[0] + "," + d[1] + "," + d[2] + ")");
  99.                             d[0] += x;
  100.                             d[1] += y;
  101.                             d[2] += z;
  102.  
  103.  
  104.                             if ((d[0] < 0) || (d[1] < 0) || (d[2] < 0)) isVisible = true;
  105.  
  106.                             /* if (!isVisible)
  107.                              {
  108.                                  print("----------------------------");
  109.                                  print("b (" + d[0] + "," + d[1] + "," + d[2] + ")");
  110.                                  print("c (" + pointTests.GetLength(0) + "," + pointTests.GetLength(1) + "," + pointTests.GetLength(2) + ")");
  111.                                  print("d (" + (d[0] > pointTests.GetLength(0)) + "," + (d[1] > pointTests.GetLength(1)) + "," + (d[2] > pointTests.GetLength(2)) + ")");
  112.                              }*/
  113.  
  114.                             if ((d[0] >= pointTests.GetLength(0)) || (d[1] >= pointTests.GetLength(1)) || (d[2] >= pointTests.GetLength(2))) isVisible = true;
  115.  
  116.                             //print ("result " + ((d[0] > pointTests.GetLength(0)) || (d[1] > pointTests.GetLength(1)) || (d[2] > pointTests.GetLength(2))));
  117.  
  118.                             // print("DIRECTIONS (" + directions[0][0] + "," + directions[0][1] + "," + directions[0][2] + ")");
  119.  
  120.                             // yield return new WaitForSeconds(0.1f);
  121.                             //print("c (" + pointTests.GetLength(0) + "," + pointTests.GetLength(1) + "," + pointTests.GetLength(2) + ")");
  122.  
  123.                             if (isVisible) break;
  124.  
  125.                             //print("SUCCESS (" + d[0] + "," + d[1] + "," + d[2] + ")");
  126.                             // print("PointTests (" + pointTests.GetLength(0) + "," + pointTests.GetLength(1) + "," + pointTests.GetLength(2) + ")");
  127.                             isVisible = !pointTests[d[0], d[1], d[2]];
  128.                         }
  129.                         #endregion
  130.                         //if visible place point
  131.                         if (isVisible)
  132.                         {
  133.                             var point = (GameObject)GameObject.Instantiate(pointGO);
  134.                             point.transform.position = new Vector3(min.X + x * inc.x, min.Y + y * inc.y, min.Z + z * inc.z);
  135.                             points[x, y, z] = point;
  136.                             count++;
  137.                             if (count % 100 == 0) yield return null;
  138.                         }
  139.  
  140.                     }
  141.             print("count: " + count);
  142.         }
  143.     }
  144.  
  145.     static bool PC(RigidBody body, JVector point)
  146.     {
  147.         var or = body.Orientation;
  148.         var pos = body.Position;
  149.         return GJKCollide.Pointcast(body.Shape, ref or, ref pos, ref point);
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement