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 Julien-Lynge · Jun 28, 2012 at 01:51 AM · shaderlightingemissionsurface shaderemissive

Surface shader: modify the emission with lighting?

Not sure if this is possible, but what I'm trying to do is create a surface shader that uses the lighting model to modify the emissions. The reason to do this is to create a day/night planet shader with lights appearing only on the night side. The lights would be defined in a texture, and would only appear on the 'dark' side of the planet (based on light direction).

Within the surf function, I'm setting the emission as so:

o.Emission = tex2D(_Lights,IN.uv_Lights.xy);

I would then like to multiply this by the inverse of the light intensity. However, light information (lightDir) is only available in the lighting model, and changes made to the EditorSurfaceOutput there seem to have no effect (not that I'd expect them to).

Is what I'm trying to do possible? Any suggestions on how to go about it? Thanks for any help.

The full shader is below.

//////////////////////////////////////////////////////////////////////////////

 Shader "TestEarthSimple"
 {
   Properties 
   {
 _MainTex("_MainTex", 2D) = "black" {}
 _Lights("_Lights", 2D) = "black" {}

   }

   SubShader 
   {
     Tags
     {
 "Queue"="Geometry"
 "IgnoreProjector"="False"
 "RenderType"="Opaque"

     }


 Cull Back
 ZWrite On
 ZTest LEqual
 ColorMask RGBA

     CGPROGRAM
     #pragma surface surf BlinnPhongEditor
     #pragma target 2.0


     sampler2D _MainTex;
     sampler2D _Lights;

       struct EditorSurfaceOutput {
         half3 Albedo;
         half3 Normal;
         half3 Emission;
         half3 Gloss;
         half Specular;
         half Alpha;
         half4 Custom;
       };

       inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
       {
         half3 spec = light.a * s.Gloss;
         half4 c;
         c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
         c.a = s.Alpha;
         return c;
       }

       inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
       {
         half3 h = normalize (lightDir + viewDir);

         half diff = max (0, dot ( lightDir, s.Normal ));

         float nh = max (0, dot (s.Normal, h));
         float spec = pow (nh, s.Specular*128.0);

         half4 res;
         res.rgb = _LightColor0.rgb * diff;
         res.w = spec * Luminance (_LightColor0.rgb);
         res *= atten * 2.0;

         return LightingBlinnPhongEditor_PrePass( s, res );
       }

       struct Input {
         float2 uv_MainTex;
         float2 uv_Lights;

       };


       void surf (Input IN, inout EditorSurfaceOutput o) {
         o.Normal = float3(0.0,0.0,1.0);
         o.Alpha = 1.0;
         o.Gloss = 0.0;
         o.Specular = 0.0;
         o.Custom = 0.0;

         float4 Sampled2D2=tex2D(_MainTex,IN.uv_MainTex.xy);
         float4 Sampled2D1=tex2D(_Lights,IN.uv_Lights.xy);
         o.Albedo = Sampled2D2;
         o.Emission = Sampled2D1;
       }
     ENDCG
   }
   Fallback "Diffuse"
 }
Comment
Add comment · Show 2
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 Mulbin · Jan 09, 2017 at 02:08 PM 0
Share

@Julien-Lynge

Sorry to dredge up such an old post, but do you still have the shader you came up with? I really need something similar to will allow emissives to glow only when in on dark side of a planet. I've tried some of the planet prefads but they are way too low resolution compared to my planet so I need something I can apply to my own, high detailed model.

avatar image Julien-Lynge Mulbin · Jan 09, 2017 at 05:16 PM 0
Share

Yeah, actually - I posted it to the wiki all those years ago:

http://wiki.unity3d.com/index.php/Earth/Planet

Note that it's a bit behind the times at this point - it's a lot less functional than the new standard shader stuff.

2 Replies

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

Answer by ScroodgeM · Jul 14, 2012 at 01:59 PM

  1. don't write to o.Emission any colors you want to control by lights
    1. write light color to some field (Custom)

    2. replace

s.Albedo * light.rgb
with

lerp(s.Custom, s.Albedo, * light.rgb)
this will make more intensity from 'Custom' instead of 'Albedo' on dark side planet shader

untitled-1.jpg (259.4 kB)
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 Julien-Lynge · Jul 15, 2012 at 10:56 PM 0
Share

Thanks @Scroodge$$anonymous$$. I had actually come to the same conclusion you suggested and just didn't close my question. I'd be curious to see the shader you're using - there's some nice atmospheric effects and interesting water specularity. I posted the shader I cam up with to the wiki: http://unifycommunity.com/wiki/index.php?title=Earth/Planet

avatar image ScroodgeM · Jul 16, 2012 at 07:20 AM 1
Share

atmospheric effects is custom lighting model with special effect implementation on light and view in opposite direction and additive shader pass to draw clouds and atmosphere light over the planet surface. specular is regular specular method from simple specular shader, just used specular map where water only have it.

i can't publish sources cause it's restricted, but can point right direction how to get such effect if you need....

avatar image
0

Answer by HannibalLicious · Nov 05, 2018 at 05:03 PM

Hey, I'm trying to make a similar shader which will make cutout textures appear in the dark (basically glow-in-the-dark tattoos), however, since I am very new to coding in general, I have no idea how to make it. Your shader seems to work in a similar way but doesn't use any additional textures which could serve as tattoos in my idea, would you mind helping me/sharing your code with me so we can figure out how it might work?

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 HannibalLicious · Nov 05, 2018 at 05:04 PM 0
Share

I most likely won't be much of a help though.

avatar image HannibalLicious · Nov 06, 2018 at 05:55 PM 0
Share

okay, i made it by now so all good

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

7 People are following this question.

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

Related Questions

Order of functions in a shader? 1 Answer

Unity Surface Shader - Deferred lighting viewDir and lightDir 0 Answers

Where does an emissive quad produce light? 0 Answers

Simple emission shader for beast lightmaps 1 Answer

Spherical Hamonics - ShadeSH9 and Surface Shader issue 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