/// <summary> /// Updates the command buffer. /// </summary> private void UpdateCommandBuffer() { // Determine which cameras are omnidirectional. List <float> sourceCamAreOmnidirectional = new List <float>(); foreach (CameraModel cameraModel in cameraSetup.cameraModels) { sourceCamAreOmnidirectional.Add(cameraModel.isOmnidirectional ? 1 : 0); } // Determine camera parameters to pass to the material as properties. List <float> sourceCamIndices; List <Vector4> sourceCamPositions; List <Matrix4x4> meshTransformationMatrices; _helperDiskBlending.UpdateBlendingParameters(ref blendingMaterial, cameraSetup.cameraModels, PMPerViewMeshesFS.meshTransforms, out sourceCamIndices, out sourceCamPositions, out meshTransformationMatrices); // Update the blending material with the current focal length. _helperFocalSurfaces.SendFocalLengthToBlendingMaterial(ref blendingMaterial); // Indicate the cameras' indices and positions. MaterialPropertyBlock properties = new MaterialPropertyBlock(); properties.SetFloatArray(_shaderNameSourceCamIndex, sourceCamIndices); properties.SetVectorArray(_shaderNameSourceCamPosXYZ, sourceCamPositions); properties.SetFloatArray(_shaderNameSourceCamIsOmnidirectional, sourceCamAreOmnidirectional); // Clear the instructions in the command buffer. _helperCommandBuffer.commandBuffer.Clear(); // Copy the camera target to a temporary render texture, e.g. to copy the skybox in the scene view. int tempID = Shader.PropertyToID("TempCopyColorBuffer"); _helperCommandBuffer.commandBuffer.GetTemporaryRT(tempID, -1, -1, 0, FilterMode.Bilinear); _helperCommandBuffer.commandBuffer.SetRenderTarget(tempID); _helperCommandBuffer.commandBuffer.ClearRenderTarget(true, true, Color.clear); _helperCommandBuffer.commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, tempID); // Clear the camera target's color and depth buffers. _helperCommandBuffer.commandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget); _helperCommandBuffer.commandBuffer.ClearRenderTarget(true, true, Color.clear); // Render the focal surfaces to the depth and color buffer using GPU instancing. _helperCommandBuffer.commandBuffer.DrawMeshInstanced(PMPerViewMeshesFS.meshTransforms[0].GetComponent <MeshFilter>().sharedMesh, 0, blendingMaterial, 0, meshTransformationMatrices.ToArray(), PMPerViewMeshesFS.meshTransforms.Length, properties); // Normalize the RGB channels of the color buffer by the alpha channel, by copying into a temporary render texture. // Note: Be sure to use ZWrite Off. Blit renders a quad, and thus - if ZWrite On - provides the target with the quad's depth, not the render texture's depth. _helperCommandBuffer.commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, tempID, blendingMaterial, 1); _helperCommandBuffer.commandBuffer.Blit(tempID, BuiltinRenderTextureType.CameraTarget); _helperCommandBuffer.commandBuffer.ReleaseTemporaryRT(tempID); }
/// <summary> /// Updates the command buffer. /// </summary> private void UpdateCommandBuffer() { // Clear the instructions in the command buffer. _helperCommandBuffer.commandBuffer.Clear(); // Clear the target textures. _helperCommandBuffer.commandBuffer.SetRenderTarget(_targetColorTexture); _helperCommandBuffer.commandBuffer.ClearRenderTarget(true, true, Color.clear); _helperCommandBuffer.commandBuffer.SetRenderTarget(_targetDepthTexture); _helperCommandBuffer.commandBuffer.ClearRenderTarget(true, true, Color.clear); // Copy the camera target's color (i.e. the rendered background) into the target color texture. renderingCaller.mainCamera.Render(); _helperCommandBuffer.commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, _targetColorTexture); // Clear the camera target's color and depth buffers. _helperCommandBuffer.commandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget); _helperCommandBuffer.commandBuffer.ClearRenderTarget(true, true, Color.clear); // Copy the target color and depth textures to the stored ones, effectively clearing the latter. _helperCommandBuffer.commandBuffer.Blit(_targetColorTexture, _storedColorTexture); _helperCommandBuffer.commandBuffer.Blit(_targetDepthTexture, _storedDepthTexture); // Determine additional camera parameters to pass to the material as properties. List <float> sourceCamIndices; List <Vector4> sourceCamPositions; List <Matrix4x4> meshTransformationMatrices; _helperDiskBlending.UpdateBlendingParameters(ref blendingMaterial, cameraSetup.cameraModels, PMPerViewMeshesQSTR.perViewMeshTransforms, out sourceCamIndices, out sourceCamPositions, out meshTransformationMatrices); // Perform a soft z-test for each source camera and blend them together, storing the result in the stored depth and color textures. MaterialPropertyBlock properties = new MaterialPropertyBlock(); for (int i = 0; i < PMPerViewMeshesQSTR.perViewMeshTransforms.Length; i++) { // Indicate the camera's index and position. properties.SetFloat(_shaderNameSourceCamIndex, sourceCamIndices[i]); properties.SetVector(_shaderNameSourceCamPosXYZ, sourceCamPositions[i]); properties.SetFloat(_shaderNameSourceCamIsOmnidirectional, cameraSetup.cameraModels[i].isOmnidirectional ? 1 : 0); // Draw the mesh into the target textures' color and depth buffers. _helperCommandBuffer.commandBuffer.SetRenderTarget(color: _targetColorTexture, depth: _targetDepthTexture); _helperCommandBuffer.commandBuffer.DrawMesh(PMPerViewMeshesQSTR.perViewMeshes[i], meshTransformationMatrices[i], blendingMaterial, 0, 0, properties); // Copy the target textures into the stored textures. _helperCommandBuffer.commandBuffer.Blit(_targetColorTexture, _storedColorTexture); _helperCommandBuffer.commandBuffer.Blit(_targetDepthTexture, _storedDepthTexture); } // Normalize the stored color texture's RGB channels by its alpha channel, and copy the stored depth and color textures to the camera target. _helperCommandBuffer.commandBuffer.Blit(null, BuiltinRenderTextureType.CameraTarget, blendingMaterial, 1); }