Advertisement
epitaque_

Untitled

May 7th, 2017
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1.     namespace H {
  2.     public class Edge {
  3.         public Vector3[] normals;
  4.         public Vector3[] positions;
  5.     }
  6.     public class GridCell {
  7.         public Edge[] edges;
  8.         public float[] densities;
  9.     }
  10.     public class HermiteData {
  11.         public GridCell[,,] cells;
  12.     }
  13.    
  14.     private CMS.H.HermiteData CreateHermiteData(int resolution, Sample sample) {
  15.         CMS.H.HermiteData h = new CMS.H.HermiteData();
  16.         h.cells = new H.GridCell[resolution,resolution,resolution];
  17.  
  18.         for(int x = 0; x < resolution; x++) {
  19.             for(int y = 0; y < resolution; y++) {
  20.                 for(int z = 0; z < resolution; z++) {
  21.                     CMS.H.GridCell cell = new CMS.H.GridCell();
  22.                     h.cells[x,y,z] = cell;
  23.                     Vector3 min = new Vector3(x, y, z);
  24.                     int corners = 0;
  25.  
  26.                     cell.densities = new float[8];
  27.                     for(int i = 0; i < 8; i++) {
  28.                         cell.densities[i] = sample(min + CMS.S.L.OFFSETS[i]);
  29.                         if(cell.densities[i] > 0) corners |= (int)Mathf.Pow(2, i);
  30.                     }
  31.  
  32.  
  33.                     if(corners == 0 || corners == 255) {
  34.                         continue;
  35.                     }
  36.                     cell.edges = new CMS.H.Edge[12];
  37.  
  38.                     for(int i = 0; i < 12; i++) {
  39.                         cell.edges[i] = new CMS.H.Edge();
  40.                         int c1 = S.L.edgevmap[i][0];
  41.                         int c2 = S.L.edgevmap[i][1];
  42.  
  43.                         int m1 = cell.densities[c1] > 0 ? 1 : 0; //(corners >> c1) & 1;
  44.                         int m2 = cell.densities[c2] > 0 ? 1 : 0; //(corners >> c2) & 1;
  45.  
  46.                         if((m1 == 0 && m2 == 0) || (m1 == 1 && m2 == 1)) {
  47.                             continue;
  48.                         }
  49.  
  50.  
  51.                         Vector3 p1 = min + S.L.OFFSETS[c1];
  52.                         Vector3 p2 = min + S.L.OFFSETS[c2];
  53.                         Vector3 p = ApproximateZeroCrossingPosition(p1, p2, sample);
  54.  
  55.                         cell.edges[i].positions = new Vector3[1];
  56.                         cell.edges[i].normals = new Vector3[1];
  57.  
  58.                         cell.edges[i].positions[0] = p;
  59.                         cell.edges[i].normals[0] = CalculateSurfaceNormal(p, sample);
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.  
  65.         return h;
  66.     }
  67.     public static Vector3 CalculateSurfaceNormal(Vector3 p, SE.IsosurfaceAlgorithm.Sample sample) {
  68.          const float H = 0.001f;
  69.          float dx = sample(p + new Vector3(H, 0, 0)) - sample(p - new Vector3(H, 0, 0));
  70.          float dy = sample(p + new Vector3(0, H, 0)) - sample(p - new Vector3(0, H, 0));
  71.          float dz = sample(p + new Vector3(0, 0, H)) - sample(p - new Vector3(0, 0, H));
  72.  
  73.         return new Vector3(dx, dy, dz).normalized;
  74.     }
  75.     public static Vector3 ApproximateZeroCrossingPosition(Vector3 p0, Vector3 p1, SE.IsosurfaceAlgorithm.Sample sample)
  76.     {
  77.         // approximate the zero crossing by finding the min value along the edge
  78.         float minValue = 100000;
  79.         float t = 0;
  80.         float currentT = 0;
  81.         const int steps = 8;
  82.         const float increment = 1f / (float)steps;
  83.         while (currentT <= 1f)
  84.         {
  85.             Vector3 p = p0 + ((p1 - p0) * currentT);
  86.             float density = Mathf.Abs(sample(p));
  87.             if (density < minValue)
  88.             {
  89.                 minValue = density;
  90.                 t = currentT;
  91.             }
  92.  
  93.             currentT += increment;
  94.         }
  95.  
  96.         return p0 + ((p1 - p0) * t);
  97.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement