public float Generate(float3 p) { var rng = new Unity.Mathematics.Random(Seed); const float offsetRange = 1000f; float noiseValue = 0f; float frequency = Scale / 100f; float amplitude = 1f; float weight = 1f; for (int i = 0; i < Octaves; i++) { float3 offset = float3((float)mul(rng.NextDouble(), 2f) - 1f, (float)mul(rng.NextDouble(), 2f) - 1f, (float)mul(rng.NextDouble(), 2f) - 1f) * offsetRange; float n = noise.snoise(p.xyz * frequency + offset + Offset); float v = 1 - abs(n); v = mul(pow(v, 2), weight); weight = max(min(v * WeightMultiplier, 1f), 0f); noiseValue += mul(v, amplitude); amplitude *= Persistence; frequency *= Lacunarity; } noiseValue = -(p.y + FloorOffset) + mul(noiseValue, Weight) + (p.y % TerraceHeight) * TerraceWeight; if (p.y < HardFloorHeight) { noiseValue += HardFloorWeight; } return(noiseValue); }
protected override void OnUpdate() { var entity = GetSingletonEntity <MarchingCubesData>(); var data = EntityManager.GetComponentData <MarchingCubesData>(entity); int pointsCount = (int)pow(data.Terrain.Resolution + 1, 3); int entityCount = query.CalculateEntityCount(); if (entityCount == 0) { return; } var entities = query.ToEntityArray(Allocator.TempJob); var chunks = GetComponentDataFromEntity <MarchingChunk>(true); var pointsArray = new NativeArray <float4> [entityCount]; NativeArray <float3> offsets = new NativeArray <float3>(data.Noise.Octaves, Allocator.TempJob); var offsetsHandle = Job.WithCode(() => { const float offsetRange = 1000; var rng = new Unity.Mathematics.Random(data.Noise.Seed); for (int i = 0; i < data.Noise.Octaves; i++) { offsets[i] = float3((float)mul(rng.NextDouble(), 2f) - 1f, (float)mul(rng.NextDouble(), 2f) - 1f, (float)mul(rng.NextDouble(), 2f) - 1f) * offsetRange; } }).Schedule(default);
void GenerateExampleSystem() { var rangen = new Unity.Mathematics.Random(_seed); objects = new List <Particle>(2 * NumObjects); objects.Add(new Particle(new double3(0.0d, 0, 0d), new double3(0.0d, 0, 0), 1.0d)); // objects.Add(new Particle(new double3(1e1d, 0d, 0d), new double3(0d, 0d, math.sqrt(G/10d)), 0.001d)); // objects.Add(new Particle(new double3(1.02e1d, 0d, 0d), new double3(0d, 0d, math.sqrt(G/10d) + math.sqrt(G*.001/.2d)), 0.0002d)); // objects.Add(new Particle(new double3(-1e1d, 0d, 0d), new double3(0d, 0d, -math.sqrt(G/10d)), 0.002d)); // objects.Add(new Particle(new double3(0d, 0d, 1.2e1d), new double3(1d, 0d, 0d), 0.0001d)); // Add some asteroids/debris while (objects.Count < NumObjects) { float angle = rangen.NextFloat(2f * math.PI); double length = (1 - math.pow(rangen.NextDouble(), 2)) * 15d + 5d; var pos = new double3(math.rotate(quaternion.RotateY(angle), new float3(1, 0, 0))) * length; var vel = new double3(-pos.z, 0, pos.x) / length * math.sqrt(G / length); var m = rangen.NextDouble(.0001d); var p = new Particle(pos, vel, m); objects.Add(p); } NumObjects = objects.Count; // Shift gameObjects so COM and total momentum is 0 var totalmass = 0d; var com = new double3(0); var velocity = new double3(0); for (int i = 0; i < NumObjects; i++) { totalmass += objects[i].mass; com += objects[i].position * objects[i].mass; velocity += objects[i].velocity * objects[i].mass; } com /= totalmass; velocity /= totalmass; for (int i = 0; i < NumObjects; i++) { objects[i] -= new Particle(com, velocity, 0); } }
/// Return a lazy sequence of samples. You typically want to call this in a foreach loop, like so: /// foreach (float2 sample in sampler.Samples()) { ... } public IEnumerable <float2> Samples(uint randomSeed) { var random = new Unity.Mathematics.Random(randomSeed); // First sample is choosen randomly if (activeSamples.Count == 0) { yield return(AddSample(new float2((float)random.NextDouble() * rectWidth, (float)random.NextDouble() * rectHeight))); } while (activeSamples.Count > 0) { // Pick a random active sample var i = (int)(random.NextDouble() * activeSamples.Count); var sample = activeSamples[i]; // Try `k` random candidates between [radius, 2 * radius] from that sample. bool found = false; for (int j = 0; j < maxAttempts; j++) { var angle = 2 * PI * random.NextFloat(); var r = Sqrt(random.NextFloat() * 3 * radius2 + radius2); // See: http://stackoverflow.com/questions/9048095/create-random-number-within-an-annulus/9048443#9048443 var candidate = sample + r * new float2(Cos(angle), Sin(angle)); // Accept candidates if it's inside the rect and farther than 2 * radius to any existing sample. if (candidate.x >= 0 && candidate.y >= 0 && candidate.x <= rectWidth && candidate.y <= rectHeight && IsFarEnough(candidate)) { found = true; yield return(AddSample(candidate)); break; } } // If we couldn't find a valid candidate after k attempts, remove this sample from the active samples queue if (!found) { activeSamples.RemoveAt(i); } } }
public void SortNativeArray_RandomFloats_ReturnSorted([Values(0, 1, 10, 1000, 10000)] int size) { var random = new Unity.Mathematics.Random(1); NativeArray <float> array = new NativeArray <float>(size, Allocator.Persistent); Assert.IsTrue(array.IsCreated); for (int i = 0; i < array.Length; i++) { array[i] = (float)random.NextDouble(); } array.Sort(); float min = float.MinValue; foreach (var i in array) { Assert.LessOrEqual(min, i); min = i; } array.Dispose(); }