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
1
Question by g_mister · Dec 07, 2012 at 01:32 AM · uvcoordinatesuv coordinates

Is there a way to access uv coordinates in a custom lighting model?

Im trying to do this in a BRDF shader I've written and basically what I want to is have a different ramp texture used based on the mask texture provided.

This is my code:

 Shader "Learning/BRDF_FULL_MASK"
 {
 
 Properties {
 
     _Color ("Main Color", Color) = (1,1,1,1)
     _SpecColor ("Specular Color", Color) = (1,1,1,1)
     _Shininess ("Specualr Power", Range(-1,2)) = 0.5
     
     _MainTex ("Main Texture", 2D) = "white" {}
     _BumpMap ("Normal Texture", 2D) = "white" {}
     _MaskTex ("Mask (RGBA)", 2D) = "white" {}
     
     _Ramp_RED ("Shading Ramp 1", 2D) = "gray" {}
     _Ramp_GREEN ("Shading Ramp 2", 2D) = "gray" {}
     _Ramp_BLUE ("Shading Ramp 3", 2D) = "gray" {}
 
 }
 
 SubShader {
 
     Tags {"RenderType" = "Opaque"}
 
     CGPROGRAM
     #pragma surface surf BRDF_FULL
     #pragma target 3.0
     
     float _Shininess;
     float4 _Color;
     sampler2D _MainTex, _MaskTex, _BumpMap, _Ramp_RED, _Ramp_GREEN, _Ramp_BLUE;
     
     struct Input {
         float2 uv_MainTex;
         float2 uv_BumpMap;
     };
     
     struct SurfaceOutputCustom {
         
         fixed3 Albedo;
 
         fixed3 Normal;
 
         fixed3 Emission;
 
         half Specular;
 
         fixed Gloss;
 
         fixed Alpha;
         
         fixed2 uv_MaskTex;
 
     };
     
     half4 LightingBRDF_FULL (SurfaceOutputCustom s, half3 lightDir, half3 viewDir, half3 atten)
     {
         float2 uv_MaskTex;
         
         //Get the dot product of the sirface normal and light direction
         half NdotL = dot (s.Normal, lightDir);
         half NdotE = dot (s.Normal, viewDir);
         
         //Normalize diffuse
         half diff = NdotL * 0.3 + 0.5;
         
         //BRDF
         float2 brdfUV = float2 (NdotE * 0.8, diff);
         float3 rBRDF = tex2D(_Ramp_RED, uv_MaskTex).rgb;
         float3 gBRDF = tex2D(_Ramp_GREEN, uv_MaskTex).rgb;
         float3 bBRDF = tex2D(_Ramp_BLUE, uv_MaskTex).rgb;
             
         //Specular
         //half3 h = normalize(lightDir + viewDir);
         //float _Shininess = max (0, dot(s.Normal, h));
         //float _SpecColor = pow (_Shininess, 48.0);
         
         float3 m = tex2D(_MaskTex, uv_MaskTex).rgb;
     
         //half4 c = tex2D (_MainTex, IN.uv_MainTex);
     
         float3 res = m;
         
         if (m.r >= .1) res = rBRDF.rgb;
     
         else if (m.g >= .1) res = gBRDF.rgb;
     
         else if (m.b >= .1) res = bBRDF.rgb;
     
         half4 c;
         //c.rgb = (s.Albedo * _LightColor0.rgb * BRDF + _LightColor0.rgb * _SpecColor) * (NdotL * atten * 2);
         c.rgb = (s.Albedo * _LightColor0.rgb * res) * (atten * 2);
         c.a = s.Alpha;
         return c;
     
     }
     
     void surf (Input IN, inout SurfaceOutputCustom o) 
     {
         //Handle textures
         //Handle Diffuse map
         fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
         //Handle Normal map
         float3 bump = UnpackNormal (tex2D(_BumpMap, IN.uv_BumpMap));
         
         //Apply textures
         //Apply Diffuse texture
         o.Albedo = tex.rgb * _Color.rgb;
         //Apply Normal map
         o.Normal = bump.rgb;
         
         //Specular
         o.Specular = _Shininess;
         o.Gloss = tex.a;
     
     }
     
     ENDCG
 
 }
 
 Fallback "Diffuse"
 
 }

and this is the result I get from that:

alt text

and the shader mask and how I want the ramps to be applied to the model:

alt text

I think my problem is that I can't get uv coordinates calculated inside the custom lighting model. Is this possible? If it isn't what is a way to make this shader work?

p.s. The model isn't mine, I got it here - www.steffenunger.com.

brdf_mask.png (429.4 kB)
mask.png (123.6 kB)
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 g_mister · Dec 07, 2012 at 10:50 PM 0
Share

$$anonymous$$ade some progress I think, still getting errors though. I found this post

http://forum.unity3d.com/threads/145320-How-can-I-get-texture-information-in-custom-surface-shader-lighting-functions

where $$anonymous$$ADmarine states that you can build your own structs to use in a custom lighting model. Awesome! So I did that, SurfaceOutputCustom, and now I get the error "Shader error in 'Learning/BRDF_FULL_$$anonymous$$AS$$anonymous$$': Program 'SurfShaderInternalFunc', incompatible type for parameter #1 ("s") at line 121". I'm pretty sure it has something to do with my SurfaceOutputCustom struct, but I don't know what.

Any help is much appreciated!

Thanks!

p.s. I've updated my code in the above post for you all to see everything.

avatar image g_mister · Dec 11, 2012 at 09:53 PM 0
Share

Fixed the error, I had to reference my custom struct in the "surf" function. The shader isn't shouting at me about errors but isn't working the way I want. I want to use a different ramp for different parts of the uv map that I've masked off in "_$$anonymous$$askTex".

I've added another mask pic to explain better what I want to do.

0 Replies

· Add your reply
  • Sort: 

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

10 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

Related Questions

Translate World transform data to UV offset 2 Answers

Is it possible to rotate uv coordinates in a script? 1 Answer

Is it possible to UV map a cylinder without duplicating a column of vertices? 1 Answer

C# PackTextures & UV Mapping 2 Answers

UV Tiling/repeat 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