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 /
  • Help Room /
avatar image
0
Question by Eudaimonium · Mar 14, 2016 at 03:41 PM · shader programmingnormalviewdir

Fresnel/Edge shader problem

Greetings,

I am having a weird problem with my custom fresnel shading code.

It apparently works correctly as long as in-game object doesn't have any rotation applied to it.

Shader code: http://pastebin.com/ZSsdk0py

As you can see I'm picking the view vector (as per materials on the net) with this function:

 float3 direction = normalize(WorldSpaceViewDir(v.vertex));

Material Preview and expected output (Yes I am aware that having majority of work in Vertex part isn't pixel perfect result, this is what I want): alt text

But in-game, it only really works on objects that have not have any rotation applied to them (and even then it doesn't appear to be perfect): alt text The right sphere was rotated by some 85° over Y. (To the left basically).

I have tried changing the direction code to this:

 float3 direction = normalize(_WorldSpaceCameraPos - v.vertex);
                 intensity = 1 / dot(direction, v.normal);

But it still produces the same result somehow. How can I obtain the actual camera->vertex vector?

Thank you!

shaderpreview.jpg (25.0 kB)
shader1.jpg (173.6 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
Best Answer

Answer by Eudaimonium · Mar 14, 2016 at 03:47 PM

I figured it out:

We need local space, not world space:

 float3 direction = normalize(ObjSpaceViewDir (v.vertex));
                 intensity = 1 / dot(direction, v.normal);
Comment
Add comment · Show 3 · 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 Eudaimonium · Mar 14, 2016 at 03:56 PM 0
Share

If anybody needs it, here is the pixel-shader version of the shader, with pixel perfect results:

http://pastebin.com/j0aJNU$$anonymous$$X

avatar image Will_Dev · Oct 03, 2017 at 01:58 PM 0
Share

Thank you for posting the shader for us. Will this work for cubes too? And is it mobile friendly?

avatar image Eudaimonium Will_Dev · Oct 03, 2017 at 02:18 PM 0
Share

It does work with cube-y objects, depending on what you want to use it for. Best try it out for yourself and see if it works for you.

Not sure what you mean by "mobile friendly". It's not any larger performance hog than any other alpha/transparency effects such as various particle materials.

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

50 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

Related Questions

Normal mapping distance of view? 0 Answers

looking for a substitution for Shader "Debug/Vertex Color" 0 Answers

How to add light maps to a custom Rflection shader 0 Answers

"particle standard surface" shader build error, PC 4 Answers

Creating a 3D portal effect using stencil buffer with a deferred shading setup? 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