Advertisement
DexterAndre

Animated Fractal Shader

Mar 14th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Fractals/Mandelbrot" {
  2. // Inspired by tutorial from World of Zero: https://youtu.be/SVj0LWmQD-E
  3.  
  4.     Properties {
  5.         _Color ("Color", Color) = (1,1,1,1)
  6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
  7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
  8.         _Metallic ("Metallic", Range(0,1)) = 0.0
  9.  
  10.         _Zoom ("Zoom", Vector) = (1, 1, 1, 1)
  11.         _Pan ("Pan", Vector) = (0, 0, 0, 1)
  12.         _AspectRatio ("Aspect Ratio", Float) = 1
  13.         _Iterations ("Iterations", Range(1, 5000)) = 5
  14.     }
  15.     SubShader {
  16.         Tags { "RenderType"="Opaque" }
  17.         LOD 200
  18.  
  19.         CGPROGRAM
  20.         // Physically based Standard lighting model, and enable shadows on all light types
  21.         #pragma surface surf Standard fullforwardshadows
  22.  
  23.         // Use shader model 3.0 target, to get nicer looking lighting
  24.         #pragma target 3.0
  25.  
  26.         sampler2D _MainTex;
  27.  
  28.         struct Input {
  29.             float2 uv_MainTex;
  30.         };
  31.  
  32.         float4 _Zoom;
  33.         float4 _Pan;
  34.         float _Iterations;
  35.         float _AspectRatio;
  36.  
  37.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
  38.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
  39.         // #pragma instancing_options assumeuniformscaling
  40.         UNITY_INSTANCING_BUFFER_START(Props)
  41.             // put more per-instance properties here
  42.         UNITY_INSTANCING_BUFFER_END(Props)
  43.  
  44.         void surf (Input IN, inout SurfaceOutputStandard o) {
  45.             // Albedo comes from a texture tinted by color
  46.             //fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
  47.  
  48.             float2 c = (IN.uv_MainTex - 0.5) * _Zoom.xy * float2(1, _AspectRatio) - _Pan.xy;
  49.             float2 v = 0;
  50.             float m = 0;
  51.             const float r = 5;
  52.  
  53.             for (int n = 0; n < _Iterations; n++)
  54.             {
  55.                 v = float2(v.x * v.x - v.y * v.y, v.x * v.y * 2) + c;
  56.                 if (dot(v, v) < (r * r - 1))
  57.                 {
  58.                     m++;
  59.                 }
  60.  
  61.                 v = clamp(v, -r, r);
  62.             }
  63.  
  64.             float4 color;
  65.  
  66.             if (m == _Iterations)
  67.             {
  68.                 color = float4(0, 0, 0, 1);
  69.             }
  70.             else
  71.             {
  72.                 color = float4(sin(m / 4), sin(m / 5), sin(m / 7), 1) / 4 + 0.75;
  73.             }
  74.  
  75.             o.Albedo = color;
  76.             o.Emission = color;
  77.             o.Alpha = color.a;
  78.         }
  79.         ENDCG
  80.     }
  81.     FallBack "Diffuse"
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement