コード例 #1
0
ファイル: LodDataMgrShadow.cs プロジェクト: yzaroui/crest
        public override void Start()
        {
            base.Start();

            _renderProperties   = new PropertyWrapperCompute();
            _updateShadowShader = ComputeShaderHelpers.LoadShader(UpdateShadow);
            if (_updateShadowShader == null)
            {
                enabled = false;
                return;
            }

            try
            {
                krnl_UpdateShadow = _updateShadowShader.FindKernel(UpdateShadow);
            }
            catch (Exception)
            {
                Debug.LogError("Could not load shadow update kernel. Disabling shadows.", _ocean);
                enabled = false;
                return;
            }

            // Setup the camera.
            UpdateCameraMain();

#if UNITY_EDITOR
            if (!OceanRenderer.Instance.OceanMaterial.IsKeywordEnabled("_SHADOWS_ON"))
            {
                Debug.LogWarning("Shadowing is not enabled on the current ocean material and will not be visible.", _ocean);
            }
#endif
        }
コード例 #2
0
ファイル: QueryBase.cs プロジェクト: skymeson/crest
        public QueryBase()
        {
            _dataArrivedAction = new System.Action <AsyncGPUReadbackRequest>(DataArrived);

            if (_maxQueryCount != OceanRenderer.Instance._lodDataAnimWaves.Settings.MaxQueryCount)
            {
                _maxQueryCount          = OceanRenderer.Instance._lodDataAnimWaves.Settings.MaxQueryCount;
                _queryPosXZ_minGridSize = new Vector3[_maxQueryCount];
            }

            _computeBufQueries = new ComputeBuffer(_maxQueryCount, 12, ComputeBufferType.Default);
            _computeBufResults = new ComputeBuffer(_maxQueryCount, 12, ComputeBufferType.Default);

            _queryResults     = new NativeArray <Vector3>(_maxQueryCount, Allocator.Persistent, NativeArrayOptions.ClearMemory);
            _queryResultsLast = new NativeArray <Vector3>(_maxQueryCount, Allocator.Persistent, NativeArrayOptions.ClearMemory);

            _shaderProcessQueries = ComputeShaderHelpers.LoadShader(QueryShaderName);
            if (_shaderProcessQueries == null)
            {
                Debug.LogError($"Could not load Query compute shader {QueryShaderName}");
                return;
            }
            _kernelHandle = _shaderProcessQueries.FindKernel(QueryKernelName);
            _wrapper      = new PropertyWrapperComputeStandalone(_shaderProcessQueries, _kernelHandle);
        }
コード例 #3
0
        public override void Start()
        {
            base.Start();

            _renderProperties   = new PropertyWrapperCompute();
            _updateShadowShader = ComputeShaderHelpers.LoadShader(UpdateShadow);
            if (_updateShadowShader == null)
            {
                enabled = false;
                return;
            }

            try
            {
                krnl_UpdateShadow = _updateShadowShader.FindKernel(UpdateShadow);
            }
            catch (Exception)
            {
                Debug.LogError("Could not load shadow update kernel. Disabling shadows.", _ocean);
                enabled = false;
                return;
            }

#if UNITY_EDITOR
            if (OceanRenderer.Instance.OceanMaterial != null &&
                OceanRenderer.Instance.OceanMaterial.HasProperty(MATERIAL_KEYWORD_PROPERTY) &&
                !OceanRenderer.Instance.OceanMaterial.IsKeywordEnabled(MATERIAL_KEYWORD))
            {
                Debug.LogWarning(ERROR_MATERIAL_KEYWORD_MISSING + " " + ERROR_MATERIAL_KEYWORD_MISSING_FIX, _ocean);
            }
#endif
        }
コード例 #4
0
        static void InitStatics()
        {
            if (OceanRenderer.RunningWithoutGPU)
            {
                // No texture arrays when no graphics card..
                return;
            }

            // Init here from 2019.3 onwards
            sp_LD_TexArray_Target = Shader.PropertyToID("_LD_TexArray_Target");

            if (_blackTextureArray == null)
            {
                CreateBlackTexArray();
            }

            if (s_clearToBlackShader == null)
            {
                s_clearToBlackShader = ComputeShaderHelpers.LoadShader(CLEAR_TO_BLACK_SHADER_NAME);
            }
            if (s_clearToBlackShader != null)
            {
                krnl_ClearToBlack = s_clearToBlackShader.FindKernel(CLEAR_TO_BLACK_SHADER_NAME);
            }
        }
コード例 #5
0
        protected virtual void OnEnable()
        {
            _dataArrivedAction = new System.Action <AsyncGPUReadbackRequest>(DataArrived);

            if (_maxQueryCount != OceanRenderer.Instance._simSettingsAnimatedWaves.MaxQueryCount)
            {
                _maxQueryCount          = OceanRenderer.Instance._simSettingsAnimatedWaves.MaxQueryCount;
                _queryPosXZ_minGridSize = new Vector3[_maxQueryCount];
            }

            _computeBufQueries = new ComputeBuffer(_maxQueryCount, 12, ComputeBufferType.Default);
            _computeBufResults = new ComputeBuffer(_maxQueryCount, 12, ComputeBufferType.Default);

            _queryResults     = new NativeArray <Vector3>(_maxQueryCount, Allocator.Persistent, NativeArrayOptions.ClearMemory);
            _queryResultsLast = new NativeArray <Vector3>(_maxQueryCount, Allocator.Persistent, NativeArrayOptions.ClearMemory);

            _shaderProcessQueries = ComputeShaderHelpers.LoadShader(QueryShaderName);
            if (_shaderProcessQueries == null)
            {
                enabled = false;
                return;
            }
            _kernelHandle = _shaderProcessQueries.FindKernel(QueryKernelName);
            _wrapper      = new PropertyWrapperComputeStandalone(_shaderProcessQueries, _kernelHandle);
        }
コード例 #6
0
 void CreateProperties()
 {
     _shader = ComputeShaderHelpers.LoadShader(ShaderSim);
     if (_shader == null)
     {
         enabled = false;
         return;
     }
     _renderSimProperties = new PropertyWrapperCompute();
 }
コード例 #7
0
        protected override void InitData()
        {
            base.InitData();

            // Setup the RenderTexture and compute shader for combining
            // different animated wave LODs. As we use a single texture array
            // for all LODs, we employ a compute shader as only they can
            // read and write to the same texture.

            int resolution = OceanRenderer.Instance.LodDataResolution;
            var desc       = new RenderTextureDescriptor(resolution, resolution, CompatibleTextureFormat, 0);

            _waveBuffers = CreateLodDataTextures(desc, "WaveBuffer", false);

            _combineBuffer = CreateCombineBuffer(desc);

            // Combine graphics shader - for 'ping pong' approach (legacy hardware)
            var combineShaderNameGraphics = "Hidden/Crest/Simulation/Combine Animated Wave LODs";
            var combineShaderGraphics     = Shader.Find(combineShaderNameGraphics);

            Debug.Assert(combineShaderGraphics != null,
                         $"Could not load shader {combineShaderNameGraphics}. Try right clicking the Crest folder in the Project view and selecting Reimport, and checking for errors.",
                         OceanRenderer.Instance);
            if (combineShaderGraphics != null)
            {
                _combineMaterial = new PropertyWrapperMaterial[OceanRenderer.Instance.CurrentLodCount];
                for (int i = 0; i < _combineMaterial.Length; i++)
                {
                    var mat = new Material(combineShaderGraphics);
                    _combineMaterial[i] = new PropertyWrapperMaterial(mat);
                }
            }

            // Combine compute shader - modern hardware
            _combineShader = ComputeShaderHelpers.LoadShader(ShaderName);
            if (_combineShader == null)
            {
                enabled = false;
                return;
            }
            krnl_ShapeCombine = _combineShader.FindKernel("ShapeCombine");
            krnl_ShapeCombine_DISABLE_COMBINE                             = _combineShader.FindKernel("ShapeCombine_DISABLE_COMBINE");
            krnl_ShapeCombine_FLOW_ON                                     = _combineShader.FindKernel("ShapeCombine_FLOW_ON");
            krnl_ShapeCombine_FLOW_ON_DISABLE_COMBINE                     = _combineShader.FindKernel("ShapeCombine_FLOW_ON_DISABLE_COMBINE");
            krnl_ShapeCombine_DYNAMIC_WAVE_SIM_ON                         = _combineShader.FindKernel("ShapeCombine_DYNAMIC_WAVE_SIM_ON");
            krnl_ShapeCombine_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE         = _combineShader.FindKernel("ShapeCombine_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE");
            krnl_ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON                 = _combineShader.FindKernel("ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON");
            krnl_ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE = _combineShader.FindKernel("ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE");
            _combineProperties = new PropertyWrapperCompute();
        }
コード例 #8
0
        static void InitStatics()
        {
            // Init here from 2019.3 onwards
            sp_LD_TexArray_Target = Shader.PropertyToID("_LD_TexArray_Target");

            if (BlackTextureArray == null)
            {
                BlackTextureArray      = CreateTexture2DArray(Texture2D.blackTexture);
                BlackTextureArray.name = "Black Texture2DArray";
            }

            s_clearToBlackShader = ComputeShaderHelpers.LoadShader(CLEAR_TO_BLACK_SHADER_NAME);
            if (s_clearToBlackShader != null)
            {
                krnl_ClearToBlack = s_clearToBlackShader.FindKernel(CLEAR_TO_BLACK_SHADER_NAME);
            }
        }
コード例 #9
0
        public override void Start()
        {
            base.Start();

            _renderProperties   = new PropertyWrapperCompute();
            _updateShadowShader = ComputeShaderHelpers.LoadShader(UpdateShadow);
            if (_updateShadowShader == null)
            {
                enabled = false;
                return;
            }

            try
            {
                krnl_UpdateShadow = _updateShadowShader.FindKernel(UpdateShadow);
            }
            catch (Exception)
            {
                Debug.LogError("Could not load shadow update kernel. Disabling shadows.", _ocean);
                enabled = false;
                return;
            }

            _cameraMain = Camera.main;
            if (_cameraMain == null)
            {
                var viewpoint = OceanRenderer.Instance.Viewpoint;
                _cameraMain = viewpoint != null?viewpoint.GetComponent <Camera>() : null;

                if (_cameraMain == null)
                {
                    Debug.LogError("Could not find main camera, disabling shadow data", _ocean);
                    enabled = false;
                    return;
                }
            }

#if UNITY_EDITOR
            if (!OceanRenderer.Instance.OceanMaterial.IsKeywordEnabled("_SHADOWS_ON"))
            {
                Debug.LogWarning("Shadowing is not enabled on the current ocean material and will not be visible.", _ocean);
            }
#endif
        }
コード例 #10
0
        protected override void InitData()
        {
            base.InitData();

            // Setup the RenderTexture and compute shader for combining
            // different animated wave LODs. As we use a single texture array
            // for all LODs, we employ a compute shader as only they can
            // read and write to the same texture.

            int resolution = OceanRenderer.Instance.LodDataResolution;
            var desc       = new RenderTextureDescriptor(resolution, resolution, TextureFormat, 0);

            _waveBuffers = CreateLodDataTextures(desc, "WaveBuffer", false);

            _combineBuffer = CreateCombineBuffer(desc);

            var combineShader = Shader.Find("Hidden/Crest/Simulation/Combine Animated Wave LODs");

            _combineMaterial = new PropertyWrapperMaterial[OceanRenderer.Instance.CurrentLodCount];
            for (int i = 0; i < _combineMaterial.Length; i++)
            {
                var mat = new Material(combineShader);
                _combineMaterial[i] = new PropertyWrapperMaterial(mat);
            }

            _combineShader = ComputeShaderHelpers.LoadShader(ShaderName);
            if (_combineShader == null)
            {
                enabled = false;
                return;
            }
            krnl_ShapeCombine = _combineShader.FindKernel("ShapeCombine");
            krnl_ShapeCombine_DISABLE_COMBINE                             = _combineShader.FindKernel("ShapeCombine_DISABLE_COMBINE");
            krnl_ShapeCombine_FLOW_ON                                     = _combineShader.FindKernel("ShapeCombine_FLOW_ON");
            krnl_ShapeCombine_FLOW_ON_DISABLE_COMBINE                     = _combineShader.FindKernel("ShapeCombine_FLOW_ON_DISABLE_COMBINE");
            krnl_ShapeCombine_DYNAMIC_WAVE_SIM_ON                         = _combineShader.FindKernel("ShapeCombine_DYNAMIC_WAVE_SIM_ON");
            krnl_ShapeCombine_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE         = _combineShader.FindKernel("ShapeCombine_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE");
            krnl_ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON                 = _combineShader.FindKernel("ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON");
            krnl_ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE = _combineShader.FindKernel("ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE");
            _combineProperties = new PropertyWrapperCompute();
        }
コード例 #11
0
ファイル: OceanDepthCache.cs プロジェクト: facemao3/crest
        public void PopulateCache()
        {
            if (_type == OceanDepthCacheType.Baked)
            {
                return;
            }

            var layerMask  = 0;
            var errorShown = false;

            foreach (var layer in _layerNames)
            {
                if (string.IsNullOrEmpty(layer))
                {
                    Debug.LogError("OceanDepthCache: An empty layer name was provided. Please provide a valid layer name. Click this message to highlight the cache in question.", this);
                    errorShown = true;
                    continue;
                }

                int layerIdx = LayerMask.NameToLayer(layer);
                if (layerIdx == -1)
                {
                    Debug.LogError("OceanDepthCache: Invalid layer specified: \"" + layer +
                                   "\". Please add this layer to the project by putting the name in an empty layer slot in Edit/Project Settings/Tags and Layers. Click this message to highlight the cache in question.", this);

                    errorShown = true;
                }
                else
                {
                    layerMask = layerMask | (1 << layerIdx);
                }
            }

            if (layerMask == 0)
            {
                if (!errorShown)
                {
                    Debug.LogError("No valid layers for populating depth cache, aborting. Click this message to highlight the cache in question.", this);
                }

                return;
            }

#if UNITY_EDITOR
            if (_type == OceanDepthCacheType.Realtime && _checkTerrainDrawInstancedOption)
            {
                // This issue only affects the built-in render pipeline. Issue 158: https://github.com/crest-ocean/crest/issues/158

                var terrains = FindObjectsOfType <Terrain>();
                foreach (var terrain in terrains)
                {
                    var mask = (int)Mathf.Pow(2f, terrain.gameObject.layer);

                    if ((mask & layerMask) == 0)
                    {
                        continue;
                    }

                    if (terrain.drawInstanced)
                    {
                        Debug.LogError($"Terrain {terrain.gameObject.name} has 'Draw Instanced' enabled. This terrain will not populate into the depth cache and therefore will not contribute to shorelines and shallow water. This option must be disabled on the terrain when the depth cache is populated (but can be enabled afterwards).", terrain);
                    }
                }
            }
#endif

            if (_depthCacheTexture == null)
            {
                RenderTextureFormat fmt;
                if (_generateSDF)
                {
                    fmt = RenderTextureFormat.RGHalf;
                }
                else
                {
#if UNITY_EDITOR_WIN
                    fmt = RenderTextureFormat.DefaultHDR;
#else
                    fmt = RenderTextureFormat.RHalf;
#endif
                }

                Debug.Assert(SystemInfo.SupportsRenderTextureFormat(fmt), "The graphics device does not support the render texture format " + fmt.ToString());
                _depthCacheTexture                   = new RenderTexture(_resolution, _resolution, 0);
                _depthCacheTexture.name              = gameObject.name + "_oceanDepth";
                _depthCacheTexture.format            = fmt;
                _depthCacheTexture.useMipMap         = false;
                _depthCacheTexture.anisoLevel        = 0;
                _depthCacheTexture.enableRandomWrite = _generateSDF;
                _depthCacheTexture.Create();
            }

            if (_depthCacheCamera == null)
            {
                _depthCacheCamera = GenerateCacheCamera(
                    layerMask,
                    _generateSDF ? "DepthSdfCam" : "DepthCacheCam",
                    _cameraMaxTerrainHeight,
                    transform,
                    _depthCacheTexture,
                    _hideDepthCacheCam
                    );
            }


            // Make sure this global is set - I found this was necessary to set it here. However this can cause glitchiness in editor
            // as it messes with this global vector, so only do it if not in edit mode
#if UNITY_EDITOR
            if (EditorApplication.isPlaying)
#endif
            {
                // Shader needs sea level to determine water depth
                var centerPoint = Vector3.zero;
                if (OceanRenderer.Instance != null)
                {
                    centerPoint.y = OceanRenderer.Instance.Root.position.y;
                }
                else
                {
                    centerPoint.y = transform.position.y;
                }

                Shader.SetGlobalVector("_OceanCenterPosWorld", centerPoint);
            }

            _depthCacheCamera.RenderWithShader(Shader.Find("Crest/Inputs/Depth/Ocean Depth From Geometry"), null);

            if (_generateSDF)
            {
                RenderTextureFormat fmt = RenderTextureFormat.RGHalf;
                RenderTexture       voronoiPingPongTexture0 = new RenderTexture(_resolution, _resolution, 0);
                voronoiPingPongTexture0.name              = gameObject.name + "_voronoiPingPong0";
                voronoiPingPongTexture0.format            = fmt;
                voronoiPingPongTexture0.useMipMap         = false;
                voronoiPingPongTexture0.anisoLevel        = 0;
                voronoiPingPongTexture0.enableRandomWrite = true;
                voronoiPingPongTexture0.Create();

                RenderTexture voronoiPingPongTexture1 = new RenderTexture(_resolution, _resolution, 0);
                voronoiPingPongTexture1.name              = gameObject.name + "_voronoiPingPong1";
                voronoiPingPongTexture1.format            = fmt;
                voronoiPingPongTexture1.useMipMap         = false;
                voronoiPingPongTexture1.anisoLevel        = 0;
                voronoiPingPongTexture1.enableRandomWrite = true;
                voronoiPingPongTexture1.Create();

                using (CommandBuffer jumpFloodCommandBuffer = new CommandBuffer())
                {
                    var  cameraToWorldMatrix     = _depthCacheCamera.cameraToWorldMatrix;
                    var  projectionMatrix        = _depthCacheCamera.projectionMatrix;
                    var  projectionToWorldMatrix = cameraToWorldMatrix * projectionMatrix.inverse;
                    uint textureDimension        = (uint)voronoiPingPongTexture0.width;
                    {
                        ComputeShader initJumpFloodShader = ComputeShaderHelpers.LoadShader("SdfInitJumpFlood");
                        int           initJumpFloodKernel = initJumpFloodShader.FindKernel("SdfInitJumpFlood");
                        jumpFloodCommandBuffer.SetComputeTextureParam(initJumpFloodShader, initJumpFloodKernel, sp_FromTexture, _depthCacheTexture);
                        jumpFloodCommandBuffer.SetComputeTextureParam(initJumpFloodShader, initJumpFloodKernel, sp_ToTexture, voronoiPingPongTexture0);
                        jumpFloodCommandBuffer.SetComputeIntParam(initJumpFloodShader, sp_textureDimension, (int)textureDimension);
                        jumpFloodCommandBuffer.SetComputeMatrixParam(initJumpFloodShader, sp_projectionToWorld, projectionToWorldMatrix);
                        jumpFloodCommandBuffer.DispatchCompute(
                            initJumpFloodShader,
                            initJumpFloodKernel,
                            _depthCacheTexture.width / 8,
                            _depthCacheTexture.height / 8,
                            1
                            );
                    }
                    ComputeShader jumpFloodShader = ComputeShaderHelpers.LoadShader("SdfJumpFlood");
                    int           jumpFloodKernel = jumpFloodShader.FindKernel("SdfJumpFlood");

                    ComputeShader sdfGradientShader = ComputeShaderHelpers.LoadShader("SdfApply");
                    int           sdfKernel         = sdfGradientShader.FindKernel("SdfApply");


                    jumpFloodCommandBuffer.name = "Jump Flood";
                    for (uint jumpSize = (uint)textureDimension / 2; jumpSize > 0; jumpSize /= 2)
                    {
                        ApplyJumpFlood(
                            jumpFloodCommandBuffer, jumpFloodShader, jumpFloodKernel,
                            sp_jumpSize, jumpSize,
                            sp_textureDimension, textureDimension,
                            sp_projectionToWorld, projectionToWorldMatrix,
                            sp_FromTexture, voronoiPingPongTexture0,
                            sp_ToTexture, voronoiPingPongTexture1
                            );
                        LodDataMgr.Swap(ref voronoiPingPongTexture1, ref voronoiPingPongTexture0);
                    }

                    for (uint roundNum = 0; roundNum < _additionalJumpFloodRounds; roundNum++)
                    {
                        uint jumpSize = (uint)1 << (int)roundNum;
                        ApplyJumpFlood(
                            jumpFloodCommandBuffer, jumpFloodShader, jumpFloodKernel,
                            sp_jumpSize, jumpSize,
                            sp_textureDimension, textureDimension,
                            sp_projectionToWorld, projectionToWorldMatrix,
                            sp_FromTexture, voronoiPingPongTexture0,
                            sp_ToTexture, voronoiPingPongTexture1
                            );
                        LodDataMgr.Swap(ref voronoiPingPongTexture1, ref voronoiPingPongTexture0);
                    }

                    jumpFloodCommandBuffer.SetComputeTextureParam(sdfGradientShader, sdfKernel, sp_FromTexture, voronoiPingPongTexture0);
                    jumpFloodCommandBuffer.SetComputeTextureParam(sdfGradientShader, sdfKernel, sp_ToTexture, _depthCacheTexture);
                    jumpFloodCommandBuffer.SetComputeIntParam(sdfGradientShader, sp_textureDimension, (int)textureDimension);
                    jumpFloodCommandBuffer.SetComputeMatrixParam(sdfGradientShader, sp_projectionToWorld, projectionToWorldMatrix);
                    jumpFloodCommandBuffer.DispatchCompute(
                        sdfGradientShader,
                        sdfKernel,
                        voronoiPingPongTexture0.width / 8,
                        voronoiPingPongTexture0.height / 8,
                        1
                        );
                    Graphics.ExecuteCommandBuffer(jumpFloodCommandBuffer);
                }
                DrawCacheQuad(ref _drawDepthCacheQuad, "SDFCache_", OceanDepthCacheType.Baked, _type == OceanDepthCacheType.Baked ? (Texture)_savedCache : _depthCacheTexture);

                voronoiPingPongTexture0.DiscardContents();
                voronoiPingPongTexture1.DiscardContents();
            }
            else
            {
                DrawCacheQuad(ref _drawDepthCacheQuad, "DepthCache_", _type, _type == OceanDepthCacheType.Baked ? (Texture)_savedCache : _depthCacheTexture);
            }
        }