private void UpdateComputeShader() { m_cachedCameraData.Set(m_mainCamera.nearClipPlane, m_mainCamera.farClipPlane, m_mainCamera.fieldOfView * Mathf.Deg2Rad, m_mainCamera.aspect); InstancingUtilities.GetFrustumPlanes(m_frustumPlanes, m_mainCamera); InstancingUtilities.UnrollFloats(m_frustumPlanes, m_frustumPlanesUnrolled); InstancingUtilities.UnrollFloats(m_mainCamera.transform.localToWorldMatrix, m_cameraToWorldMatrixUnrolled); //Used to linearize Z buffer values. x is (1 - far / near), y is (far / near), z is (x / far) and w is (y / far). float farOverNear = m_mainCamera.farClipPlane / m_mainCamera.nearClipPlane; Vector4 _ZBufferParams = new Vector4(1f - farOverNear, farOverNear, 1, 1); m_computeShader.SetTextureFromGlobal(m_cachedComputeMaxDepthKernel, "_DepthTexture", "_CameraDepthTexture"); m_computeShader.SetVector("_ZBufferParams", _ZBufferParams); m_computeShader.SetFloat("maxShadowRange", m_maxShadowRange); m_computeShader.SetInt("yAxisDownsample", m_yAxisDownsample); m_computeShader.SetFloats("frustumPlanes", m_frustumPlanesUnrolled); m_computeShader.SetVector("camData", m_cachedCameraData); m_computeShader.SetFloats("cameraToWorldMatrix", m_cameraToWorldMatrixUnrolled); m_computeShader.SetVector("lightDir", m_directionalLight.transform.forward); }
private void UpdateComputeShader() { if (m_debugAppendCount) { m_argsBuffersArray[0][0].GetData(m_argsArray[0][0]); m_argsBuffersArray[1][0].GetData(m_argsArray[1][0]); m_argsBuffersArray[2][0].GetData(m_argsArray[2][0]); Debug.Log("Previous frame append count: " + m_argsArray[0][0][1].ToString("f3") + " and lod1: " + m_argsArray[1][0][1].ToString("f3") + " and lod2: " + m_argsArray[2][0][1].ToString("f3")); } #if UNITY_EDITOR if (m_dirtyPositionInstances.Count > 0 || m_dirtyRotationInstances.Count > 0 || m_dirtyScaleInstances.Count > 0) { if (m_dirtyPositionInstances.Count > 0 || m_dirtyScaleInstances.Count > 0) { m_positionBuffer.SetData(m_instanceData.m_positions); m_dirtyPositionInstances.Clear(); m_dirtyScaleInstances.Clear(); } if (m_dirtyRotationInstances.Count > 0) { m_rotationBuffer.SetData(m_instanceData.m_rotations); m_dirtyRotationInstances.Clear(); } } #endif InstancingUtilities.GetFrustumPlanes(m_frustumPlanes, m_mainCamera); Vector4 screenSpaceLODSize = Vector4.zero; for (int i = 0; i < m_cachedNumLods; i++) { screenSpaceLODSize[i] = Mathf.Pow((m_LodSettings[i].screenSize * 0.5f), 2); } Vector4 frustumBoundingSphere = InstancingUtilities.GenerateFrustumBoundingSphere(m_mainCamera, m_coarseCullFarDistance); int[] shadowSettingsArray = new int[m_cachedNumLods]; for (int i = 0; i < m_cachedNumLods; i++) { shadowSettingsArray[i] = (int)m_LodSettings[i].shadowSetting; } InstancingUtilities.UnrollFloats(m_frustumPlanes, m_frustumPlanesUnrolled); InstancingUtilities.UnrollFloats(m_shadowRegionPlanes, m_shadowRegionPlanesUnrolled); InstancingUtilities.UnrollFloats(m_mainCamera.projectionMatrix, m_cameraProjectionMatrixUnrolled); m_computeShader.SetVector("cameraData", m_mainCamera.transform.position); m_computeShader.SetFloats("cameraProjectionMatrix", m_cameraProjectionMatrixUnrolled); m_computeShader.SetFloats("frustumPlanes", m_frustumPlanesUnrolled); m_computeShader.SetFloats("shadowRegionPlanes", m_shadowRegionPlanesUnrolled); m_computeShader.SetVector("screenSpaceLODSize", screenSpaceLODSize); //should not occur every update m_computeShader.SetFloat("Time", Time.time); m_computeShader.SetInts("castShadowsLOD", shadowSettingsArray); //should not occur every update m_computeShader.SetVector("frustumBoundingSphere", frustumBoundingSphere); for (int i = 0; i < m_cachedNumLods; i++) { m_appendBuffers[i].SetCounterValue(0); } m_computeShader.Dispatch(m_cachedComputeKernel, Mathf.CeilToInt(m_instanceCount / 256f), 1, 1); for (int i = 0; i < m_cachedNumLods; i++) { for (int j = 0; j < m_LodSettings[i].mesh.subMeshCount; j++) { ComputeBuffer.CopyCount(m_appendBuffers[i], m_argsBuffersArray[i][j], 1 * sizeof(uint)); } } }