Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NeatFreak · Mar 01, 2021 at 05:11 PM · shadershadersshader programming

Setting custom camera projection matrix

Hi. I am trying to set "unity_CameraProjection" parameter of the shader for material to use different FOV but unity prevents me just setting it directly. I know i can set it to a different float4x4 and change it in vertex program but i want this to effect unity's Standart shaders too and i can't access their vertex programs. alt text

Also tried rendering meshes with Graphics.DrawMeshNow with GL.LoadProjectionMatrix(MyCustomProjection) but DrawMeshNow doesn't calculate lightning and DrawMesh not working with GL.LoadProjectionMatrix(MyCustomProjection).

Basically I want to render specific meshes with different FOV without using custom shaders. Any ideas that my noob brain couldn't come with? Thanks.

Update: overriding in vertex program of surface shaders also works but it doesn't change lighting.

         #pragma surface surf Standard fullforwardshadows vertex:vert
 
         // ............
 
         float4x4 custom_MatrixVP;
 
         void vert (inout appdata_full v) {
             unity_MatrixVP = custom_MatrixVP;
         }
2.png (20.8 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Mar 01, 2021 at 11:52 PM

The Camera class is the origin for all normal mesh rendering operations. So you should set the projection matrix of your camera. Usually you only have one camera and if that is tagged Main Camera you can get that camera by Camera.main.


Though if you want to render only certain objects with a different fov, this is kinda tricky and most likely does not work the way you think. You can use two cameras where the second one is rendering over the first one and seperate your geometry by layers and appropriate culling masks. However the result may not be what you think it is. Most lighting calculation would probably break as the two different projections are not really compatible in any way. Keep in mind that the screen only has one depth buffer, one stencil buffer and everything has to be rendered into the same screen buffer.


So your request seems kinda strange and I'm wondering what you think the endresult should look like? What you could do is apply your own additional deformation matrix in a custom shader to your objects since that's essentially what a different fov does. Keep in mind that the fov is a property of the camera and has nothing to do with the actual object being viewed. What exact problem you try to solve with your custom projection matrix and what should the endresult look like?

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 NeatFreak · Mar 02, 2021 at 10:03 AM 0
Share

I already know the two cameras method but I am using stencil write/read with some objects and it just doesn't work with 2 cameras. I've tried trust me. Also it's expensive to use two cameras.

All lighting/geometry calculated on the shader of the object being rendered with using the model view and projection matrices passed to the shader by unity itself internally. And i want to override these matrices for specific objects that uses standart shaders but it seems that's not possible for some reason, unity prevents me to just set them with material.SetMatrix("unity_CameraProjection").

I have already achived the result that i want by passing a custom view * projection matrix to a custom shader and replacing ObjectToClipPos() calculation in vertex program.

I am trying to render environment with FOV 90, gun and the arms with FOV 30 so when the user changes FOV it doesn't affect how the gun looks.

avatar image
0

Answer by NeatFreak · Mar 02, 2021 at 11:44 AM

UPDATE: Currently trying to override "UnityShaderVariables.cginc" with a custom version that changes view model matrix from unity_MatrixVP to custom_MatrixVP that is passed from custom script, but still it doesn't seems to work for some reason.

         //  Base forward pass (directional light, emission, lightmaps, ...)
         Pass {
             Name "FORWARD"
             Tags { "LightMode" = "ForwardBase" }
 
             Blend [_SrcBlend] [_DstBlend]
             ZWrite [_ZWrite]
 
             CGPROGRAM
             #pragma target 3.0
 
             // -------------------------------------
             #pragma shader_feature_local _NORMALMAP
             #pragma shader_feature_local _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
             #pragma shader_feature _EMISSION
             #pragma shader_feature_local _METALLICGLOSSMAP
             #pragma shader_feature_local _DETAIL_MULX2
             #pragma shader_feature_local _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
             #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
             #pragma shader_feature_local _GLOSSYREFLECTIONS_OFF
             #pragma shader_feature_local _PARALLAXMAP
 
             #pragma multi_compile_fwdbase
             #pragma multi_compile_fog
             #pragma multi_compile_instancing
             // Uncomment the following line to enable dithering LOD crossfade. Note: there are more in the file to uncomment for other passes.
             //#pragma multi_compile _ LOD_FADE_CROSSFADE
 
             #pragma vertex vertBase
             #pragma fragment fragBase
             
             #include "OverrideUnityShaderVariables.cginc" // custom include file that overrides matrices
             #include "UnityStandardCoreForward.cginc"
 
             ENDCG
         }


Also tried overriding unity_CameraProjection and unity_CameraInvProjection within custom include file. Still no results.

Comment
Add comment · Show 2 · 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 NeatFreak · Mar 02, 2021 at 11:19 PM 0
Share

Another dead end. Turns out this cginc file cannot be overwritten or discarded from shaders.

avatar image ToruTheRedFox NeatFreak · Feb 08 at 06:27 PM 0
Share

it very much can, but requires the shader to be set up in a way that makes it completely independent from anything that's in there, and that usually would be done by defining the stuff you need elsewhere

afaik, certain types of shaders do require it though

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

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

Shader Problem 0 Answers

perform action on shader compilation/update 0 Answers

Unity 3d Sprite Shader (How do I limit Max Brightness to 1 with Multiple Lights Hitting) 0 Answers

Stretching when using world coordinates 2 Answers

Shader: How to write a shader that respects sorting order first and then depth 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