Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 khromasquad · May 26, 2018 at 12:33 AM · renderinggraphics

Static GameObjects Losing Normal On Play

Please, someone help! I'm loosing my mind over this...

Parented to my MainCam I have another camera (CopyCam) with the same params, local transform equal to Identity and the Camera Component disabled. On my MainCam I added a script that, at OnRenderImage, calls RenderWithShader on the CopyCam with a shader that renders the fragments normals as RGB. The CopyCam.targetTexture is assigned as a RenderTexture (serialized RenderTexture that gets a temporary render texture when the screen changes size). All works fine, and the generated texture looks correct (left image), but when I hit play, some of the normals goes missing (right image): alt text

Did some digging and found that this only happens when my GameObjects are marked as static. WHY!? Is this a setting? Can I have them as static and keep the normals correct?

unity-question.png (39.4 kB)
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

1 Reply

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

Answer by Bunny83 · May 26, 2018 at 12:52 AM

This is most likely an issue with the shader you're using. Keep in mind that static gameobjects are statically batched. So they will be combined into a single gameobject and rendered as a single object. So the origin and local to world matrix will be different from those of the original objects.

Comment
Add comment · Show 3 · 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 khromasquad · May 26, 2018 at 01:05 AM 0
Share

Thanks for replying so quickly! So, this is my replacement shader:

 Shader "ReplacementShader/NormalAndDepth"
 { 
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 100
 
         Pass
         {
             CGPROGRA$$anonymous$$
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float3 normal : NOR$$anonymous$$AL;
             };
 
             struct v2f
             { 
                 float4 vertex : SV_POSITION;
                 float3 camPos : TEXCOORD0;
                 float3 normal : TEXCOORD1;
             };
 
             
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.camPos = UnityObjectToViewPos(v.vertex);
                 o.normal = v.normal; 
                 return o;
             }
 
             fixed4 frag (v2f i) : SV_Target
             { 
                 return  fixed4(normalize(i.normal).xyz, (-i.camPos.z * _ProjectionParams.w)); 
             }
             ENDCG
         }
     }
 }
 

Am I doing something wrong?

avatar image Bunny83 khromasquad · May 26, 2018 at 02:02 AM 1
Share

You do not transform the normal from local to world space. You just use the local space normal. When the objects are batched all vertex parameters get a new local space and are transformed.

use

 o.normal = UnityObjectToWorldNormal(v.normal);

ins$$anonymous$$d of

 o.normal = v.normal; 



Also it seems a bit weird that you call the other variable "camPos" as it doesn't represent the position of the camera but the position of the vertex in the localspace of the camera.

avatar image khromasquad Bunny83 · May 26, 2018 at 04:26 AM 0
Share

Thanks, it seems to be working now! And yes, its a typo, supposed to be viewPos. Again, thanks!

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

104 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 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 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 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 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

Why is the CPU usage so high? 1 Answer

Complex Depth Shader 0 Answers

How to make transparent only colliding part of object? 0 Answers

Shading with GL.TRIANGLE_STRIP 2 Answers

Native rendering plugin D3D11 device type not being set in x86 builds. 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