private static Vector3 FindIntersection(Vector3 org, Vector3 dest) { const int BISECTION_STEPCOUNT = 8; float mu = 0.0f; float muStepSize = 0.25f; float epsilonVal = 1.0f / Mathf.Pow(2.0f, BISECTION_STEPCOUNT); float orgVal = Density.Eval(org) - iso; float destVal = Density.Eval(dest) - iso; for (int i = 0; i < BISECTION_STEPCOUNT; ++i) { Vector3 vertexCoord = org + (dest - org) * mu; // isnt this just lerp or something? float vertexVal = Density.Eval(vertexCoord) - iso; if (Mathf.Abs(vertexVal) < epsilonVal) { break; } if (orgVal < 0.0f) { mu += vertexVal < 0.0f ? muStepSize : -muStepSize; } else { mu += vertexVal < 0.0f ? -muStepSize : muStepSize; } muStepSize *= 0.5f; } return(org + (dest - org) * mu); // isnt this just lerp or something? // ignoring normals that could be generated here for now.. }
private static float iso = 0.0f; // where surface is [-1,1] ( < iso = solid) prob never gonna change??? public static MeshData CalculateMeshData() { List <Vector3> verts = new List <Vector3>(); List <Vector3> norms = new List <Vector3>(); List <int> tris = new List <int>(); Dictionary <int, int> vertexToEdge = new Dictionary <int, int>(); int i; int cell; int edgeType; int eff = 64 - 1; int xCoord, yCoord, zCoord; float[] values = new float[4]; for (int z = 1; z < eff; ++z) { zCoord = z * size; for (int y = 1; y < eff; ++y) { yCoord = y * size; values[0] = Density.Eval(new Vector3(size, yCoord + size, zCoord)); values[1] = Density.Eval(new Vector3(size, yCoord, zCoord)); values[2] = Density.Eval(new Vector3(size, yCoord + size, zCoord + size)); values[3] = Density.Eval(new Vector3(size, yCoord, zCoord + size)); for (int x = 1; x < eff; ++x) { xCoord = x * size; cell = 0; if (values[1] < iso) { cell |= 1; } if (values[0] < iso) { cell |= 2; } values[0] = Density.Eval(new Vector3(xCoord + size, yCoord + size, zCoord)); if (values[0] < iso) { cell |= 4; } values[1] = Density.Eval(new Vector3(xCoord + size, yCoord, zCoord)); if (values[1] < iso) { cell |= 8; } if (values[3] < iso) { cell |= 16; } if (values[2] < iso) { cell |= 32; } values[2] = Density.Eval(new Vector3(xCoord + size, yCoord + size, zCoord + size)); if (values[2] < iso) { cell |= 64; } values[3] = Density.Eval(new Vector3(xCoord + size, yCoord, zCoord + size)); if (values[3] < iso) { cell |= 128; } edgeType = edgeTable[cell]; if (edgeType != 0) { if ((edgeType & 8) != 0) { vertexToEdge[GetEdgeId(x, y, z, 3)] = GenerateVertex(xCoord, yCoord, zCoord, 3, verts); } if ((edgeType & 1) != 0) { vertexToEdge[GetEdgeId(x, y, z, 0)] = GenerateVertex(xCoord, yCoord, zCoord, 0, verts); } if ((edgeType & 256) != 0) { vertexToEdge[GetEdgeId(x, y, z, 8)] = GenerateVertex(xCoord, yCoord, zCoord, 8, verts); } if (x == eff - 1) { if ((edgeType & 4) != 0) { vertexToEdge[GetEdgeId(x, y, z, 2)] = GenerateVertex(xCoord, yCoord, zCoord, 2, verts); } if ((edgeType & 2048) != 0) { vertexToEdge[GetEdgeId(x, y, z, 11)] = GenerateVertex(xCoord, yCoord, zCoord, 11, verts); } } if (y == eff - 1) { if ((edgeType & 2) != 0) { vertexToEdge[GetEdgeId(x, y, z, 1)] = GenerateVertex(xCoord, yCoord, zCoord, 1, verts); } if ((edgeType & 512) != 0) { vertexToEdge[GetEdgeId(x, y, z, 9)] = GenerateVertex(xCoord, yCoord, zCoord, 9, verts); } } if (z == eff - 1) { if ((edgeType & 16) != 0) { vertexToEdge[GetEdgeId(x, y, z, 4)] = GenerateVertex(xCoord, yCoord, zCoord, 4, verts); } if ((edgeType & 128) != 0) { vertexToEdge[GetEdgeId(x, y, z, 7)] = GenerateVertex(xCoord, yCoord, zCoord, 7, verts); } } if (x == eff - 1 && y == eff - 1) { if ((edgeType & 1024) != 0) { vertexToEdge[GetEdgeId(x, y, z, 10)] = GenerateVertex(xCoord, yCoord, zCoord, 10, verts); } } if (x == eff - 1 && z == eff - 1) { if ((edgeType & 64) != 0) { vertexToEdge[GetEdgeId(x, y, z, 6)] = GenerateVertex(xCoord, yCoord, zCoord, 6, verts); } } if (y == eff - 1 && z == eff - 1) { if ((edgeType & 32) != 0) { vertexToEdge[GetEdgeId(x, y, z, 5)] = GenerateVertex(xCoord, yCoord, zCoord, 5, verts); } } for (i = 0; triangleTable[cell, i] != -1; ++i) { tris.Add(GetEdgeId(x, y, z, triangleTable[cell, i])); } } } } } for (i = 0; i < tris.Count; ++i) { tris[i] = vertexToEdge[tris[i]]; } MeshData data = new MeshData(verts.ToArray()); data.triangles = tris.ToArray(); return(data); }