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 Balint3DDesign · Feb 27, 2013 at 06:07 AM · shadertexturegraphics

Hatching shader (Two o.Albedo ?)

Hi, all!

I have succesfully implemented the Normal,Specular map to the hatching shader and plus some outline :) But i ran into a problem, no matter how hard I try i can't get the Diffuse working. Perhaps because the hatching uses the Diffuse (o.Albedo)?

So my question is , how could be the (_MainTex) implemented into the shader, so that it has two (2) o.Albedo, or is it not possible?

Any help would greatly appreciated :)

Here is the shader :

 Shader "Custom/Silhoutted Hatching" {
     Properties {
         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
         _Outline ("Outline width", Range (0.0, 0.03)) = .005
         _ShinPower ("Shininess", Range(0,1)) = 0.5
         _GlossPower ("Gloss", Range(0.01,1)) = 0.5
         _BumpMap ("Bumpmap", 2D) = "bump" {}
         _SpecularTex ("Specular Map", 2D) = "gray" {}
         _Hatch0 ("Hatch 0", 2D) = "white" {}
         _Hatch1 ("Hatch 1", 2D) = "white" {}
         _Hatch2 ("Hatch 2", 2D) = "white" {}
         _Hatch3 ("Hatch 3", 2D) = "white" {}                        
     }
     
     CGINCLUDE
     #include "UnityCG.cginc"
 
     struct appdata {
         float4 vertex : POSITION;
         float3 normal : NORMAL;
     };
 
     struct v2f {
         float4 pos : POSITION;
         float4 color : COLOR;
     };
 
     uniform float _Outline;
     uniform float4 _OutlineColor;
 
     v2f vert(appdata v) {
         v2f o;
         o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 
         float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
         float2 offset = TransformViewToProjection(norm.xy);
 
         o.pos.xy += offset * o.pos.z * _Outline;
         o.color = _OutlineColor;
         return o;
     }
     ENDCG
 
     SubShader {
         Tags { "Queue" = "Geometry+11" "RenderType" = "Opaque" }
         Pass {
             Name "OUTLINE"
             Tags { "LightMode" = "Always" }
             Cull Off
             ZWrite Off
 
             Blend SrcAlpha OneMinusSrcAlpha
 
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
 
         half4 frag(v2f i) : COLOR {
             return i.color;
         }
         ENDCG
         }
 
         CGPROGRAM
         #pragma surface surf Hatching
         #pragma only_renderers d3d9
         #pragma target 3.0
         
         struct Input 
         {
             float2 uv_Hatch0;
             float2 uv_BumpMap;
             float3 worldNormal;
             INTERNAL_DATA
         };
         
         sampler2D _Hatch0, _Hatch1, _Hatch2, _Hatch3;
         sampler2D _BumpMap;
         sampler2D _SpecularTex;
         float _ShinPower;
         float _GlossPower;
 
         inline half4 LightingHatching (inout SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
         {
             half3 h = normalize (lightDir + viewDir);
             
             float2 uv = s.Albedo.xy;        
             s.Albedo = 0.0;            
         
             half NdotL = dot (s.Normal, lightDir);
             half diff = NdotL * 0.75 + 0.45;
             
             float nh = max (0, dot (s.Normal, h));
             float spec = pow (nh, s.Gloss * 64) * s.Specular;
             
             half4 c;
             half lightColor = _LightColor0.r * 0.3 + _LightColor0.g * 0.59 + _LightColor0.b * 0.11 * spec * s.Specular;
             half intensity = lightColor * (diff * atten * 2);
             intensity = saturate(intensity);
             
             float part = 1 / 4.0;
             if (intensity <= part)
             {    
                 float temp = intensity;
                 temp *= 4;
                 c.rgb = lerp(tex2D(_Hatch0, uv), tex2D(_Hatch1, uv), temp);            
             }
             if (intensity > part && intensity <= part * 2)
             {
                 float temp = intensity - part;
                 temp *= 4;                
                 c.rgb = lerp(tex2D(_Hatch1, uv), tex2D(_Hatch2, uv), temp);            
             }
             if (intensity > part * 2 && intensity <= part * 3)
             {
                 float temp = intensity - part * 2;
                 temp *= 4;                
                 c.rgb = lerp(tex2D(_Hatch2, uv), tex2D(_Hatch3, uv), temp);            
             }
             if (intensity > part * 3)
             {
                 float temp = intensity - part * 3;
                 temp *= 4;                
                 c.rgb = lerp(tex2D(_Hatch3, uv), 1, temp);
             }
             return c;
         }
 
         void surf (Input IN, inout SurfaceOutput o) 
         {
             o.Albedo.xy = IN.uv_Hatch0;
             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
             float3 specGloss = tex2D(_SpecularTex, IN.uv_BumpMap).rgb;
               o.Specular = specGloss.r * _ShinPower;
               o.Gloss = specGloss.g * _GlossPower;
         }
         ENDCG
     }
     Fallback "Diffuse"
 }
Comment
Add comment · Show 3
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 Sylker · Jul 31, 2013 at 01:11 AM 0
Share

Have you found any solution on this Balint3DDesign? I am trying to accomplish it too, but I am just a beginner. $$anonymous$$y contributions cannot help much. I am keeping trying, tho...

avatar image Balint3DDesign · Aug 01, 2013 at 03:22 PM 0
Share

Yes, Farfarer solved my problem with the shader. I post it as an answer below :)

avatar image Sylker · Aug 03, 2013 at 02:53 AM 0
Share

Thank you very much for sharing your solution. This was puzzling my $$anonymous$$d for weeks. Now I can understand better the concept and adapt it to my own needs and future works. Great work.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Balint3DDesign · Aug 01, 2013 at 03:24 PM

 Shader "Custom/Silhoutted Hatching" {
 
     Properties {
 
         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
 
         _Outline ("Outline width", Range (0.0, 0.03)) = .005
 
         _ShinPower ("Shininess", Range(0,1)) = 0.5
 
         _GlossPower ("Gloss", Range(0.01,1)) = 0.5
 
         _MainTex ("Texture", 2D) = "white" {}
 
         _BumpMap ("Bumpmap", 2D) = "bump" {}
 
         _SpecularTex ("Specular Map", 2D) = "gray" {}
 
         _Ramp ("Shading Ramp", 2D) = "gray" {}
 
         _Hatch0 ("Hatch 0", 2D) = "white" {}
 
         _Hatch1 ("Hatch 1", 2D) = "gray" {}
 
         _Hatch2 ("Hatch 2", 2D) = "gray" {}
 
         _Hatch3 ("Hatch 3", 2D) = "black" {}
 
     }
 
  
 
  
 
  
 
  
 
  
 
     SubShader {
 
         Tags { "Queue" = "Geometry" "RenderType" = "Opaque" }
 
  
 
         Pass {
 
             Name "OUTLINE"
 
             Tags { "LightMode" = "Always" }
 
             Cull Off
 
             ZWrite Off
 
             Blend SrcAlpha OneMinusSrcAlpha
 
  
 
             CGPROGRAM
 
             #pragma vertex vert
 
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
  
 
             struct appdata {
 
                 float4 vertex : POSITION;
 
                 float3 normal : NORMAL;
 
             };
 
  
 
             struct v2f {
 
                 float4 pos : POSITION;
 
                 float4 color : COLOR;
 
             };
 
  
 
             uniform float _Outline;
 
             uniform float4 _OutlineColor;
 
  
 
             v2f vert(appdata v) {
 
                 v2f o;
 
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 
                 float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
 
                 float2 offset = TransformViewToProjection(norm.xy);
 
                 o.pos.xy += offset * o.pos.z * _Outline;
 
                 o.color = _OutlineColor;
 
                 return o;
 
             }
 
  
 
             half4 frag(v2f i) : COLOR {
 
                 return i.color;
 
             }
 
             ENDCG
 
         }
 
  
 
         CGPROGRAM
 
  
 
         #pragma surface surf Hatching noambient
 
         #pragma only_renderers d3d9
 
         #pragma target 3.0
 
  
 
         struct SurfaceOutputHatch {
 
             fixed3 Albedo;
 
             fixed3 Normal;
 
             fixed4 Hatch;
 
             fixed3 Emission;
 
             fixed Specular;
 
             fixed Gloss;
 
             float Alpha;
 
         };
 
  
 
         struct Input
 
         {
 
             float2 uv_Hatch0;
 
             float2 uv_MainTex;
 
         };
 
  
 
         sampler2D _Hatch0, _Hatch1, _Hatch2, _Hatch3;
 
         sampler2D _MainTex;
 
         sampler2D _BumpMap;
 
         sampler2D _SpecularTex;
 
         sampler2D _Ramp;
 
  
 
         float _ShinPower;
 
         float _GlossPower;
 
  
 
         inline half4 LightingHatching (inout SurfaceOutputHatch s, half3 lightDir, half3 viewDir, half atten)
 
         {
 
             float3 h = normalize (lightDir + viewDir);
 
             float NdotL = dot (s.Normal, lightDir) * 0.5 + 0.5;
 
             float nh = max (0, dot (s.Normal, h));
 
             float spec = pow (nh, s.Gloss * 64) * s.Specular;
 
             
 
             float intensity = saturate((NdotL + spec) * atten);
 
             
 
             fixed hatch;
 
             hatch = lerp ( s.Hatch.r, 1.0, saturate((intensity - 0.75) * 4));
 
             hatch = lerp ( s.Hatch.g, hatch, saturate((intensity - 0.5) * 4));
 
             hatch = lerp ( s.Hatch.b, hatch, saturate((intensity - 0.25) * 4));
 
             hatch = lerp ( s.Hatch.a, hatch, saturate((intensity) * 4));
 
             
 
             fixed3 ramp = tex2D (_Ramp, float2(intensity)).rgb;
 
             half4 c;
 
             c.rgb = s.Albedo * ramp * _LightColor0.rgb * hatch;
 
             c.a = 0.5;
 
             return c;
 
         }
 
  
 
         void surf (Input IN, inout SurfaceOutputHatch o)
 
         {
 
             o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
 
             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
 
             fixed2 specGloss = tex2D(_SpecularTex, IN.uv_MainTex).rg;
 
             o.Specular = specGloss.r * _ShinPower;
 
             o.Gloss = specGloss.g * _GlossPower;
 
             o.Hatch.r = tex2D(_Hatch0, IN.uv_Hatch0);
 
             o.Hatch.g = tex2D(_Hatch1, IN.uv_Hatch0);
 
             o.Hatch.b = tex2D(_Hatch2, IN.uv_Hatch0);
 
             o.Hatch.a = tex2D(_Hatch3, IN.uv_Hatch0);
 
         }
 
         ENDCG
 
     }
 
     Fallback "Diffuse"
 
 }
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.

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

Multiple Cars not working 1 Answer

Tracing in Unity3D 2 Answers

Black Texture on Initial Load 1 Answer

How can I prevent lights overlapping in intensity? 1 Answer

How to blend colors from two texture maps in VFX graph 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