Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 laurG · Sep 24, 2017 at 02:35 PM · shadershadershlsl

Simple Unlit HLSL Shader with Rim Lighting?

Hi!

I'm looking for a way to create Rim Lighting effect on a simple Unlit shader. Everywhere I looked people are using the surface shaders and I'm not familiar with them yet.

Here is my current shader. Is simple Unlit that uses vertex colors.

 struct appdata
             {
                 float4 vertex : POSITION;
                 fixed4 color : COLOR;
             };
 
             struct v2f {
                 float4 pos : SV_POSITION;
                 fixed4 color : TEXCOORD2;
             };
 
             v2f vert (appdata v)
             {
                 v2f o;
                 
                 o.pos = UnityObjectToClipPos(v.vertex);
 
                 o.color = v.color;
                 UNITY_TRANSFER_FOG(o, o.pos);
                 return o;
             } 
 
             half4 frag (v2f i) : COLOR
             {
                 return i.color;
             }

I would like to have this effect: alt text

Where could I find an example for this using standard HLSL shaders instead of the Surf ones.

Thank you!

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

Answer by laurG · Sep 25, 2017 at 10:48 PM

I found this:

 #version 330
  
 uniform mat4 u_pm;
 uniform mat4 u_vm;
  
 layout( location = 0 ) out vec4 fragcolor;
  
 in vec3 v_norm;
 in vec4 v_pos; 
  
 void main() {
  
   vec3 n = normalize(mat3(u_vm) * v_norm);      // convert normal to view space, u_vm (view matrix), is a rigid body transform.
   vec3 p = vec3(u_vm * v_pos);                   // position in view space
   vec3 v = normalize(-p);                       // eye vector
   float vdn = 1.0 - max(dot(v, n), 0.0);        // the rim contribution
  
   fragcolor.a = 1.0;
   fragcolor.rgb = vec3(smoothstep(0.6, 1.0, vdn));
 }

But I suck at shading languages, been struggling with them for a while and can't seem to get it working.

How can I apply this logic to my shader using Unity?

Comment
Add comment · Show 1 · 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 laurG · Sep 29, 2017 at 09:12 AM 0
Share

I finally cracked it:

 struct appdata
             {
                 float4 vertex : POSITION;
                 fixed4 color : COLOR;
                 float3 normal : NOR$$anonymous$$AL;
             };
     
             struct v2f {
                 float4 pos : SV_POSITION;
                 float3 normal : NOR$$anonymous$$AL;
                 float4 posWorld : TEXCOORD0;
                 fixed4 color : COLOR;
             };
     
             v2f vert(appdata v)
             {
                 v2f o;
     
                 o.pos = UnityObjectToClipPos(v.vertex);
             
                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
                 o.normal = normalize( mul ( float4(v.normal, 0.0), unity_WorldToObject).xyz);
     
                 o.color = v.color;
                 UNITY_TRANSFER_FOG(o, o.pos);
     
                 return o;
             }
     
             half4 frag(v2f i) : COLOR
             {
                 float3 normalDir = i.normal;
                 float3 viewDir = normalize( _WorldSpaceCameraPos.xyz - i.posWorld.xyz);
                 
                 float rim = 1 - saturate ( dot(viewDir, normalDir) );
     
                 float3 rimLight = pow(rim, _Power) * _Color;
     
                 return float4( i.color.xyz + rimLight, 1.0f);
             }
 

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

112 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 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 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 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 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 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

Set list in shader (uniform) 1 Answer

"Floor" function produces artifacts in shader 1 Answer

2D Water top surface profile 1 Answer

HLSL Post Process Shader Fixing UV/Texcoord 1 Answer

Shader float4 component values computing strangely 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