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 Morgan · Aug 11, 2013 at 07:44 AM · shaderswaterspotlightcookiereflective

Best way to make reflective Pro water receive a spotlight cookie?

I need my flashlight beam (and cookie) to not stop at the water's edge--doesn't look natural. (Pro reflective water; target is Mac and PC standalone.)

What's the best solution? I'm guessing I need another plane situated just above the water. If so, what kind of shader would make the plane invisible but let it receive the spotlight's beam?

Or is there another way?

Thanks for any ideas.

Comment
Add comment · Show 1
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 Benproductions1 · Aug 11, 2013 at 09:11 AM 0
Share

Shadows/Lighting work using the "depth buffer". You can't both have transparency and shadow casting/receiving ability AFAI$$anonymous$$

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by Morgan · Aug 11, 2013 at 11:04 AM

Thanks; that includes transparency done by adding the color value to what's behind?

The answer I've come up with is a fake cookie (a standard quad rotated 90 degrees on X) hovering just above the water. It looks pretty good, at least in my situation! Maybe a shader wizard could come up with something more elegant.

I considered flipping the fake face-down (and lifting it off the surface a bit more) so the player doesn't see it directly--only its reflection. A hacky way to make the cookie respond to the wave movements--but it seemed like too much movement; it felt like a reflection and not light cast on the surface. So I just left it face-up and gave it enough transparency that the rippling water shows through, it actually looks as though it's rippling too. (Use a soft/blurry cookie for best results.)

Note that a simple quad can't accurately create the precise effect of a spotlight on the surface: a spotlight is a cone, and the cookie should land distorted like a trapezoid, with the far end bigger. So this effect here is more accurate for a cylindrical light beam, but in practice it still looks good. It DOES stretch as you shine the beam farther away, but not in a trapezoidal way. (To do the job better would require manipulating the quad's vertices or maybe some transformation matrix. Putting my brain through that math wasn't worth it!)

Also, you may want to add horizontal falloff (if your fog isn't enough) since the cookie should get weaker with distance. This script has the beginnings of that, but the alpga gets weaker with VERTICAL distance, since in my case I'm approaching the water from the sky above, not traveling across the surface as in most games.

Here's my script in case it helps anyone tackle cookies on water:


 #pragma strict
 
 //Fake cookie example by Morgan Adams - cast a spotlight cookie onto reflective water or other non-cookie-compatible flat surface
 //(Put this script on any object--doesn't matter. Put the cookie and its "holder" anywhere at scene root.)
 
 //Use shader Particles/Additive for cookie; adjust alpha to set max cookie strength
 
 var lamp : Transform; //The spotlight
 var cookie : GameObject; //The quad for the fake cookie (standard quad rotated 90 degrees on X to lie flat)
 var cookieFalloff : float; //Max visible distance (in this example the alpha falloff is VERTICAL; most applications should be tweaked for a falloff into the horizontal distance)
 var holder : GameObject; //Empty GameObject - make the cookie a child of this
 var waterBoundary : Transform; //The reflective surface, or any GameObject at that vertical position
 
 private var waterOffset : float = .001; //Raise cookie above surface to prevent plane-fighting
 private var waterHeight : float;
 private var cookieColor : Color;
 private var cookieAlpha : float;
 
 function Start() {
     waterHeight = waterBoundary.position.y;
     cookieColor = cookie.renderer.material.GetColor("_TintColor");
     cookieAlpha = cookieColor.a; //Max alpha as pre-set in material
 }
 
 function Update () {
     holder.transform.position = Vector3( lamp.position.x, waterHeight+waterOffset, lamp.position.z );
     holder.transform.eulerAngles.y = lamp.eulerAngles.y;
     
     cookie.transform.localPosition.z = -( Mathf.Abs(lamp.position.y-waterHeight) / Mathf.Tan(Mathf.PI - lamp.eulerAngles.x *Mathf.PI/180) );
     var distanceFactor = Mathf.Abs(lamp.position.y-waterHeight)/2;
     var angleFactor = cookie.transform.localPosition.z/2;
     cookie.transform.localScale = Vector3( distanceFactor+angleFactor, .5+distanceFactor+angleFactor, distanceFactor+angleFactor );
     
     cookieColor.a = cookieAlpha * (1 - Mathf.Min(1, distanceFactor/cookieFalloff)); //Alpha falloff
     cookie.renderer.material.SetColor ("_TintColor",cookieColor);
 }
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

15 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

Related Questions

Reflective surfaces 0 Answers

Top perspective 2D water 0 Answers

Blend Cubemaps in reflection probe 1 Answer

Ocean shader that works for mobile devices 0 Answers

Shaders and Water bugged in Built Version 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