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 S_Darkwell · Jun 14, 2014 at 09:26 PM · lightingshadersvertexwireframe

Modifying Wireframe Vertex Shader to Include Lighting & Shadows

Hello fellow developers!

I humbly seek the community’s assistance in combining two seemingly simple shaders into a single perfect shader.

Until a week ago, I was completely ignorant of the inner workings of shaders. Today, many hours of research later, I actually have a clue, but still fail to discover the solution for the puzzle that inspired it all.

I wish to create a wireframe vertex shader that only traces the edge of objects, but also receives lighting and casts shadows. I stumbled upon a shader that renders the wireframes almost perfectly, and another shader that that handles the lighting and shadows wonderfully.

As much as I strive, however, I fail to combine them.

Below is a composite image created from both individual shaders:

This image perfectly illustrates the effect I hope to achieve.

The above image perfectly illustrates the effect I hope to achieve.

The shader code used to create the wireframes are as follows:


Wireframe Shader

 Shader "Custom/WireFrame"
 {
     Properties
     {
         _LineColor ("Line Color", Color) = (1,1,1,1)
         _GridColor ("Grid Color", Color) = (1,1,1,0)
         _LineWidth ("Line Width", float) = 0.2
     }
     
     SubShader
     {
         Tags { "RenderType" = "Transparent" }
         Blend SrcAlpha OneMinusSrcAlpha
         AlphaTest Greater 0.5
     
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
              
             uniform float4 _LineColor;
             uniform float4 _GridColor;
             uniform float _LineWidth;
              
             // vertex input: position, uv1, uv2
             struct appdata
             {
                 float4 vertex : POSITION;
                 float4 texcoord1 : TEXCOORD0;
                 float4 color : COLOR;
             };
              
             struct v2f
             {
                 float4 pos : POSITION;
                 float4 texcoord1 : TEXCOORD0;
                 float4 color : COLOR;
             };
              
             v2f vert (appdata v)
             {
                 v2f o;
                 o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
                 o.texcoord1 = v.texcoord1;
                 o.color = v.color;
                 return o;
             }
              
             fixed4 frag(v2f i) : COLOR
             {
                 fixed4 answer;
                  
                 float lx = step(_LineWidth, i.texcoord1.x);
                 float ly = step(_LineWidth, i.texcoord1.y);
                 float hx = step(i.texcoord1.x, 1.0 - _LineWidth);
                 float hy = step(i.texcoord1.y, 1.0 - _LineWidth);
                  
                 answer = lerp(_LineColor, _GridColor, lx*ly*hx*hy);
                  
                 return answer;
             }
             ENDCG
         }
     }
 Fallback "Vertex Colored", 1
 }


And the shader that renders the shadows and lighting is below:


Shadow / Lighting Shader

 Shader "Sample/Diffuse" 
 {
     Properties 
     {
         _DiffuseTexture ("Diffuse Texture", 2D) = "white" {}
         _DiffuseTint ( "Diffuse Tint", Color) = (1, 1, 1, 1)
     }
 
     SubShader 
     {
         Tags { "RenderType"="Opaque" }
 
         pass
         {        
             Tags { "LightMode"="ForwardBase"}
 
             CGPROGRAM
 
             #pragma target 3.0
             #pragma fragmentoption ARB_precision_hint_fastest
 
             #pragma vertex vertShadow
             #pragma fragment fragShadow
             #pragma multi_compile_fwdbase
 
             #include "UnityCG.cginc"
             #include "AutoLight.cginc"
 
             sampler2D _DiffuseTexture;
             float4 _DiffuseTint;
             float4 _LightColor0;
 
             struct v2f
             {
                 float4 pos : SV_POSITION;
                 float3 lightDir : TEXCOORD0;
                 float3 normal : TEXCOORD1;
                 float2 uv : TEXCOORD2;
                 LIGHTING_COORDS(3, 4)
             };
 
             v2f vertShadow(appdata_base v)
             {
                 v2f o;
 
                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.uv = v.texcoord;
                 o.lightDir = normalize(ObjSpaceLightDir(v.vertex));
                 o.normal = normalize(v.normal).xyz;
 
                 TRANSFER_VERTEX_TO_FRAGMENT(o);
 
                 return o; 
             }
 
             float4 fragShadow(v2f i) : COLOR
             {                    
                 float3 L = normalize(i.lightDir);
                 float3 N = normalize(i.normal);     
 
                 float attenuation = LIGHT_ATTENUATION(i) * 2;
                 float4 ambient = UNITY_LIGHTMODEL_AMBIENT * 2;
 
                 float NdotL = saturate(dot(N, L));
                 float4 diffuseTerm = NdotL * _LightColor0 * _DiffuseTint * attenuation;
 
                 float4 diffuse = tex2D(_DiffuseTexture, i.uv);
 
                 float4 finalColor = (ambient + diffuseTerm) * diffuse;
 
                 return finalColor;
             }
 
             ENDCG
         }        
 
     } 
     FallBack "Diffuse"
 }
 


I will be exceedingly grateful to those who help me to achieve or provide the shader code that I seek.

Thank you all in advance, and I hope you have a fantastic weekend!

example.png (67.3 kB)
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

Answer by S_Darkwell · Aug 26, 2014 at 05:35 AM

Here is the combined shader that I created. It only contains a single lighting pass, which is all that I required. I hope that others find this shader useful as well!

 Shader "Custom/Blueprint"
 {
     Properties
     {
         _LineColor ("Line Color", Color) = (1,1,1,1)
         _GridColor ("Grid Color", Color) = (1,1,1,0)
         _LineWidth ("Line Width", float) = 0.2
     }
     
     SubShader
     {
         Tags { "RenderType" = "Opaque" }
     
         Blend SrcAlpha OneMinusSrcAlpha
         AlphaTest Greater 0.5
     
         Pass
         {
             Tags { "LightMode" = "ForwardBase" }
         
             CGPROGRAM
             
             #pragma target 3.0
             #pragma fragmentoption ARB_precision_hint_fastest
             
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_fwdbase
                         
             #include "UnityCG.cginc"
             #include "AutoLight.cginc"
              
             float4 _LineColor;
             float4 _GridColor;
             float4 _LightColor0;
             float _LineWidth;
              
             struct v2f
             {
                 float4 pos : POSITION;
                 float4 texcoord : TEXCOORD0;
                 float3 normal : TEXCOORD1;
                 float4 uv : TEXCOORD2;
 
                 LIGHTING_COORDS(3,4)
             };
              
             v2f vert (appdata_base v)
             {
                 v2f o;
                 o.pos = mul( UNITY_MATRIX_MVP, v.vertex);
                 o.uv = v.texcoord;
                 o.normal = normalize(v.normal).xyz;
                 o.texcoord = v.texcoord;
 
                 TRANSFER_VERTEX_TO_FRAGMENT(o);
                 
                 return o;
             }
              
             fixed4 frag(v2f i) : COLOR
             {
                 float3 L = normalize( normalize( ObjSpaceLightDir(i.pos) ) );
                 float3 N = normalize( i.normal );    
                 
                 float attenuation = LIGHT_ATTENUATION(i) * 2;
                 float4 ambient = UNITY_LIGHTMODEL_AMBIENT * 4;
                 
                 float NdotL = saturate( dot(N, L) );
                 float4 diffuseTerm = NdotL * _LightColor0 * attenuation;
                  
                 float lx = step(_LineWidth, i.texcoord.x);
                 float ly = step(_LineWidth, i.texcoord.y);
                 float hx = step(i.texcoord.x, 1.0 - _LineWidth);
                 float hy = step(i.texcoord.y, 1.0 - _LineWidth);
 
                 return lerp( _LineColor, _GridColor, lx*ly*hx*hy ) * ( ambient + diffuseTerm );
 
             }
             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

2 People are following this question.

avatar image avatar image

Related Questions

Lighting works for iPad but not iPhone? 0 Answers

Help Turning Sprites into 3D Objects and Casting Shadows 1 Answer

[Shader] No Fog on Particles/Standard Unlit 0 Answers

How to get 2d lighting data in shadergraph? 0 Answers

unity_LightColor[1] and up are 0 with 2 point lights near the object. 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