• 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 Nesokas · Nov 03, 2013 at 12:46 AM · shaderlerp

Lerp in Shader

Hello guys,

I'm trying to do some shaders but I got stuck. The idea is to make a LED board/screen with a texture.

Here is the code:

 Shader "Custom/LEDScreen" {
     Properties 
     {
         _MainTex ("Main Texture", 2D) = "white" {}
         _PixelSize ("Pixel Size", float) = 0
         _BillboardSize_x ("Billboard Size X", float) = 0
         _BillboardSize_y ("Billboard Size Y", float) = 0
         _Tolerance ("Tolerance", float) = 0
         _PixelRadius ("Pixel Radius", float) = 0
     }
     
     SubShader 
     {
         Tags { "Queue" = "Geometry" }
         
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
             
             #define KERNEL_SIZE 9
             
             uniform sampler2D _MainTex;
             uniform float _PixelSize;
             uniform float _BillboardSize_x;
             uniform float _BillboardSize_y;
             uniform float _Tolerance;
             uniform float _PixelRadius;
             
             float2 texCoords0;
             float2 texCoords1;
             float2 texCoords2;
             float2 texCoords3;
             float2 texCoords4;
             float2 texCoords5;
             float2 texCoords6;
             float2 texCoords7;
             float2 texCoords8;
             
             struct vertexInput
             {
                 float4 texcoord : TEXCOORD0;
             };
             
             struct fragmentInput
             {
                 float4 pos : SV_POSITION;
                 half2 uv : TEXCOORD0;
             };
             
             fragmentInput vert(vertexInput i)
             {
                 fragmentInput o;
                 o.uv = i.texcoord;
                 
                 return o;
             }
             
             half4 frag(fragmentInput i) : COLOR
             {
                 float4 avgColor;
                 float2 texCoordsStep = float2(1.0/(float(_BillboardSize_x)/float(_PixelSize)), 1.0/(float(_BillboardSize_y)/float(_PixelSize)));
                 float2 pixelRegionCoords = frac(i.uv/texCoordsStep);
                 float2 pixelBin = floor(i.uv/texCoordsStep);
                 float2 inPixelStep = texCoordsStep/3.0;
                 float2 inPixelHalfStep = inPixelStep/2.0;
                 
                 texCoords0 = float2(inPixelHalfStep.x, inPixelStep.y*2.0 + inPixelHalfStep.y) + pixelBin * texCoordsStep;
                 texCoords1 = float2(inPixelStep.x + inPixelHalfStep.x, inPixelStep.y*2.0 + inPixelHalfStep.y) + pixelBin * texCoordsStep;
                 texCoords2 = float2(inPixelStep.x*2.0 + inPixelHalfStep.x, inPixelStep.y*2.0 + inPixelHalfStep.y) + pixelBin * texCoordsStep;
                 texCoords3 = float2(inPixelHalfStep.x, inPixelStep.y + inPixelHalfStep.y) + pixelBin * texCoordsStep;
                 texCoords4 = float2(inPixelStep.x + inPixelHalfStep.x, inPixelStep.y + inPixelHalfStep.y) + pixelBin * texCoordsStep;
                 texCoords5 = float2(inPixelStep.x*2.0 + inPixelHalfStep.x, inPixelStep.y + inPixelHalfStep.y) + pixelBin * texCoordsStep;
                 texCoords6 = float2(inPixelHalfStep.x, inPixelHalfStep.y) + pixelBin * texCoordsStep;
                 texCoords7 = float2(inPixelStep.x + inPixelHalfStep.x, inPixelHalfStep.y) + pixelBin * texCoordsStep;
                 texCoords8 = float2(inPixelStep.x*2.0 + inPixelHalfStep.x, inPixelHalfStep.y) + pixelBin * texCoordsStep;
                 
                 avgColor = tex2D(_MainTex, texCoords0) + 
                             tex2D(_MainTex, texCoords1) + 
                             tex2D(_MainTex, texCoords2) + 
                             tex2D(_MainTex, texCoords3) + 
                             tex2D(_MainTex, texCoords4) + 
                             tex2D(_MainTex, texCoords5) + 
                             tex2D(_MainTex, texCoords6) + 
                             tex2D(_MainTex, texCoords7) +
                             tex2D(_MainTex, texCoords8);
                            
                 avgColor = avgColor/float(9);
                 
                 float2 powers = pow(abs(pixelRegionCoords - 0.5), float2(2.0));
                 float radiusSqrd = pow(_PixelRadius, 2.0);
                 float gradient = smoothstep(radiusSqrd - _Tolerance, radiusSqrd + _Tolerance, powers.x + powers.y);
                 
                 return lerp(avgColor, float4(0, 0, 0, 1.0), gradient);
             }
                     
             ENDCG
         }
     }
     
     Fallback "Diffuse"
 }

Basically what's happening is that I'm getting an error somewhere in this code. If I comment one of the lines where I'm adding the average color (any line will do) the problem no longer exists. However if the line stays and I return just the avgColor I also don't get the error.

Any idea on why this is happening??

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 Nesokas · Nov 03, 2013 at 02:52 PM 0
Share

Yes that solved the problem thanks a lot.

avatar image Professor Snake · Nov 24, 2013 at 01:54 PM 0
Share

Please click the tick button next to my answer in order to mark this question as answered.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Professor Snake · Nov 03, 2013 at 11:11 AM

Try using #pragma target 3.0 to explicitly target Shader Model 3. Maybe you've hit the limitations of SM2.0.

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Cubemaps Lerp 0 Answers

Weird artifacts in cg shader with lerp() 1 Answer

Shader conversion 0 Answers

Blending between Unlit/Color and Standard shaders 1 Answer

shader if else performance 3 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