• 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 Dixiepig · Oct 09, 2017 at 04:55 AM · shadersshader programming

Pass UV coordinates to fragment shader without textures?

I want to make procedural textures that use UV input as coordinates.

For example, the simple shader below makes a linear black->white blend across the UV map.

Below I'm using the TRANSFORM_TEX() function to get the UVs in the vertex shader, but it requires a texture as input.

Is there a function like TRANSFORM_TEX() that returns UVs, but doesn't require a texture input?

 Shader "Unlit/heatmap" {
     Properties {
         _MainTex ("Texture", 2D) = "white" {}
     }
 
     SubShader {
         Pass {
             CGPROGRAM
 
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
 
             struct v2f{
                 float4 pos: SV_POSITION;
                 fixed2 uv: TEXCOORD0;
             };
 
             v2f vert (appdata_base v) {
                 v2f vOutput;
                 vOutput.pos = UnityObjectToClipPos(v.vertex);
                 vOutput.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
                 return vOutput;
             }
 
             fixed4 frag (v2f vOutput) : SV_Target {
                 fixed f = vOutput.uv[0];
                 fixed4 texcol = fixed4 (f, f, f, 1.0);
                 return texcol;
             }
 
             ENDCG
         }
     }
 }
 
Comment
Add comment · Show 1
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 vTimePawel · Dec 03, 2018 at 04:11 PM 0
Share

any updates on this?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Brad-Newman · Feb 15, 2019 at 09:49 PM

Instead of doing a TRANSFORM_TEX, you can manually apply tiling and offsets in the frag. @vTimePawel

 Shader "Unlit/heatmap" {
      Properties {
          _TileX ("Tile X", Float) = 1.0
          _TileY ("Tile Y", Float) = 1.0
          _OffsetX ("Offset X", Float ) = 0.0
          _OffsetY ("Offset Y", Float ) = 0.0
      }
  
      SubShader {
          Pass {
              CGPROGRAM
  
              #pragma vertex vert
              #pragma fragment frag
              #include "UnityCG.cginc"
 
              float _TileX;
              float _TileY;
              float _OffsetX;
              float _OffsetY;
 
              struct v2f{
                  float4 pos: SV_POSITION;
                  fixed2 uv: TEXCOORD0;
              };
  
              v2f vert (appdata_base v) {
                  v2f vOutput;
                  vOutput.pos = UnityObjectToClipPos(v.vertex);
                  vOutput.uv = v.texcoord;
 
                  return vOutput;
              }
  
              fixed4 frag (v2f vOutput) : SV_Target {
                  fixed2 uvs = float2(vOutput.uv.x * _TileX, vOutput.uv.y * _TileY);
                  uvs += float2(_OffsetX, _OffsetY);
                  fixed f = uvs[0];
                  
                  fixed4 texcol = fixed4 (f, f, f, 1.0);
                  return texcol;
              }
  
              ENDCG
          }
      }
  }
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 Bunny83 · Feb 15, 2019 at 10:37 PM 0
Share

Right, actually the "TRANSFOR$$anonymous$$_TEX" is not a method but a simple macro which looks like this:

 // Transforms 2D UV by scale/bias property
 #define TRANSFOR$$anonymous$$_TEX(tex,name) (tex.xy * name##_ST.xy + name##_ST.zw)

You can find it in the UnityCG.cginc file inside the CGIncludes folder (/Unity/Editor/Data/CGIncludes/)


This macro just uses the "name" of the texture sampler you pass in and just does the calculations inline. So when you do

 vOutput.uv = TRANSFOR$$anonymous$$_TEX (v.texcoord, _$$anonymous$$ainTex);

the actual code generated is simply:

 vOutput.uv = (v.texcoord.xy * _$$anonymous$$ainTex_ST.xy + _$$anonymous$$ainTex_ST.zw);

In this case Unity automatically sets the "_$$anonymous$$ainTex_ST" variable based on the settings of the material. If you don't have any texture sampler, Unity doesn't have any prepared scaling / offset for you, so you have to provide your own if you actually need them. There's no problem to directly use the inco$$anonymous$$g UV. The scaling and offset is just a material specific parameter to scale / offset the coordinates.

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

80 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

Related Questions

Adding object to scene causes problem with other objects material/shader 0 Answers

Flat Shading / No Vertex Interpolation 2 Answers

Shader that renders pixel with highest alpha value? 0 Answers

Multi-shader Stencil Buffer not working as expected,Multi-shader Stencil not working as expected 0 Answers

Unlit Transparent With Color Support 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