예제 #1
0
        public void GenerateMarching()
        {
            Vector2 uv = Vector2.zero;
            int     k;

            for (int z = -1; z < sizeZ; z++)
            {
                for (int y = -1; y < sizeY; y++)
                {
                    for (int x = -1; x < sizeX; x++)
                    {
                        IntVector3 offset        = new IntVector3(x, y, z);
                        IntVector3 patternOffset = _GetPatternPos(offset);

                        if ((offset.x == -1 && patternOffset.x != -1) || (offset.y == sizeY && patternOffset.y != -1))
                        {
                            continue;
                        }

                        colorList.Clear();
                        for (int i = 0; i < cornerPos.Length; i++)
                        {
                            cornerPos[i] = offset + dirs[i];
                            cornerUV[i]  = _GetPatternUV(cornerPos[i]);
                            k            = cornerColor[i] = _GetColor(cornerUV[i]);
                            if (k != 0 && !colorList.Contains(k))
                            {
                                colorList.Add(k);
                            }
                        }

                        for (int j = 0; j < colorList.Count; j++)
                        {
                            k = colorList[j];
                            int cubeIndex = 0;
                            for (int i = 0; i < cornerPos.Length; i++)
                            {
                                corner[i] = cornerColor[i] == k;
                                if (corner[i])
                                {
                                    cubeIndex |= 1 << i;
                                    uv         = cornerUV[i];
                                }
                            }

                            if (cubeIndex == 255)
                            {
                                break;
                            }

                            for (int i = 0; i < vertList.Length; i++)
                            {
                                if (edgeTable[cubeIndex].Bit(i))
                                {
                                    vertList[i] = InterpVertex(cornerPos[cornerPair[i * 2]], cornerPos[cornerPair[i * 2 + 1]], corner[cornerPair[i * 2]], corner[cornerPair[i * 2 + 1]]);
                                }
                            }

                            //uv = new Vector2(k / 3f, 0);
                            uv = Atlas.Coord2Uv(tex, uv + new Vector2(0.5f, 0.5f));

                            int[] table = triTable[cubeIndex];
                            for (int i = 0; table[i] != -1; i += 3)
                            {
                                int index0 = table[i];
                                int index1 = table[i + 1];
                                int index2 = table[i + 2];

                                int     index = vertices.Count;
                                Vector3 v     = vertList[index0] - Center;
                                vertices.Add(v);
                                v = vertList[index1] - Center;
                                vertices.Add(v);
                                v = vertList[index2] - Center;
                                vertices.Add(v);

                                uvs.Add(uv);
                                uvs.Add(uv);
                                uvs.Add(uv);

                                indexes.Add(index + 2);
                                indexes.Add(index + 1);
                                indexes.Add(index + 0);
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        private void Quad(IntVector3 bottomRight, IntVector3 topRight, IntVector3 topLeft, IntVector3 bottomLeft,
                          Vector2 brCoord, Vector2 trCoord, Vector2 tlCoord, Vector2 blCoord,
                          int pixel, int d, bool backFace, bool multiColor)
        {
            int index = vertices.Count;

            Vector3 v1 = (bottomLeft - Center);
            Vector3 v2 = (topLeft - Center);
            Vector3 v3 = (bottomRight - Center);
            Vector3 v4 = (topRight - Center);

            vertices.Add(v1);
            vertices.Add(v2);
            vertices.Add(v3);
            vertices.Add(v4);

            if (multiColor)
            {
                Vector2 uv = UvEncode[pixel];
                uvs.Add(uv);
                uvs.Add(uv);
                uvs.Add(uv);
                uvs.Add(uv);
            }
            else
            {
                if (trCoord.x >= blCoord.x)
                {
                    if (d == 2)
                    {
                        trCoord.x++; brCoord.x++;
                    }
                    else
                    {
                        trCoord.x++; tlCoord.x++;
                    }
                }
                else
                {
                    if (d == 2)
                    {
                        blCoord.x++; tlCoord.x++;
                    }
                    else
                    {
                        blCoord.x++; brCoord.x++;
                    }
                }

                if (trCoord.y >= blCoord.y)
                {
                    if (d == 2)
                    {
                        trCoord.y++; tlCoord.y++;
                    }
                    else
                    {
                        trCoord.y++; brCoord.y++;
                    }
                }
                else
                {
                    if (d == 2)
                    {
                        blCoord.y++; brCoord.y++;
                    }
                    else
                    {
                        blCoord.y++; tlCoord.y++;
                    }
                }

                uvs.Add(Atlas.Coord2Uv(tex, blCoord));
                uvs.Add(Atlas.Coord2Uv(tex, tlCoord));
                uvs.Add(Atlas.Coord2Uv(tex, brCoord));
                uvs.Add(Atlas.Coord2Uv(tex, trCoord));
            }

            if (backFace)
            {
                indexes.Add(index + 2);
                indexes.Add(index);
                indexes.Add(index + 1);
                indexes.Add(index + 1);
                indexes.Add(index + 3);
                indexes.Add(index + 2);
            }
            else
            {
                indexes.Add(index + 2);
                indexes.Add(index + 3);
                indexes.Add(index + 1);
                indexes.Add(index + 1);
                indexes.Add(index);
                indexes.Add(index + 2);
            }
        }