//Get wind vector at a single position
        public Vector3 GetWind(Vector3 position)
        {
            //create and set result buffer
            if (wind != null)
            {
                wind.Release();
            }
            wind = new ComputeBuffer(1, sizeof(float) * 3);
            getWindCompute.SetBuffer(getWindKernelSinglePos, "Wind", wind);

            //set other variables
            getWindCompute.SetTexture(getWindKernelSinglePos, "windFieldStatic", windField.GetStaticWindField());
            getWindCompute.SetTexture(getWindKernelSinglePos, "windFieldDynamic", windField.GetDynamicWindField());
            getWindCompute.SetTexture(getWindKernelSinglePos, "windFieldNoise", windField.GetNoiseWindField());
            Vector3 globalWind = windField.GetGlobalWind();

            getWindCompute.SetFloats("globalWind", new float[3] {
                globalWind.x, globalWind.y, globalWind.z
            });
            getWindCompute.SetFloat("windFieldCellSize", windField.GetCellSize());
            Vector3 leastCorner = windField.LeastCorner;

            getWindCompute.SetFloats("windFieldStart", new float[3] {
                leastCorner.x, leastCorner.y, leastCorner.z
            });
            getWindCompute.SetFloats("samplePosition", new float[3] {
                position.x, position.y, position.z
            });

            //dispatch shader
            getWindCompute.Dispatch(getWindKernelSinglePos, 1, 1, 1);

            //need to send buffer data to an array in order to return a Vector3
            Vector3[] windArr = new Vector3[1];
            wind.GetData(windArr);
            return(windArr[0]);
        }
Пример #2
0
        private void UpdateWindFieldBuffer()
        {
            Vector3Int windFieldNumCells = windField.GetNumCells();

            if (windField1DBuffer != null)
            {
                windField1DBuffer.Release();
            }
            windField1DBuffer = new ComputeBuffer(windFieldNumCells.x * windFieldNumCells.y * windFieldNumCells.z, bufferStride);
            windFieldTo1DCompute.SetBuffer(kernel, "Result", windField1DBuffer);

            windFieldTo1DCompute.SetTexture(kernel, "windFieldStatic", windField.GetStaticWindField());
            windFieldTo1DCompute.SetTexture(kernel, "windFieldDynamic", windField.GetDynamicWindField());
            windFieldTo1DCompute.SetTexture(kernel, "windFieldNoise", windField.GetNoiseWindField());
            Vector3 globalWind = windField.GetGlobalWind();

            windFieldTo1DCompute.SetFloats("globalWind", new float[3] {
                globalWind.x, globalWind.y, globalWind.z
            });
            windFieldTo1DCompute.SetInt("numCellsX", windFieldNumCells.x);
            windFieldTo1DCompute.SetInt("numCellsY", windFieldNumCells.y);
            windFieldTo1DCompute.SetFloat("cellSize", windField.GetCellSize());
            Vector3 leastCorner = windField.LeastCorner;

            windFieldTo1DCompute.SetFloats("leastCorner", new float[3] {
                leastCorner.x, leastCorner.y, leastCorner.z
            });

            /*
             * int[] numGroups = new int[3]
             * {
             *  Mathf.Max(1, Mathf.CeilToInt(windFieldNumCells.x / (float)groupSizes[0])),
             *  Mathf.Max(1, Mathf.CeilToInt(windFieldNumCells.y / (float)groupSizes[1])),
             *  Mathf.Max(1, Mathf.CeilToInt(windFieldNumCells.z / (float)groupSizes[2]))
             * };
             */

            int numGroupsX = Mathf.Max(1, Mathf.CeilToInt((windFieldNumCells.x * windFieldNumCells.y * windFieldNumCells.z) / 64));

            //windFieldTo1DCompute.Dispatch(kernel, numGroups[0], numGroups[1], numGroups[2]);
            windFieldTo1DCompute.Dispatch(kernel, numGroupsX, 1, 1);
        }