Advertisement
Guest User

Untitled

a guest
May 19th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.42 KB | None | 0 0
  1. #include "headers/baseshadersheader.fxh"
  2.  
  3. #define inoise noiseFast
  4. #define fBm    fBmFast
  5. #define turbulence turbulenceFast
  6. #define ridgedMF ridgedMFFast
  7.  
  8. int Enum_Circuit_None = 0;
  9. int Enum_Circuit_Circle = 1;
  10. int Enum_Circuit_Square = 2;
  11. int Enum_Circuit_Diamond = 3;
  12. int Enum_Perlin_fBm = 0;
  13. int Enum_Perlin_turbulence = 1;
  14. int Enum_Perlin_ridgedMf = 2;
  15. int Enum_Worley_Euclidian = 0;
  16. int Enum_Worley_Manhattan = 1;
  17. int Enum_Worley_Chebyshev = 2;
  18.  
  19.  
  20. void FuncConstantVector(float4 constant, out float4 result)
  21. {
  22.   result = constant;
  23. }
  24.  
  25.  
  26. void FuncScale(float4 a, float factor, out float4 result)
  27. {
  28.   result = a*factor;
  29. }
  30.  
  31.  
  32. void FuncCircuit(float4 position, float hOffset, float vOffset, float hThickness, float vThickness, int centerShape, float centerSize, float2 cellSize, float edgeProbability, float2 jitter, out float4 hLine, out float4 vLine, out float4 center)
  33. {
  34.   float2 cellID = floor(position.xy / cellSize);
  35.   float2 cellPos = frac(position.xy / cellSize);
  36.  
  37.   float randN = perm2d(cellID.xy*.432).r;
  38.   float randW = perm2d(cellID.xy*.432).g;
  39.   float randS = perm2d((cellID.xy + float2(0,1))*.432).r;
  40.   float randE = perm2d((cellID.xy + float2(1,0))*.432).g;
  41.  
  42.   float hasN = (randN <= edgeProbability) ? 1 : 0;
  43.   float hasW = (randW <= edgeProbability) ? 1 : 0;
  44.   float hasS = (randS <= edgeProbability) ? 1 : 0;
  45.   float hasE = (randE <= edgeProbability) ? 1 : 0;
  46.  
  47.   jitter = jitter * (float2(perm(cellID.x*.324), perm(cellID.y*.324))*2-1);
  48.  
  49.   hOffset += jitter.x;
  50.   vOffset += jitter.y;
  51.  
  52.   float2 cellLine = cellPos - float2(hOffset, vOffset);  // now, 0,0 should be center.
  53.  
  54.   float nMod = 1-saturate(cellLine.y / vThickness);
  55.   float sMod = 1-saturate(-cellLine.y / vThickness);
  56.   float wMod = 1-saturate(cellLine.x / hThickness);
  57.   float eMod = 1-saturate(-cellLine.x / hThickness);
  58.  
  59.   nMod = smoothstep(0.1, 0.2, nMod);
  60.   sMod = smoothstep(0.1, 0.2, sMod);
  61.   eMod = smoothstep(0.1, 0.2, eMod);
  62.   wMod = smoothstep(0.1, 0.2, wMod);
  63.  
  64.   float nLine = hasN * wMod * eMod * nMod;
  65.   float sLine = hasS * wMod * eMod * sMod;
  66.   float eLine = hasE * nMod * sMod * eMod;
  67.   float wLine = hasW * nMod * sMod * wMod;
  68.  
  69.   hLine = max(eLine, wLine);
  70.   vLine = max(nLine, sLine);
  71.  
  72.   float2 centerComp = abs(cellPos - float2(hOffset, vOffset));
  73.  
  74.   center = 1000;
  75.   if(centerShape == Enum_Circuit_Circle)
  76.     center = length(centerComp);
  77.   if(centerShape == Enum_Circuit_Square)
  78.     center = max(centerComp.x, centerComp.y);
  79.   if(centerShape == Enum_Circuit_Diamond)
  80.     center = centerComp.x + centerComp.y;
  81.  
  82.   center = smoothstep(centerSize*0.98, centerSize*1.02, center);
  83.   center = saturate(1.0 - center);
  84.  
  85.   float hasAll = hasN + hasW + hasS + hasE;
  86.   if(hasAll == 0 || hasAll == 2)
  87.     center = 0;
  88.    
  89.  
  90. }
  91.  
  92.  
  93. void FuncPerlin(float4 position, int octaves, float lacunarity, float gain, float ridgeOffset, int noiseType, out float4 noise)
  94. {
  95.   float freq = 1.0, amp = 0.5;
  96.   float sum = 0;   
  97.   float prev = 1.0;
  98.   if(octaves == 1)
  99.     amp = 1;
  100.   for(int i=0; i<octaves; i++)
  101.   {
  102.     float noiseVal = noisePerlin(position.xyz*freq);
  103.     if(noiseType == Enum_Perlin_turbulence)
  104.       noiseVal = abs(noiseVal);
  105.     if(noiseType == Enum_Perlin_ridgedMf)
  106.     {
  107.       noiseVal = ridge(noiseVal, ridgeOffset);
  108.       float prevPrev = prev;
  109.       prev = noiseVal;
  110.       noiseVal *= prevPrev;
  111.     }
  112.     sum += noiseVal * amp;
  113.     freq *= lacunarity;
  114.     amp *= gain;
  115.   }
  116.   noise = sum.xxxx;
  117.   if(noiseType == Enum_Perlin_fBm)
  118.     noise = noise * 0.5 + 0.5;
  119. }
  120.  
  121.  
  122. void FuncWorley(float4 position, float jitter, int distanceMetric, int pointsPerCell, out float4 f1, out float4 f2, out float4 f3, out float4 f4, out float4 nearest)
  123. {
  124.   float3 p1, p2, p3, p4;
  125.   float of1, of2, of3, of4;
  126.   int distanceType = DISTANCE_EUCLIDIAN;
  127.   if(distanceMetric == Enum_Worley_Manhattan)
  128.     distanceType = DISTANCE_MANHATTAN;
  129.   if(distanceMetric == Enum_Worley_Chebyshev)
  130.     distanceType = DISTANCE_CHEBYSHEV;
  131.   voronoi(position.xyz, of1, p1, of2, p2, of3, p3, of4, p4, jitter, distanceType, pointsPerCell);
  132.  
  133.   f1 = of1.xxxx;
  134.   f2 = of2.xxxx;
  135.   f3 = of3.xxxx;
  136.   f4 = of4.xxxx;
  137.   nearest = float4(p1.xyz,1);
  138. }
  139.  
  140.  
  141. void FuncLinearCombination(float4 a, float4 b, float4 c, float4 d, float factora, float factorb, float factorc, float factord, out float4 result)
  142. {
  143.   result = a*factora + b*factorb + c*factorc + d*factord;
  144. }
  145.  
  146.  
  147. void FuncMultiply(float4 a, float4 b, out float4 result)
  148. {
  149.   result = a*b;
  150. }
  151.  
  152.  
  153. void FuncSubtract(float4 a, float4 b, out float4 result)
  154. {
  155.   result = a-b;
  156. }
  157.  
  158.  
  159. void FuncSaturate(float4 a, out float4 result)
  160. {
  161.   result = saturate(a);
  162. }
  163.  
  164.  
  165. void FuncScaleRange(float4 a, float oldMin, float oldMax, float newMin, float newMax, out float4 result)
  166. {
  167.   result = (a-oldMin)/(oldMax-oldMin); // rescale to 0..1
  168.   result = result*(newMax-newMin) + newMin;
  169. }
  170.  
  171.  
  172. void FuncLerp(float4 a, float4 b, float4 t, out float4 result)
  173. {
  174.   result = lerp(a, b, t.x);
  175.  
  176. }
  177.  
  178.  
  179.  
  180.  
  181. void ProceduralTexture(float3 positionIn, float2 texCoordIn, out float4 colorOut, out float heightOut)
  182. {
  183.   float4 position = float4(positionIn, 1);
  184.   float4 texCoord = float4(texCoordIn, 0, 0);
  185.  
  186.   float4 generated_result_0 = float4(0,0,0,0);
  187.   FuncConstantVector(float4(0.2, 0.7, 0.4, 1), generated_result_0);
  188.  
  189.   float4 generated_result_1 = float4(0,0,0,0);
  190.   FuncConstantVector(float4(0.4, 0.5, 1, 0), generated_result_1);
  191.  
  192.   float4 generated_result_2 = float4(0,0,0,0);
  193.   FuncScale(position, float(10), generated_result_2);
  194.  
  195.   float4 generated_hLine_0 = float4(0,0,0,0);
  196.   float4 generated_vLine_0 = float4(0,0,0,0);
  197.   float4 generated_center_0 = float4(0,0,0,0);
  198.   FuncCircuit(position, float(0.5), float(0.5), float(0.04), float(0.04), int(Enum_Circuit_Diamond), float(0.15), float2(0.1, 0.1), float(0.2), float2(0.4, 0.4), generated_hLine_0, generated_vLine_0, generated_center_0);
  199.  
  200.   float4 generated_hLine_1 = float4(0,0,0,0);
  201.   float4 generated_vLine_1 = float4(0,0,0,0);
  202.   float4 generated_center_1 = float4(0,0,0,0);
  203.   FuncCircuit(position, float(0.5), float(0.5), float(0.02), float(0.02), int(Enum_Circuit_Diamond), float(0.1), float2(0.2, 0.2), float(0.7), float2(-0.2, 0.3), generated_hLine_1, generated_vLine_1, generated_center_1);
  204.  
  205.   float4 generated_noise_0 = float4(0,0,0,0);
  206.   FuncPerlin(generated_result_2, int(1), float(2), float(0.5), float(1), int(Enum_Perlin_ridgedMf), generated_noise_0);
  207.  
  208.   float4 generated_f1_0 = float4(0,0,0,0);
  209.   float4 generated_f2_0 = float4(0,0,0,0);
  210.   float4 generated_f3_0 = float4(0,0,0,0);
  211.   float4 generated_f4_0 = float4(0,0,0,0);
  212.   float4 generated_nearest_0 = float4(0,0,0,0);
  213.   FuncWorley(generated_result_2, float(0.9), int(Enum_Worley_Euclidian), int(1), generated_f1_0, generated_f2_0, generated_f3_0, generated_f4_0, generated_nearest_0);
  214.  
  215.   float4 generated_result_3 = float4(0,0,0,0);
  216.   FuncLinearCombination(generated_hLine_0, generated_vLine_0, generated_center_0, float4(0,0,0,0), float(1), float(1), float(1), float(1), generated_result_3);
  217.  
  218.   float4 generated_result_4 = float4(0,0,0,0);
  219.   FuncLinearCombination(generated_hLine_1, generated_vLine_1, generated_center_1, float4(0,0,0,0), float(1), float(1), float(1), float(1), generated_result_4);
  220.  
  221.   float4 generated_result_5 = float4(0,0,0,0);
  222.   FuncMultiply(generated_result_0, generated_noise_0, generated_result_5);
  223.  
  224.   float4 generated_result_6 = float4(0,0,0,0);
  225.   FuncSubtract(generated_f2_0, generated_f1_0, generated_result_6);
  226.  
  227.   float4 generated_result_7 = float4(0,0,0,0);
  228.   FuncSaturate(generated_result_3, generated_result_7);
  229.  
  230.   float4 generated_result_8 = float4(0,0,0,0);
  231.   FuncSaturate(generated_result_4, generated_result_8);
  232.  
  233.   float4 generated_result_9 = float4(0,0,0,0);
  234.   FuncScaleRange(generated_result_6, float(0), float(1), float(0.3), float(1), generated_result_9);
  235.  
  236.   float4 generated_result_10 = float4(0,0,0,0);
  237.   FuncMultiply(generated_result_8, generated_result_5, generated_result_10);
  238.  
  239.   float4 generated_result_11 = float4(0,0,0,0);
  240.   FuncMultiply(generated_result_9, generated_result_1, generated_result_11);
  241.  
  242.   float4 generated_result_12 = float4(0,0,0,0);
  243.   FuncMultiply(generated_result_11, generated_result_7, generated_result_12);
  244.  
  245.   float4 generated_result_13 = float4(0,0,0,0);
  246.   FuncLerp(generated_result_12, generated_result_10, generated_result_8, generated_result_13);
  247.  
  248.   float4 color = generated_result_13;
  249.   float4 height = float4(0,0,0,0);
  250.  
  251.  
  252.   colorOut = color.xyzw;
  253.   heightOut = height.x;
  254. }
  255.  
  256. #include "headers/baseshaders.fxh"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement