コード例 #1
1
        internal override float SignedDistance(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator)
        {
            Vector3 localPosition = position - m_translation;
            Vector3.Transform(ref localPosition, ref m_invRotation, out localPosition);

            var primaryDistance = new Vector2(localPosition.X, localPosition.Z).Length() - m_primaryRadius;
            var signedDistance = new Vector2(primaryDistance, localPosition.Y).Length() - m_secondaryRadius;

            var potentialHalfDeviation = m_potentialHalfDeviation + lodVoxelSize;
            if (signedDistance > potentialHalfDeviation)
                return 1f;
            else if (signedDistance < -potentialHalfDeviation)
                return -1f;

            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                float normalizer = 0.5f * m_deviationFrequency;
                var tmp = localPosition * normalizer;
                float halfDeviationRatio = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
                signedDistance -= halfDeviationRatio * m_secondaryHalfDeviation;
            }

            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                float normalizer = 0.5f * m_detailFrequency;
                var tmp = localPosition * normalizer;
                signedDistance += m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }

            return signedDistance / lodVoxelSize;
        }
コード例 #2
0
        private float SignedDistanceInternal(float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref Vector3 localPosition, float distance)
        {
            float halfDeviationRatio;

            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                float distScale = 0f;
                if (distance != 0f)
                {
                    distScale = 1f / distance;
                }
                float normalizer = m_deviationFrequency * m_radius * distScale;
                var   tmp        = localPosition * normalizer;
                halfDeviationRatio = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }
            else
            {
                halfDeviationRatio = 0f;
            }

            float signedDistance = distance - m_radius - halfDeviationRatio * m_halfDeviation;

            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                float normalizer = m_detailFrequency * m_radius / distance;
                var   tmp        = localPosition * normalizer;
                signedDistance += m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }

            return(signedDistance / lodVoxelSize);
        }
コード例 #3
0
        internal override float SignedDistance(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator)
        {
            var pa = position - m_pointA;
            var ba = m_pointB - m_pointA;
            float h = MathHelper.Clamp(pa.Dot(ref ba) / ba.LengthSquared(), 0.0f, 1.0f);
            float signedDistance = (pa - ba * h).Length() - m_radius;

            var potentialHalfDeviation = m_potentialHalfDeviation + lodVoxelSize;
            if (signedDistance > potentialHalfDeviation)
                return 1f;
            else if (signedDistance < -potentialHalfDeviation)
                return -1f;

            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                var halfDeviationRatio = (float)macroModulator.GetValue(
                    position.X * m_deviationFrequency,
                    position.Y * m_deviationFrequency,
                    position.Z * m_deviationFrequency);
                signedDistance -= halfDeviationRatio * m_halfDeviation;
            }

            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                signedDistance += m_detailSize * (float)detailModulator.GetValue(
                    position.X * m_detailFrequency,
                    position.Y * m_detailFrequency,
                    position.Z * m_detailFrequency);
            }

            return signedDistance / lodVoxelSize;
        }
コード例 #4
0
        private float SignedDistanceInternal(float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref Vector3 localPosition, float distance, float?noiseValue = null)
        {
            float signedDistance = distance - m_shapeAttributes.Radius;
            float normalizer     = m_deviationFrequency * m_shapeAttributes.Radius / distance;
            var   tmp            = localPosition * normalizer;

            float terrainValue = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);

            if (terrainValue > m_hillBlendTreshold)
            {
                float hillValue = (float)m_hillModule.GetValue(tmp.X, tmp.Y, tmp.Z);

                float blendValue = MathHelper.Saturate((terrainValue - m_hillBlendTreshold) / (m_hillAttributes.Treshold - m_hillBlendTreshold));
                signedDistance -= MathHelper.Lerp(terrainValue * m_halfDeviation, hillValue * m_hillHalfDeviation, blendValue);
            }
            else if (terrainValue < m_canyonBlendTreshold)
            {
                float blendValue = MathHelper.Saturate((terrainValue - m_canyonBlendTreshold) / (m_canyonAttributes.Treshold - m_canyonBlendTreshold));
                signedDistance -= MathHelper.Lerp(terrainValue * m_halfDeviation, -m_canyonHalfDeviation, blendValue);
            }
            else
            {
                signedDistance -= terrainValue * m_halfDeviation;
                normalizer      = m_detailFrequency * m_shapeAttributes.Radius / distance;
                tmp             = localPosition * normalizer;
                signedDistance -= m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }

            return(signedDistance / lodVoxelSize);
        }
コード例 #5
0
        public ProceduralFactionSeed SeedAt(Vector3D pos)
        {
            ulong noise = 0;

            for (var i = 0; i < 60 / m_factionShiftBase; i++)
            {
                var noiseSegment = (long)(m_factionNoise.GetValue(pos) * (1L << m_factionShiftBase));
                if (noiseSegment < 0)
                {
                    noiseSegment = 0;
                }
                if (noiseSegment >= (1L << m_factionShiftBase))
                {
                    noiseSegment = (1L << m_factionShiftBase) - 1;
                }
                noise |= (ulong)noiseSegment << (i * m_factionShiftBase);
                pos   /= 2.035;
            }
            Ob_ProceduralFaction recipe;

            if (m_database.TryGetFaction(noise, out recipe))
            {
                return(new ProceduralFactionSeed(recipe));
            }
            var rand        = new Random((int)noise);
            var nameSeed    = (ulong)rand.NextLong();
            var stationSeed = (ulong)rand.NextLong();
            var result      = new ProceduralFactionSeed(m_names.Generate(nameSeed), stationSeed);

            m_database.StoreFactionBlueprint(result);
            return(result);
        }
コード例 #6
0
        internal override float SignedDistance(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator)
        {
            Vector3 localPosition = position - m_translation;

            float distance = localPosition.Length();
            if ((m_innerRadius - lodVoxelSize) > distance)
                return -1f;
            if ((m_outerRadius + lodVoxelSize) < distance)
                return 1f;

            float halfDeviationRatio;
            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                float normalizer = m_deviationFrequency * m_radius / distance;
                var tmp = localPosition * normalizer;
                halfDeviationRatio = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }
            else
            {
                halfDeviationRatio = 0f;
            }

            float signedDistance = distance - m_radius - halfDeviationRatio * m_halfDeviation;
            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                float normalizer = m_detailFrequency * m_radius / distance;
                var tmp = localPosition * normalizer;
                signedDistance += m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }

            return signedDistance / lodVoxelSize;
        }
コード例 #7
0
        private float SignedDistanceInternal(float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref Vector3 localPosition, float distance)
        {
            float halfDeviationRatio;
            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                float normalizer = m_deviationFrequency * m_radius / distance;
                var tmp = localPosition * normalizer;
                halfDeviationRatio = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }
            else
            {
                halfDeviationRatio = 0f;
            }

            float signedDistance = distance - m_radius - halfDeviationRatio * m_halfDeviation;
            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                float normalizer = m_detailFrequency * m_radius / distance;
                var tmp = localPosition * normalizer;
                signedDistance += m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }

            return signedDistance / lodVoxelSize;
        }
コード例 #8
0
        public override void Remap(MyObjectBuilder_CubeGrid grid)
        {
            if (m_localDamage == null)
            {
                return;
            }
            var removed = new List <Vector3I>();

            for (var i = 0; i < grid.CubeBlocks.Count; i++)
            {
                var cube        = grid.CubeBlocks[i];
                var position    = Vector3D.Transform((Vector3I)cube.Min * MyDefinitionManager.Static.GetCubeSize(grid.GridSizeEnum), grid.PositionAndOrientation?.GetMatrix() ?? MatrixD.Identity);
                var localDamage = m_localDamage.GetValue(position) - DamageOffset;
                localDamage *= localDamage * localDamage;
                if (localDamage < 0.2)
                {
                    continue;
                }
                cube.IntegrityPercent = 1 - MyMath.Clamp((float)localDamage, 0, 1);
                if (cube.IntegrityPercent < 0.1)
                {
                    removed.Add(cube.Min);
                }
                else
                {
                    grid.CubeBlocks[i - removed.Count] = cube;
                }
            }
            if (removed.Count > 0)
            {
                grid.CubeBlocks.RemoveRange(grid.CubeBlocks.Count - removed.Count, removed.Count);
            }
        }
コード例 #9
0
        private float SignedDistanceInternal(float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref Vector3 localPosition, float distance)
        {
            float num;

            if (!base.m_enableModulation)
            {
                num = 0f;
            }
            else
            {
                float num3 = 0f;
                if (distance != 0f)
                {
                    num3 = 1f / distance;
                }
                float   num4   = (this.m_deviationFrequency * this.m_radius) * num3;
                Vector3 vector = localPosition * num4;
                num = (float)macroModulator.GetValue((double)vector.X, (double)vector.Y, (double)vector.Z);
            }
            float num2 = (distance - this.m_radius) - (num * this.m_halfDeviation);

            if ((base.m_enableModulation && (-base.m_detailSize < num2)) && (num2 < base.m_detailSize))
            {
                float   num5    = (this.m_detailFrequency * this.m_radius) / ((distance == 0f) ? 1f : distance);
                Vector3 vector2 = localPosition * num5;
                num2 += base.m_detailSize * ((float)detailModulator.GetValue((double)vector2.X, (double)vector2.Y, (double)vector2.Z));
            }
            return(num2 / lodVoxelSize);
        }
コード例 #10
0
        public override MyVoxelMaterialDefinition GetMaterialForPosition(ref Vector3 pos, float lodSize)
        {
            Vector3 localPosition  = pos - Shape.Center();
            float   lenghtToCenter = localPosition.Length();
            float   angleToPole    = Vector3.Dot(localPosition / lenghtToCenter, Vector3.Up);

            int   nearestMaterial = -1;
            float minDistance     = float.MaxValue;
            float noiseValue      = (float)(0.5 * m_noise.GetValue(pos.X, pos.Y, pos.Z) + 0.5);


            for (int i = 0; i < m_materialLayers.Length; ++i)
            {
                float heightStartDistance = m_materialLayers[i].HeightStartDeviation * noiseValue;
                float angleStartDistance  = m_materialLayers[i].AngleStartDeviation * noiseValue;

                float heightEndDistance = m_materialLayers[i].HeightEndDeviation * noiseValue;
                float angleEndDistance  = m_materialLayers[i].AngleEndDeviation * noiseValue;

                if (lenghtToCenter >= (m_materialLayers[i].StartHeight - lodSize - heightStartDistance) && (m_materialLayers[i].EndHeight + lodSize + heightEndDistance) >= lenghtToCenter &&
                    angleToPole > m_materialLayers[i].StartAngle - angleStartDistance && m_materialLayers[i].EndAngle + angleEndDistance > angleToPole)
                {
                    float distanceTolayer = Math.Abs(lenghtToCenter - m_materialLayers[i].StartHeight + heightStartDistance);
                    if (minDistance > distanceTolayer)
                    {
                        minDistance     = distanceTolayer;
                        nearestMaterial = i;
                    }
                }
            }
            return(nearestMaterial == -1 ? null : m_materialLayers[nearestMaterial].MaterialDefinition);
        }
コード例 #11
0
        private IEnumerable <MyTuple <Vector4I, Vector4D> > GetSpawnsIn(Vector4I cell, BoundingBoxD aabbGlobal, Func <BoundingBoxD, bool> test)
        {
            var aabb = GetNodeAABB(cell);

            if (!aabbGlobal.Intersects(aabb))
            {
                yield break;
            }
            if (test != null && !test.Invoke(aabb))
            {
                yield break;
            }

            var densityNoise = m_densityNoise.GetValue(aabb.Center) * m_depth;
            var depthDensity = (int)MyMath.Clamp((float)Math.Floor(densityNoise), 0, m_depth - 1);

            if (depthDensity <= cell.W)
            {
                var localDensity = densityNoise - depthDensity;
                // When local density is zero we just came from a subdivision which means there is a (1/8) chance.  When it is one we want 100% chance.
                var probability   = (1 + localDensity * 7) / 8.0;
                var placementRoll = MyMath.Clamp((float)(m_probabilityModule.GetValue(aabb.Center) + 1) / 2, 0, 1);
                if (placementRoll <= probability)
                {
                    yield break;
                }

                // Try to spawn in this cell.
                var placement = new Vector3(SampleNoiseNorm(m_placementNoise[0], aabb.Center),
                                            SampleNoiseNorm(m_placementNoise[1], aabb.Center),
                                            SampleNoiseNorm(m_placementNoise[2], aabb.Center));

                var warp = new Vector3(SampleNoiseNorm(m_warpNoise[0], aabb.Center),
                                       SampleNoiseNorm(m_warpNoise[1], aabb.Center),
                                       SampleNoiseNorm(m_warpNoise[2], aabb.Center));

                var localPos = Vector3.Clamp((warp * 0.25f) + (placement * 0.75f), Vector3.MinusOne, Vector3.One);

                var worldPos = aabb.Center + aabb.HalfExtents * localPos;
                yield return(MyTuple.Create(cell, new Vector4D(worldPos, densityNoise / m_depth)));
            }
            else
            {
                // Subdivide.
                for (var i = 0; i < 8; i++)
                {
                    var x = (cell.X << 1) | (i & 1);
                    var y = (cell.Y << 1) | ((i >> 1) & 1);
                    var z = (cell.Z << 1) | ((i >> 2) & 1);
                    foreach (var k in GetSpawnsIn(new Vector4I(x, y, z, cell.W + 1), aabbGlobal, test))
                    {
                        yield return(k);
                    }
                }
            }
        }
コード例 #12
0
        private float SignedDistanceInternal(float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref Vector3 localPosition, float distance)
        {
            float signedDistance = distance - m_shapeAttributes.Radius;

            Debug.Assert(m_deviationFrequency != 0f);
            float normalizer   = m_deviationFrequency * m_shapeAttributes.Radius / distance;
            var   tmp          = localPosition * normalizer;
            bool  changed      = false;
            float terrainValue = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);

            // Debug.Assert(terrainValue <= 1.0f);
            if (terrainValue > m_hillBlendTreshold)
            {
                changed = true;
                float hillValue = (float)m_hillModule.GetValue(tmp.X, tmp.Y, tmp.Z);

                if (terrainValue > m_hillAttributes.Treshold)
                {
                    signedDistance -= hillValue * m_hillHalfDeviation;
                }
                else
                {
                    float blendValue = MathHelper.Saturate((terrainValue - m_hillBlendTreshold) / (m_hillAttributes.Treshold - m_hillBlendTreshold));
                    signedDistance -= MathHelper.Lerp(terrainValue * m_halfDeviation, hillValue * m_hillHalfDeviation, blendValue);
                }
            }

            if (terrainValue < m_canyonBlendTreshold)
            {
                changed = true;
                float canoynValue = 1.0f;

                if (terrainValue < m_canyonAttributes.Treshold)
                {
                    signedDistance += canoynValue * m_canyonHalfDeviation;
                }
                else
                {
                    float blendValue = MathHelper.Saturate((terrainValue - m_canyonBlendTreshold) / (m_canyonAttributes.Treshold - m_canyonBlendTreshold));
                    signedDistance -= MathHelper.Lerp(terrainValue * m_halfDeviation, -canoynValue * m_canyonHalfDeviation, blendValue);
                }
            }

            if (changed == false)
            {
                signedDistance -= terrainValue * m_halfDeviation;
            }

            normalizer      = m_detailFrequency * m_shapeAttributes.Radius / distance;
            tmp             = localPosition * normalizer;
            signedDistance -= m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);

            return(signedDistance / lodVoxelSize);
        }
コード例 #13
0
        private MyProceduralCell GenerateObjectSeedsCell(ref Vector3I cellId)
        {
            MyProceduralCell cell = new MyProceduralCell(cellId);

            ProfilerShort.Begin("GenerateObjectSeedsCell");
            IMyModule cellDensityFunction = CalculateCellDensityFunction(ref cellId);

            if (cellDensityFunction == null)
            {
                ProfilerShort.End();
                return(null);
            }
            int cellSeed = GetCellSeed(ref cellId);

            using (MyRandom.Instance.PushSeed(cellSeed))
            {
                var random = MyRandom.Instance;

                int      index     = 0;
                Vector3I subCellId = Vector3I.Zero;
                Vector3I max       = new Vector3I(SUBCELLS - 1);
                for (var iter = new Vector3I.RangeIterator(ref Vector3I.Zero, ref max); iter.IsValid(); iter.GetNext(out subCellId))
                {
                    Vector3D position = new Vector3D(random.NextDouble(), random.NextDouble(), random.NextDouble());
                    position += (Vector3D)subCellId / SUBCELL_SIZE;
                    position += cellId;
                    position *= CELL_SIZE;

                    if (!MyEntities.IsInsideWorld(position))
                    {
                        continue;
                    }

                    ProfilerShort.Begin("GetValue");
                    var value = cellDensityFunction.GetValue(position.X, position.Y, position.Z);
                    ProfilerShort.End();

                    if (value < m_objectDensity) // -1..+1
                    {
                        var objectSeed = new MyObjectSeed(cell, position, GetObjectSize(random.NextDouble()));
                        objectSeed.Type  = GetSeedType(random.NextDouble());
                        objectSeed.Seed  = random.Next();
                        objectSeed.Index = index++;

                        GenerateObject(cell, objectSeed, ref index, random, cellDensityFunction);
                    }
                }
            }

            ProfilerShort.End();
            return(cell);
        }
コード例 #14
0
        HalfVector2 CalculateNoiseValuesAtPoint(IMyModule macroModulator, ref Vector3 localPos)
        {
            var   tmp        = localPos * m_deviationFrequency;
            float noiseValue = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);

            noiseValue = MathHelper.Clamp(noiseValue, -1, 1);
            Vector2 noiseValues = Vector2.Zero;

            noiseValues.X = GetValueNoiseValue(noiseValue + m_shapeAttributes.NoiseFrequency / 30.0f, false, ref tmp);
            noiseValues.Y = GetValueNoiseValue(noiseValue - m_shapeAttributes.NoiseFrequency / 30.0f, true, ref tmp);

            return(new HalfVector2(noiseValues));
        }
コード例 #15
0
 private float SignedDistanceInternal(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref float signedDistance)
 {
     if (base.m_enableModulation)
     {
         float num = (float)macroModulator.GetValue((double)(position.X * this.m_deviationFrequency), (double)(position.Y * this.m_deviationFrequency), (double)(position.Z * this.m_deviationFrequency));
         signedDistance -= num * this.m_halfDeviation;
     }
     if ((base.m_enableModulation && (-base.m_detailSize < signedDistance)) && (signedDistance < base.m_detailSize))
     {
         signedDistance += base.m_detailSize * ((float)detailModulator.GetValue((double)(position.X * this.m_detailFrequency), (double)(position.Y * this.m_detailFrequency), (double)(position.Z * this.m_detailFrequency)));
     }
     return(signedDistance / lodVoxelSize);
 }
コード例 #16
0
        protected override MyProceduralCell GenerateProceduralCell(ref VRageMath.Vector3I cellId)
        {
            MyProceduralCell cell = new MyProceduralCell(cellId, this.CELL_SIZE);

            ProfilerShort.Begin("GenerateProceduralCell");

            IMyModule densityFunctionFilled = GetCellDensityFunctionFilled(cell.BoundingVolume);

            if (densityFunctionFilled == null)
            {
                ProfilerShort.End();
                return(null);
            }
            IMyModule densityFunctionRemoved = GetCellDensityFunctionRemoved(cell.BoundingVolume);

            int cellSeed = GetCellSeed(ref cellId);
            var random   = MyRandom.Instance;

            using (random.PushSeed(cellSeed))
            {
                Vector3D position = new Vector3D(random.NextDouble(), random.NextDouble(), random.NextDouble());
                position *= (CELL_SIZE - 2 * OBJECT_SEED_RADIUS) / CELL_SIZE;
                position += OBJECT_SEED_RADIUS / CELL_SIZE;
                position += (Vector3D)cellId;
                position *= CELL_SIZE;

                if (MyEntities.IsInsideWorld(position))
                {
                    ProfilerShort.Begin("GetValue");
                    var value = densityFunctionFilled.GetValue(position.X, position.Y, position.Z);
                    ProfilerShort.End();

                    if (value < m_objectDensity) // -1..+1
                    {
                        var size       = MathHelper.Lerp(PLANET_SIZE_MIN, PLANET_SIZE_MAX, random.NextDouble());
                        var objectSeed = new MyObjectSeed(cell, position, size);
                        objectSeed.Params.Type  = MyObjectSeedType.Planet;
                        objectSeed.Params.Seed  = random.Next();
                        objectSeed.Params.Index = 0;
                        objectSeed.UserData     = new MySphereDensityFunction(position, PLANET_SIZE_MAX / 2.0 * GRAVITY_SIZE_MULTIPLIER + FALLOFF, FALLOFF);

                        int index = 1;
                        GenerateObject(cell, objectSeed, ref index, random, densityFunctionFilled, densityFunctionRemoved);
                    }
                }
            }

            ProfilerShort.End();
            return(cell);
        }
コード例 #17
0
        public Vector4I GetOctreeNodeAt(Vector3D pos)
        {
            var rootCell = new Vector4I(Vector3I.Floor(pos / m_cubeSideMax), 0);

            var cell = rootCell;

            while (true)
            {
                var aabb         = GetNodeAABB(cell);
                var densityNoise = m_densityNoise.GetValue(aabb.Center) * m_depth;
                var depthDensity = (int)MyMath.Clamp((float)Math.Floor(densityNoise), 0, m_depth - 1);
                if (depthDensity <= cell.W)
                {
                    return(cell);
                }
                else
                {
                    var nx = (cell.X << 1) + (aabb.Center.X > pos.X ? 1 : 0);
                    var ny = (cell.Y << 1) + (aabb.Center.Y > pos.Y ? 1 : 0);
                    var nz = (cell.Z << 1) + (aabb.Center.Z > pos.Z ? 1 : 0);
                    cell = new Vector4I(nx, ny, nz, cell.W + 1);
                }
            }
        }
コード例 #18
0
 private float SignedDistanceInternal(float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref Vector3 localPosition, ref float signedDistance)
 {
     if (base.m_enableModulation)
     {
         float   num    = 0.5f * this.m_deviationFrequency;
         Vector3 vector = localPosition * num;
         float   num2   = (float)macroModulator.GetValue((double)vector.X, (double)vector.Y, (double)vector.Z);
         signedDistance -= num2 * this.m_secondaryHalfDeviation;
     }
     if ((base.m_enableModulation && (-base.m_detailSize < signedDistance)) && (signedDistance < base.m_detailSize))
     {
         float   num3    = 0.5f * this.m_detailFrequency;
         Vector3 vector2 = localPosition * num3;
         signedDistance += base.m_detailSize * ((float)detailModulator.GetValue((double)vector2.X, (double)vector2.Y, (double)vector2.Z));
     }
     return(signedDistance / lodVoxelSize);
 }
コード例 #19
0
        internal override float SignedDistance(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator)
        {
            Vector3 localPosition = position - m_translation;

            float distance = localPosition.Length();

            if ((m_innerRadius - lodVoxelSize) > distance)
            {
                return(-1f);
            }
            if ((m_outerRadius + lodVoxelSize) < distance)
            {
                return(1f);
            }

            float halfDeviationRatio;

            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                float normalizer = m_deviationFrequency * m_radius / distance;
                var   tmp        = localPosition * normalizer;
                halfDeviationRatio = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }
            else
            {
                halfDeviationRatio = 0f;
            }

            float signedDistance = distance - m_radius - halfDeviationRatio * m_halfDeviation;

            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                float normalizer = m_detailFrequency * m_radius / distance;
                var   tmp        = localPosition * normalizer;
                signedDistance += m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }

            return(signedDistance / lodVoxelSize);
        }
コード例 #20
0
        private float SignedDistanceInternal(float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref Vector3 localPosition, ref float signedDistance)
        {
            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                float normalizer         = 0.5f * m_deviationFrequency;
                var   tmp                = localPosition * normalizer;
                float halfDeviationRatio = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
                signedDistance -= halfDeviationRatio * m_secondaryHalfDeviation;
            }

            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                float normalizer = 0.5f * m_detailFrequency;
                var   tmp        = localPosition * normalizer;
                signedDistance += m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }

            return(signedDistance / lodVoxelSize);
        }
コード例 #21
0
        private float SignedDistanceInternal(float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref Vector3 localPosition, ref float signedDistance)
        {
            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                float normalizer = 0.5f * m_deviationFrequency;
                var tmp = localPosition * normalizer;
                float halfDeviationRatio = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
                signedDistance -= halfDeviationRatio * m_secondaryHalfDeviation;
            }

            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                float normalizer = 0.5f * m_detailFrequency;
                var tmp = localPosition * normalizer;
                signedDistance += m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }

            return signedDistance / lodVoxelSize;
        }
コード例 #22
0
        internal override float SignedDistance(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator)
        {
            var   pa             = position - m_pointA;
            var   ba             = m_pointB - m_pointA;
            float h              = MathHelper.Clamp(pa.Dot(ref ba) / ba.LengthSquared(), 0.0f, 1.0f);
            float signedDistance = (pa - ba * h).Length() - m_radius;

            var potentialHalfDeviation = m_potentialHalfDeviation + lodVoxelSize;

            if (signedDistance > potentialHalfDeviation)
            {
                return(1f);
            }
            else if (signedDistance < -potentialHalfDeviation)
            {
                return(-1f);
            }

            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                var halfDeviationRatio = (float)macroModulator.GetValue(
                    position.X * m_deviationFrequency,
                    position.Y * m_deviationFrequency,
                    position.Z * m_deviationFrequency);
                signedDistance -= halfDeviationRatio * m_halfDeviation;
            }

            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                signedDistance += m_detailSize * (float)detailModulator.GetValue(
                    position.X * m_detailFrequency,
                    position.Y * m_detailFrequency,
                    position.Z * m_detailFrequency);
            }

            return(signedDistance / lodVoxelSize);
        }
コード例 #23
0
        internal override float SignedDistance(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator)
        {
            Vector3 localPosition = position - m_translation;

            Vector3.Transform(ref localPosition, ref m_invRotation, out localPosition);

            var primaryDistance = new Vector2(localPosition.X, localPosition.Z).Length() - m_primaryRadius;
            var signedDistance  = new Vector2(primaryDistance, localPosition.Y).Length() - m_secondaryRadius;

            var potentialHalfDeviation = m_potentialHalfDeviation + lodVoxelSize;

            if (signedDistance > potentialHalfDeviation)
            {
                return(1f);
            }
            else if (signedDistance < -potentialHalfDeviation)
            {
                return(-1f);
            }

            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                float normalizer         = 0.5f * m_deviationFrequency;
                var   tmp                = localPosition * normalizer;
                float halfDeviationRatio = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
                signedDistance -= halfDeviationRatio * m_secondaryHalfDeviation;
            }

            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                float normalizer = 0.5f * m_detailFrequency;
                var   tmp        = localPosition * normalizer;
                signedDistance += m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }

            return(signedDistance / lodVoxelSize);
        }
コード例 #24
0
        private float SignedDistanceInternal(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref float signedDistance)
        {
            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                var halfDeviationRatio = (float)macroModulator.GetValue(
                    position.X * m_deviationFrequency,
                    position.Y * m_deviationFrequency,
                    position.Z * m_deviationFrequency);
                signedDistance -= halfDeviationRatio * m_halfDeviation;
            }

            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                signedDistance += m_detailSize * (float)detailModulator.GetValue(
                    position.X * m_detailFrequency,
                    position.Y * m_detailFrequency,
                    position.Z * m_detailFrequency);
            }

            return(signedDistance / lodVoxelSize);
        }
コード例 #25
0
        private float SignedDistanceInternal(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref float signedDistance)
        {
            if (m_enableModulation)
            {
                Debug.Assert(m_deviationFrequency != 0f);
                var halfDeviationRatio = (float)macroModulator.GetValue(
                    position.X * m_deviationFrequency,
                    position.Y * m_deviationFrequency,
                    position.Z * m_deviationFrequency);
                signedDistance -= halfDeviationRatio * m_halfDeviation;
            }

            if (m_enableModulation && -m_detailSize < signedDistance && signedDistance < m_detailSize)
            {
                Debug.Assert(m_detailFrequency != 0f);
                signedDistance += m_detailSize * (float)detailModulator.GetValue(
                    position.X * m_detailFrequency,
                    position.Y * m_detailFrequency,
                    position.Z * m_detailFrequency);
            }

            return signedDistance / lodVoxelSize;
        }
コード例 #26
0
        internal override float SignedDistance(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator)
        {
            Vector3 localPosition = position - m_translation;
            float distance = localPosition.Length();
            if ((m_innerRadius - lodVoxelSize) > distance)
                return -1f;
            if ((m_outerRadius + lodVoxelSize) < distance)
                return 1f;

            float signedDistance = distance - m_shapeAttributes.Radius;

            Debug.Assert(m_deviationFrequency != 0f);
            float normalizer = m_deviationFrequency * m_shapeAttributes.Radius / distance;
            var tmp = localPosition * normalizer;

            bool changed = false;
            float terrainValue = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            // Debug.Assert(terrainValue <= 1.0f);
            if (terrainValue > m_hillBlendTreshold)
            {
                changed = true;
                float hillValue = (float)m_hillModule.GetValue(tmp.X, tmp.Y, tmp.Z);

                if (terrainValue > m_hillAttributes.Treshold)
                {
                    signedDistance -= hillValue * m_hillHalfDeviation;
                }
                else
                {
                    float normalValue = (float)m_normalModule.GetValue(tmp.X, tmp.Y, tmp.Z);
                    float blendValue = MathHelper.Saturate((terrainValue - m_hillBlendTreshold) / (m_hillAttributes.Treshold - m_hillBlendTreshold));
                    signedDistance -= MathHelper.Lerp(normalValue * m_halfDeviation, hillValue * m_hillHalfDeviation, blendValue);
                }
            } 
           
            if (terrainValue < m_canyonBlendTreshold)
            {
                changed = true;
                float canoynValue = (float)m_canyonModule.GetValue(tmp.X, tmp.Y, tmp.Z);

                if (terrainValue < m_canyonAttributes.Treshold)
                {
                    signedDistance += canoynValue * m_canyonHalfDeviation;
                }
                else
                {
                    float normalValue = (float)m_normalModule.GetValue(tmp.X, tmp.Y, tmp.Z);
                    float blendValue = MathHelper.Saturate((terrainValue - m_canyonBlendTreshold) / (m_canyonAttributes.Treshold - m_canyonBlendTreshold));
                    signedDistance -= MathHelper.Lerp(normalValue * m_halfDeviation, -canoynValue * m_canyonHalfDeviation, blendValue);
                }
            }

            if(changed == false)
            {
                float normalValue = (float)m_normalModule.GetValue(tmp.X, tmp.Y, tmp.Z);
                signedDistance -= normalValue * m_halfDeviation;
            }

            return signedDistance / lodVoxelSize;
        }
コード例 #27
0
        private void GenerateObject(MyProceduralCell cell, MyObjectSeed objectSeed, ref int index, MyRandom random, IMyModule densityFunctionFilled, IMyModule densityFunctionRemoved)
        {
            cell.AddObject(objectSeed);

            IMyAsteroidFieldDensityFunction func = objectSeed.UserData as IMyAsteroidFieldDensityFunction;
            if (func != null)
            {
                ChildrenAddDensityFunctionRemoved(func);
            }

            switch (objectSeed.Type)
            {
                case MyObjectSeedType.Moon:
                    break;
                case MyObjectSeedType.Planet:
                    m_tmpClusterBoxes.Add(objectSeed.BoundingVolume);

                    for (int i = 0; i < MOONS_MAX; ++i)
                    {
                        var direction = GetRandomDirection(random);
                        var size = MathHelper.Lerp(MOON_SIZE_MIN, MOON_SIZE_MAX, random.NextDouble());
                        var distance = MathHelper.Lerp(MOON_DISTANCE_MIN, MOON_DISTANCE_MAX, random.NextDouble());
                        var position = objectSeed.BoundingVolume.Center + direction * (size + objectSeed.BoundingVolume.HalfExtents.Length() * 2 + distance);

                        ProfilerShort.Begin("GetValue");
                        var value = densityFunctionFilled.GetValue(position.X, position.Y, position.Z);
                        ProfilerShort.End();

                        if (value < MOON_DENSITY) // -1..+1
                        {
                            var clusterObjectSeed = new MyObjectSeed(cell, position, size);
                            clusterObjectSeed.Seed = random.Next();
                            clusterObjectSeed.Type = MyObjectSeedType.Moon;
                            clusterObjectSeed.Index = index++;
                            clusterObjectSeed.UserData = new MySphereDensityFunction(position, OBJECT_SEED_RADIUS, OBJECT_SEED_RADIUS);

                            bool overlaps = false;
                            foreach (var box in m_tmpClusterBoxes)
                            {
                                if (overlaps |= clusterObjectSeed.BoundingVolume.Intersects(box))
                                {
                                    break;
                                }
                            }

                            if (!overlaps)
                            {
                                m_tmpClusterBoxes.Add(clusterObjectSeed.BoundingVolume);
                                GenerateObject(cell, clusterObjectSeed, ref index, random, densityFunctionFilled, densityFunctionRemoved);
                            }
                        }
                    }
                    m_tmpClusterBoxes.Clear();
                    break;
                case MyObjectSeedType.Empty:
                    break;
                default:
                    throw new InvalidBranchException();
                    break;
            }
        }
コード例 #28
0
        internal override float SignedDistance(ref Vector3 position, float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator)
        {
            Vector3 localPosition = position - m_translation;
            float   distance      = localPosition.Length();

            if ((m_innerRadius - lodVoxelSize) > distance)
            {
                return(-1f);
            }
            if ((m_outerRadius + lodVoxelSize) < distance)
            {
                return(1f);
            }

            float signedDistance = distance - m_shapeAttributes.Radius;

            Debug.Assert(m_deviationFrequency != 0f);
            float normalizer = m_deviationFrequency * m_shapeAttributes.Radius / distance;
            var   tmp        = localPosition * normalizer;

            bool  changed      = false;
            float terrainValue = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);

            // Debug.Assert(terrainValue <= 1.0f);
            if (terrainValue > m_hillBlendTreshold)
            {
                changed = true;
                float hillValue = (float)m_hillModule.GetValue(tmp.X, tmp.Y, tmp.Z);

                if (terrainValue > m_hillAttributes.Treshold)
                {
                    signedDistance -= hillValue * m_hillHalfDeviation;
                }
                else
                {
                    float normalValue = (float)m_normalModule.GetValue(tmp.X, tmp.Y, tmp.Z);
                    float blendValue  = MathHelper.Saturate((terrainValue - m_hillBlendTreshold) / (m_hillAttributes.Treshold - m_hillBlendTreshold));
                    signedDistance -= MathHelper.Lerp(normalValue * m_halfDeviation, hillValue * m_hillHalfDeviation, blendValue);
                }
            }

            if (terrainValue < m_canyonBlendTreshold)
            {
                changed = true;
                float canoynValue = (float)m_canyonModule.GetValue(tmp.X, tmp.Y, tmp.Z);

                if (terrainValue < m_canyonAttributes.Treshold)
                {
                    signedDistance += canoynValue * m_canyonHalfDeviation;
                }
                else
                {
                    float normalValue = (float)m_normalModule.GetValue(tmp.X, tmp.Y, tmp.Z);
                    float blendValue  = MathHelper.Saturate((terrainValue - m_canyonBlendTreshold) / (m_canyonAttributes.Treshold - m_canyonBlendTreshold));
                    signedDistance -= MathHelper.Lerp(normalValue * m_halfDeviation, -canoynValue * m_canyonHalfDeviation, blendValue);
                }
            }

            if (changed == false)
            {
                float normalValue = (float)m_normalModule.GetValue(tmp.X, tmp.Y, tmp.Z);
                signedDistance -= normalValue * m_halfDeviation;
            }

            return(signedDistance / lodVoxelSize);
        }
コード例 #29
0
        protected override MyProceduralCell GenerateProceduralCell(ref VRageMath.Vector3I cellId)
        {
            MyProceduralCell cell = new MyProceduralCell(cellId, this);

            ProfilerShort.Begin("GenerateObjectSeedsCell");

            IMyModule densityFunctionFilled = GetCellDensityFunctionFilled(cell.BoundingVolume);

            if (densityFunctionFilled == null)
            {
                ProfilerShort.End();
                return(null);
            }
            IMyModule densityFunctionRemoved = GetCellDensityFunctionRemoved(cell.BoundingVolume);

            int cellSeed = GetCellSeed(ref cellId);
            var random   = MyRandom.Instance;

            using (random.PushSeed(cellSeed))
            {
                int      index     = 0;
                Vector3I subCellId = Vector3I.Zero;
                Vector3I max       = new Vector3I(SUBCELLS - 1);
                for (var iter = new Vector3I.RangeIterator(ref Vector3I.Zero, ref max); iter.IsValid(); iter.GetNext(out subCellId))
                {
                    // there is a bug in the position calculation which can very rarely cause overlaping objects but backwards compatibility so meh
                    Vector3D position = new Vector3D(random.NextDouble(), random.NextDouble(), random.NextDouble());
                    position += (Vector3D)subCellId / SUBCELL_SIZE;
                    position += cellId;
                    position *= CELL_SIZE;

                    if (!MyEntities.IsInsideWorld(position))
                    {
                        continue;
                    }

                    ProfilerShort.Begin("Density functions");
                    double valueRemoved = -1;
                    if (densityFunctionRemoved != null)
                    {
                        valueRemoved = densityFunctionRemoved.GetValue(position.X, position.Y, position.Z);

                        if (valueRemoved <= -1)
                        {
                            ProfilerShort.End();
                            continue;
                        }
                    }

                    var valueFilled = densityFunctionFilled.GetValue(position.X, position.Y, position.Z);

                    if (densityFunctionRemoved != null)
                    {
                        if (valueRemoved < valueFilled)
                        {
                            ProfilerShort.End();
                            continue;
                        }
                    }
                    ProfilerShort.End();

                    if (valueFilled < m_objectDensity) // -1..+1
                    {
                        var objectSeed = new MyObjectSeed(cell, position, GetObjectSize(random.NextDouble()));
                        objectSeed.Type  = GetSeedType(random.NextDouble());
                        objectSeed.Seed  = random.Next();
                        objectSeed.Index = index++;

                        GenerateObject(cell, objectSeed, ref index, random, densityFunctionFilled, densityFunctionRemoved);
                    }
                }
            }

            ProfilerShort.End();
            return(cell);
        }
コード例 #30
0
        private void GenerateObject(MyProceduralCell cell, MyObjectSeed objectSeed, ref int index, MyRandom random, IMyModule densityFunctionFilled, IMyModule densityFunctionRemoved)
        {
            cell.AddObject(objectSeed);
            switch (objectSeed.Type)
            {
            case MyObjectSeedType.Asteroid:
                break;

            case MyObjectSeedType.AsteroidCluster:
                objectSeed.Type = MyObjectSeedType.Asteroid;

                m_tmpClusterBoxes.Add(objectSeed.BoundingVolume);

                for (int i = 0; i < OBJECT_MAX_IN_CLUSTER; ++i)
                {
                    var direction             = MyProceduralWorldGenerator.GetRandomDirection(random);
                    var size                  = GetClusterObjectSize(random.NextDouble());
                    var distance              = MathHelper.Lerp(OBJECT_MIN_DISTANCE_CLUSTER, OBJECT_MAX_DISTANCE_CLUSTER, random.NextDouble());
                    var clusterObjectPosition = objectSeed.BoundingVolume.Center + direction * (size + objectSeed.BoundingVolume.HalfExtents.Length() * 2 + distance);

                    ProfilerShort.Begin("Density functions");
                    double valueRemoved = -1;
                    if (densityFunctionRemoved != null)
                    {
                        valueRemoved = densityFunctionRemoved.GetValue(clusterObjectPosition.X, clusterObjectPosition.Y, clusterObjectPosition.Z);

                        if (valueRemoved <= -1)
                        {
                            ProfilerShort.End();
                            continue;
                        }
                    }

                    var valueFilled = densityFunctionFilled.GetValue(clusterObjectPosition.X, clusterObjectPosition.Y, clusterObjectPosition.Z);

                    if (densityFunctionRemoved != null)
                    {
                        if (valueRemoved < valueFilled)
                        {
                            ProfilerShort.End();
                            continue;
                        }
                    }
                    ProfilerShort.End();

                    if (valueFilled < OBJECT_DENSITY_CLUSTER)     // -1..+1
                    {
                        var clusterObjectSeed = new MyObjectSeed(cell, clusterObjectPosition, size);
                        clusterObjectSeed.Seed  = random.Next();
                        clusterObjectSeed.Index = index++;
                        clusterObjectSeed.Type  = GetClusterSeedType(random.NextDouble());

                        bool overlaps = false;
                        foreach (var box in m_tmpClusterBoxes)
                        {
                            if (overlaps |= clusterObjectSeed.BoundingVolume.Intersects(box))
                            {
                                break;
                            }
                        }

                        if (!overlaps)
                        {
                            m_tmpClusterBoxes.Add(clusterObjectSeed.BoundingVolume);
                            GenerateObject(cell, clusterObjectSeed, ref index, random, densityFunctionFilled, densityFunctionRemoved);
                        }
                    }
                }
                m_tmpClusterBoxes.Clear();
                break;

            case MyObjectSeedType.EncounterAlone:
            case MyObjectSeedType.EncounterSingle:
            case MyObjectSeedType.EncounterMulti:
                break;

            default:
                throw new InvalidBranchException();
                break;
            }
        }
コード例 #31
0
        private float SignedDistanceInternal(float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref Vector3 localPosition, float distance)
        {
            float signedDistance = distance - m_shapeAttributes.Radius;

            Debug.Assert(m_deviationFrequency != 0f);
            float normalizer = m_deviationFrequency * m_shapeAttributes.Radius / distance;
            var tmp = localPosition * normalizer;
            bool changed = false;
            float terrainValue = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            // Debug.Assert(terrainValue <= 1.0f);
            if (terrainValue > m_hillBlendTreshold)
            {
                changed = true;
                float hillValue = (float)m_hillModule.GetValue(tmp.X, tmp.Y, tmp.Z);

                if (terrainValue > m_hillAttributes.Treshold)
                {
                    signedDistance -= hillValue * m_hillHalfDeviation;
                }
                else
                {                  
                    float blendValue = MathHelper.Saturate((terrainValue - m_hillBlendTreshold) / (m_hillAttributes.Treshold - m_hillBlendTreshold));
                    signedDistance -= MathHelper.Lerp(terrainValue * m_halfDeviation, hillValue * m_hillHalfDeviation, blendValue);
                }
            }

            if (terrainValue < m_canyonBlendTreshold)
            {
                changed = true;
                float canoynValue = 1.0f;

                if (terrainValue < m_canyonAttributes.Treshold)
                {
                    signedDistance += canoynValue * m_canyonHalfDeviation;
                }
                else
                {
                    float blendValue = MathHelper.Saturate((terrainValue - m_canyonBlendTreshold) / (m_canyonAttributes.Treshold - m_canyonBlendTreshold));
                    signedDistance -= MathHelper.Lerp(terrainValue * m_halfDeviation, -canoynValue * m_canyonHalfDeviation, blendValue);
                }
            }

            if (changed == false)
            { 
                signedDistance -= terrainValue * m_halfDeviation;
            }

            normalizer = m_detailFrequency * m_shapeAttributes.Radius / distance;
            tmp = localPosition * normalizer;
            signedDistance -= m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);

            return signedDistance / lodVoxelSize;
        }
コード例 #32
0
        private static float SampleNoiseNorm(IMyModule src, Vector3D pos)
        {
            var nn = (float)src.GetValue(pos);

            return(MyMath.Clamp(nn * Math.Abs(nn), -1, 1));
        }
コード例 #33
0
 public double GetValue(double x)
 {
     return(noise.GetValue(x));
 }
コード例 #34
0
        HalfVector2 CalculateNoiseValuesAtPoint(IMyModule macroModulator, ref Vector3 localPos)
        {       
            var tmp = localPos * m_deviationFrequency;
            float noiseValue = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            noiseValue = MathHelper.Clamp(noiseValue, -1, 1);
            Vector2 noiseValues = Vector2.Zero;

            noiseValues.X = GetValueNoiseValue(noiseValue + m_shapeAttributes.NoiseFrequency/30.0f, false, ref tmp);
            noiseValues.Y = GetValueNoiseValue(noiseValue - m_shapeAttributes.NoiseFrequency/30.0f, true,ref tmp);

            return new HalfVector2(noiseValues);
        }
コード例 #35
0
        public override IEnumerable <ProceduralObject> Generate(BoundingSphereD include, BoundingSphereD?exclude)
        {
            var root        = Vector3D.Transform(include.Center, m_invTransform);
            var excludeRoot = exclude.HasValue
                ? Vector3D.Transform(exclude.Value.Center, m_invTransform)
                : default(Vector3D);

            var minLocal = root - include.Radius;
            var maxLocal = root + include.Radius;

            minLocal = Vector3D.Max(minLocal, Shape.RelevantArea.Min);
            maxLocal = Vector3D.Min(maxLocal, Shape.RelevantArea.Max);
            for (var i = 0; i < m_layers.Length; i++)
            {
                var layer = m_layers[i];
                var includePaddedSquared = include.Radius + layer.AsteroidSpacing * 2;
                includePaddedSquared *= includePaddedSquared;
                var excludePaddedSquared = exclude.HasValue ? exclude.Value.Radius - layer.AsteroidSpacing * 2 : 0;
                excludePaddedSquared *= excludePaddedSquared;

                var minPos = Vector3I.Floor(minLocal / layer.AsteroidSpacing);
                var maxPos = Vector3I.Ceiling(maxLocal / layer.AsteroidSpacing);
                for (var itr = new Vector3I_RangeIterator(ref minPos, ref maxPos); itr.IsValid(); itr.MoveNext())
                {
                    var seed     = new Vector4I(itr.Current.X, itr.Current.Y, itr.Current.Z, i);
                    var localPos = ((Vector3D)itr.Current + 0.5) * layer.AsteroidSpacing;

                    // Very quick, include/exclude.
                    if (Vector3D.DistanceSquared(root, localPos) > includePaddedSquared)
                    {
                        continue;
                    }
                    if (exclude.HasValue && Vector3D.DistanceSquared(excludeRoot, localPos) < excludePaddedSquared)
                    {
                        continue;
                    }
                    var localWeight = Shape.Weight(localPos) + layer.UsableRegion - 1;
                    if (1 - layer.AsteroidDensity > localWeight)
                    {
                        continue;
                    }

                    var densityNoise = Math.Abs(m_noise.GetValue(localPos + Math.PI)) * localWeight;
                    if (1 - layer.AsteroidDensity > densityNoise)
                    {
                        continue;
                    }

                    localPos.X += 0.45 * m_noise.GetValue(localPos) * layer.AsteroidSpacing;
                    localPos.Y += 0.45 * m_noise.GetValue(localPos) * layer.AsteroidSpacing;
                    localPos.Z += 0.45 * m_noise.GetValue(localPos) * layer.AsteroidSpacing;

                    localPos.X += 0.35 * Shape.WarpSize.X * m_noiseWarp.GetValue(localPos);
                    localPos.Y += 0.35 * Shape.WarpSize.Y * m_noiseWarp.GetValue(localPos);
                    localPos.Z += 0.05 * Shape.WarpSize.Z * m_noiseWarp.GetValue(localPos);

                    var worldPos = Vector3D.Transform(localPos, m_transform);
                    ProceduralAsteroid procAst;
                    if (!m_asteroids.TryGetValue(seed, out procAst))
                    {
                        var size = m_noise.GetValue(worldPos) * (layer.AsteroidMaxSize - layer.AsteroidMinSize) +
                                   layer.AsteroidMinSize;
                        m_asteroids[seed] = procAst = new ProceduralAsteroid(this, seed, worldPos, size, m_layers[i]);
                    }

                    procAst.SpawnIfNeeded((procAst.m_boundingBox.Center - include.Center).LengthSquared());
                    yield return(procAst);
                }
            }
        }
コード例 #36
0
ファイル: MySinus.cs プロジェクト: carlosmaid/SEModUtils
 public double GetValue(double x)
 {
     return(Math.Sin(module.GetValue(x) * Math.PI));
 }
コード例 #37
0
        private float SignedDistanceInternal(float lodVoxelSize, IMyModule macroModulator, IMyModule detailModulator, ref Vector3 localPosition, float distance,float? noiseValue=null)
        {
            float signedDistance = distance - m_shapeAttributes.Radius;
            float normalizer = m_deviationFrequency * m_shapeAttributes.Radius / distance;
            var tmp = localPosition * normalizer;

            float terrainValue = (float)macroModulator.GetValue(tmp.X, tmp.Y, tmp.Z);

            if (terrainValue > m_hillBlendTreshold)
            {
                float hillValue = (float)m_hillModule.GetValue(tmp.X, tmp.Y, tmp.Z);

                float blendValue = MathHelper.Saturate((terrainValue - m_hillBlendTreshold) / (m_hillAttributes.Treshold - m_hillBlendTreshold));
                signedDistance -= MathHelper.Lerp(terrainValue * m_halfDeviation, hillValue * m_hillHalfDeviation, blendValue);
            }
            else if (terrainValue < m_canyonBlendTreshold)
            {
                float blendValue = MathHelper.Saturate((terrainValue - m_canyonBlendTreshold) / (m_canyonAttributes.Treshold - m_canyonBlendTreshold));
                signedDistance -= MathHelper.Lerp(terrainValue * m_halfDeviation, -m_canyonHalfDeviation, blendValue);
            }
            else 
            {
                signedDistance -= terrainValue * m_halfDeviation;
                normalizer = m_detailFrequency * m_shapeAttributes.Radius / distance;
                tmp = localPosition * normalizer;
                signedDistance -= m_detailSize * (float)detailModulator.GetValue(tmp.X, tmp.Y, tmp.Z);
            }

            return signedDistance / lodVoxelSize;
        }
コード例 #38
0
 public double GetValue(ref VRageMath.Vector3D position)
 {
     return(noise.GetValue(position.X, position.Y, position.Z));
 }
コード例 #39
0
        private void GenerateObject(MyProceduralCell cell, MyObjectSeed objectSeed, ref int index, MyRandom random, IMyModule densityFunction)
        {
            cell.AddObject(objectSeed);
            switch (objectSeed.Type)
            {
                case MyObjectSeedType.Asteroid:
                    m_asteroidSeedCount++;
                    break;
                case MyObjectSeedType.AsteroidCluster:
                    objectSeed.Type = MyObjectSeedType.Asteroid;
                    m_asteroidSeedCount++;

                    m_tmpClusterBoxes.Add(objectSeed.BoundingVolume);

                    for (int j = 0; j < OBJECT_MAX_IN_CLUSTER; ++j)
                    {
                        var direction = GetRandomDirection(random);
                        var size = GetClusterObjectSize(random.NextDouble());
                        var distance = MathHelper.Lerp(OBJECT_MIN_DISTANCE_CLUSTER, OBJECT_MAX_DISTANCE_CLUSTER, random.NextDouble());
                        var clusterObjectPosition = objectSeed.BoundingVolume.Center + direction * (size + objectSeed.BoundingVolume.HalfExtents.Length() * 2 + distance);

                        ProfilerShort.Begin("GetValue");
                        var value = densityFunction.GetValue(clusterObjectPosition.X, clusterObjectPosition.Y, clusterObjectPosition.Z);
                        ProfilerShort.End();

                        if (value < OBJECT_DENSITY_CLUSTER) // -1..+1
                        {
                            var clusterObjectSeed = new MyObjectSeed(cell, clusterObjectPosition, size);
                            clusterObjectSeed.Seed = random.Next();
                            clusterObjectSeed.Index = index++;
                            clusterObjectSeed.Type = GetClusterSeedType(random.NextDouble());

                            bool overlaps = false;
                            foreach (var box in m_tmpClusterBoxes)
                            {
                                if (overlaps |= clusterObjectSeed.BoundingVolume.Intersects(box))
                                {
                                    break;
                                }
                            }

                            if (!overlaps)
                            {
                                m_tmpClusterBoxes.Add(clusterObjectSeed.BoundingVolume);
                                GenerateObject(cell, clusterObjectSeed, ref index, random, densityFunction);
                            }
                        }
                    }
                    m_tmpClusterBoxes.Clear();
                    break;
                case MyObjectSeedType.EncounterAlone:
                case MyObjectSeedType.EncounterSingle:
                case MyObjectSeedType.EncounterMulti:
                    m_encounterSeedCount++;
                    break;
                default:
                    throw new InvalidBranchException();
                    break;
            }
        }
コード例 #40
0
 public static double GetValue(this IMyModule module, Vector2 pos)
 {
     return(module.GetValue(pos.X, pos.Y));
 }
コード例 #41
0
        private void GenerateObject(MyProceduralCell cell, MyObjectSeed objectSeed, ref int index, MyRandom random, IMyModule densityFunctionFilled, IMyModule densityFunctionRemoved)
        {
            cell.AddObject(objectSeed);

            IMyAsteroidFieldDensityFunction func = objectSeed.UserData as IMyAsteroidFieldDensityFunction;

            if (func != null)
            {
                ChildrenAddDensityFunctionRemoved(func);
            }

            switch (objectSeed.Params.Type)
            {
            case MyObjectSeedType.Moon:
                break;

            case MyObjectSeedType.Planet:
                m_tmpClusterBoxes.Add(objectSeed.BoundingVolume);

                for (int i = 0; i < MOONS_MAX; ++i)
                {
                    var direction = MyProceduralWorldGenerator.GetRandomDirection(random);
                    var size      = MathHelper.Lerp(MOON_SIZE_MIN, MOON_SIZE_MAX, random.NextDouble());
                    var distance  = MathHelper.Lerp(MOON_DISTANCE_MIN, MOON_DISTANCE_MAX, random.NextDouble());
                    var position  = objectSeed.BoundingVolume.Center + direction * (size + objectSeed.BoundingVolume.HalfExtents.Length() * 2 + distance);

                    ProfilerShort.Begin("GetValue");
                    var value = densityFunctionFilled.GetValue(position.X, position.Y, position.Z);
                    ProfilerShort.End();

                    if (value < MOON_DENSITY)     // -1..+1
                    {
                        var clusterObjectSeed = new MyObjectSeed(cell, position, size);
                        clusterObjectSeed.Params.Seed  = random.Next();
                        clusterObjectSeed.Params.Type  = MyObjectSeedType.Moon;
                        clusterObjectSeed.Params.Index = index++;
                        clusterObjectSeed.UserData     = new MySphereDensityFunction(position, MOON_SIZE_MAX / 2.0 * GRAVITY_SIZE_MULTIPLIER + FALLOFF, FALLOFF);

                        bool overlaps = false;
                        foreach (var box in m_tmpClusterBoxes)
                        {
                            if (overlaps |= clusterObjectSeed.BoundingVolume.Intersects(box))
                            {
                                break;
                            }
                        }

                        if (!overlaps)
                        {
                            m_tmpClusterBoxes.Add(clusterObjectSeed.BoundingVolume);
                            GenerateObject(cell, clusterObjectSeed, ref index, random, densityFunctionFilled, densityFunctionRemoved);
                        }
                    }
                }
                m_tmpClusterBoxes.Clear();
                break;

            case MyObjectSeedType.Empty:
                break;

            default:
                throw new InvalidBranchException();
                break;
            }
        }