public void Update_MultipleNoisesInRadiusOfListener_ListenerNotifiedForMaximumAmount() { var noiseData = new NoiseData { NoiseLocation = new Vector3(120.0f, 20.0f, 3.0f), NoiseType = ENoiseType.Talking, NoiseRadius = 1.0f }; for (var i = 0; i < NoiseServiceConstants.NoisesPerUpdate + NoiseServiceConstants.NoisesPerUpdate; i++) { _noise.RecordNoise(noiseData); } _noise.RegisterListener(_listener, noiseData.NoiseLocation); _noise.TestUpdate(); Assert.AreEqual(NoiseServiceConstants.NoisesPerUpdate, _listener.NoiseHeardCount); // Test other noises are eventually considered _noise.TestUpdate(); Assert.AreEqual(NoiseServiceConstants.NoisesPerUpdate + NoiseServiceConstants.NoisesPerUpdate, _listener.NoiseHeardCount); _noise.UnregisterListener(_listener); }
public static BiomeData getBiome(float x, float y, BiomeHub biomes, NoiseData selector, NoiseData secondarySelector) { float biomeTypeNum = selector.SamplePosition(x, y); float diversityNum = secondarySelector.SamplePosition(x, y); //BiomeData ret = biomes.biomes[biomes.biomes.Length-1].biome; GroupBiomeDataWeight data = biomes.biomeSelection2D[0]; for (int i = biomes.biomeSelection2D.Length - 1; i >= 0; i--) { if (biomeTypeNum >= biomes.biomeSelection2D[i].weight) { data = biomes.biomeSelection2D[i]; break; } } if (data == null) { data = biomes.biomeSelection2D[0]; } for (int i = data.biome.Length - 1; i >= 0; i--) { if (biomeTypeNum >= data.biome[i].weight) { return(data.biome[i].biome); } } return(data.biome[0].biome); }
// Init perlin noise shader without setting kernel name and kernel specific parameters static ComputeShader InitPerlinNoiseShader(int seed, NoiseData noiseData, float radius, int meshGridSize, int noiseResolution, Vector3 offset, Vector3 rotation, Vector3 textureOffset) { ComputeShader perlinNoiseShader = (ComputeShader)Resources.Load("Shaders/PerlinCS"); //offset *= noiseResolution / 10; // TODO: move this somewhere else //textureOffset *= noiseResolution; // TODO: move this somewhere else //offset *= 100; // TODO: move this somewhere else textureOffset *= noiseResolution; // TODO: move this somewhere else // Init shader perlinNoiseShader.SetFloat("Octaves", noiseData.octaves); perlinNoiseShader.SetFloat("Frequency", noiseData.frequency); perlinNoiseShader.SetFloat("Amplitude", noiseData.amplitude); perlinNoiseShader.SetFloat("Lacunarity", noiseData.lacunarity); perlinNoiseShader.SetFloat("Persistence", noiseData.persistence); perlinNoiseShader.SetFloat("MaxNoiseHeight", noiseData.maxNoiseHeight); perlinNoiseShader.SetVector("Offset", offset); perlinNoiseShader.SetVector("Rotation", rotation); perlinNoiseShader.SetVector("TextureOffset", textureOffset); perlinNoiseShader.SetFloat("Resolution", noiseResolution); perlinNoiseShader.SetFloat("Radius", radius); return(perlinNoiseShader); }
void ReactToNoise(NoiseData data) { if (data.fraction && myUnit.fraction.GetAttitude(data.fraction) != AiFraction.EAttitude.EEnemy) { return; } Vector2 toNoise = (Vector2)transform.position - data.position; if (toNoise.sqrMagnitude > hearingDistance * hearingDistance || Random.value > reactionChance) { return; } MemoryEvent ev = new MemoryEvent(); ev.velocity = data.velocity * velocityPredictionScale; ev.forward = data.velocity; ev.exactPosition = data.position; ev.lifetimeTimer.Restart(); noiseStorage.PerceiveEvent(ev); #if UNITY_EDITOR Debug.DrawRay(ev.exactPosition, Vector3.up, Color.blue, 0.5f); #endif }
public static void SpreadNoise(NoiseData data) { if (tSpreadNoise.IsReadyRestart()) { onSpreadNoise(data); } }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { double scale = new double(); double strength = new double(); double multiplier = new double(); double velocity = new double(); if (!DA.GetData(0, ref scale)) { return; } if (!DA.GetData(1, ref strength)) { return; } if (!DA.GetData(2, ref multiplier)) { return; } if (!DA.GetData(3, ref velocity)) { return; } NoiseData noiseData = new NoiseData((float)scale, (float)strength, (float)multiplier, (float)velocity); DA.SetData(0, noiseData); }
NoiseData GetRawData(int[] ints, int layer, bool solid, float flatness, int heightDrop, bool convert) { NoiseData nd = new NoiseData(width, layer, solid); for (int x = 0; x < width; x++) { float noise = OctaveNoise(x, ints, Octaves, Persistance); nd.Data[x] = noise; if (noise < nd.Min) { nd.Min = noise; } else if (noise > nd.Max) { nd.Max = noise; } } if (solid) { nd.Data = SetDataToStartFromMin(nd.Data, nd.Min); } if (convert) { for (int x = 0; x < width; x++) { nd.Data[x] = ScaleTo01(nd.Data[x], nd.Min, nd.Max) * (height - 10) * flatness - heightDrop; } } return(nd); }
/// <summary> /// Create noise bitmap with currently used parameters. /// </summary> private void CreateBitmap() { //Debug.WriteLine("Bitmap"); int stride = (Width * _pf.BitsPerPixel + 7) / 8; _data = new NoiseData() { width = Width, height = Height, stride = stride, lacunarity = Lacunarity, persistance = Persistance, scale = Scale, octaves = Octaves, offset = new System.Numerics.Vector2(OffsetX, OffsetY), turbulence = Turbulence }; if (Bitmap.PixelWidth != Width || Bitmap.PixelHeight != Height) { Bitmap = new WriteableBitmap(Width, Height, 96, 96, _pf, null); } if (_worker.IsBusy) { _worker.CancelAsync(); _worker = new BackgroundWorker() { WorkerSupportsCancellation = true }; _worker.DoWork += StartGenerate; _worker.RunWorkerCompleted += UpdateBitmap; } _worker.RunWorkerAsync(); }
private void OnTriggerStay2D(Collider2D collision) { if (!collision.attachedRigidbody) { return; } AiPerceiveUnit unit = collision.GetComponent <AiPerceiveUnit>(); if (!unit) { return; } _noiseAccumulator += collision.attachedRigidbody.velocity.magnitude * walkVelocityScale; if (_noiseAccumulator > noiseThreshold && AiSenseNoise.CanSpreadNoise()) { _noiseAccumulator = 0; NoiseData noiseData = new NoiseData(); noiseData.position = collision.transform.position; noiseData.velocity = collision.attachedRigidbody.velocity; noiseData.fraction = unit.fraction; AiSenseNoise.SpreadNoise(noiseData); } }
// Render Texture public static RenderTexture GenerateSphericalHeigtmapTex(int seed, NoiseData noiseData, float radius, int meshGridSize, int noiseResolution, Vector3 offset, Vector3 rotation, Vector3 textureOffset) { if (noiseResolution % 32 != 0) { noiseResolution = (noiseResolution - (noiseResolution % 32) + 32); // Ceil resolution to be divisible by 32 } // Init shader ComputeShader perlinNoiseShader = InitPerlinNoiseShader(seed, noiseData, radius, meshGridSize, noiseResolution, offset, rotation, textureOffset); // Create render texture RenderTexture texture = RenderTexture.GetTemporary(noiseResolution, noiseResolution, 0, RenderTextureFormat.RFloat); //RenderTexture texture = RenderTexture.GetTemporary(noiseResolution, noiseResolution, 0, RenderTextureFormat.RFloat, RenderTextureReadWrite.Linear); texture.filterMode = FilterMode.Point; texture.enableRandomWrite = true; texture.Create(); // Dispatch Shader int kernelIndex = perlinNoiseShader.FindKernel(kernelNameRenderTex); perlinNoiseShader.SetTexture(kernelIndex, "tex", texture); perlinNoiseShader.Dispatch(kernelIndex, noiseResolution / 32, noiseResolution / 32, 1); return(texture); }
public void Update(Vector3 center, float radius, NoiseData noise, float scale, Material terrainMaterial, Material waterMaterial) { if (generated) { // We load the materials to prevent null references. if (terrainMaterial == null) { //terrainMaterial = Resources.Load<Material>("Materials/StandardTerrain"); terrainMaterial = new Material(Resources.Load <Material>("Materials/StandardTerrain")); terrainMaterial.SetColor("_Sand", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f)); terrainMaterial.SetColor("_Grass", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f)); terrainMaterial.SetColor("_DarkGrass", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f)); terrainMaterial.SetColor("_Rocks", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f)); terrainMaterial.SetColor("_DarkRocks", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f)); terrainMaterial.SetColor("_Snow", new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), 1f)); } if (waterMaterial == null) { waterMaterial = Resources.Load <Material>("Materials/StandardWater"); } // We make sure the noise never get a null reference. if (noise == null) { noise = Resources.Load <NoiseData>("NoiseDatas/Standard"); } // We make sure the planet is unique. noise.RecalculateOffsets(); // Generate the GameObjects. // Terrain. planet = new GameObject(); planet.name = "Planet"; planet.transform.position = center; planet.AddComponent <MeshRenderer>().material = terrainMaterial; // Water. GameObject water = GameObject.CreatePrimitive(PrimitiveType.Sphere); water.transform.parent = planet.transform; water.GetComponent <MeshRenderer>().material = waterMaterial; water.transform.localScale *= radius * 2; // Generate Mesh. Mesh mesh = new Mesh(); mesh.Clear(); // We apply the noise to each point of the icosphere. mesh.vertices = ArrayConvertionManager.Vector3ToVector3Height(ico.GetPoints(), radius, noise);; mesh.triangles = ico.GetTriangles(); // We make sure to recalculate everything to good mesh behaviour. mesh.RecalculateTangents(); mesh.RecalculateBounds(); mesh.RecalculateNormals(); // Apply mesh to the terrain. planet.AddComponent <MeshFilter>().mesh = mesh; planet.AddComponent <MeshCollider>().sharedMesh = mesh; planet.transform.localScale *= scale; generated = false; } }
public float[,] GenerateNoiseMap(NoiseData noiseData, int res = ProceduralTerrain.TerrainResolution, Vector2 position = default(Vector2)) { int seed = noiseData.Seed; float scale = noiseData.Scale; int octaves = noiseData.Octaves; float persistance = noiseData.Persistance; float lacunarity = noiseData.Lacunarity; //Store temp heightmap data float[,] noiseMap = new float[res, res]; //Get a random modifier based on seed System.Random rand = new System.Random(seed); Vector2[] octaveOffsets = new Vector2[octaves]; float maxheight = 0; float amplitude = 1; float frequency = 1; for (int i = 0; i < octaves; i++) { float randX = rand.Next(-100000, 100000) + position.y; float randY = rand.Next(-100000, 100000) - position.x; octaveOffsets[i] = new Vector2(randX, randY); maxheight += amplitude; amplitude *= persistance; } //Generate the noisemap for (int x = 0; x < res; x++) { for (int y = 0; y < res; y++) { amplitude = 1; frequency = 1; float heightValue = 0; for (int i = 0; i < octaves; i++) { float xCoord = (x - octaveOffsets[i].x) / scale * frequency; float yCoord = (y - octaveOffsets[i].y) / scale * frequency; float perlinValue = Mathf.PerlinNoise(xCoord, yCoord); //Set height to perlin value heightValue += perlinValue * amplitude; amplitude *= persistance; frequency *= lacunarity; } //Assign value noiseMap[x, y] = heightValue; } } return(noiseMap); }
public static NoiseData CreateClone(NoiseData noiseData) { NoiseData outputNoiseData = CreateInstance <NoiseData>(); outputNoiseData.SetParameters(noiseData); return(outputNoiseData); }
public void SetParameters(NoiseData noiseData) { Seed = noiseData.Seed; Octaves = noiseData.Octaves; Persistance = noiseData.Persistance; Lacunarity = noiseData.Lacunarity; Scale = noiseData.Scale; }
//For multithreading public void RequestNoiseMap(NoiseData noiseData, int res, Vector2 position, Action <float[, ]> callback) { ThreadStart threadStart = delegate { NoiseMapDataThread(noiseData, res, position, callback); }; new Thread(threadStart).Start(); }
// Request methods private void RequestNoiseData(Vector2 inChunkCoords, Chunk inChunk) { NoiseData noiseData = new NoiseData(); Action callbackMethod = new Action(() => OnNoiseDataReceived(noiseData, inChunk)); ThreadStart threadStart = delegate { noiseData.Generate(callbackMethod, inChunkCoords, _world.worldGenData, this); }; new Thread(threadStart).Start(); }
public override void Load(InstrumentData d) { NoiseData data = d as NoiseData; base.Load(data); ID = data.ID; output.ID = data.jackOutID; speedDial.setPercent(data.dialState); }
private void RequestTexture(NoiseData inNoiseData, Chunk inChunk) { TextureData textureData = new TextureData(); Action callbackMethod = new Action(() => OnTextureReceived(textureData, inChunk)); ThreadStart threadStart = delegate { textureData.Generate(callbackMethod, inNoiseData, _world.worldGenData, this); }; new Thread(threadStart).Start(); }
private void RequestMeshData(NoiseData inNoiseData, Chunk inChunk) { MeshData meshData = new MeshData(_world.worldGenData.chunkSize); Action callbackMethod = new Action(() => OnMeshDataReceived(meshData, inChunk)); ThreadStart threadStart = delegate { meshData.Generate(callbackMethod, inNoiseData, _world.worldGenData, this); }; new Thread(threadStart).Start(); }
void NoiseMapDataThread(NoiseData noiseData, int res, Vector2 position, Action <float[, ]> callback) { float[,] noiseMap = GenerateNoiseMap(noiseData, res, position); //Lock means while thread is executing this code, no other thread can access it lock (noiseMapThreadInfoQueue) { noiseMapThreadInfoQueue.Enqueue(new NoiseMapThreadInfo <float[, ]>(callback, noiseMap)); } }
/// <summary> /// 添加噪声数据到数据库 /// </summary> /// <param name="data">噪声数据对象</param> public static int AddNoiseData(NoiseData data) { try { string sql = string.Empty; int query = 0; string strAmp = string.Empty; for (int i = 0; i < data.Amplitude.Length; i++) { if (i == data.Amplitude.Length - 1) { strAmp += data.Amplitude[i]; } else { strAmp += data.Amplitude[i] + ","; } } string strFrq = string.Empty; for (int i = 0; i < data.Frequency.Length; i++) { if (i == data.Frequency.Length - 1) { strFrq += data.Frequency[i]; } else { strFrq += data.Frequency[i] + ","; } } string strDa = string.Empty; for (int i = 0; i < data.OriginalData.Length; i++) { if (i == data.OriginalData.Length - 1) { strDa += data.OriginalData[i]; } else { strDa += data.OriginalData[i] + ","; } } sql = string.Format(@"INSERT INTO DL_Noise_Real(GroupId, RecorderId, LeakValue, FrequencyValue, OriginalData, CollTime, UnloadTime, HistoryFlag) VALUES({0},{1},'{2}','{3}','{4}','{5}','{6}',{7})", data.GroupID, data.RecorderID, strAmp, strFrq, strDa, data.ReadTime, data.UploadTime, data.UploadFlag); query = SQLHelper.ExecuteNonQuery(sql); return(query); } catch (Exception ex) { throw ex; //return -1; } }
// Callback methods private void OnNoiseDataReceived(NoiseData inNoiseData, Chunk inChunk) { inChunk.noiseData = inNoiseData; _world.chunkObjectGenerator.GenerateChunkObjects(inChunk); RequestMeshData(inNoiseData, inChunk); RequestTexture(inNoiseData, inChunk); }
//Simplex fractal noise public static float SimplexNoiseOctaves(float3 point, NoiseData noiseData) { float density = 0; for (int i = 0; i < noiseData.octaves; i++) { density += (noise.snoise(point * noiseData.scale * noiseData.mainScale * math.pow(noiseData.lacunarity, i))) * math.pow(noiseData.persistence, i); } return(density * noiseData.height); }
//Cellular fractal noise (type 2) public static float CellularNoise2Octaves(float3 point, NoiseData noiseData) { float density = 0; for (int i = 0; i < noiseData.octaves; i++) { density += (1 - noise.cellular(point * noiseData.scale * noiseData.mainScale * math.pow(noiseData.lacunarity, i)).y) * math.pow(noiseData.persistence, i); } return(density * noiseData.height); }
public void RecordNoise_SubmitsNoiseToService() { var data = new NoiseData { NoiseLocation = new Vector3(10.0f, 12.0f, 13.0f), NoiseType = ENoiseType.Talking, NoiseRadius = 200.0f }; _emitter.RecordNoise(data); Assert.AreEqual(data, _noise.RecordedNoise); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { double scale = new double(); double strength = new double(); double multiplier = new double(); double velocity = new double(); Mesh mesh = null; bool mapScale = new bool(); bool mapStrength = new bool(); bool mapMultiplier = new bool(); if (!DA.GetData(0, ref scale)) { return; } if (!DA.GetData(1, ref strength)) { return; } if (!DA.GetData(2, ref multiplier)) { return; } if (!DA.GetData(3, ref velocity)) { return; } if (!DA.GetData(4, ref mesh)) { return; } if (mesh == null || mesh.VertexColors.Count == 0) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Input mesh must have vertex colors, please check your input"); return; } if (!DA.GetData(5, ref mapScale)) { return; } if (!DA.GetData(6, ref mapStrength)) { return; } if (!DA.GetData(7, ref mapMultiplier)) { return; } NoiseData noiseData = new NoiseData((float)scale, (float)strength, (float)multiplier, (float)velocity, mesh, mapScale, mapStrength, mapMultiplier); DA.SetData(0, noiseData); }
public void Initialised_IdleTimeDoesNotPass_DoesNotSetTargetToNoiseLocation() { var expectedNoise = new NoiseData { NoiseLocation = new Vector3(2.0f, 3.0f, -1.0f) }; UnityMessageEventFunctions.InvokeMessageEventWithDispatcher(_pathfinding.gameObject, new NoiseHeardMessage(expectedNoise)); _goal.Initialise(); Assert.IsNull(_pathfinding.TargetLocation); }
public void Initialised_IdleTimePasses_SetsTargetToNoiseLocation() { var expectedNoise = new NoiseData { NoiseLocation = new Vector3(2.0f, 3.0f, -1.0f) }; UnityMessageEventFunctions.InvokeMessageEventWithDispatcher(_pathfinding.gameObject, new NoiseHeardMessage(expectedNoise)); _goal.Initialise(); _goal.Update(_params.IdleDelayOnDetection + 0.1f); Assert.AreEqual(expectedNoise.NoiseLocation, _pathfinding.TargetLocation); }
public override InstrumentData GetData() { NoiseData data = new NoiseData(); data.deviceType = menuItem.deviceType.Noise; GetTransformData(data); data.dialState = speedDial.percent; data.jackOutID = output.transform.GetInstanceID(); return(data); }
public void Desirability_Terminated_Zero() { var expectedNoise = new NoiseData { NoiseLocation = new Vector3(2.0f, 3.0f, -1.0f) }; UnityMessageEventFunctions.InvokeMessageEventWithDispatcher(_pathfinding.gameObject, new NoiseHeardMessage(expectedNoise)); _goal.CalculateDesirability(); _goal.Initialise(); _goal.Terminate(); Assert.AreEqual(0.0f, _goal.CalculateDesirability()); }
public AddNoiseEffect () { EffectData = new NoiseData (); }