Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by taoa · Jun 06, 2010 at 09:39 PM · shader-replacementrenderwithshaderonpostrender

What's the good usage of RenderWithShader?

Hello

I'm trying to use the underrated Camera function called RenderWithShader.

For this, I've made a fake shader that just renders pink:

Shader "pink"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        Pass { Color (1,0,1,0) }
    }
    FallBack "Specular"
}

and I want to use it in order to replace any material that uses a shader tagged "Opaque".

Here's my code attempt:

using UnityEngine;

public class RWStest : MonoBehaviour { Shader repl = null; public RenderTexture RT = null; void Start( ) { repl = Shader.Find( "pink" ); }

 void OnPostRender( )
 {
     Debug.Log( "OnPostRender" );
     RT = RenderTexture.GetTemporary( Screen.width / 4, Screen.height / 4 );
     gameObject.camera.targetTexture = RT;
     gameObject.camera.RenderWithShader( repl, "Opaque" );
     gameObject.camera.targetTexture = null;

     RenderTexture.ReleaseTemporary( RT );
 }

}

Running this attached to the main camera produces strange results:

First, all I end up with is a dark grey screen.

Second, in the console, I only see my debug "OnPostRender" appear once. Even though it's still running. And no error message whatsoever.

If I move my RenderTexture.GetTemporary into an Update function, then a new temporary render texture gets created every frame (so Update is definitely called every frame) but none of those gets released (because OnPostRender seems to be called only once).

Finally, if I create a RenderTexture at Start instead of getting a temporary one like so:

public RenderTexture RT = null;
void Start( )
{
    RT = new RenderTexture( Screen.width / 4, Screen.height / 4, 32 );
    RT.Create( );
    repl = Shader.Find( "pink" );
}

I still end up with a dark grey screen, but I can see in the inspector the RenderTexture created (since I made it public) and it's basically my scene if it was totally empty (you know that neutral blue whenever you create a new scene).

Am I doing something wrong here? Is OnPostRender supposed to be called only once like that? Should RenderWithShader be called from a specific place? And don't tell me I can't use RenderTextures with it otherwise I fail to see the point of it :)

Or is this a bug?

Thank you.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image taoa · Jun 06, 2010 at 09:56 PM 0
Share

I realized I made a mistake in the way I was using the function. I replaced "Opaque" by "RenderType" like so: gameObject.camera.RenderWithShader( repl, "RenderType" and now my geometry is being rendered pink in the render texture. But only once. If I move the camera a bit, the content of the render texture doesn't change because OnPostRender isn't called more than once.

Replacing OnPostRender with OnPreRender makes no difference to those symptoms.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by taoa · Jun 15, 2010 at 01:05 PM

It is a bug, it has been reported as such, and it isn't going to be fixed because it might hurt other things.

A workaround is to use another camera.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TommiH · Oct 19, 2012 at 08:43 AM 0
Share

Hello. I know this is an old answer, but I'm having a similar issue. Could you clarify which one of taoa's issues is a bug? The OnPostRender thing or using RenderWithShader entirely?

avatar image
0

Answer by Tetrad · Jun 06, 2010 at 09:41 PM

Are you absolutely sure it's only being called once? It's a common mistake to have "Collapse" turned on in the console log and only see the first time a log is put in the console.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image taoa · Jun 06, 2010 at 09:53 PM 0
Share

Yes I am.......

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Will Camera.RenderWithShader work with Graphics.DrawMesh (and DrawMeshInstanced)? 0 Answers

I cannot pass parameters to my custom shaders... help please... 2 Answers

Displaying a different shader ONLY where a spotlight hits an object. 1 Answer

render to framebuffer twice - how? 0 Answers

Setting Material Tag at runtime for replacement shader tricks 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges