Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "DynamicDeformation"
- {
- Properties
- {
- [Header(Textures and Colors)]
- [NoScaleOffset]_MainTex("Base (RGB)", 2D) = "white" {} //Base triplanar texture
- [NoScaleOffset]_DispTex("Displacement Texture", 2D) = "gray" {} //Displacement map, medium grey smoothly peaking in white works best.
- [NoScaleOffset]_BumpMap("Normal Map", 2D) = "bump" {} //Normal triplanar texture
- _IntersectionColor("Intersection Color", Color) = (1, 0, 1, 1) //color when intersecting
- _NormalIntensity("Normal Intensity", Range(0, 5)) = 1 //Intensity of normals
- _NormalScaling("Normal Scaling", Range(0, 1)) = 0.5 //Scaling intensity of normals
- _IntersectionLength("Intersection Length", Range(0, 2)) = 2.0 //distance of intersecting effect
- _TextureScale("Texture Scale", Range(0.01, 2)) = 1 //Scale of triplanar textures.
- [Header(Displacement)]
- _CheckAngle("Disp Dir & Tess Amt", Vector) = (0,1,0) //Direction of effect displacement
- _DispSmoothing("Dsip. Smoothing", Range(0.1, 10)) = 1 //Displacement Smoothing effect
- _Displacement("Displacement Scale", Range(0.01, 1.0)) = 0.25 //Displacement scale
- _UndersideDisp("Underside Scaling", Range(-0.1, 1.0)) = 0.1 //Displacement underside scaling
- _MossReduction("Disp Reduction", Range(0, 15)) = 0 //Displacement reduction
- _AngleReduction("Angle Reduction", Range(1, -5)) = 0 //Angle of displacement reduction
- [Header(Tesselation)]
- _MinimumTess("Min Tess. Ammount", Range(1, 10)) = 2 //Minimum tesselation value
- _MinDist("Min Tess. Distance", Range(0, 10)) = 5 //Inner limit of tesselation distance
- _MaxDist("Max Tess. Distance", Range(10, 50)) = 25 //Outer limit of tesselation distance
- [Header(Properties)]
- _Gloss("Smoothness", Range(0, 1)) = 0 //Standard property values
- _Metalness("Mettalic", Range(0, 1)) = 0 //Standard property values
- }
- SubShader
- {
- Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
- CGPROGRAM
- #pragma surface surf Standard addshadow fullforwardshadows vertex:vert tessellate:tessEdge nolightmap
- #pragma target 5.0
- #include "Tessellation.cginc"
- #include "UnityPBSLighting.cginc"
- sampler2D _CameraDepthTexture; //Shader Gobal accessed for intersection values.
- sampler2D _MainTex;
- sampler2D _BumpMap;
- sampler2D _DispTex;
- half4 _IntersectionColor;
- float3 _CheckAngle;
- float _TextureScale;
- float _Displacement;
- float _DispSmoothing;
- float _UndersideDisp;
- float _MossReduction;
- float _NormalIntensity;
- float _NormalScaling;
- float _MinimumTess;
- float _AngleReduction;
- float _MinDist;
- float _MaxDist;
- float _IntersectionLength;
- float _Gloss;
- float _Metalness;
- struct appdata
- {
- float4 vertex : POSITION;
- float4 tangent : TANGENT;
- float3 normal : NORMAL;
- float2 texcoord : TEXCOORD0;
- };
- struct Input
- {
- float2 uv_MainTex;
- float3 vertex;
- float3 worldNormal;
- float3 worldPos;
- float4 screenPos;
- INTERNAL_DATA
- };
- //Tesselation function, use the dot product of the averaged normal per polygon & a given direciton to decide on tesselation ammount.
- float4 tessEdge(appdata v0, appdata v1, appdata v2)
- {
- float tessValue = _MinimumTess;
- float3 averagedNormal = normalize(v0.normal + v1.normal + v2.normal);
- if (dot(mul(unity_ObjectToWorld, averagedNormal), _CheckAngle) < 0)
- {
- tessValue = 0;
- }
- return UnityDistanceBasedTess(v0.vertex, v1.vertex, v2.vertex, _MinDist, _MaxDist, tessValue);
- }
- //Vertex function to deform tesselated surface, based on a triplanar map.
- void vert(inout appdata v)
- {
- float2 uvX = v.vertex.zy; // x facing plane
- float2 uvY = v.vertex.xz; // y facing plane
- float2 uvZ = v.vertex.xy; // z facing plane
- half3 worldDisp;
- worldDisp.x = tex2Dlod(_DispTex, float4(uvX, 0, 0)) * _Displacement;
- worldDisp.y = tex2Dlod(_DispTex, float4(uvY, 0, 0)) * _Displacement;
- worldDisp.z = tex2Dlod(_DispTex, float4(uvZ, 0, 0)) * _Displacement;
- float resultAngle = smoothstep(10, 1, _DispSmoothing);
- if (_CheckAngle.x || _CheckAngle.y || _CheckAngle.z)
- {
- resultAngle = smoothstep(0, _DispSmoothing, saturate(dot(mul(unity_ObjectToWorld, float4(v.normal, 0.0)).xyz, _CheckAngle) + (_AngleReduction * length(_CheckAngle))));
- }
- //Apply displacement based on the dot product, and various values given in the material.
- v.vertex.xyz += lerp(v.normal.xyz * -worldDisp.xyz * _UndersideDisp, (v.normal.xyz * worldDisp.xyz) - (v.normal.xyz * _MossReduction), resultAngle);
- }
- //Surface function to add material values and textures.
- void surf(Input IN, inout SurfaceOutputStandard o)
- {
- //Calculate distance from depth values
- float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)));
- float surfZ = -mul(UNITY_MATRIX_V, float4(IN.worldPos.xyz, 1)).z;
- float diff = sceneZ - surfZ;
- float result = 1 - saturate(diff / _IntersectionLength);
- //Calculate triplanar values
- float3 localPos = IN.worldPos - mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;;
- float2 uvX = localPos.zy * _TextureScale; // x facing plane
- float2 uvY = localPos.xz * _TextureScale; // y facing plane
- float2 uvZ = localPos.xy * _TextureScale; // z facing plane
- //Find resulting albedo
- half4 resultTex = (tex2D(_MainTex, uvX) + tex2D(_MainTex, uvY) + tex2D(_MainTex, uvZ)) / 3;
- o.Albedo = lerp(resultTex, resultTex * _IntersectionColor, result);
- //Calculate normal values & blend
- float dist = distance(_WorldSpaceCameraPos, IN.worldPos);
- o.Normal = UnpackScaleNormal(tex2D(_BumpMap, uvX), max(_NormalIntensity, dist * _NormalScaling)) +
- UnpackScaleNormal(tex2D(_BumpMap, uvY), max(_NormalIntensity, dist * _NormalScaling)) +
- UnpackScaleNormal(tex2D(_BumpMap, uvZ), max(_NormalIntensity, dist * _NormalScaling));
- o.Smoothness = _Gloss;
- o.Metallic = _Metalness;
- }
- ENDCG
- }
- FallBack "Diffuse"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement