Thanks for your reply, aubergine. However I cannot see what the difference is to what I tried to do in the first place (see initial post above). Maybe you can help me figure out what the problem could be. Perhaps I’m doing something wrong in the shader? Here’s the initialization code of the RenderTextures (note that they’re not temporary, but this shouldn’t be an issue…)
RenderTexture[] mrtTex = new RenderTexture[3];
RenderBuffer[] mrtRB = new RenderBuffer[3];
//...
void Start()
{
Shader shader = Shader.Find( "Custom/GBuffers" );
if( !shader )
throw new Exception( "GBuffers shader not found" );
this.gBuffersMaterial = new Material( shader );
this.cameraCopy = new GameObject();
this.cameraCopy.transform.localPosition = Camera.main.transform.localPosition;
this.cameraCopy.transform.localRotation = Camera.main.transform.localRotation;
this.cameraCopy.transform.localScale = Camera.main.transform.localScale;
this.cameraCopy.name = Camera.main.name + " copy";
Camera cam = this.cameraCopy.AddComponent<Camera>();
cam.CopyFrom( Camera.main.camera );
cam.enabled = false;
this.positionTarget = new RenderTexture( (int)this.cameraCopy.camera.pixelWidth, (int)this.cameraCopy.camera.pixelHeight, 32, RenderTextureFormat.ARGBFloat );
this.positionTarget.wrapMode = TextureWrapMode.Clamp;
this.positionTarget.filterMode = FilterMode.Point;
this.normalTarget = new RenderTexture( (int)this.cameraCopy.camera.pixelWidth, (int)this.cameraCopy.camera.pixelHeight, 32, RenderTextureFormat.ARGBFloat );
this.normalTarget.wrapMode = TextureWrapMode.Clamp;
this.normalTarget.filterMode = FilterMode.Point;
this.ambientTarget = new RenderTexture( (int)this.cameraCopy.camera.pixelWidth, (int)this.cameraCopy.camera.pixelHeight, 32, RenderTextureFormat.ARGBFloat );
this.ambientTarget.wrapMode = TextureWrapMode.Clamp;
this.ambientTarget.filterMode = FilterMode.Point;
this.mrtTex[0] = this.positionTarget;
this.mrtTex[1] = this.normalTarget;
this.mrtTex[2] = this.ambientTarget;
for( int i = 0; i < this.mrtTex.Length; i++ )
this.mrtRB[i] = this.mrtTex[i].colorBuffer;
}
In OnPreRender I do the following:
void OnPreRender()
{
if( !this.enabled )
return;
this.cameraCopy.camera.CopyFrom( Camera.main.camera );
RenderTexture lastActive = RenderTexture.active;
Graphics.SetRenderTarget( mrtRB, mrtTex[0].depthBuffer );
GL.Clear( false, true, Color.clear );
this.cameraCopy.camera.RenderWithShader( this.gBuffersMaterial.shader, "" );
RenderTexture.active = lastActive;
}
Again, replacementTag is empty, causing a rendering of all the objects in the scene. My fragment shader looks as follows:
struct PixelOutput
{
float4 pos : COLOR0;
float4 normal : COLOR1;
float4 ambient : COLOR2;
};
PixelOutput frag(v2f i) : COLOR {
PixelOutput o;
o.pos = float4( 1.0, 0.0, 0.0, 1.0 );
o.normal = float4( 0.0, 1.0, 0.0, 1.0 );
o.ambient = float4( 0.0, 0.0, 1.0, 1.0 );
return o;
}
Do you see any problems with that code? There is no output on the three set rendertargets whatsoever.
Thanks,
iko