public void FindClosestAnimalParticleToCursor(float xCoord, float yCoord) { int kernelCSMeasureInitCursorDistances = computeShaderAnimalParticles.FindKernel("CSMeasureInitCursorDistances"); computeShaderAnimalParticles.SetBuffer(kernelCSMeasureInitCursorDistances, "animalParticlesRead", animalParticlesCBuffer); computeShaderAnimalParticles.SetBuffer(kernelCSMeasureInitCursorDistances, "cursorDistancesWrite", cursorDistances1024); computeShaderAnimalParticles.SetFloat("_MouseCoordX", xCoord); computeShaderAnimalParticles.SetFloat("_MouseCoordY", yCoord); computeShaderAnimalParticles.Dispatch(kernelCSMeasureInitCursorDistances, animalParticlesCBuffer.count / 32, 1, 1); Vector4[] cursorDistanceArray1024 = new Vector4[1024]; cursorDistances1024.GetData(cursorDistanceArray1024); // Manual Sort! Vector4[] swapBuffer = cursorDistanceArray1024; swapBuffer = ReduceDistancesArray(cursorDistanceArray1024); for (int tierID = 0; tierID < 9; tierID++) { Vector4[] writeBuffer = ReduceDistancesArray(swapBuffer); swapBuffer = new Vector4[writeBuffer.Length]; for (int x = 0; x < writeBuffer.Length; x++) { swapBuffer[x] = writeBuffer[x]; } } closestZooplanktonToCursorIndex = Mathf.RoundToInt(swapBuffer[0].x); //string txt = "Closest = " + swapBuffer[0].x.ToString() + ", D: " + swapBuffer[0].y.ToString(); // + "__ " + swapBuffer[3].x.ToString() + " __ (" + cursorDistanceArray1024[1023].ToString() + ") " + cursorDistanceArray1024[511].ToString(); //Debug.Log(txt); // Now Fetch the actual particleData::::: int kernelCSFetchParticleByID = computeShaderAnimalParticles.FindKernel("CSFetchParticleByID"); computeShaderAnimalParticles.SetBuffer(kernelCSFetchParticleByID, "selectedAnimalParticleDataCBuffer", cursorClosestParticleDataCBuffer); computeShaderAnimalParticles.SetInt("_SelectedParticleID", selectedAnimalParticleIndex); computeShaderAnimalParticles.SetInt("_ClosestParticleID", Mathf.RoundToInt(swapBuffer[0].x)); computeShaderAnimalParticles.SetBuffer(kernelCSFetchParticleByID, "animalParticlesRead", animalParticlesCBuffer); computeShaderAnimalParticles.Dispatch(kernelCSFetchParticleByID, 1, 1, 1); cursorClosestParticleDataCBuffer.GetData(cursorParticleDataArray); //string txt = "nearestIndex = " + cursorParticleDataArray[0].index + " (" + cursorParticleDataArray[0].age + ") " + (cursorParticleDataArray[0].biomass * 1000f).ToString("F0"); // "lngth: " + swapBuffer.Length.ToString() + ", " + swapBuffer[0].ToString() + " " + swapBuffer[1].ToString() + " " + swapBuffer[swapBuffer.Length - 2].ToString() + " " + swapBuffer[swapBuffer.Length - 1].ToString(); //Debug.Log(txt); closestAnimalParticleData = cursorParticleDataArray[1]; selectedAnimalParticleData = cursorParticleDataArray[0]; }
public void InitializeAnimalParticles(int numAgents, ComputeShader computeShader) { //float startTime = Time.realtimeSinceStartup; //Debug.Log((Time.realtimeSinceStartup - startTime).ToString()); computeShaderAnimalParticles = computeShader; animalParticlesCBuffer = new ComputeBuffer(numAnimalParticles, GetAnimalParticleDataSize()); animalParticlesCBufferSwap = new ComputeBuffer(numAnimalParticles, GetAnimalParticleDataSize()); AnimalParticleData[] animalParticlesArray = new AnimalParticleData[numAnimalParticles]; float minParticleSize = 1f; // settingsRef.avgAnimalParticleRadius / settingsRef.animalParticleRadiusVariance; float maxParticleSize = 2f; // settingsRef.avgAnimalParticleRadius * settingsRef.animalParticleRadiusVariance; for (int i = 0; i < animalParticlesCBuffer.count; i++) { AnimalParticleData data = new AnimalParticleData(); data.index = i; data.worldPos = Vector3.zero; // new Vector3(UnityEngine.Random.Range(0f, SimulationManager._MapSize), UnityEngine.Random.Range(0f, SimulationManager._MapSize), 0f); data.radius = Random.Range(minParticleSize, maxParticleSize); // obsolete! data.biomass = 0.001f; // data.radius * data.radius * Mathf.PI; // * settingsRef.animalParticleNutrientDensity; data.isActive = 0f; data.isDecaying = 0f; data.age = 0f; // UnityEngine.Random.Range(1f, 2f); data.color = Random.ColorHSV(); data.genomeVector = Vector4.zero; data.extra0 = 0f; data.energy = 0f; animalParticlesArray[i] = data; } //Debug.Log("Fill Initial Particle Array Data CPU: " + (Time.realtimeSinceStartup - startTime).ToString()); animalParticlesCBuffer.SetData(animalParticlesArray); animalParticlesCBufferSwap.SetData(animalParticlesArray); //Debug.Log("Set Data GPU: " + (Time.realtimeSinceStartup - startTime).ToString()); /* * animalParticlesNearestCritters1024 = new RenderTexture(numAnimalParticles, numAgents, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear); * animalParticlesNearestCritters1024.wrapMode = TextureWrapMode.Clamp; * animalParticlesNearestCritters1024.filterMode = FilterMode.Point; * animalParticlesNearestCritters1024.enableRandomWrite = true; * animalParticlesNearestCritters1024.Create(); // actually creates the renderTexture -- don't forget this!!!!! *** * //Debug.Log("Create RT 1024: " + (Time.realtimeSinceStartup - startTime).ToString()); * animalParticlesNearestCritters32 = new RenderTexture(32, numAgents, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear); * animalParticlesNearestCritters32.wrapMode = TextureWrapMode.Clamp; * animalParticlesNearestCritters32.filterMode = FilterMode.Point; * animalParticlesNearestCritters32.enableRandomWrite = true; * animalParticlesNearestCritters32.Create(); // actually creates the renderTexture -- don't forget this!!!!! *** * //Debug.Log("Create RT 32: " + (Time.realtimeSinceStartup - startTime).ToString()); * animalParticlesNearestCritters1 = new RenderTexture(1, numAgents, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear); * animalParticlesNearestCritters1.wrapMode = TextureWrapMode.Clamp; * animalParticlesNearestCritters1.filterMode = FilterMode.Point; * animalParticlesNearestCritters1.enableRandomWrite = true; * animalParticlesNearestCritters1.Create(); // actually creates the renderTexture -- don't forget this!!!!! *** */ //Debug.Log("Pre Buffer Creation: " + (Time.realtimeSinceStartup - startTime).ToString()); closestAnimalParticlesDataArray = new AnimalParticleData[numAgents]; closestAnimalParticlesDataCBuffer = new ComputeBuffer(numAgents, GetAnimalParticleDataSize()); animalParticlesEatAmountsCBuffer = new ComputeBuffer(numAgents, sizeof(float) * 1); animalParticlesEatAmountsArray = new float[numAgents]; animalParticleMeasurementTotalsData = new AnimalParticleData[1]; animalParticlesMeasure32 = new ComputeBuffer(32, GetAnimalParticleDataSize()); animalParticlesMeasure1 = new ComputeBuffer(1, GetAnimalParticleDataSize()); //Debug.Log("End: " + (Time.realtimeSinceStartup - startTime).ToString()); critterNearestZooplankton32 = new RenderTexture(32, numAgents, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear); critterNearestZooplankton32.wrapMode = TextureWrapMode.Clamp; critterNearestZooplankton32.filterMode = FilterMode.Point; critterNearestZooplankton32.enableRandomWrite = true; critterNearestZooplankton32.Create(); closestZooplanktonDistancesCBuffer = new ComputeBuffer(numAgents, sizeof(float) * 4); closestZooplanktonArray = new Vector4[numAgents]; cursorClosestParticleDataCBuffer = new ComputeBuffer(2, GetAnimalParticleDataSize()); // 0 = selected, 1 = closest to cursor cursorParticleDataArray = new AnimalParticleData[2]; cursorDistances1024 = new ComputeBuffer(1024, sizeof(float) * 4); int numMutations = 4; // don't change this zooplanktonSlotGenomeCurrent = new WorldLayerZooplanktonGenome(); zooplanktonSlotGenomeCurrent.representativeData = animalParticlesArray[0]; zooplanktonSlotGenomeCurrent.swimSpeed01 = 0.5f; zooplanktonSlotGenomeCurrent.agingRate01 = 0.5f; zooplanktonSlotGenomeCurrent.attractForce01 = 0.5f; zooplanktonSlotGenomeCurrent.name = "Zooplankton, Bebe!"; zooplanktonSlotGenomeCurrent.textDescriptionMutation = "Swim Speed: " + zooplanktonSlotGenomeCurrent.swimSpeed01.ToString("F2") + "\nAging Rate: " + zooplanktonSlotGenomeCurrent.agingRate01.ToString("F2") + "\nAttraction: " + zooplanktonSlotGenomeCurrent.attractForce01.ToString("F2"); zooplanktonSlotGenomeMutations = new WorldLayerZooplanktonGenome[numMutations]; GenerateWorldLayerZooplanktonGenomeMutationOptions(); zooplanktonRepresentativeGenomeCBuffer = new ComputeBuffer(1, GetAnimalParticleDataSize()); AnimalParticleData[] zooplanktonRepresentativeGenomeArray = new AnimalParticleData[1]; zooplanktonRepresentativeGenomeArray[0] = zooplanktonSlotGenomeCurrent.representativeData; zooplanktonRepresentativeGenomeCBuffer.SetData(zooplanktonRepresentativeGenomeArray); }
public void ProcessSlotMutation() { AnimalParticleData[] zooplanktonRepresentativeGenomeArray = new AnimalParticleData[1]; zooplanktonRepresentativeGenomeArray[0] = zooplanktonSlotGenomeCurrent.representativeData; zooplanktonRepresentativeGenomeCBuffer.SetData(zooplanktonRepresentativeGenomeArray); }