water distortion shader

Questions à propos du scripting Shader.
UnityNoobUser
Messages : 20
Inscription : 21 Fév 2021 14:54

water distortion shader

Message par UnityNoobUser » 04 Juil 2021 11:08

Slt,

je cherche un shader qui me permettrait d'avoir un effet de distortion et de mouvement sur le background comme si le player était dans un liquide. j'aimerai que le rendu soit comme dans le jeux spore quand la cellule est dans l'eau:

https://www.youtube.com/watch?v=Bmk_QEHxYdQ

j'ai essayé de trouver un shader sur l'asset store qui fait ça car je ne comprend pas grand chose aux shaders et étant sous le standard renderer pipeline je n'ai pas accès a shader graph pour tester en suivant des tutos.
j'ai acheté un "shader" qui donne un peu cet effet mais en fait c'est un script qui utilise "OnRenderImage" et un shader, qui s'applique a l'image reçue par la camera et il n'y a pas d'options qui permet d'exclure du traitement certains objects.
https://assetstore.unity.com/packages/v ... ect-127395

ensuite j'ai trouve le code d'un shader sur github (https://github.com/Daachun/Unity-Shader ... ster/Slime qui fait a la limite cette partie distorsion mais il y a un soucis même en modifiant l'attribut color il y a tjrs un effet "fumé" , le shader n'est pas complétement transparent:

Image

si quelqu'un sait ou je peux trouver un shader me permettant de donner cet effet ou comment modifier le code du shader "slime" pour ne pas avoir cet effet "fumé"

le code du shader:

Code : Tout sélectionner

Shader "Slime"
{
	Properties
	{
		_Color("Color", Color) = ( 1, 1, 1, 1)
		_RimColor("Rim Color", Color) = (0, 0, 0, 0)
		_RimIntensity("Rim Intensity", Range(0, 5)) = 1
		_mySpecColor("Specular Color", Color) = (1, 1, 1, 1)
		_SpecInt("Specular Itensity", Float) = 1
		_SpecSmooth("Smoothness", Float) = 1
		_Bounce("Bounce", Float) = 1
		_DistortTex("Texture", 2D) = "white" {}
		_DistortIntensity("Distort Intensity", Range(0, 5)) = 1
		_Speed("Distort Speed", Float) = 1
		_SkyDistort("Distort Reflection", Float) = 5
		_SkyWeight("Reflective", Range(0, 1)) = .5
		_Banding("Banding", Float) = 1
		_BandingWeight("Banding Weight", Range(0, 1)) = .5
		_MeltVal("Melt", Float) = 0
		_MeshFloor("Floor (Feet)", Float) = 0
		_Adjustment1("Adjust 1", Float) = 0
		_Adjustment2("Adjust 2", Float) = 0

	}
		SubShader
		{
			Tags {
				"RenderType" = "Transparent"
				"Queue" = "Transparent"
				"LightMode" = "ForwardBase"}
			LOD 100

			Cull Off

			GrabPass{
				"_GrabTex"
			}

			Pass
			{
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"
				#include "UnityLightingCommon.cginc"

				struct appdata
				{
					float4 vertex : POSITION;
					float3 normal : NORMAL;
					float2 uv : TEXCOORD0;
				};

				struct v2f
				{
					float2 uv : TEXCOORD0;
					float4 uvGrab : TEXCOORD1;
					half3 worldRefl : TEXCOORD2;
					float2 uvVert : TEXCOORD3;
					float3 worldView : TEXCOORD4;
					float3 normal : NORMAL;
					float4 vertex : SV_POSITION;
					fixed4 diffuse : COLOR;
				};

				sampler2D _DistortTex;
				fixed4 _DistortTex_ST;

				sampler2D _GrabTex;

				fixed4 _Color;
				fixed4 _RimColor;
				fixed4 _mySpecColor;

				fixed _Bounce;

				fixed _RimIntensity;

				fixed _DistortIntensity;
				fixed _Speed;

				fixed _SpecInt;
				fixed _SpecSmooth;

				fixed _SkyDistort;
				fixed _SkyWeight;
				fixed _Banding;
				fixed _BandingWeight;

				fixed _MeltVal;
				fixed _MeshFloor;

				fixed _Adjustment1;
				fixed _Adjustment2;

				v2f vert(appdata v)
				{
					v2f o;

					float3 worldNormal = UnityObjectToWorldNormal(v.normal);
					//Vertex Deform
					v.vertex.y += (v.vertex.y + 1) * sin(_Time.y) * _Bounce / 100;
					v.vertex.xz -= v.vertex.xz * cos(_Time.y) * _Bounce / 100;

					//Height of where the vertex will start "melting"
					float melt = ((v.vertex.y - _MeshFloor) - _MeltVal) * 20;
					melt = max(0, 1 - melt);

					//Not a very elegant solution but it works for now
					v.vertex.y -= _MeltVal;
					if (v.vertex.y < _MeshFloor)
						v.vertex.y = _MeshFloor * (1 - v.vertex.y) * _Adjustment1 - _Adjustment2;
					v.vertex.xz += (v.normal.xz * .002) * melt;

					o.vertex = UnityObjectToClipPos(v.vertex);
					o.uv = v.uv;

					//GrabPass UVs
					o.uvGrab = ComputeScreenPos(o.vertex);
					COMPUTE_EYEDEPTH(o.uvGrab.z);

					//Lighting

					half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
					o.diffuse = saturate((nl * _LightColor0) + .5);

					//World Reflection
					float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
					float3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));
					fixed4 rim = 1 - saturate(dot(normalize(worldViewDir), worldNormal));
					o.worldRefl = reflect(-worldViewDir, worldNormal);


					rim = pow(rim, 2) * _RimIntensity * _RimColor;

					o.normal = worldNormal;
					o.uvVert = v.vertex.y;
					o.diffuse += clamp(0, 5, rim);
					o.worldView = worldViewDir;
					return o;
				}

				fixed4 frag(v2f i) : SV_Target
				{
					half time = _Time.x * _Speed;
					float2 sceneUVs = (i.uvGrab.xy / i.uvGrab.w);
					fixed distort = tex2D(_DistortTex, float2(i.uv.x, i.uv.y + time) * _DistortTex_ST.xy + _DistortTex_ST.zw) * (_DistortIntensity / 100);

					fixed3 h = normalize(_WorldSpaceLightPos0.xyz + i.worldView);
					fixed nh = max(0, dot(i.normal, h));
					fixed4 spec = saturate(pow(nh, _SpecInt * 20) * _SpecSmooth) * _mySpecColor;


					fixed4 c = tex2D(_GrabTex, sceneUVs + distort) * _Color * i.diffuse * max(.4, unity_AmbientSky);
					half4 skyData = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, i.worldRefl + distort * _SkyDistort);
					half3 skyColor = DecodeHDR(skyData, unity_SpecCube0_HDR) * _SkyWeight;

					fixed banding = tex2D(_DistortTex, i.uvVert + distort * _Banding + time);
					banding = saturate(banding + (1 - _BandingWeight));
					c.rgb += skyColor + spec;
					c.rgb *= banding;
					return saturate(c);
				}
				ENDCG
			}
		}
}
merci

ReMix
Messages : 15
Inscription : 26 Oct 2018 15:57

Re: water distortion shader

Message par ReMix » 12 Août 2021 14:26

J'ai bricolé un shader vite fait, appliquer le à un quad et met le devant tout tes objets, et tu devrais avoir ce que tu cherches :

Code : Tout sélectionner

Shader "Unlit/DistoShader"
{
    Properties
    {
        _DistordTex1("Distortion 1", 2D) = "grey" {}
        _DistordIntensity1("Intensité de Distortion 1", Float) = 0.1
        _DistordSpeed1("Vitesse de Distortion 1", Vector) = (0.1, 0.1, 0,0)

        _DistordTex2("Distortion 2", 2D) = "grey" {}
        _DistordIntensity2("Intensité de Distortion 2", Float) = 0.03
        _DistordSpeed2("Vitesse de Distortion 2", Vector) = (0.03, -0.03, 0,0)
    }
    SubShader
    {
        Tags {
            "RenderType"="Transparent"
            "Queue"="Overlay"
        }
        LOD 100

        GrabPass{
            "_GrabTex"
        }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float4 grabUV : TEXCOORD1;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 grabUV : TEXCOORD1;
                float4 vertex : SV_POSITION;
            };

            sampler2D _GrabTex;

            sampler2D _DistordTex1;
            float4 _DistordTex1_ST;
            float _DistordIntensity1;
            float2 _DistordSpeed1;

            sampler2D _DistordTex2;
            float4 _DistordTex2_ST;
            float _DistordIntensity2;
            float2 _DistordSpeed2;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                //GrabPass UVs
                o.grabUV = ComputeScreenPos(o.vertex);
                COMPUTE_EYEDEPTH(o.grabUV.z);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                float2 distortion = _DistordIntensity1 * ( tex2D(_DistordTex1, i.uv * _DistordTex1_ST.xy + _DistordTex1_ST.zw + _Time.y* _DistordSpeed1).xy * 2 - 1 );
                distortion += _DistordIntensity2 * (tex2D(_DistordTex2, i.uv * _DistordTex2_ST.xy + _DistordTex2_ST.zw + _Time.y * _DistordSpeed2).xy * 2 - 1);

                // sample the texture
                float2 sceneUVs = (i.grabUV.xy / i.grabUV.w);
                fixed4 col = tex2D(_GrabTex, sceneUVs + distortion);
                return col;
            }
            ENDCG
        }
    }
}

Répondre

Revenir vers « les Shaders »