private void HandleHeightBuffer(Vector3 viewPoint) { if (viewPoint.x >= _minVisiblePos.x && viewPoint.x <= _maxVisiblePos.x && viewPoint.z >= _minVisiblePos.y && viewPoint.z <= _maxVisiblePos.y) { if (_heightMapBuffer == null) { if (_heightMapData != null) { _heightMapBuffer = new ComputeBuffer((_heightMapData.Length + 1) / 2, Constants.StrideSizeUint); _heightMapBuffer.SetData(_heightMapData); } else { _heightMapBuffer = new ComputeBuffer((_compactHeightMapDataLength + 3) / 4, Constants.StrideSizeUint); IntPtr p = new IntPtr(_compactHeightMapData.GetBytesIntPtr().ToInt64() + _compactHeightMapDataStart); _heightMapBuffer.SetDataWithIntPtr(p, _compactHeightMapDataLength); } } } else if (viewPoint.x < _minVisiblePos.x - Constants.DetailDisableBufferLength || viewPoint.x > _maxVisiblePos.x + Constants.DetailDisableBufferLength || viewPoint.z < _minVisiblePos.y - Constants.DetailDisableBufferLength || viewPoint.z > _maxVisiblePos.y + Constants.DetailDisableBufferLength) { if (_heightMapBuffer != null) { _heightMapBuffer.Release(); _heightMapBuffer = null; } } }
public override void BuildBuffer(ComputeBuffer heightBuffer) { var kernelId = _instantiationShader.FindKernel(Constants.CsKernel.Common); ComputeBuffer countInUnit = new ComputeBuffer((_resolution * _resolution + 1) / 2, Constants.StrideSizeUint); int[] initialCounter = { 0 }; _instantiationShader.SetBuffer(kernelId, Constants.TerrainVariable.HeightMapData, heightBuffer); _instantiationShader.SetBuffer(kernelId, Constants.DetailVariable.DividedDetailData, countInUnit); _terrainProperty.SetDetailInstantiationProperty(_instantiationShader); var count = _totalCountInLayer.Length; for (int i = 0; i < count; ++i) { if (_totalCountInLayer[i] == 0) { continue; } if (_transform[i] == null) { _transform[i] = new ComputeBuffer(_totalCountInLayer[i], Constants.StrideSizeMatrix4x4); _normal[i] = new ComputeBuffer(_totalCountInLayer[i], Constants.StrideSizeFloat3); _color[i] = new ComputeBuffer(_totalCountInLayer[i], Constants.StrideSizeFloat3); _count[i] = new ComputeBuffer(1, Constants.StrideSizeInt); } _countInCpu[i] = -1; _count[i].SetData(initialCounter); _instantiationShader.SetBuffer(kernelId, Constants.DetailVariable.TransformData, _transform[i]); _instantiationShader.SetBuffer(kernelId, Constants.DetailVariable.NormalData, _normal[i]); _instantiationShader.SetBuffer(kernelId, Constants.DetailVariable.ColorData, _color[i]); _instantiationShader.SetBuffer(kernelId, Constants.ShaderVariable.CounterData, _count[i]); if (_countInNode != null) { countInUnit.SetData(_countInNode[i]); } else { IntPtr p = new IntPtr(_compactCountInNode.WholeData.GetBytesIntPtr().ToInt64() + _compactCountInNode.StartIndices[i]); countInUnit.SetDataWithIntPtr(p, _compactCountInNode.Lengths[i]); } _detailProperties[i].SetDetailInstantiationProperty(_instantiationShader, _index); _instantiationShader.Dispatch(kernelId, _resolution / Constants.DetailInstantiationThreadCount, _resolution / Constants.DetailInstantiationThreadCount, 1); } countInUnit.Release(); }