Created
February 25, 2022 11:49
-
-
Save unitycoder/9c0d0b9c3bb152c7967dfd953bb8186a to your computer and use it in GitHub Desktop.
Curved Surface Shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://forum.unity.com/threads/adding-vertex-effect-to-surface-shader.1245025/ | |
| Shader "Custom/CurvedSurfaceShader" | |
| { | |
| Properties | |
| { | |
| _Color ("Colour", Color) = (1,1,1,1) | |
| _MainTex ("Albedo (RGB)", 2D) = "white" {} | |
| _Glossiness ("Smoothness", Range(0,1)) = 0.5 | |
| _Metallic ("Metallic", Range(0,1)) = 0.0 | |
| } | |
| SubShader | |
| { | |
| Tags { "RenderType"="Opaque" } | |
| CGPROGRAM | |
| #pragma target 3.0 | |
| #include "UnityCG.cginc" | |
| //Physically based Standard lighting model, and enable shadows on all light types. | |
| #pragma surface surf Standard fullforwardshadows addshadow | |
| //Vertex needed to generate curve effect. | |
| #pragma vertex vert | |
| float3 _BendAmount; | |
| float3 _BendOrigin; | |
| float _BendFallOff; | |
| float _BendFallOffStr; | |
| void vert (inout appdata_full v) | |
| { | |
| //Getting world space location of a particular vertex. | |
| float4 world = mul(unity_ObjectToWorld, v.vertex); | |
| //Calculating the distance between the vertex position and where the _BendOrigin is. | |
| float dist = length(world.xyz - _BendOrigin.xyz); | |
| //Distance value is prevented from going below 0, This prevents undefined behaviour when using power function (negative values don't work with power). | |
| dist = max(0, dist - _BendFallOff); | |
| //Distance value is raised to _BendFallOffStr value, which defines the steepness of the curve itself. | |
| dist = pow(dist, _BendFallOffStr); | |
| //_BendAmount should be dependent on the distance value so as to have a falloff curve, So we simply multiply it with distance and add to 'world'. | |
| world.xyz += dist * _BendAmount; | |
| //Convert the new world space of vertex and convert back to local space. | |
| float4 vertex = mul(unity_WorldToObject, world); | |
| v.vertex = vertex; | |
| } | |
| sampler2D _MainTex; | |
| struct Input | |
| { | |
| float2 uv_MainTex; | |
| }; | |
| half _Glossiness; | |
| half _Metallic; | |
| fixed4 _Color; | |
| void surf (Input IN, inout SurfaceOutputStandard o) | |
| { | |
| // Albedo comes from a texture tinted by colour | |
| fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; | |
| o.Albedo = c.rgb; | |
| // Metallic and smoothness come from slider variables | |
| o.Metallic = _Metallic; | |
| o.Smoothness = _Glossiness; | |
| o.Alpha = c.a; | |
| } | |
| ENDCG | |
| } | |
| FallBack "Diffuse" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment