SubstanceFlow - 图1

    1. // OSL version Mads Drøschler
    2. // WebGL version by Witek https://www.shadertoy.com/view/ltKyRR)
    3. // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
    4. // Modified 03.22.2021 by Saul Espinosa for Redshift 3d
    5. #include <vector2.h>
    6. #define vec2 vector2
    7. #define vec3 vector
    8. #include <vector4.h>
    9. #define vec4 vector4
    10. float fract ( float x)
    11. {
    12. return x-floor(x);
    13. }
    14. vec3 random3(vec3 c)
    15. {
    16. float j = 4096.0*sin(dot(c, vec3(17.0, 59.4, 15.0)));
    17. vec3 r;
    18. r[2] = fract(512.0 * j);
    19. j *= 0.125;
    20. r[0] = fract(512.0 * j);
    21. j *= 0.125;
    22. r[1] = fract(512.0 * j);
    23. return r - 0.5;
    24. }
    25. float simplex3d(vec3 p)
    26. {
    27. float F3 = 0.3333333;
    28. float G3 = 0.1666667;
    29. vec3 s = floor(p + dot(p, vec3(F3)));
    30. vec3 x = p - s + dot(s, vec3(G3));
    31. vec3 e = step(vec3(0.0), x - vec3(x[1],x[2],x[0]));
    32. vec3 i1 = e*(1.0 - vec3(e[2],e[0],e[1]));
    33. vec3 i2 = 1.0 - vec3(e[2],e[0],e[1])*(1.0 - e);
    34. vec3 x1 = x - i1 + G3;
    35. vec3 x2 = x - i2 + 2.0*G3;
    36. vec3 x3 = x - 1.0 + 3.0*G3;
    37. vec4 w, d;
    38. w.x = dot(x, x);
    39. w.y = dot(x1, x1);
    40. w.z = dot(x2, x2);
    41. w.w = dot(x3, x3);
    42. w = max(0.6 - w, 0.0);
    43. d.x = dot(random3(s), x);
    44. d.y = dot(random3(s + i1), x1);
    45. d.z = dot(random3(s + i2), x2);
    46. d.w = dot(random3(s + 1.0), x3);
    47. w *= w;
    48. w *= w;
    49. d *= w;
    50. return dot(d, vec4(52.0,52.0,52.0,52.0));
    51. }
    52. float simplex3d_fractal(vec3 m, int steps)
    53. {
    54. float sum = 0.0;
    55. for (int i = 0; i < steps; ++i)
    56. {
    57. float scale = pow(2.0, float(i));
    58. sum += simplex3d(scale * m) / scale;
    59. }
    60. return sum;
    61. }
    62. vec3 flow_texture( vec3 p, float Time, int steps)
    63. {
    64. // animate initial coordinates
    65. vec3 p1 = 0.1 * p + vec3(1.0 + Time * 0.0023, 2.0 - Time * 0.0017, 4.0 + Time * 0.0005);
    66. // distort noise sampling coordinates using the same noise function
    67. vec3 p2 = p + 8.1 * simplex3d_fractal(p1,steps) + 0.5;
    68. vec3 p3 = p2 + 4.13 * simplex3d_fractal(0.5 * p2 + vec3(5.0, 4.0, 8.0 + Time * 0.07),steps) + 0.5;
    69. vec3 ret;
    70. ret[0] = simplex3d_fractal(p3 + vec3(0.0, 0.0, 0.0 + Time * 0.3),steps);
    71. ret[1] = simplex3d_fractal(p3 + vec3(0.0, 0.0, 0.2 + Time * 0.3),steps);
    72. ret[2] = simplex3d_fractal(p3 + vec3(0.0, 0.0, 0.3 + Time * 0.3),steps);
    73. // scale output & map
    74. ret = 0.5 + 0.5 * ret;
    75. ret = smoothstep(vec3(0.15), vec3(0.85), ret);
    76. return ret;
    77. }
    78. shader SubstanceFlow
    79. [[ string help = "Substance Flow Noise",
    80. string label = "Substance Flow" ]]
    81. (
    82. point Po = P
    83. [[
    84. string label = "UVW"
    85. ]],
    86. float Time = 0
    87. [[
    88. float min = 0,
    89. float max = 100
    90. ]],
    91. int steps = 12
    92. [[
    93. int min = 1,
    94. int max = 25
    95. ]],
    96. float scale = 1
    97. [[
    98. float min = 0,
    99. float max = 5
    100. ]],
    101. int aces = 0
    102. [[ string widget = "checkBox",
    103. string label = "ACES",
    104. int connectable = 0 ]],
    105. output color outColor = 0,
    106. )
    107. {
    108. // ACES sRGB Transform
    109. matrix aces_tm = matrix(
    110. 0.6131, 0.0701, 0.0206, 0,
    111. 0.3395, 0.9164, 0.1096, 0,
    112. 0.0474, 0.0135, 0.8698, 0,
    113. 0, 0, 0, 1);
    114. float iResolution = scale;
    115. int numSamples = 1;
    116. vec3 result = vec3(0.0);
    117. vec2 fragCoord = vec2(Po[0],Po[1]);
    118. // cheap AA
    119. for (int x = 0; x < numSamples; ++x)
    120. {
    121. for (int y = 0; y < numSamples; ++y)
    122. {
    123. vec2 offset = vec2(float(x), float(y)) / float(numSamples);
    124. vec2 uv = (fragCoord + offset) / iResolution;
    125. vec3 p = vec3(uv.x,uv.y, Time*0.001);
    126. result += flow_texture(p * 6.0,Time,steps);
    127. }
    128. }
    129. result /= float(numSamples * numSamples);
    130. vector Out = vec3(sqrt(result));
    131. Out = pow(Out,2.2);
    132. float r = Out[0], g = Out[1], b = Out[2];
    133. // ACES Output
    134. if (aces == 0)
    135. outColor = Out;
    136. else
    137. {
    138. outColor = transform(aces_tm, vector(r,g,b));
    139. }
    140. }