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 gibber10 · Dec 18, 2016 at 06:32 PM · shadertransparencyrenderqueue

Order dependent transparency, shaders (planet atmosphere rendering)

Hello.

I'm attempting to achieve an atmosphere effect on a planet (simple sphere primative) by a billboard / quad with an RGBA blended texture displayed in front of the sphere.

To get the best sizing for the quad, its positioned at the sphere center, and rendered with a custom shader which has Z-test always. A script orients the quad to look at the camera.

However, my atmospheres for planets are all drawn on top even if another planet/sphere should obscure an atmosphere (as expected).

I need to draw the most distant planet from the camera first, then its atmosphere, then the next nearest and so on. I've looked at render queues but this only allows changes to the order for all at once (eg: drawing all atmospheres last).

How do I achieve this render order? Do I need to manually calculate planet/camera distance (not too bad), but how do I set the render order? can I do that with render queues? This doesn't seem to be the right way.

Regards Matt

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Pangamini · Dec 19, 2016 at 09:34 AM

You may try and create a quad mesh with pivot offset (the quad not passing through [0;0;0] point, but being moved backwards a bit, resulting in the actual pivot distance of the atmosphere mesh being closer to the camera than the planet's pivot. Then keep planets and atmospheres in the same renderQueue (transparent one, ofc) and let the sorting by depth do the rest

Comment
Add comment · Show 2 · 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 gibber10 · Dec 20, 2016 at 09:59 PM 0
Share

That was my initial approach. And it works with respect to transparency ordering.

However having the quad closer to the camera causes sizing and parallax issues when the camera gets closer. I've tried to resolve these by dynamically sizing the quad depending on the camera distance, however its very fiddly and requires so much tweaking that I gave up on that approach.

If its the only way, I'll look again.

avatar image Pangamini gibber10 · Dec 21, 2016 at 12:36 PM 0
Share

Ah no you misunderstood - you need to create a new quad mesh that will have the offset baked in. Then you can just put the visible quad exactly where it's supposed to be, but with the pivot sticking out so it's rendered later

avatar image
0

Answer by gibber10 · Dec 27, 2016 at 05:03 AM

In the end I wrote a surface shader for a sphere that surrounds the planet. It uses the dot product of viewDir and the normal then manipulates that number to calculate the transparency.

The shader is shown below in case anyone finds this thread. There will certainly be much better ways to get the effect to appear on the rim of the sphere other than uses sin / cos like this, but the principle is sound I think. You have to tweak the values in the inspector quite a bit to get the desired effect.

 Shader "Atmosphere2" {
     Properties{
         _RimColor("_Color", Color) = (0.26,0.19,0.16,0.0)
         _RimPower("_RimPower", Range(-8.0,8.0)) = 3.0
         _SinFactor("_SinFactor", Range(-10.0,10.0)) = 7.0
         _CosFactor("_CosFactor", Range(-10.0,10.0)) = 7.0
         _SinOffset("_SinOffset", Range(-10.0,10.0)) = -4.0
         _CosOffset("_CosOffset", Range(-10.0,10.0)) = -4.0
         _SinFrequency("_SinFrequency", Range(-10.0,10.0)) = 1.0
         _CosFrequency("_CosFrequency", Range(-10.0,10.0)) = 1.0
     }
         SubShader{
         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
         LOD 200
         ZWrite Off
 
         //Blend SrcAlpha OneMinusSrcAlpha
         CGPROGRAM
 #pragma surface surf Lambert alpha
         struct Input {
         float3 viewDir;
     };
     float4 _RimColor;
     float _RimPower;
     float _SinFactor;
     float _CosFactor;
     float _SinOffset;
     float _CosOffset;
     float _SinFrequency;
     float _CosFrequency;
     void surf(Input IN, inout SurfaceOutput o) {
     
         float vdot = dot(normalize(IN.viewDir), o.Normal);
         float cosdot = _SinOffset + cos(vdot * _SinFrequency) * _SinFactor;
         float sindot = _CosOffset + sin(vdot * _CosFrequency) * _CosFactor;
 
         half rim = cosdot * sindot;
 
         o.Albedo = _RimColor.rgb;
         o.Alpha = pow(rim, _RimPower) * rim * _RimColor.a;
         //o.Emission = _RimColor.rgb * rim;
     }
     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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does the 3D Text shader show specular at certain angles? 2 Answers

Two transparent objects passing through each other problem 1 Answer

Can I make any parts of an object inside a partially transparent object fully invisible? 1 Answer

Unity System Fog Access 0 Answers

Add Color Property to Unlit Alpha? 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