- Home /
Problems about rendertarget
Simple version:
I have a group of procedural meshes, I need to draw each of them first to screen(call it main draw), then to another buffer(RenderTexture maybe). the order can't be rearranged, since every main draw needs info that the last mesh drawed in the second buffer. How to write code?
here's a full description.
I'm implementing Volume Rendering in Unity these days (using guide at http://http.developer.nvidia.com/GPUGems/gpugems_ch39.html). And I've come with some problems when I try to implement the lighting effects(I'll quickly describe it below, no need to check the page). So for now, I have a script does the rendering( it's a Monobehaviour, I attatched it to a gameobject that represents volume.
void Update(){
// some calculation
...
for ( float slicePlane = 0 ; slicePlane < destination; slicePlane += sliceDistance){
Mesh mesh = new Mesh();
//calculate the slice mesh.
...
//draw the slice mesh.
Graphics.DrawMesh(mesh, Matrix4x4.TRS(transform.position, transform.rotation, transform.localScale),volumeMaterial,gameObject.layer);
}
}
As you can see, it does the rendering by slice the volume many times then draw the mesh. And the result is composed by alpha blending. This works perfectly for now.
So in order to implement the lighting effect, I have to draw the slice mesh to another texture(called light buffer) after the main draw of a slice is over(the next slice drawing requires info from the light buffer, so the correspoinding light buffer draw must be done after each slice is drawn). So I changed my code like this.
void Update(){
// some calculation
...
for ( float slicePlane = 0 ; slicePlane < destination; slicePlane += sliceDistance){
Mesh mesh = new Mesh();
//calculate the slice mesh.
...
//draw the slice mesh.
Graphics.DrawMesh(mesh, Matrix4x4.TRS(transform.position, transform.rotation, transform.localScale),volumeMaterial,gameObject.layer);
//calculate and set some shader properties
...
//draw the slice mesh in light buffer.
Graphics.SetRenderTarget(lightBuffer);
Graphics.DrawMesh(mesh, Matrix4x4.TRS(transform.position, transform.rotation, transform.localScale),lightBufferMaterial, gameObject.layer);
}
}
then it failes, It seems it's due to while DrawMesh submit the draw request to pipeline, SetRenderTarget does its job where it is.
Also, I tried to use a second camera to do the light buffer rendering, by change the parameter in DrawMesh to only draw in the second camera. But when I open the Frame Debugger, I fount that draw calls are grouped together by camera, so the light buffer rendering begins after all the slices are rendered in the main camera( I didn't really examine this, but I assume the draw call order is the same as shown in the hierachry of frame debugger.). But as I mentioned, the light buffer should be drawed to instantly after each slice is drawn in the main camera.
I'm quite confused now. plz help.
so I've found a workaround for my project. Ins$$anonymous$$d of using Draw$$anonymous$$esh, I use Draw$$anonymous$$eshNow in OnRenderObject. And Draw$$anonymous$$eshNow works well with SetRenderTarget as expected. But Draw$$anonymous$$eshNow doesn't interact with lighting or reflection, which is a disadvantage. I wonder if there's a better solution?
Your answer