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 Gundam · Jan 30, 2013 at 06:52 AM · camerashaderrendertexturerenderwithshader

How to use Camera.RenderWithShader() to modify rendered image?

Hi

Can anybody please help me understand how to work with the Camera.RenderWithShader() function?


I am rendering one of my cameras into a RenderTexture and everything works as it should, however I desperately need to "trim" the rendered image by cutting off (via alpha), lets say, 10% of pixels from left, right, top and bottom sides...


I believe I can cut off parts of the texture with the help of the following shader, however it requires a texture _TestTex to work... And I have no idea how to pass a texture to this shader :( I tried calling

Shader.SetGlobalTexture("_TestTex", myCamera.targetTexture);

and

Shader.SetGlobalTexture("_TestTex", myCamera.renderer.material.mainTexture);,

but I'm definitely doing something wrong... Please help me solve my dilemma!


Shader "Test/TestShader" { Properties { _TestTex ("Base (RGB)", 2D) = "white" {} }

 SubShader
 {
     Tags
     {
         "RenderType" = "Opaque"
         "Queue" = "Transparent"
         "IgnoreProjector" = "True"
     }

     LOD 100

     Pass
     {
         Cull Off
         Lighting Off
         ZWrite Off
         Offset -1, -1
         Fog { Mode Off }
         Blend SrcAlpha OneMinusSrcAlpha

         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #pragma fragmentoption ARB_precision_hint_fastest

         #include "UnityCG.cginc"

         sampler2D _TestTex;

         struct appdata_t
         {
             fixed4 vertex : POSITION;
             fixed4 color : COLOR;
             fixed2 texcoord : TEXCOORD0;
         };

         struct v2f
         {
             fixed4 vertex : POSITION;
             fixed4 color : COLOR;
         };

         v2f vert (appdata_t v)
         {
             v2f o;
             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
             o.color.xy = v.texcoord.xy;
             o.color.zw = o.vertex.xy;
             return o;
         }

         fixed4 frag (v2f IN) : COLOR
         {
             fixed4 result = fixed4(0, 0, 0, 0);

             if (IN.color.z < 0.1 || IN.color.z > 0.8 || IN.color.w < 0.1 || IN.color.w > 0.8)
             {
                 result.rgb = tex2D(_TestTex, IN.color.xy).rgb;
                 result.a = 1;
             }
             
             return result;
         }
         ENDCG
     }
 }

}

Comment
Add comment · Show 2
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 Gundam · Feb 08, 2013 at 05:40 AM 0
Share

Thank you very much everybody, much obliged! :)

avatar image iwaldrop · Feb 08, 2013 at 05:52 AM 0
Share

Have you tried looking it up? Does the search I just did help you out?

https://www.google.com/search?q=unity+camera.renderwithshader&oq=unity+camera.renderwithshader&aqs=chrome.0.57j0j60j0j62l2.4003&sourceid=chrome&ie=UTF-8

http://docs.unity3d.com/Documentation/ScriptReference/Camera.RenderWithShader.html

http://answers.unity3d.com/questions/18919/whats-the-good-usage-of-renderwithshader.html

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

10 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

Related Questions

Applying post-processing effects to RenderTextures 1 Answer

How to get the depth values of the camera view? 1 Answer

One-frame delay with 2 cameras 0 Answers

What is the procedure of capturing a particular region using Render Texture 0 Answers

Unity Grabpass Feature 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