protected override string initializeBuffers() { string result = null; cellBuffer1 = new FluidCell[fluidParameters.gridSize, fluidParameters.gridSize]; cellBuffer2 = new FluidCell[fluidParameters.gridSize, fluidParameters.gridSize]; externalAdditions = new FluidCell[fluidParameters.gridSize, fluidParameters.gridSize]; operationData = new FluidCellOperationData[fluidParameters.gridSize, fluidParameters.gridSize]; for (int i = 0; i < fluidParameters.gridSize; i++) { for (int j = 0; j < fluidParameters.gridSize; j++) { cellBuffer1[i, j] = new FluidCell(); cellBuffer2[i, j] = new FluidCell(); externalAdditions[i, j] = new FluidCell(); operationData[i, j] = new FluidCellOperationData(); } } inCells = cellBuffer1; outCells = cellBuffer2; return(result); }
void ShowCellData(string cellName, FluidSimulator simulator, FluidCellIndex cellIndex, ref bool showCell, bool showOperationData) { return; int gridSize = simulator.fluidParameters.gridSize; if (cellIndex.x >= 0 && cellIndex.y >= 0 && cellIndex.z >= 0 && cellIndex.x < gridSize && cellIndex.y < gridSize && cellIndex.z < gridSize) { FluidCell cell = simulator.GetCell(cellIndex); showCell = EditorGUILayout.Foldout(showCell, string.Format("{0} {1}", cellName, cellIndex.ToString())); if (showCell) { EditorGUILayout.LabelField("Density", "" + cell.density); EditorGUILayout.LabelField("Velocity", "" + cell.velocity); EditorGUILayout.LabelField("Raw Divergence", "" + cell.rawDivergence); EditorGUILayout.LabelField("Relaxed Divergence", "" + cell.relaxedDivergence); if (showOperationData) { FluidCellOperationData operationData = simulator.GetCellOperationData(cellIndex); EditorGUILayout.LabelField("Advect Index Velocity", "" + operationData.advectIdVelocity.ToString()); EditorGUILayout.LabelField("Advect Past Index", "" + operationData.advectPastId.ToString()); EditorGUILayout.LabelField("Advect SamplePercentages", "" + operationData.advectSamplePercentages); } EditorGUILayout.Space(); } } }
protected override void prepareNextFrame() { externalAdditionBuffer.SetData(initialCells); for (int j = 0; j < fluidParameters.gridSize; j++) { for (int i = 0; i < fluidParameters.gridSize; i++) { externalAdditions[j, i] = new FluidCell(); } } }
public void Initialize(FluidSimulator simulator, FluidRenderer fluidRenderer, FluidCell cellTemplate, int xIndex, int yIndex) { if (this.FluidRenderer != null) { Debug.LogError("Attempting to re-initialize FluidCellRenderer."); } this.fluidRenderer = fluidRenderer; XIndex = xIndex; YIndex = yIndex; cell = new FluidCell() { density = cellTemplate.density, velocity = cellTemplate.velocity, rawDivergence = cellTemplate.rawDivergence, relaxedDivergence = cellTemplate.relaxedDivergence, }; this.simulator = simulator; GetComponent <FluidColliderCell>().Initialize(simulator, new FluidCellIndex(xIndex, yIndex, 0)); }
protected override string initializeBuffers() { clampDataKernel = fluidComputer.FindKernel("ClampData"); applyExternalsKernel = fluidComputer.FindKernel("ApplyExternals"); diffuseKernel = fluidComputer.FindKernel("Diffuse"); advectKernel = fluidComputer.FindKernel("Advect"); computeDivergenceKernel = fluidComputer.FindKernel("ComputeDivergence"); relaxDivergenceKernel = fluidComputer.FindKernel("RelaxDivergence"); removeDivergenceKernel = fluidComputer.FindKernel("RemoveDivergence"); zeroedBoundariesKernel = fluidComputer.FindKernel("EmptyBoundaries"); // This assumes all kernels of compute shader use the same number of threads per group. threadsPerGroup = new uint[3]; fluidComputer.GetKernelThreadGroupSizes(applyExternalsKernel, out threadsPerGroup[0], out threadsPerGroup[1], out threadsPerGroup[2]); threadGroups = new int[] { (int)(fluidParameters.gridSize / threadsPerGroup[0]), (int)(fluidParameters.gridSize / threadsPerGroup[1]), (int)(fluidParameters.gridSize / threadsPerGroup[2]) }; // TODO When 3D is added this can be removed. threadGroups[2] = 1; fluidComputer.SetVector("threadsPerGroup", new Vector4(threadsPerGroup[0], threadsPerGroup[1], threadsPerGroup[2])); fluidComputer.SetVector("threadGroups", new Vector4(threadGroups[0], threadGroups[1], threadGroups[2], 1)); // TODO Move these to inspector as uneditable fields. Debug.Log("Threads per Group: " + threadsPerGroup[0] + " " + threadsPerGroup[1] + " " + threadsPerGroup[2]); Debug.Log("Thread Groups: " + threadGroups[0] + " " + threadGroups[1] + " " + threadGroups[2]); initialCells = new FluidCell[fluidParameters.gridSize, fluidParameters.gridSize]; externalAdditions = new FluidCell[fluidParameters.gridSize, fluidParameters.gridSize]; operationData = new FluidCellOperationData[fluidParameters.gridSize, fluidParameters.gridSize]; for (int j = 0; j < fluidParameters.gridSize; j++) { for (int i = 0; i < fluidParameters.gridSize; i++) { initialCells[j, i] = cellParameters.defaultCell; externalAdditions[j, i] = new FluidCell(); operationData[j, i] = new FluidCellOperationData(); } } int cellStride = Marshal.SizeOf(new FluidCell()); fluidBuffer1 = new ComputeBuffer(fluidParameters.gridSize * fluidParameters.gridSize, cellStride); fluidBuffer1.SetData(initialCells); fluidBuffer2 = new ComputeBuffer(fluidParameters.gridSize * fluidParameters.gridSize, cellStride); fluidBuffer2.SetData(initialCells); inFluidBuffer = fluidBuffer1; outFluidBuffer = fluidBuffer2; externalAdditionBuffer = new ComputeBuffer(fluidParameters.gridSize * fluidParameters.gridSize, cellStride); externalAdditionBuffer.SetData(initialCells); int operationDataStride = Marshal.SizeOf(new FluidCellOperationData()); operationDataBuffer = new ComputeBuffer(fluidParameters.gridSize * fluidParameters.gridSize, operationDataStride); operationDataBuffer.SetData(operationData); fluidComputer.SetFloat("cellSize", cellParameters.cellSize); fluidComputer.SetFloat("cellsPerSide", fluidParameters.gridSize); return(null); }
protected override void setExternal(FluidCellIndex index, FluidCell applyCell) { externalAdditions[index.y, index.x].density = applyCell.density; externalAdditions[index.y, index.x].velocity = applyCell.velocity; }
protected abstract void setExternal(FluidCellIndex index, FluidCell applyCell);
public void SetExternal(FluidCellIndex index, FluidCell applyCell) { setExternal(index, applyCell); }
void Update() { if (mouseInput != null && mouseInput.SelectedFluid != null) { FluidCellIndex selectedIndex = mouseInput.SelectedFluid.SelectedCellIndex; foreach (var member in members) { if (member.Simulator != mouseInput.SelectedFluid) { member.Simulator.SelectedCellIndex = selectedIndex; } } } for (int i = 0; i < sharedInfo.fluidParameters.gridSize; i++) { for (int j = 0; j < sharedInfo.fluidParameters.gridSize; j++) { sharedExternals[i, j].density = 0; sharedExternals[i, j].velocity = Vector3.zero; } } // Sum up external additions of all member simulators. FluidCellIndex index = new FluidCellIndex(); foreach (var member in members) { for (int i = 0; i < sharedInfo.fluidParameters.gridSize; i++) { for (int j = 0; j < sharedInfo.fluidParameters.gridSize; j++) { index.x = i; index.y = j; index.z = 0; FluidCell memberCell = member.Simulator.GetExternal(index); sharedExternals[i, j].density += memberCell.density; sharedExternals[i, j].velocity += memberCell.velocity; } } } // Apply summed externals to member simulators to ensure identical input. foreach (var member in members) { for (int i = 0; i < sharedInfo.fluidParameters.gridSize; i++) { for (int j = 0; j < sharedInfo.fluidParameters.gridSize; j++) { index.x = i; index.y = j; index.z = 0; member.Simulator.SetExternal(index, sharedExternals[i, j]); } } } foreach (var member in members) { member.Simulator.Simulate(); } }