Ejemplo n.º 1
0
        /// <summary>
        /// 链条。若干个点用直线连接起来。
        /// </summary>
        /// <param name="pointCount">有多少个点</param>
        /// <param name="length">点的范围(长度)</param>
        /// <param name="width">点的范围(宽度)</param>
        /// <param name="height">点的范围(高度)</param>
        public ChainModel(int pointCount = 10, int length = 5, int width = 5, int height = 5)
        {
            var positions = new vec3[pointCount];

            for (int i = 0; i < pointCount; i++)
            {
                var point = new vec3();
                point.x      = (float)random.NextDouble() * length;
                point.y      = (float)random.NextDouble() * width;
                point.z      = (float)random.NextDouble() * height;
                positions[i] = point;
            }
            positions.Move2Center();
            this.Positions = positions;

            this.Colors = new vec3[pointCount];
            {
                for (int i = 0; i < pointCount; i++)
                {
                    uint p     = (uint)((256 * 256 * 256) / pointCount * (i + 1));
                    var  color = new vec3();
                    color.x        = ((p >> 0) & 0xFF) / 255.0f;
                    color.y        = ((p >> 8) & 0xFF) / 255.0f;
                    color.z        = ((p >> 16) & 0xFF) / 255.0f;
                    this.Colors[i] = color;
                }
            }
        }
Ejemplo n.º 2
0
        public CylinderModel(float radius, float height, uint sliceCount)
        {
            {
                var positions = new vec3[1 + sliceCount + sliceCount + 1];
                int t         = 0;
                positions[t++] = new vec3(0, height / 2, 0);
                for (int i = 1; i < sliceCount + 1; i++)
                {
                    double d = 2 * Math.PI * i / (sliceCount);
                    positions[t++] = new vec3((float)Math.Sin(d) * radius, height / 2, (float)Math.Cos(d) * radius);
                }
                for (int i = 1; i < sliceCount + 1; i++)
                {
                    double d = 2 * Math.PI * i / (sliceCount);
                    positions[t++] = new vec3((float)Math.Sin(d) * radius, -height / 2, (float)Math.Cos(d) * radius);
                }
                positions[t++] = new vec3(0, -height / 2, 0);
                BoundingBox box = positions.Move2Center();
                this.positions = positions;
                this.modelSize = box.MaxPosition - box.MinPosition;
            }
            {
                var  indexes = new uint[3 * (sliceCount + sliceCount * 2 + sliceCount)];
                uint t       = 0;
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = 0;
                    indexes[t++] = i + 1;
                    indexes[t++] = (i + 1) % sliceCount + 1;
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = (i + 1) % sliceCount + 1;
                    indexes[t++] = (i + 1);
                    indexes[t++] = sliceCount + (i + 1) % sliceCount + 1;

                    indexes[t++] = (i + 1);
                    indexes[t++] = sliceCount + (i + 1);
                    indexes[t++] = sliceCount + (i + 1) % sliceCount + 1;
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = sliceCount + sliceCount + 1;
                    indexes[t++] = sliceCount + (i + 1) % sliceCount + 1;
                    indexes[t++] = sliceCount + (i + 1);
                }
                this.indexes = indexes;
            }
        }
Ejemplo n.º 3
0
        public PrismoidModel(float topXLength, float topZLength, float bottomXLength, float bottomZLength, float height)
        {
            float xDiff     = (bottomXLength - topXLength) / 2;
            float zDiff     = (bottomZLength - topZLength) / 2;
            var   positions = new vec3[8];

            positions[0] = new vec3(topXLength + xDiff, height, topZLength + zDiff);
            positions[1] = new vec3(topXLength + xDiff, height, 0);
            positions[2] = new vec3(bottomXLength, 0, bottomZLength);
            positions[3] = new vec3(bottomXLength, 0, 0);
            positions[4] = new vec3(xDiff, height, topZLength + zDiff);
            positions[5] = new vec3(xDiff, height, zDiff);
            positions[6] = new vec3(0, 0, bottomZLength);
            positions[7] = new vec3(0, 0, 0);
            BoundingBox box = positions.Move2Center();

            this.positions = positions;
            this.modelSize = box.MaxPosition - box.MinPosition;
        }
Ejemplo n.º 4
0
        public AnnulusModel(float radius, float thickness, uint sliceCount, uint secondSliceCount)
        {
            {
                var positions = new vec3[sliceCount * secondSliceCount];
                int t         = 0;
                var y         = new vec3(0, 1, 0);
                for (int i = 0; i < sliceCount; i++)
                {
                    double di     = 2 * Math.PI * i / sliceCount;
                    var    center = new vec3(radius * (float)Math.Sin(di), 0, radius * (float)Math.Cos(di));
                    float  length = center.length();
                    center = center.normalize();
                    for (int j = 0; j < secondSliceCount; j++)
                    {
                        double dj = 2 * Math.PI * j / secondSliceCount;
                        positions[t++] = center * (length - thickness * (float)Math.Cos(dj))
                                         + y * (thickness * (float)Math.Sin(dj));
                    }
                }
                BoundingBox box = positions.Move2Center();
                this.positions = positions;
                this.modelSize = box.MaxPosition - box.MinPosition;
            }
            {
                var  indexes = new uint[sliceCount * (secondSliceCount * 3 + secondSliceCount * 3)];
                uint t       = 0;
                for (uint i = 0; i < sliceCount; i++)
                {
                    for (uint j = 0; j < secondSliceCount; j++)
                    {
                        indexes[t++] = i * secondSliceCount + j;
                        indexes[t++] = i * secondSliceCount + (j + 1) % secondSliceCount;
                        indexes[t++] = (i * secondSliceCount + secondSliceCount + (j + 1) % secondSliceCount) % (sliceCount * secondSliceCount);

                        indexes[t++] = i * secondSliceCount + j;
                        indexes[t++] = (i * secondSliceCount + secondSliceCount + (j + 1) % secondSliceCount) % (sliceCount * secondSliceCount);
                        indexes[t++] = (i * secondSliceCount + secondSliceCount + j) % (sliceCount * secondSliceCount);
                    }
                }
                this.indexes = indexes;
            }
        }
Ejemplo n.º 5
0
        public DiskModel(float radius, float holeRadius, float halfThickness, uint sliceCount, uint secondSliceCount)
        {
            float planeRadius = radius - holeRadius - halfThickness;

            uint[] outUpSphere   = new uint[sliceCount];
            uint[] outDownSphere = new uint[sliceCount];
            uint[] inUpSphere    = new uint[sliceCount];
            uint[] inDownSphere  = new uint[sliceCount];
            {
                var  positions = new vec3[sliceCount * ((secondSliceCount + 1) * 2)];
                uint t         = 0;
                var  y         = new vec3(0, 1, 0);
                for (int i = 0; i < sliceCount; i++)
                {
                    outUpSphere[i] = t;
                    double di     = 2 * Math.PI * i / sliceCount;
                    var    center = new vec3(radius * (float)Math.Sin(di), 0, radius * (float)Math.Cos(di));
                    center = center.normalize();
                    for (int j = 0; j < (secondSliceCount + 1); j++)
                    {
                        double dj = Math.PI / 2 + Math.PI * j / (secondSliceCount);
                        positions[t++] = center * ((radius + planeRadius) - halfThickness * (float)Math.Cos(dj))
                                         + y * (halfThickness * (float)Math.Sin(dj));
                    }
                    outDownSphere[i] = t - 1;
                }
                for (int i = 0; i < sliceCount; i++)
                {
                    inDownSphere[i] = t;
                    double di     = 2 * Math.PI * i / sliceCount;
                    var    center = new vec3(radius * (float)Math.Sin(di), 0, radius * (float)Math.Cos(di));
                    center = center.normalize();
                    for (int j = 0; j < (secondSliceCount + 1); j++)
                    {
                        double dj = Math.PI / 2 + Math.PI + Math.PI * j / (secondSliceCount);
                        positions[t++] = center * ((radius - planeRadius) - halfThickness * (float)Math.Cos(dj))
                                         + y * (halfThickness * (float)Math.Sin(dj));
                    }
                    inUpSphere[i] = t - 1;
                }

                BoundingBox box = positions.Move2Center();
                this.positions = positions;
                this.modelSize = box.MaxPosition - box.MinPosition;
            }
            {
                var indexes = new uint[(sliceCount * (secondSliceCount) * 2 * 3 * 2)
                                       + sliceCount * 2 * 3 + sliceCount * 2 * 3];
                uint t = 0;
                for (uint i = 0; i < sliceCount; i++)
                {
                    for (uint j = 0; j < secondSliceCount; j++)
                    {
                        indexes[t++] = outUpSphere[i] + j;
                        indexes[t++] = outUpSphere[i] + (j + 1);
                        indexes[t++] = outUpSphere[(i + 1) % sliceCount] + j + 1;

                        indexes[t++] = outUpSphere[i] + j;
                        indexes[t++] = outUpSphere[(i + 1) % sliceCount] + j + 1;
                        indexes[t++] = outUpSphere[(i + 1) % sliceCount] + j;
                    }
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    for (uint j = 0; j < secondSliceCount; j++)
                    {
                        indexes[t++] = inDownSphere[i] + j;
                        indexes[t++] = inDownSphere[i] + (j + 1);
                        indexes[t++] = inDownSphere[(i + 1) % sliceCount] + j + 1;

                        indexes[t++] = inDownSphere[i] + j;
                        indexes[t++] = inDownSphere[(i + 1) % sliceCount] + j + 1;
                        indexes[t++] = inDownSphere[(i + 1) % sliceCount] + j;
                    }
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = inUpSphere[i];
                    indexes[t++] = outUpSphere[i];
                    indexes[t++] = outUpSphere[(i + 1) % sliceCount];

                    indexes[t++] = inUpSphere[i];
                    indexes[t++] = outUpSphere[(i + 1) % sliceCount];
                    indexes[t++] = inUpSphere[(i + 1) % sliceCount];
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = inDownSphere[i];
                    indexes[t++] = outDownSphere[(i + 1) % sliceCount];
                    indexes[t++] = outDownSphere[i];

                    indexes[t++] = inDownSphere[i];
                    indexes[t++] = inDownSphere[(i + 1) % sliceCount];
                    indexes[t++] = outDownSphere[(i + 1) % sliceCount];
                }
                this.indexes = indexes;
            }
        }
Ejemplo n.º 6
0
        public DiskModel(float radius, float holeRadius, float thickness, uint sliceCount, uint secondSliceCount)
        {
            uint upPlane, downPlane;

            uint[] upSphere   = new uint[sliceCount];
            uint[] downSphere = new uint[sliceCount];
            {
                var  positions = new vec3[sliceCount * secondSliceCount + sliceCount + sliceCount];
                uint t         = 0;
                var  y         = new vec3(0, 1, 0);
                for (int i = 0; i < sliceCount; i++)
                {
                    upSphere[i] = t;
                    double di     = 2 * Math.PI * i / sliceCount;
                    var    center = new vec3(radius * (float)Math.Sin(di), 0, radius * (float)Math.Cos(di));
                    float  length = center.length();
                    center = center.normalize();
                    for (int j = 0; j < secondSliceCount; j++)
                    {
                        double dj = Math.PI / 2 + Math.PI * j / (secondSliceCount - 1);
                        positions[t++] = center * (length - thickness * (float)Math.Cos(dj))
                                         + y * (thickness * (float)Math.Sin(dj));
                    }
                    downSphere[i] = t - 1;
                }
                upPlane = t;
                for (int i = 0; i < sliceCount; i++)
                {
                    double di = 2 * Math.PI * i / sliceCount;
                    positions[t++] = new vec3(holeRadius * (float)Math.Sin(di), thickness, holeRadius * (float)Math.Cos(di));
                }
                downPlane = t;
                for (int i = 0; i < sliceCount; i++)
                {
                    double di = 2 * Math.PI * i / sliceCount;
                    positions[t++] = new vec3(holeRadius * (float)Math.Sin(di), -thickness, holeRadius * (float)Math.Cos(di));
                }
                BoundingBox box = positions.Move2Center();
                this.positions = positions;
                this.modelSize = box.MaxPosition - box.MinPosition;
            }
            {
                var indexes = new uint[sliceCount * (secondSliceCount - 1) * 2 * 3
                                       + sliceCount * 2 * 3 + sliceCount * 2 * 3
                                       + sliceCount * 2 * 3];
                uint t = 0;
                for (uint i = 0; i < sliceCount; i++)
                {
                    for (uint j = 0; j < secondSliceCount - 1; j++)
                    {
                        indexes[t++] = i * secondSliceCount + j;
                        indexes[t++] = i * secondSliceCount + (j + 1) % secondSliceCount;
                        indexes[t++] = (i * secondSliceCount + secondSliceCount + (j + 1) % secondSliceCount) % (sliceCount * secondSliceCount);

                        indexes[t++] = i * secondSliceCount + j;
                        indexes[t++] = (i * secondSliceCount + secondSliceCount + (j + 1) % secondSliceCount) % (sliceCount * secondSliceCount);
                        indexes[t++] = (i * secondSliceCount + secondSliceCount + j) % (sliceCount * secondSliceCount);
                    }
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = upPlane + i;
                    indexes[t++] = upSphere[i];
                    indexes[t++] = upPlane + (i + 1) % sliceCount;

                    indexes[t++] = upPlane + (i + 1) % sliceCount;
                    indexes[t++] = upSphere[i];
                    indexes[t++] = upSphere[(i + 1) % sliceCount];
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = downPlane + i;
                    indexes[t++] = downPlane + (i + 1) % sliceCount;
                    indexes[t++] = downSphere[i];

                    indexes[t++] = downPlane + (i + 1) % sliceCount;
                    indexes[t++] = downSphere[(i + 1) % sliceCount];
                    indexes[t++] = downSphere[i];
                }
                for (uint i = 0; i < sliceCount; i++)
                {
                    indexes[t++] = upPlane + i;
                    indexes[t++] = upPlane + (i + 1) % sliceCount;
                    indexes[t++] = downPlane + i;

                    indexes[t++] = downPlane + i;
                    indexes[t++] = upPlane + (i + 1) % sliceCount;
                    indexes[t++] = downPlane + (i + 1) % sliceCount;
                }
                this.indexes = indexes;
            }
        }