Beispiel #1
0
    private void AddCube(Vector3 center, ref List <Vector3> vertices, ref List <int> tris)
    {
        int index = 0;

        //get the edge index position according to the function values
        for (int i = 0; i < positions.Length; i++)
        {
            float value = Defines.Map(positions[i] * scale + center + transform.position);
            index += value > 0.5 ? 1 << i : 0;
        }
        int[] edgesIndices = Defines.triTable[index];
        for (int i = 0; i < edgesIndices.Length; i += 3)
        {
            if (edgesIndices[i] == -1)
            {
                break;
            }

            int c = vertices.Count;
            //adapt each vertex to where the value actually crosses, not just in the middle of the edge
            Vector3 p0 = GetVertexOnEdge(center, edgesIndices, i);
            Vector3 p1 = GetVertexOnEdge(center, edgesIndices, i + 1);
            Vector3 p2 = GetVertexOnEdge(center, edgesIndices, i + 2);
            vertices.AddRange(new Vector3[] { p0, p1, p2 });
            tris.AddRange(new int[] { c, c + 1, c + 2 });
        }
    }
Beispiel #2
0
    private Vector3 GetVertexOnEdge(Vector3 center, int[] edgesIndices, int i)
    {
        //proportional offset: calculate where the value would exactly be the threshold, if we assume a linear growth
        //this linear growth is - in most cases - wrong for SDF, but its much simpler to compute
        Vector3 p0 = positions[(int)edges[edgesIndices[i]].x] * scale + center;
        Vector3 p1 = positions[(int)edges[edgesIndices[i]].y] * scale + center;
        float   v0 = Defines.Map(p0 + transform.position);
        float   v1 = Defines.Map(p1 + transform.position);
        float   d  = (0.5f - v0) / (v1 - v0);
        Vector3 p  = p0 + (p1 - p0) * d;

        return(p);
    }