shaderlab 瓶中水 尝试实现记录

alyx瓶中水实现笔记

观察alyx水的特性
参考代码
https://www.patreon.com/posts/quick-game-art-18245226

1.有类似透明的效果,但是无法穿过瓶子看到瓶子后的物体,瓶身有磨砂处理使得这种特性看起来合理。(似乎在室外特别亮的地方能透过瓶子看到后面细微的表现,这样的话就不需要做折射了???)
2.可能使用了类似sss贴图的东西,让瓶子看起来透光(但是无法透过瓶子看到后面)

3.液体可能使用了体积约束的一些近似求值方式。
4.液体中有气泡产生(不仅仅是晃动的时候,晃动会出现更多更大的气泡)。

5.瓶子有漫反射
6.瓶子有镜面反射
7.瓶子能被阴影影响。
8.瓶子有高光
9.液体表面有高光
10.啤酒摇晃会有浮沫

c#脚本往shader传值的方法
先声明
Renderer rend;
再获取实例(一般在start里面)
rend = GetComponent();
然后用实例对象控制shader里面的值(在update里更新就好)
rend.material.SetFloat("_TestVol", vol);

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Wobble02 : MonoBehaviour
{
    Renderer rend;
    Vector3 lastPos;
    Vector3 velocity;
    Vector3 lastRot;  
    Vector3 angularVelocity;
    public float MaxWobble = 0.03f;
    public float WobbleSpeed = 1f;
    public float Recovery = 1f;
    float wobbleAmountX;
    float wobbleAmountZ;
    float wobbleAmountToAddX;
    float wobbleAmountToAddZ;
    float pulse;
    float time = 0.5f;
    
    // Use this for initialization
    void Start()
    {
        rend = GetComponent<Renderer>();
    }
    private void Update()
    {
        time += Time.deltaTime;
        // decrease wobble over time
        wobbleAmountToAddX = Mathf.Lerp(wobbleAmountToAddX, 0, Time.deltaTime * (Recovery));
        wobbleAmountToAddZ = Mathf.Lerp(wobbleAmountToAddZ, 0, Time.deltaTime * (Recovery));
 
        // make a sine wave of the decreasing wobble
        pulse = 2 * Mathf.PI * WobbleSpeed;
        wobbleAmountX = wobbleAmountToAddX * Mathf.Sin(pulse * time);
        wobbleAmountZ = wobbleAmountToAddZ * Mathf.Sin(pulse * time);
 
        // send it to the shader
        rend.material.SetFloat("_WobbleX", wobbleAmountX);
        rend.material.SetFloat("_WobbleZ", wobbleAmountZ);
 
        // velocity
        velocity = (lastPos - transform.position) / Time.deltaTime;
        angularVelocity = transform.rotation.eulerAngles - lastRot;
 
 
        // add clamped velocity to wobble
        wobbleAmountToAddX += Mathf.Clamp((velocity.x + (angularVelocity.z * 0.2f)) * MaxWobble, -MaxWobble, MaxWobble);
        wobbleAmountToAddZ += Mathf.Clamp((velocity.z + (angularVelocity.x * 0.2f)) * MaxWobble, -MaxWobble, MaxWobble);
 
        // keep last position
        lastPos = transform.position;
        lastRot = transform.rotation.eulerAngles;
    }
}
Shader "Custom/BottleTest02"
{
    Properties
    {
		_Tint ("Tint", Color) = (1,1,1,1)
		_MainTex ("Texture", 2D) = "white" {}
        _FillAmount ("Fill Amount", Range(-10,10)) = 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 + _FillAmount;
 
			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.5) - 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
        }
 
    }
}
posted @ 2020-09-17 20:00  Victor2k  阅读(136)  评论(0)    收藏  举报