Exemple #1
0
        void generateGem(Vector3 position)
        {
            position.Y = position.Y + 2.0f;
            float radiusMax = (DensityFieldSize / 2);
            float radiusMin = (DensityFieldSize / 16);
            Vector3 cylinderCenter = Vector3.One * DensityFieldSize * 0.5f;
            minPos = maxPos = cylinderCenter;
            DensityField = new byte[DensityFieldSize * DensityFieldSize * DensityFieldSize];

            for (int x = 0; x < DensityFieldSize; x++)
            {
                for (int y = 1; y < (DensityFieldSize - 1); y++)
                {
                    for (int z = 0; z < DensityFieldSize; z++)
                    {
                        Vector3 pos = new Vector3(x, y, z);

                        float offset = Math.Abs(pos.Y - cylinderCenter.Y);
                        pos.Y = cylinderCenter.Y;
                        float radius = MathHelper.Lerp(radiusMax, radiusMin, offset/cylinderCenter.Y);
                        float density = Math.Max(1.0f - (pos - cylinderCenter).Length() / (radius), 0.0f);
                        if (density > 0.0f)
                        {
                            pos = (pos / DensityFieldSize) * 2.0f - Vector3.One;
                            minPos = Vector3.Min(pos, minPos);
                            maxPos = Vector3.Max(pos, maxPos);
                        }
                        DensityField[x + (y + z * DensityFieldSize) * DensityFieldSize] = (byte)(density * 255.0f);
                    }
                }
            }

            gemGeometry = new VoxelGeometry();
            gemGeometry.renderElement.Transform = new Matrix[1] { Matrix.CreateScale(5.0f) * Matrix.CreateTranslation(position) };
            gemGeometry.GenerateGeometry(ref DensityField, IsoValue, DensityFieldSize, DensityFieldSize, DensityFieldSize, DensityFieldSize - 1, DensityFieldSize - 1, DensityFieldSize - 1, 0, 0, 0, 2.0f / (float)(DensityFieldSize - 1));

            boundingBox = new BoundingBox();
            boundingBox.Max = Vector3.Transform(maxPos, gemGeometry.renderElement.Transform[0]);
            boundingBox.Min = Vector3.Transform(minPos, gemGeometry.renderElement.Transform[0]);

            collected = false;

            Vector3 lightPosition = position;
            lightPosition.X = position.X + 12.0f;
            emitterLight = new Light(LightType.Point, new Vector3(0.83f, 0.06f, 0.36f), lightPosition, false);
            emitterLight.Parameters = new Vector4(70, 45, 0, 0);
        }
        void GenerateGeometry()
        {
            byte[] DensityField;
            int DensityFieldSize = 17;
            byte IsoValue = 127;
            float radiusMax = (DensityFieldSize / 2);
            float radiusMin = (DensityFieldSize / 16);
            Vector3 cylinderCenter = Vector3.One * DensityFieldSize * 0.5f;
            Vector3 minPos, maxPos;
            minPos = maxPos = cylinderCenter;
            DensityField = new byte[DensityFieldSize * DensityFieldSize * DensityFieldSize];

            for (int x = 0; x < DensityFieldSize; x++)
            {
                for (int y = 1; y < (DensityFieldSize - 1); y++)
                {
                    for (int z = 0; z < DensityFieldSize; z++)
                    {
                        Vector3 pos = new Vector3(x, y, z);

                        float offset = Math.Abs(pos.Y - cylinderCenter.Y);
                        pos.Y = cylinderCenter.Y;
                        float radius = MathHelper.Lerp(radiusMax, radiusMin, offset / cylinderCenter.Y);
                        float density = Math.Max(1.0f - (pos - cylinderCenter).Length() / (radius), 0.0f);
                        if (density > 0.0f)
                        {
                            pos = (pos / DensityFieldSize) * 2.0f - Vector3.One;
                            minPos = Vector3.Min(pos, minPos);
                            maxPos = Vector3.Max(pos, maxPos);
                        }
                        DensityField[x + (y + z * DensityFieldSize) * DensityFieldSize] = (byte)(density * 255.0f);
                    }
                }
            }

            Geometry = new VoxelGeometry();
            Vector3 ratio = Vector3.One * 2.0f / (float)(DensityFieldSize - 1);
            Geometry.GenerateGeometry(ref DensityField, IsoValue, DensityFieldSize, DensityFieldSize, DensityFieldSize, DensityFieldSize - 1, DensityFieldSize - 1, DensityFieldSize - 1, 0, 0, 0, ratio, Matrix.Identity);
        }
        void GenerateGeometry()
        {
            byte[] DensityField;
            int DensityFieldSize = 17;
            byte IsoValue = 127;

            DensityField = new byte[DensityFieldSize * DensityFieldSize * DensityFieldSize];
            Vector3 center = Vector3.One * DensityFieldSize * 0.5f;
            Vector3 minPos = center;
            Vector3 maxPos = center;

            float radius = DensityFieldSize / 2;

            for (int x = 0; x < DensityFieldSize; x++)
            {
                for (int y = 1; y < (DensityFieldSize - 1); y++)
                {
                    for (int z = 0; z < DensityFieldSize; z++)
                    {
                        Vector3 pos = new Vector3(x, y, z);

                        float density = MathHelper.Clamp(1.0f-(pos-center).Length()/radius, 0, 1);
                        if (density > 0.0f)
                        {
                            pos = (pos / DensityFieldSize) * 2.0f - Vector3.One;
                            minPos = Vector3.Min(pos, minPos);
                            maxPos = Vector3.Max(pos, maxPos);
                        }
                        DensityField[x + (y + z * DensityFieldSize) * DensityFieldSize] = (byte)(density * 255.0f);
                    }
                }
            }

            Geometry = new VoxelGeometry();
            Vector3 ratio = 2.0f * Vector3.One / (float)(DensityFieldSize - 1);
            Geometry.GenerateGeometry(ref DensityField, IsoValue, DensityFieldSize, DensityFieldSize, DensityFieldSize, DensityFieldSize - 1, DensityFieldSize - 1, DensityFieldSize - 1, 0, 0, 0, ratio, Matrix.Identity);
        }
Exemple #4
0
        void GenerateGeometry()
        {
            byte[] DensityField;
            int DensityFieldSize = 17;
            byte IsoValue = 127;

            DensityField = new byte[DensityFieldSize * DensityFieldSize * DensityFieldSize];
            Vector3 center = Vector3.One * DensityFieldSize * 0.5f;
            Vector3 minPos = center;
            Vector3 maxPos = center;

            float radiusInner = DensityFieldSize / 6;

            float numSpikes = 5;

            float period = MathHelper.TwoPi * numSpikes;

            float minRad = DensityFieldSize / 4;

            float maxRad = DensityFieldSize / 3;

            for (int x = 0; x < DensityFieldSize; x++)
            {
                for (int y = 1; y < (DensityFieldSize - 1); y++)
                {
                    for (int z = 0; z < DensityFieldSize; z++)
                    {
                        Vector3 pos = new Vector3(x, y, z);

                        float innerRingTerm = (1.0f - new Vector2(pos.X-center.X, pos.Y-center.Y).Length() / radiusInner);
                        float innerRing = (innerRingTerm > 0.0f) ? 1 : 0;

                        float zElem = Math.Abs((z - center.Z) / center.Z);

                        float sinTerm = (float)Math.Sin(period * zElem);
                        float cosTerm = (float)Math.Cos(period * zElem);

                        float radiusOuter = minRad;

                        if (cosTerm <= 0.0f)
                        {
                            radiusOuter = MathHelper.Lerp(minRad, maxRad, 1.0f + cosTerm);
                        }

                        float outerRingTerm = (1.0f - new Vector2(pos.X - center.X, pos.Y - center.Y).Length() / radiusOuter);
                        float density = MathHelper.Clamp(outerRingTerm-innerRing, 0, 1);
                        if (density > 0.0f)
                        {
                            pos = (pos / DensityFieldSize) * 2.0f - Vector3.One;
                            minPos = Vector3.Min(pos, minPos);
                            maxPos = Vector3.Max(pos, maxPos);
                        }
                        DensityField[x + (y + z * DensityFieldSize) * DensityFieldSize] = (byte)(density * 255.0f);
                    }
                }
            }

            gateGeometry = new VoxelGeometry();
            gateGeometry.renderElement.Transform = new Matrix[1] { this.Transformation.GetTransform() };
            gateGeometry.GenerateGeometry(ref DensityField, IsoValue, DensityFieldSize, DensityFieldSize, DensityFieldSize, DensityFieldSize - 1, DensityFieldSize - 1, DensityFieldSize - 1, 0, 0, 0, 2.0f / (float)(DensityFieldSize - 1));

            bounds = new BoundingBox();
            bounds.Max = Vector3.Transform(maxPos, this.Transformation.GetTransform());
            bounds.Min = Vector3.Transform(minPos, this.Transformation.GetTransform());
        }