- Home /
"CameraClearFlags.Nothing" does not work?
so i was recently looking into trying to make a camera not clear on each update. i went through documentation, and found this. when applying it to my script (seen below) it doesn't work. i'm calling the function, but it doesn't keep the deformations. am i doing something wrong? thanks for the reply.
using UnityEngine;
public class cameraData : MonoBehaviour {
private Camera cam;
void Awake()
{
cam = GetComponent<Camera>();
}
public void DontClear()
{
cam.clearFlags = CameraClearFlags.Nothing;
}
}
Answer by JonPQ · May 01, 2019 at 06:19 PM
is this script on the main camera, if not, it wont find the camera try finding main scene camera like this. cam = Camera.main;
also if you have multiple cameras, the other ones will still be clearing
sorry, i didn't mention that. these are separate cameras in the scene. i have the script applied to each camera i want to not clear, but it still isn't working...
i'm referencing it with Get Component from the other script (it's a child object).
"hit.transform.gameObject.GetComponentInChildren().DontClear();"
you may need GetComponentInChildren or GetComponentInParent if the script is not on the same object as the camera.
also... unless you specify a target render texture for each camera..... they will all be rendering to the same screen... so if any one of them is still clearing.... you;ll still get a cleared screen every frame.
hey! couldn't reply because internet went out yesterday.... also i have the script calling the function with a GetComponentInChildren. each camera has it's own separate render texture so that shouldn't be the issue...
I've since set up a public bool in the function I'm calling to test if the function is being called in the first place, and it actually comes out true. that means that I'm calling it properly in the other script.
I suppose I'm not calling the camera properly? i since changed it to public, and set each camera to it's own corresponding script to make sure nothing is getting in the way of each other, but no luck...
can you spot anything in the inspector I have that may be messing with it? i can't see anything....
Answer by Bunny83 · May 02, 2019 at 03:59 PM
I'm not sure if you really understand what "CameraClearFlags.Nothing" really does. You almost never want to use this setting. It keeps everything as it is. That means the color as well as the depth buffer will stay untouched. Since the depth buffer isn't cleared you most likely can't draw anything unless it's actually closer to the camera.
Keep in mind that not clearing the camera doesn't mean the scene isn't rendered. However as i said if the rendered geometry is further away than what is currently in the depth buffer nothing will be written at all.
On the other hand if you used shaders which do not write into the depth buffer (like most transparent shaders) the image would be overdrawn / blended every frame.
It's not clear what you want to use this for, so we can't suggest what you should do since we don't know what you want to do.
i'll explain it to you then.
So, what this camera is doing is sending the output to a custom render texture. the texture is applied to a tessellated plane to calculate depth. the reason I'm calling this function is so so during run time the camera won't update.
i set the camera just under a plane that the character can walk through so it only needs a very small distance for it's clipping plane.
I'm going for an end result of a deformable mesh that, when a character walks through it, the mesh would leave a trail in the mesh where the character walks.
$$anonymous$$d you, i already know that the camera is capturing the plane's depth properly because it shows it in run time. it just doesn't $$anonymous$$EEP the deformations. it still wants to update which is what I'm trying to figure out here...
also, I've since set up a public bool in the function I'm calling it from to test if the function is being called in the first place, and it actually comes out true. that means that I'm calling it properly in the other script. i don't know what i'm doing wrong here..... alt text
nice idea! so you are using the texture depth to modify the vertex positions. I've done some similar stuff. So if you are rendering ortho camera, to same z-depth plane. Its either going to overwrite every pixel at the same z-depth each render... or not draw anything. depending on if the shader's z-test is set to equals or less than or equal. As you say you can see the distortion ok as the character moves... sounds like its replacing it every frame. So its probably not clearing... its just rendering over same pixels every frame. If you think of Z-depth as a color ... maybe you need to modify the color combiner in the shader.... to be something like multiply... rather than the usual opaque render. e.g supposing a footprint is dark/black... and untouched snow is high /white. If you render on top of that using a multiply.... the black areas (footprints) will stay black from frame to frame.
Your answer
![](https://koobas.hobune.stream/wayback/20220612200536im_/https://answers.unity.com/themes/thub/images/avi.jpg)