├── Preview_image.gif ├── README.md └── paralaxNode.cs /Preview_image.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anaseinea/Parallax-Mapping-Node-Unity/HEAD/Preview_image.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Parallax-Mapping-in-Unity-Shader-Graph 2 | 3 | Node code used in the [Tutorial](https://youtu.be/LKhGqKYOmbo) 4 | 5 | [My Material Pack](https://www.assetstore.unity3d.com/#!/content/136692) 6 | 7 | ![Screenshot](http://anaseinea.github.io/Parallax-Mapping-Node-Unity/Preview_image.gif) 8 | -------------------------------------------------------------------------------- /paralaxNode.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor.ShaderGraph; 3 | using System.Reflection; 4 | [Title("Custom", "paralax")] 5 | public class ParalaxNode : CodeFunctionNode 6 | { 7 | public ParalaxNode() 8 | { 9 | name = "paralax"; 10 | } 11 | 12 | protected override MethodInfo GetFunctionToConvert() 13 | { 14 | return GetType().GetMethod("MyCustomFunction", 15 | BindingFlags.Static | BindingFlags.NonPublic); 16 | } 17 | static string MyCustomFunction( 18 | [Slot(0, Binding.None)] Vector1 heightScale, 19 | [Slot(1, Binding.TangentSpaceViewDirection)] Vector3 viewDir, 20 | [Slot(2, Binding.None)] Texture2D HeightTex, 21 | [Slot(3, Binding.MeshUV0)] Vector2 uv, 22 | [Slot(4, Binding.None)] SamplerState sampleState, 23 | [Slot(5, Binding.None)] out Vector2 Out) 24 | 25 | { 26 | Out = uv; 27 | return 28 | @" 29 | { 30 | // determine required number of layers 31 | const float minLayers = 30; 32 | const float maxLayers = 60; 33 | float numLayers = lerp(maxLayers, minLayers, abs(dot(float3(0, 0, 1), viewDir))); 34 | 35 | float numSteps = numLayers;//60.0f; // How many steps the UV ray tracing should take 36 | float height = 1.0; 37 | float step = 1.0 / numSteps; 38 | 39 | float2 offset = uv.xy; 40 | float4 HeightMap = HeightTex.Sample(sampleState, offset); 41 | 42 | float2 delta = viewDir.xy * heightScale / (viewDir.z * numSteps); 43 | 44 | // find UV offset 45 | for (float i = 0.0f; i < numSteps; i++) { 46 | if (HeightMap.r < height) { 47 | height -= step; 48 | offset += delta; 49 | HeightMap = HeightTex.Sample(sampleState, offset); 50 | } else { 51 | break; 52 | } 53 | } 54 | Out = offset; 55 | } 56 | "; 57 | } 58 | 59 | 60 | } --------------------------------------------------------------------------------