Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 Deadsilver · Oct 13, 2021 at 03:53 PM · shadershadersshader programmingshader writing

shader that makes object look hollow

I am using the Standard render pipeline. I have found a shader I would like to use for a project. It simulates a liquid that can be filled up in the shape of the object its attached to.

A liquid should have a top surface, and the shader has a variable for setting _TopColor which also looks fine when used. But if another object intersects with the top surface it becomes clear that there is no surface and the object looks hollow.

As seen on these pictures where I took a standard sphere object, attached the shader and filled it to around 75%, and stuck a cube object into it, you can see that it looks like a hollow bowl. On the second picture I faked the result I would like to achieve by adding another object inside, which makes it look like the liquid actually has a top surface.

alt text

alt text

Here is the code for the shader:

 Shader "Unlit/LiquidShader"
 {
     Properties
     {
         _Tint("Tint", Color) = (1,1,1,1)
         _MainTex("Texture", 2D) = "white" {}
         _FillAmount("Fill Amount", Range(0,100)) = 0.0
         [HideInInspector] _WobbleX("WobbleX", Range(-1,1)) = 0.0
         [HideInInspector] _WobbleZ("WobbleZ", Range(-1,1)) = 0.0
         _TopColor("Top Color", Color) = (1,1,1,1)
         _FoamColor("Foam Line Color", Color) = (1,1,1,1)
         _Rim("Foam Line Width", Range(0,0.1)) = 0.0
         _RimColor("Rim Color", Color) = (1,1,1,1)
         _RimPower("Rim Power", Range(0,10)) = 0.0
     }
 
         SubShader
         {
             Tags {"Queue" = "Geometry"  "DisableBatching" = "True" }
 
                     Pass
             {
              Zwrite On
              Cull Off // we want the front and back faces
              AlphaToMask On // transparency
 
              CGPROGRAM
 
 
              #pragma vertex vert
              #pragma fragment frag
             // make fog work
             #pragma multi_compile_fog
 
             #include "UnityCG.cginc"
 
             struct appdata
             {
               float4 vertex : POSITION;
               float2 uv : TEXCOORD0;
               float3 normal : NORMAL;
             };
 
             struct v2f
             {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
                float3 viewDir : COLOR;
                float3 normal : COLOR2;
                float fillEdge : TEXCOORD2;
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
             float _FillAmount, _WobbleX, _WobbleZ;
             float4 _TopColor, _RimColor, _FoamColor, _Tint;
             float _Rim, _RimPower;
 
             float4 RotateAroundYInDegrees(float4 vertex, float degrees)
             {
                float alpha = degrees * UNITY_PI / 180;
                float sina, cosa;
                sincos(alpha, sina, cosa);
                float2x2 m = float2x2(cosa, sina, -sina, cosa);
                return float4(vertex.yz , mul(m, vertex.xz)).xzyw;
             }
 
 
             v2f vert(appdata v)
             {
                v2f o;
 
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                // get world position of the vertex
                float3 worldPos = mul(unity_ObjectToWorld, v.vertex.xyz);
                // rotate it around XY
                float3 worldPosX = RotateAroundYInDegrees(float4(worldPos,0),360);
                // rotate around XZ
                float3 worldPosZ = float3 (worldPosX.y, worldPosX.z, worldPosX.x);
                // combine rotations with worldPos, based on sine wave from script
                float3 worldPosAdjusted = worldPos + (worldPosX * _WobbleX) + (worldPosZ * _WobbleZ);
                // how high up the liquid is
                o.fillEdge = worldPosAdjusted.y * 139 + (_FillAmount - 50); 
 
                o.viewDir = normalize(ObjSpaceViewDir(v.vertex));
                o.normal = v.normal;
                return o;
             }
 
             fixed4 frag(v2f i, fixed facing : VFACE) : SV_Target
             {
                 // sample the texture
                 fixed4 col = tex2D(_MainTex, i.uv) * _Tint;
             // apply fog
             UNITY_APPLY_FOG(i.fogCoord, col);
 
             // rim light
             float dotProduct = 1 - pow(dot(i.normal, i.viewDir), _RimPower);
             float4 RimResult = smoothstep(0.5, 1.0, dotProduct);
             RimResult *= _RimColor;
 
             // foam edge
             float4 foam = (step(i.fillEdge, 0.5) - step(i.fillEdge, (0.5 - _Rim)));
             float4 foamColored = foam * (_FoamColor * 0.9);
             // rest of the liquid
             float4 result = step(i.fillEdge, 0.1) - foam;
             float4 resultColored = result * col;
             // both together, with the texture
             float4 finalResult = resultColored + foamColored;
             finalResult.rgb += RimResult;
 
             // color of backfaces/ top
             float4 topColor = _TopColor * (foam + result);
             //VFACE returns positive for front facing, negative for backfacing
             return facing > 0 ? finalResult : topColor;
 
           }
           ENDCG
          }
 
         }
 }

Can anyone with a better understanding of writing shaders help with what should be changed in the shader code or if what I am trying to achieve is even possible?

nosurfaceliquid.png (132.8 kB)
surfaceliquid.png (131.0 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
0

Answer by khusainova · Oct 14, 2021 at 06:29 AM

I think it's not easy task because you need appropriate values in z-buffer. You can try next steps:

  1. draw the sphere with your shader

  2. draw back polygons of the sphere in the stensil buffer with special shader pass

  3. draw a some plane like a top surface only in z-buffer with depth shader pass use stensil buffer like a mask

Comment
Add comment · Show 1 · 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 Deadsilver · Oct 14, 2021 at 03:20 PM 0
Share

This is not something I am capable of just doing

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

196 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

Related Questions

Making shader not ignore Light on transparent areas ? 0 Answers

Wireframe shader with constant width and no diagonals 1 Answer

Is a water shader with these features possible or easy to make? 1 Answer

How to add Emission to my custom shader? 2 Answers

Adding a clip() to the default shader? 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