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 NorthernVisionStudio · Jun 11, 2015 at 01:49 AM · unity 5shaderimage effectswtf

After upgrade to Unity 5, my post effect fails

Hi and thanks for all your help. I'm not sure where to begin with this one.

Basically, inside of an image effect, during the OnRenderImage method, I create a camera, render with special shader, then delete it. This worked in Unity 4.

Can we no longer render a new camera within OnRenderImage of the main camera?

Here is the shader code:

 Shader "Lemon Joker/Image Effects/LJ_AOFactor"
 {
     Properties
     {
         _AOIntensity ("AO Intensity", Float) = 1.0
     }
     SubShader
     {
         Tags { "AOFactorType"="Properties" }
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             float _AOIntensity;
 
             float4 vert(float4 v : POSITION) : SV_POSITION
             {
                 return mul(UNITY_MATRIX_MVP, v);
             }
 
             float4 frag() : COLOR
             {
                 return float4(_AOIntensity, 0, 0, 0);
             }
             ENDCG
         }
     }
 }

here is the OnRenderImage code:

         [ImageEffectOpaque]
         void OnRenderImage (RenderTexture source, RenderTexture destination)
         {
             // Adjust size
             int height = Screen.height;
             int width = Screen.width;
             if (destination != null)
             {
                 height = destination.height;
                 width = destination.width;
             }
 
             if (outputTex.width != width || outputTex.height != height)
             {
                 RecreateSurfaces(width, height);
             }
 
             var aoFactorTex = RenderTexture.GetTemporary(Screen.width, Screen.height, 16, RenderTextureFormat.RFloat);
             RenderAOFactor(aoFactorTex);
 
             Graphics.Blit(source, depthNormalTex, material);
  
             RenderHBAO(outputTex.GetNativeTexturePtr(), Util.MatrixToArray(GetComponent<Camera>().projectionMatrix),
                 radius, bias, exponent, blurEnable, (int)blurIterations, sharpness, coarseAO, detailAO);
 
             Material blendMat = new Material(Shader.Find("Lemon Joker/Image Effects/BlendAO"));
             blendMat.mainTexture = source;
             blendMat.SetTexture("_AOTex", outputTex);
             blendMat.SetTexture("_AOFactorTex", aoFactorTex);
             Graphics.Blit(source, destination, blendMat);
 
             RenderTexture.ReleaseTemporary(aoFactorTex);
         }

and here is the code which is called from OnRenderImage:

 private void RenderAOFactor(RenderTexture target)
         {
             var go = new GameObject("", typeof(Camera));
             Camera cam = go.GetComponent<Camera>();
             cam.CopyFrom(GetComponent<Camera>());
             cam.renderingPath = RenderingPath.Forward;
             cam.depthTextureMode = DepthTextureMode.None;
             cam.backgroundColor = Color.white;
             cam.targetTexture = target;
             cam.enabled = true;
             cam.RenderWithShader(Shader.Find("Lemon Joker/Image Effects/LJ_AOFactor"), "AOFactorType");
             DestroyImmediate(go);                
         }

If I remove RenderAOFactor, everything else works as expected. And here is a video of what the result of this camera render looks like. Its an RFloat texture format.

Bug Video

Then you get this image sometimes.. what is the tire texture doing here? It appears to me as some kind of bad memory pointer somewhere. Thank you for your help! I'll be sure to return the favor.alt text

2015-06-10-nvs-shaderbug.jpg (134.1 kB)
Comment
Add comment · Show 3
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 zach-r-d · Jun 11, 2015 at 04:44 AM 0
Share

What happens if you create the gameobject+camera only once? You can still call copyfrom and change all the other settings, and you can set enabled=false to prevent it from drawing as part of the normal pipeline.

avatar image NorthernVisionStudio · Jun 11, 2015 at 11:40 PM 0
Share

Thanks, that made it work perfectly. I don't know exactly why that helped, but I have a feeling it has to do with the init of the camera requiring one game loop. If you add an answer to this post, I'll mark it as correct.

avatar image zach-r-d · Jun 12, 2015 at 02:59 AM 0
Share

Great, thanks! I'm not sure 100% why either, but I had a hunch along the same lines.

1 Reply

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

Answer by zach-r-d · Jun 12, 2015 at 02:57 AM

Try creating the gameobject+camera only once. You can still call copyfrom and change all the other settings, and you can set enabled=false to prevent it from drawing as part of the normal pipeline.

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

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Image Effect creation, Wulff Net 0 Answers

Edge Detection script [Legacy Image Effect] - NOT working on mobile (iOS, Android) 0 Answers

Image Effects with 2D sprites 0 Answers

G-Buffers don't exist... 0 Answers

How to make a 2d shader with edge waves 0 Answers


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