protected float cellSize; //size of cells of given wind field //N.B. OnEnable() rather than Start() because wind producers need to be added to their wind field before the wind field tries to access them (which it does on Start()). //This works but the flow of it isn't clear, ESPECIALLY BECAUSE THE WIND FIELD NEEDS TO INITIALISE ITS PRODUCER LISTS BEFORE THIS TRIES TO ADD THEM (which it does in Awake()) //so I should try to find a better way to do it protected virtual void OnEnable() { if (windField == null) { throw new NullReferenceException("No wind field given for WindFieldProducer " + ToString() + "!"); } cellSize = windField.GetCellSize(); windPointsBuffer = CalcWindFieldPoints(); AddToWindField(); StartCoroutine(UpdateProducer()); }
//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]); }
//identical to drawing wind points for wind producers, but set the wind field cell size in the shader (affects wind arrow scale) protected override void DrawWindPoints(ComputeBuffer windPoints) { thisObjMaterial.SetFloat("_WindFieldCellSize", windField.GetCellSize()); base.DrawWindPoints(windPoints); }