Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by samnunn92 · Jul 07, 2016 at 05:48 PM · shaderrenderinggraphicsshader writinghlsl

Custom shader samples pixels per-texture rather than from the screen

Hi all,

Thanks for reading (and sorry if this is a bit wordy), I've had a Google and a think on this but can't seem to work out what's happening. I'm new to shaders and after a few tutorials have started to try to write my own, and I've started by trying to attempt to emulate old VHS/CRT effects for a game I'm planning. So far I've done a very basic colour blur/bleed effect, which works, and I've set it as the render material for the Main Camera using SetReplacementShader() as I'm intending to use it as an image effect. The shader code is here: http://pastebin.com/09NaxyKU

It works to a degree, and all the objects in the scene are rendered with their colours displaced, but it seems to be sampling on a per-texture basis rather than sampling from the whole screen as I had expected. This means that the effect on a 50x50 texture is way more obvious (and less convincing) than on a 500X500 texture, and scaling the images down or up doesn't change the effect size, just makes the pixels look really blocky. The effect also abruptly cuts off at the edge of the sprite instead of having it bleed over into the background which is what I was expecting.

I wanted the effect to apply to the whole screen equally rather than varying on each texture, and I thought by applying it to the camera it would do so. Is there something I've missed/fundamentally not understood (more likely!) I am suspicious that _MainTex needs to be set to a render texture or something but can't for the life of me work out how to capture a render texture, apply the effect, then display the result.

Any help would be greatly appreciated! Thanks

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 Namey5 · Jul 08, 2016 at 04:08 AM

You would want to do this as an image effect, otherwise this is just going to be changing the albedo texture per object. The shader would need to be more along the lines of;

 Shader "Hidden/ColourBleedShader"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
     }
     SubShader
     {
  
         Pass
         {
             Cull Off
             ZWrite Off
             ZTest Always
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
            
             #include "UnityCG.cginc"
  
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
  
             struct v2f
             {
                 float2 uv : TEXCOORD0;
                 float4 vertex : SV_POSITION;
             };
  
             sampler2D _MainTex;
             float4 _MainTex_TexelSize;
            
             v2f vert (appdata v)
             {
                 v2f o;
                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.uv = v.uv;
                 return o;
             }
  
             float4 colorBleed (v2f i, sampler2D mainTexture, float4 texelSize)
             {
                 fixed4 tex = tex2D(_MainTex, i.uv);
  
                 float2 RoffsetUV = float2(i.uv.x - texelSize.x*2, i.uv.y);
                 float2 BoffsetUV = float2(i.uv.x + texelSize.x*2, i.uv.y);
  
                 fixed4 rTex = tex2D(_MainTex, RoffsetUV);
                 fixed4 bTex = tex2D(_MainTex, BoffsetUV);
  
                 rTex = float4 (rTex.r, 0, 0, 0.5);
                 bTex = float4 (0, 0, bTex.b, 0.5);
                 tex = float4 (0, tex.g, 0, 0.5);
  
                 float2 myuv = float2(i.uv.x+_MainTex_TexelSize.x*5, i.uv.y+_MainTex_TexelSize.x*20);
                 fixed4 endTex = float4 (rTex + bTex + tex);
  
                 return endTex;
  
             }
            
             fixed4 frag (v2f i) : SV_Target
             {
  
                 float4 finalTex = colorBleed(i, _MainTex, _MainTex_TexelSize);
  
                 return finalTex;
  
                 //TODO: make picture a bit blurrier, add grain effect, scanlines? CRT screen bend?
  
  
             }
             ENDCG
         }
     }
 }

And a script to use this as an image effect to attach to your camera (the following is JS, I can change if you wish).

 #pragma strict
 
 @script ExecuteInEditMode
 @script AddComponentMenu ("Image Effects/Custom/Colour Bleed")
 
 private var mat : Material;
 
 private var shader : Shader;
 
 function OnRenderImage (src : RenderTexture, dest : RenderTexture)
 {
     if (!shader)
     {
         shader = Shader.Find ("Hidden/ColourBleedShader");
     }
     if (!mat)
     {
         mat = new Material (shader);
     }
 
     Graphics.Blit (src, dest, mat);
 }
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 samnunn92 · Jul 08, 2016 at 12:19 PM 0
Share

Thanks so much for your reply - that camera script seems to have done the trick! I can't see any difference in your shader script beyond the ShaderLab instructions inside Pass, am I missing something? Anyway thanks so much, it's working as intended now :) cheers!

avatar image Namey5 samnunn92 · Jul 08, 2016 at 01:31 PM 0
Share
 Cull Off
 ZWrite Off
 ZTest Always

Have been added and the blending has been removed. This is important as it is an image effect, and is being applied to the camera 'quad' rather than an object.

avatar image samnunn92 Namey5 · Jul 08, 2016 at 09:53 PM 0
Share

Ah ok, that makes sense! I'll look into culling/depth as it's something I really need to brush up on. Thanks again for your help.

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

82 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

Related Questions

Opaque ("overwrite") Projector Shader for Decal Projection 1 Answer

HTC Vive Controllers are PINK in Unity 2 Answers

How can I rotate Cubemap? 0 Answers

Simple "Render on Top of Everything" Shader? 1 Answer

[URP] Transp Shadergraph has shadows? 2 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