Пример #1
0
        public Sphere(float diameter = 1.0f, int tessellation = 16, bool toLeftHanded = false)
        {
            if (tessellation < 3)
            {
                throw new ArgumentOutOfRangeException("tessellation", "Must be >= 3");
            }

            int verticalSegments   = tessellation;
            int horizontalSegments = tessellation * 2;

            var vertices = new VertexPositionNormalTexture[(verticalSegments + 1) * (horizontalSegments + 1)];
            var indices  = new int[(verticalSegments) * (horizontalSegments + 1) * 6];

            float radius = diameter / 2;

            int vertexCount = 0;


            for (int i = 0; i <= verticalSegments; i++)
            {
                float v = 1.0f - (float)i / verticalSegments;

                var latitude = (float)((i * Math.PI / verticalSegments) - Math.PI / 2.0);
                var dy       = (float)Math.Sin(latitude);
                var dxz      = (float)Math.Cos(latitude);

                for (int j = 0; j <= horizontalSegments; j++)
                {
                    float u = (float)j / horizontalSegments;

                    var longitude = (float)(j * 2.0 * Math.PI / horizontalSegments);
                    var dx        = (float)Math.Sin(longitude);
                    var dz        = (float)Math.Cos(longitude);

                    dx *= dxz;
                    dz *= dxz;

                    var normal            = new Vector3(dx, dy, dz);
                    var textureCoordinate = new Vector2(u, v);

                    vertices[vertexCount++] = new VertexPositionNormalTexture(normal * radius, normal, textureCoordinate);
                }
            }

            int stride = horizontalSegments + 1;

            int indexCount = 0;

            for (int i = 0; i < verticalSegments; i++)
            {
                for (int j = 0; j <= horizontalSegments; j++)
                {
                    int nextI = i + 1;
                    int nextJ = (j + 1) % stride;

                    indices[indexCount++] = (i * stride + j);
                    indices[indexCount++] = (nextI * stride + j);
                    indices[indexCount++] = (i * stride + nextJ);

                    indices[indexCount++] = (i * stride + nextJ);
                    indices[indexCount++] = (nextI * stride + j);
                    indices[indexCount++] = (nextI * stride + nextJ);
                }
            }

            Vertices = vertices;
            Indices  = indices.Select(el => (ushort)el).ToArray();

            if (toLeftHanded)
            {
                for (int i1 = 0; i1 < Indices.Length; i1 += 3)
                {
                    Utilities.Swap(ref Indices[i1], ref Indices[i1 + 2]);
                }
            }
        }
Пример #2
0
        public Plane(float sizeX, float sizeY, int tessellation, Vector2 uvFactor, bool toLeftHanded = false)
        {
            if (tessellation < 1)
            {
                throw new ArgumentOutOfRangeException("tessellation", "tessellation must be > 0");
            }
            int num1 = tessellation + 1;

            VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[num1 * num1];
            int[] indices = new int[tessellation * tessellation * 6];
            float num2    = sizeX / (float)tessellation;
            float num3    = sizeY / (float)tessellation;

            sizeX /= 2f;
            sizeY /= 2f;
            int     num4   = 0;
            int     num5   = 0;
            Vector3 normal = Vector3.UnitZ;

            for (int index1 = 0; index1 < tessellation + 1; ++index1)
            {
                for (int index2 = 0; index2 < tessellation + 1; ++index2)
                {
                    Vector3 position          = new Vector3((float)(-(double)sizeX + (double)num2 * (double)index2), sizeY - num3 * (float)index1, 0.0f);
                    Vector2 textureCoordinate = new Vector2(1f * (float)index2 / (float)tessellation * uvFactor.X, 1f * (float)index1 / (float)tessellation * uvFactor.Y);
                    vertices[num4++] = new VertexPositionNormalTexture(position, normal, textureCoordinate);
                }
            }
            for (int index1 = 0; index1 < tessellation; ++index1)
            {
                for (int index2 = 0; index2 < tessellation; ++index2)
                {
                    int   num6      = num1 * index1 + index2;
                    int[] numArray1 = indices;
                    int   index3    = num5;
                    int   num7      = 1;
                    int   num8      = index3 + num7;
                    int   num9      = num6 + 1;
                    numArray1[index3] = num9;
                    int[] numArray2 = indices;
                    int   index4    = num8;
                    int   num10     = 1;
                    int   num11     = index4 + num10;
                    int   num12     = num6 + 1 + num1;
                    numArray2[index4] = num12;
                    int[] numArray3 = indices;
                    int   index5    = num11;
                    int   num13     = 1;
                    int   num14     = index5 + num13;
                    int   num15     = num6 + num1;
                    numArray3[index5] = num15;
                    int[] numArray4 = indices;
                    int   index6    = num14;
                    int   num16     = 1;
                    int   num17     = index6 + num16;
                    int   num18     = num6 + 1;
                    numArray4[index6] = num18;
                    int[] numArray5 = indices;
                    int   index7    = num17;
                    int   num19     = 1;
                    int   num20     = index7 + num19;
                    int   num21     = num6 + num1;
                    numArray5[index7] = num21;
                    int[] numArray6 = indices;
                    int   index8    = num20;
                    int   num22     = 1;
                    num5 = index8 + num22;
                    int num23 = num6;
                    numArray6[index8] = num23;
                }
            }
            Vertices = vertices;
            Indices  = indices.Select(el => (ushort)el).ToArray();
            if (toLeftHanded)
            {
                for (int i1 = 0; i1 < Indices.Length; i1 += 3)
                {
                    Utilities.Swap(ref Indices[i1], ref Indices[i1 + 2]);
                }
            }
        }