public void SetCamera(Camera camera)
        {
            if (cameraData == null)
            {
                cameraData = new GPUInstancerCameraData(camera);
            }
            else
            {
                cameraData.SetCamera(camera);
            }

            if (cameraData.hiZOcclusionGenerator != null)
            {
                DestroyImmediate(cameraData.hiZOcclusionGenerator);
            }

            if (isOcclusionCulling)
            {
                cameraData.hiZOcclusionGenerator = cameraData.mainCamera.GetComponent <GPUInstancerHiZOcclusionGenerator>();
                if (cameraData.hiZOcclusionGenerator == null)
                {
                    cameraData.hiZOcclusionGenerator = cameraData.mainCamera.gameObject.AddComponent <GPUInstancerHiZOcclusionGenerator>();
                }
                cameraData.hiZOcclusionGenerator.Initialize(cameraData.mainCamera);
            }
        }
        public void UpdateBuffers(GPUInstancerCameraData renderingCameraData)
        {
            if (renderingCameraData != null && renderingCameraData.mainCamera != null && SystemInfo.supportsComputeShaders)
            {
                if (isOcclusionCulling && renderingCameraData.hiZOcclusionGenerator == null)
                {
                    SetupOcclusionCulling(renderingCameraData);
                }

                renderingCameraData.CalculateCameraData();

                instancingBounds.center = renderingCameraData.mainCamera.transform.position;

                if (lastDrawCallFrame != Time.frameCount)
                {
                    lastDrawCallFrame     = Time.frameCount;
                    timeSinceLastDrawCall = Time.realtimeSinceStartup - lastDrawCallTime;
                    lastDrawCallTime      = Time.realtimeSinceStartup;
                }

                UpdateSpatialPartitioningCells(renderingCameraData);

                GPUInstancerUtility.UpdateGPUBuffers(_cameraComputeShader, _cameraComputeKernelIDs, _visibilityComputeShader, _instanceVisibilityComputeKernelIDs, runtimeDataList, renderingCameraData, isFrustumCulling,
                                                     isOcclusionCulling, showRenderedAmount, isInitial);
                isInitial = false;

                if (GPUInstancerUtility.matrixHandlingType == GPUIMatrixHandlingType.CopyToTexture)
                {
                    GPUInstancerUtility.DispatchBufferToTexture(runtimeDataList, _bufferToTextureComputeShader, _bufferToTextureComputeKernelID);
                }

                GPUInstancerUtility.GPUIDrawMeshInstancedIndirect(runtimeDataList, instancingBounds, renderingCameraData, layerMask, lightProbeDisabled);
            }
        }
        public void SetupOcclusionCulling(GPUInstancerCameraData renderingCameraData)
        {
            if (renderingCameraData == null || renderingCameraData.mainCamera == null)
            {
                return;
            }

#if UNITY_EDITOR
            if (renderingCameraData.mainCamera.name == GPUInstancerEditorSimulator.sceneViewCameraName)
            {
                return;
            }
#endif

            if (isOcclusionCulling)
            {
                if (renderingCameraData.hiZOcclusionGenerator == null)
                {
                    GPUInstancerHiZOcclusionGenerator hiZOcclusionGenerator =
                        renderingCameraData.mainCamera.GetComponent <GPUInstancerHiZOcclusionGenerator>();

                    if (hiZOcclusionGenerator == null)
                    {
                        hiZOcclusionGenerator = renderingCameraData.mainCamera.gameObject.AddComponent <GPUInstancerHiZOcclusionGenerator>();
                    }

                    renderingCameraData.hiZOcclusionGenerator = hiZOcclusionGenerator;
                    renderingCameraData.hiZOcclusionGenerator.Initialize(renderingCameraData.mainCamera);
                }
            }
        }
Esempio n. 4
0
        public void UpdateBuffers(GPUInstancerCameraData renderingCameraData)
        {
            if (renderingCameraData != null && renderingCameraData.mainCamera != null && SystemInfo.supportsComputeShaders)
            {
                if (isOcclusionCulling && renderingCameraData.hiZOcclusionGenerator == null)
                {
                    SetupOcclusionCulling(renderingCameraData);
                }

                renderingCameraData.CalculateCameraData();

                instancingBounds.center = renderingCameraData.mainCamera.transform.position;

                if (lastDrawCallFrame != Time.frameCount)
                {
                    lastDrawCallFrame     = Time.frameCount;
                    timeSinceLastDrawCall = Time.realtimeSinceStartup - lastDrawCallTime;
                    lastDrawCallTime      = Time.realtimeSinceStartup;
                }

                UpdateSpatialPartitioningCells(renderingCameraData);

                GPUInstancerUtility.UpdateGPUBuffers(_visibilityComputeShader, _instanceVisibilityComputeKernelIDs, runtimeDataList, renderingCameraData, isFrustumCulling,
                                                     isOcclusionCulling, showRenderedAmount, isInitial);
                isInitial = false;
#if !UNITY_EDITOR && UNITY_ANDROID
                if (SystemInfo.graphicsDeviceType != GraphicsDeviceType.Vulkan)
                {
                    GPUInstancerUtility.DispatchBufferToTexture(runtimeDataList, _bufferToTextureComputeShader, _bufferToTextureComputeKernelID);
                }
#endif
                GPUInstancerUtility.GPUIDrawMeshInstancedIndirect(runtimeDataList, instancingBounds, renderingCameraData);
            }
        }
Esempio n. 5
0
        public void SetupOcclusionCulling(GPUInstancerCameraData renderingCameraData)
        {
            if (renderingCameraData == null || renderingCameraData.mainCamera == null)
            {
                return;
            }

#if UNITY_EDITOR
            if (renderingCameraData.mainCamera.name == GPUInstancerEditorSimulator.sceneViewCameraName)
            {
                return;
            }
#endif

            // Occlusion Culling with VR is not implemented yet.
#if UNITY_2017_2_OR_NEWER
            if (isOcclusionCulling && UnityEngine.XR.XRSettings.enabled)
#else
            if (isOcclusionCulling && UnityEngine.VR.VRSettings.enabled)
#endif
            {
                isOcclusionCulling = false;
                Debug.LogWarning("Currently GPU Instancer doesn't support Occlusion Culling for VR. Disabling the Occlusion Culling feature.");
            }

            if (isOcclusionCulling)
            {
                if (renderingCameraData.hiZOcclusionGenerator == null)
                {
                    GPUInstancerHiZOcclusionGenerator hiZOcclusionGenerator =
                        renderingCameraData.mainCamera.GetComponent <GPUInstancerHiZOcclusionGenerator>();

                    if (hiZOcclusionGenerator == null)
                    {
                        hiZOcclusionGenerator = renderingCameraData.mainCamera.gameObject.AddComponent <GPUInstancerHiZOcclusionGenerator>();
                    }

                    renderingCameraData.hiZOcclusionGenerator = hiZOcclusionGenerator;

                    renderingCameraData.mainCamera.depthTextureMode      = DepthTextureMode.Depth;
                    renderingCameraData.hiZOcclusionGenerator.mainCamera = renderingCameraData.mainCamera;
                    renderingCameraData.hiZOcclusionGenerator.OnPreRender();
                }
            }
        }
Esempio n. 6
0
        public GPUInstancerEditorSimulator(GPUInstancerManager gpuiManager)
        {
            this.gpuiManager = gpuiManager;

            if (sceneViewCameraData == null)
            {
                sceneViewCameraData = new GPUInstancerCameraData(null);
            }
            sceneViewCameraData.renderOnlySelectedCamera = true;

            if (gpuiManager != null)
            {
                EditorApplication.update -= FindSceneViewCamera;
                EditorApplication.update += FindSceneViewCamera;
#if UNITY_2017_2_OR_NEWER
                EditorApplication.pauseStateChanged -= HandlePauseStateChanged;
                EditorApplication.pauseStateChanged += HandlePauseStateChanged;
#else
                EditorApplication.playmodeStateChanged = HandlePlayModeStateChanged;
#endif
            }
        }
 public virtual void UpdateSpatialPartitioningCells(GPUInstancerCameraData renderingCameraData)
 {
 }