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
0
Question by mattirwin · Dec 18, 2010 at 12:25 AM · camerarendertexturedrawtexture

Fading in one camera over another as an overlay with RenderTexture and GUI.DrawTexture. Any way to control pixel clearing?

Update: As of now I'm using this script in my project and locking my objects' movement during the transition. This works fine, but solving the original problem would be a more ideal, general solution.

Download example package - RenderTextureQuestionSubmit.unitypackage

I'm trying to use a RenderTexture and GUI.DrawTexture to do a fade-up of a second camera as an overlay. Once the fade is complete, it switches to a live view of both cameras. I have attached screen shots and a unitypackage showing a ghosting problem that I'm getting.

The example fades in a moving foreground over a static background. Once the fade-in is complete, the foreground camera's targetTexture is set to null, causing both background and foreground cameras to be rendered live. The depth-only option for Clear Flags is used on the foreground camera. The Culling mask of the foreground camera is set to a layer called "Foreground" (you'll have to add this yourself as a custom layer if you download the example package) and the gray plane is assigned to that layer. I have included the shader, but I don't think it's the problem here.

The ghosting seems to be caused by a lack of clearing of the RenderTexture between Camera renders. You can see it on the second screen shot: The horizontal ping-pong motion of the gray plane causes the ghosting effect. I tried to fix it by adding a RenderTexture.Release() each frame, but that causes it to appear darker so the ending of the fade doesn't match the live camera when they are swapped, which causes a jump in apparent brightness value. This is easiest to see in the example if you press M to stop moving, and press R to activate Release-each-frame mode. I think the "darkening" is actually fast flicker.

Thanks in advance for any help!

Key script on my "foreground" camera:

public class CameraFadeUpScript: MonoBehaviour {

public Camera thisCamera;

public bool inTransition; public bool releaseEachFrame=false; public float fadeSpeed;

public float alpha = 1.0f;

private RenderTexture renderTex; private Texture tex;

// Use this for initialization void Start () {

}

void OnEnable() {

 if (renderTex==null) renderTex = new RenderTexture(Screen.width, Screen.height, 24);
 renderTex.Release();

 Debug.Log(Time.time + " Enabled");
 alpha = 0f;
 inTransition = true;
 thisCamera.targetTexture = renderTex;
 tex = renderTex;
 Debug.Log(Time.time + "FG cam enabled");

}

// Update is called once per frame void Update () {

 if (Input.GetKeyDown(KeyCode.R))
 {
     releaseEachFrame = !releaseEachFrame;
 }

 if (inTransition)
 {

     alpha = Mathf.MoveTowards(alpha, 1f, Time.deltaTime * fadeSpeed);
     alpha = Mathf.Clamp(alpha, 0f, 1f);

     Debug.Log(Time.time + "Update: " + alpha);

     if (releaseEachFrame)
     {
         renderTex.Release();
     }
     tex = renderTex;

     if (alpha > 0.99999f)
     {
         inTransition = false;
         thisCamera.targetTexture = null;
         renderTex.Release();
         Debug.Log(Time.time + "Transition to foreground complete");
     }
 }

}

void OnGUI() {

 if (inTransition && alpha < 1f)
 {
     GUI.color = new Color(1f, 1f, 1f, alpha);
     GUI.DrawTexture(new Rect(0, 1, Screen.width, Screen.height), tex, ScaleMode.StretchToFill, true);
     Debug.Log(Time.time + " GUI.DrawTexture");
 }

} }

Notes:

  • This technique is similar to the CrossFadePro script in the wiki.
  • I tried a double-buffering solution with two render textures, where each frame it would alternate between drawing one and Release()-ing one, but this didn't seem to improve upon the darkening observed when you release the rendertexture every frame.
  • Side note, I noticed that the transition between GUI.DrawTexture and the live camera looks best if this rectangle is used: Rect(0, 1, Screen.width, Screen.height). Anyone else see a one pixel jump if you use Rect(0, 0, Screen.width, Screen.height)?

Screenshots:

alt text

alt text

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

No one has followed this question yet.

Related Questions

How to get the rendered texture from a camera, save for later display on another camera? 1 Answer

Camera.RenderWithShader not working as of Unity 5.3? 0 Answers

Graphics.DrawTexture vs GUI.DrawTexture vs Graphics.Blit 0 Answers

First Person Camera Pixelation 1 Answer

DrawTexture on RenderTexture without Camera 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