- Home /
My command buffer gets depth instead of color
I have a commandBuffer for implementing fog of war. It's pretty basic, I have a texture that contains a vision texture, and i want to combine it with the main camera's output to black out areas the players cant see.
my current implementation is this (attached to the main camera)
CameraEvent camEvent = CameraEvent.AfterImageEffects;// also tried .BeforeImageEffects;
private void OnPreRender()
{
if (BlitMat == null || FogOfWarTex == null)
{
return;
}
bool createdCmdBuffer = false;
if (commandBuffer == null)
{
commandBuffer = new CommandBuffer();
commandBuffer.name = "FogOfWar";
createdCmdBuffer = true;
}
else
{
commandBuffer.Clear();
}
BlitMat.SetMatrix("_ViewProjectInverse", (camera.projectionMatrix * camera.worldToCameraMatrix).inverse);
BlitMat.SetTexture("_FowMaskTex", FogOfWarTex);
BlitMat.SetVector("_MinPoint", MapHelper.Instance.MinMapPoint);
BlitMat.SetVector("_MaxPoint", MapHelper.Instance.MaxMapPoint);
int inverseMatrixId = Shader.PropertyToID("_ViewProjectInverse");
int maskId = Shader.PropertyToID("_FowMaskTex");
int minPointId = Shader.PropertyToID("_MinPoint");
int maxPointId = Shader.PropertyToID("_MaxPoint");
int targetId = Shader.PropertyToID("_FogTarget");
commandBuffer.GetTemporaryRT(targetId, -1, -1);
commandBuffer.SetGlobalVector(minPointId, MapHelper.Instance.MinMapPoint);
commandBuffer.SetGlobalVector(maxPointId, MapHelper.Instance.MaxMapPoint);
commandBuffer.SetGlobalMatrix(inverseMatrixId, (camera.projectionMatrix * camera.worldToCameraMatrix).inverse);
commandBuffer.SetGlobalTexture(maskId, FogOfWarTex);
commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, targetId, BlitMat);
commandBuffer.Blit(targetId, BuiltinRenderTextureType.CameraTarget);
if (createdCmdBuffer)
{
camera.AddCommandBuffer(camEvent, commandBuffer);
}
}
this works fine in one of my scenes, but in others, _mainTex in my shader looks like it's a depth buffer which i don't want (i need the regular color buffer). does anybody know what might cause this? i've been trying different arguments for the blit call, as well as injecting my command buffer into different render events, but no avail. please help!
Your answer

Follow this Question
Related Questions
Unexpected Release of RenderTexture 2 Answers
Oculus VR and Sharing RenderTextures Across Both Eyes 1 Answer
dynamic hole in layer / texture / camera 0 Answers
How can I build a RenderTexture to Texture2D hidden booth for UI elements? 1 Answer
Is there a way to write to Deferred Depth from a command buffer in CameraEvent.AfterImageEffects? 1 Answer