Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 TheSpanadian · Dec 17, 2021 at 02:45 AM · shadershadersshader programmingshader writingplanet

Water shader for planet with animated foam on shores

Hello everyone, I've been trying to use Sebastian Lague's procedural planet generation and his ocean shader to make a game. Here's the video for reference: https://www.youtube.com/watch?v=lctXaT9pxA0 (The link to the procedural planet generation code: https://github.com/SebLague/Solar-System/tree/Episode-03)

Now I've tried updating this shader to add foam to the shorelines in a pattern that surrounds the shores and animates towards the inner part of the ocean, such as in his latest video (https://www.youtube.com/watch?v=sLqXFF8mlEU&t=1044s): islands with foam on the shore

The code I'm trying to update to do so is the following:

 Shader "Hidden/Ocean"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
     }
     SubShader
     {
         // No culling or depth
         Cull Off ZWrite Off ZTest Always
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
             #include "../Includes/Math.cginc"
             #include "../Includes/Triplanar.cginc"
 
             struct appdata {
                     float4 vertex : POSITION;
                     float2 uv : TEXCOORD0;
             };
 
             struct v2f {
                     float4 pos : SV_POSITION;
                     float2 uv : TEXCOORD0;
                     float3 viewVector : TEXCOORD1;
             };
             
             v2f vert (appdata v) {
                     v2f output;
                     output.pos = UnityObjectToClipPos(v.vertex);
                     output.uv = v.uv;
                     // Camera space matches OpenGL convention where cam forward is -z. In unity forward is positive z.
                     // (https://docs.unity3d.com/ScriptReference/Camera-cameraToWorldMatrix.html)
                     float3 viewVector = mul(unity_CameraInvProjection, float4(v.uv * 2 - 1, 0, -1));
                     output.viewVector = mul(unity_CameraToWorld, float4(viewVector,0));
                     return output;
             }
 
             float4 colA;
             float4 colB;
             float4 specularCol;
             float depthMultiplier;
             float alphaMultiplier;
             float smoothness;
 
 
             sampler2D waveNormalA;
             sampler2D waveNormalB;
             float waveStrength;
             float waveNormalScale;
             float waveSpeed;
 
             sampler2D _MainTex;
             sampler2D _CameraDepthTexture;
             float4 params;
 
             float planetScale;
             float3 oceanCentre;
             float oceanRadius;
             float3 dirToSun;
 
 
             fixed4 frag (v2f i) : SV_Target
             {
     
                 fixed4 originalCol = tex2D(_MainTex, i.uv);
 
                 float3 rayPos = _WorldSpaceCameraPos;
                 float viewLength = length(i.viewVector);
                 float3 rayDir = i.viewVector / viewLength;
 
                 float nonlin_depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv);
             float sceneDepth = LinearEyeDepth(nonlin_depth) * viewLength;
 
                 float2 hitInfo = raySphere(oceanCentre, oceanRadius, rayPos, rayDir);
                 float dstToOcean = hitInfo.x;
                 float dstThroughOcean = hitInfo.y;
                 float3 rayOceanIntersectPos = rayPos + rayDir * dstToOcean - oceanCentre;
 
                 // dst that view ray travels through ocean (before hitting terrain / exiting ocean)
                 float oceanViewDepth = min(dstThroughOcean, sceneDepth - dstToOcean);
 
 
                 if (oceanViewDepth > 0) {
                     float3 clipPlanePos = rayPos + i.viewVector * _ProjectionParams.y;
 
                     float dstAboveWater = length(clipPlanePos - oceanCentre) - oceanRadius;
 
                     float t = 1 - exp(-oceanViewDepth / planetScale * depthMultiplier);
                     float alpha =  1-exp(-oceanViewDepth / planetScale * alphaMultiplier);
                     float4 oceanCol = lerp(colA, colB, t);
 
                     float3 oceanSphereNormal = normalize(rayOceanIntersectPos);
 
                     float2 waveOffsetA = float2(_Time.x * waveSpeed, _Time.x * waveSpeed * 0.8);
                     float2 waveOffsetB = float2(_Time.x * waveSpeed * - 0.8, _Time.x * waveSpeed * -0.3);
                     float3 waveNormal = triplanarNormal(rayOceanIntersectPos, oceanSphereNormal, waveNormalScale / planetScale, waveOffsetA, waveNormalA);
                     waveNormal = triplanarNormal(rayOceanIntersectPos, waveNormal, waveNormalScale / planetScale, waveOffsetB, waveNormalB);
                     waveNormal = normalize(lerp(oceanSphereNormal, waveNormal, waveStrength));
                     //return float4(oceanNormal * .5 + .5,1);
                     float diffuseLighting = saturate(dot(oceanSphereNormal, dirToSun));
                     float specularAngle = acos(dot(normalize(dirToSun - rayDir), waveNormal));
                     float specularExponent = specularAngle / (1 - smoothness);
                     float specularHighlight = exp(-specularExponent * specularExponent);
                 
                     oceanCol *= diffuseLighting;
                     oceanCol += specularHighlight * (dstAboveWater > 0) * specularCol;
                     
                     //return float4(oceanSphereNormal,1);
                     float4 finalCol =  originalCol * (1-alpha) + oceanCol * alpha;
                     return float4(finalCol.xyz, params.x);
                 }
 
                 
                 return originalCol;
             }
             ENDCG
         }
     }
 }
 

I know that in his latest video he calculates this with a distance to shoreline map. However, I know this can be done with the depth of the water at any given point, but I do not know how to use this information to create the foam. If this is not possible, I would be fine with just creating foam at the shores when the depth of the water is under a maximum depth and then animating it with noise, as what this shader does: https://github.com/IronWarrior/ToonWaterShader/blob/master/Assets/Shaders/ToonWater.shader (how the shader looks is on the main GitHub page).

I have also tried integrating that shader's code into the shader that I have, but the furthest I've gotten is converting all water into white.

Any help, even just pointing me into the correct direction would be much appreciated.

Thank you in advance.

shorelinefoamexample.png (483.8 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

0 Replies

· Add your reply
  • Sort: 

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

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

How do I make an Unlit Shader with two textures? 0 Answers

Unity 3d Sprite Shader (How do I limit Max Brightness to 1 with Multiple Lights Hitting) 0 Answers

How to make a plane to sphere using vertex displacement shader graph, with respect to the camera position? 0 Answers

"Stencil Operation" property setup? 1 Answer

Stretching when using world coordinates 2 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