• 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
Question by Earlybird · Jan 19, 2014 at 02:40 PM · shadershader writinguv2optimise

Is it possible to re-use a texture within a shader on separate uv channels?

Hi, Im trying to optimise a my shader as far as possible, I have a single texture that i want to split into 2 essentially, here's what im trying to do: 1 texture - rgd = uv1 a = uv2

I have an asset that has a diffuse texture layout in uv1 and an AO Bake hidden in the alpha channel that requires uv2.

Currently I am having to pull in the texture with a sampler2D - attach that to a fixed4 and apply the uv1 to it but then to achieve the second uv2 I need to repeat the above? im also having to use two separate texture slots for the same texture? is there a more efficient way of doing this?

my current shader below:

 Shader "Custom/OptimalDiffuse_uv2-AOBake" {
 
 Properties
 {
     _MainTex ("Main Texture (RGB)", 2D) = "white" {}
     _AOBake ("Main Texture (A) - AO Bake", 2D) = "white" {}
 }
 
 SubShader {
     Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="opaque"}
     LOD 250
 
 CGPROGRAM
 #pragma surface surf Lambert noforwardadd 

 
 sampler2D _MainTex;
 sampler2D _AOBake;
 
 struct Input
 {
 fixed2 uv_MainTex;
 fixed2 uv2_AOBake;
 };
 
 
 void surf (Input IN, inout SurfaceOutput o) {
     fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
     fixed4 c2 = tex2D (_AOBake, IN.uv2_AOBake);
     o.Albedo = (c.rgb * c2.a);
 }
 
 ENDCG
 }
 
 Fallback "Mobile/Diffuse"
 }
 

Any Advice would really be welcome, Thanks.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Owen-Reynolds · Jan 19, 2014 at 05:40 PM

You can definitely have a single texture, which is looked-up (`tex2D`'d) multiple times, with different UVs.

Maybe you can compute uv2 from uv1. Otherwise you'll have to figure out how to tell Unity to send just a UV2 array.

But I'm not sure that sending the same texture twice is even a problem. The texture manager usually handles all textrues for all draws. So should notice AOBake uses an already loaded repeat.

Comment
Lo0NuhtiK

People who like this

1 Show 8 · 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 Earlybird · Jan 19, 2014 at 07:52 PM 0
Share

Thanks for the reply, as you can see im pretty new to shaders. would you be able to shed some light in how i might be able to achieve this?

from a users point of view I'd like to be able to pull everything from the single texture?

avatar image Earlybird · Jan 19, 2014 at 07:58 PM 0
Share

This is my attempt but it is now broken:

 Shader "Custom/OptimalDiffuse_uv2-AOBake - WIP" {
 
 Properties
 {
     _MainTex ("Main Texture (RGB)", 2D) = "white" {}
     //_AOBake ("Main Texture (A) - AO Bake", 2D) = "white" {}
 }
 
 SubShader {
     Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="opaque"}
     LOD 250
 
 CGPROGRAM
 #pragma surface surf Lambert noforwardadd 
 
 sampler2D _MainTex;
 //sampler2D _AOBake;
 
 struct Input
 {
 fixed2 uv_MainTex;
 fixed2 uv2_MainTex;
 };
 
 
 void surf (Input IN, inout SurfaceOutput o) {
     fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
     fixed4 c2 = tex2D (_MainTex, IN.uv2_MainTex);
     o.Albedo = (c.rgb * c2.a);
 }
 
 ENDCG
 }
 
 Fallback "Mobile/Diffuse"
 }
avatar image Owen-Reynolds · Jan 20, 2014 at 04:37 PM 0
Share

Using mainTex twice like that (in surf) is absolutely fine. But I think, since you don't have a 2nd texture, you don't have a uv2. Now, something like tex2D(_MainTex, IN.uv_MainTex*2) would be fine. Any legal float2 can be in that 2nd slot.

Non-Unity, you manually send all vertex data, so can just send more to have uv2 in the shader. But Unity does so much for you, I'm not sure how to tell it to send more arbitrary vertex data. Using a 2nd dummy texture (even an empty one?) might be the simplest way to get uv2.

avatar image Earlybird · Jan 20, 2014 at 09:47 PM 0
Share

Hi Owen, thanks again. Can I just confirm by "surf" are you refering to it being a surface shader? Also what is the "*" doing in tex2D(_MainTex, IN.uv_MainTex*2)?

I haven't played with this yet but will do hopefully later on :)

avatar image Owen-Reynolds · Jan 21, 2014 at 05:28 AM 0
Share

Shaders have some magic, but the guts are just programming. The star is the same as in A*2

Show more comments

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

19 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

Related Questions

Shader Help 0 Answers

Any way to access SpriteRenderer mesh UV2? 0 Answers

Trying to blend 3 textures in shader 1 Answer

Shader Working in Shader Graph But Not In Scene 1 Answer

Shader Graph Gore Shader 0 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