Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
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
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
1
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
Add comment · 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
 {
     _$$anonymous$$ainTex ("$$anonymous$$ain Texture (RGB)", 2D) = "white" {}
     //_AOBake ("$$anonymous$$ain Texture (A) - AO Bake", 2D) = "white" {}
 }
 
 SubShader {
     Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="opaque"}
     LOD 250
 
 CGPROGRA$$anonymous$$
 #pragma surface surf Lambert noforwardadd 
 
 sampler2D _$$anonymous$$ainTex;
 //sampler2D _AOBake;
 
 struct Input
 {
 fixed2 uv_$$anonymous$$ainTex;
 fixed2 uv2_$$anonymous$$ainTex;
 };
 
 
 void surf (Input IN, inout SurfaceOutput o) {
     fixed4 c = tex2D (_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex);
     fixed4 c2 = tex2D (_$$anonymous$$ainTex, IN.uv2_$$anonymous$$ainTex);
     o.Albedo = (c.rgb * c2.a);
 }
 
 ENDCG
 }
 
 Fallback "$$anonymous$$obile/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(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex*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($$anonymous$$ainTex, IN.uv$$anonymous$$ainTex*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 program$$anonymous$$g. 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.

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

Depth mask with alpha / adding alpha to frag grabpass shader 0 Answers

Combining a custom shader with a normal one 0 Answers

How to add Emission to my custom shader? 2 Answers

Adding a clip() to the default shader? 0 Answers

Shader - What is float3.xy? 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