• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

,Can camera output negative values to render texture? 0 Answers

RenderTexture behaviour inconsistent to Cameras, Rendering two times to a RenderTexture respecting depth, Rendering object visibility 0 Answers

Applying post-processing effects to RenderTextures 1 Answer

Best way to render specific area darker? 0 Answers

One-frame delay with 2 cameras 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges