Advertisement
beep

Bevel OSL

May 25th, 2018
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. void rng_seed(output int rng, int seed)
  2. {
  3. int chash = seed;
  4. if (chash == 0) chash = 1;
  5. rng = chash * 30391861;
  6. }
  7.  
  8. float rng_uniform(output int rng)
  9. {
  10. float res = rng / float(2137483647) * 0.5 + 0.5;
  11. rng *= 30391861;
  12. return res;
  13. }
  14.  
  15. void to_unit_disk(float x, float y, output float x_out, output float y_out)
  16. {
  17. float r, phi;
  18. float a = 2.0 * x - 1.0;
  19. float b = 2.0 * y - 1.0;
  20.  
  21. if(a > -b)
  22. { if(a > b)
  23. { r = a;
  24. phi = M_PI_4 *(b/a);
  25. }
  26. else
  27. { r = b;
  28. phi = M_PI_4 *(2.0 - a/b);
  29. } }
  30. else
  31. { if(a < b)
  32. { r = -a;
  33. phi = M_PI_4 *(4.0 + b/a);
  34. }
  35. else
  36. { r = -b;
  37. if(b != 0.0) phi = M_PI_4 *(6.0 - a/b);
  38. else phi = 0.0;
  39. } }
  40. x_out = r * cos(phi);
  41. y_out = r * sin(phi);
  42. }
  43.  
  44. void make_orthonormals(vector N, output vector a, output vector b)
  45. {
  46. if(N[0] != N[1] || N[0] != N[2]) a = cross(vector(1, 1, 1), N);
  47. else a = cross(vector(-1, 1, 1), N);
  48.  
  49. a = normalize(a);
  50. b = cross(N, a);
  51. }
  52.  
  53. vector sample_cos_hemisphere(vector N, float randu, float randv)
  54. {
  55. vector T, B;
  56.  
  57. make_orthonormals(N, T, B);
  58. to_unit_disk(randu, randv, randu, randv);
  59. float costheta = sqrt(max(1.0 - randu * randu - randv * randv, 0.0));
  60.  
  61. return randu * T + randv * B + costheta * N;
  62. }
  63.  
  64. shader edge_smooth(
  65. int Concave = 1,
  66. int Convex = 1,
  67. int Samples = 4,
  68. float Mask = 1,
  69. float Distance = 0.1,
  70. normal Normal = N,
  71. output normal outNormal = 0
  72. )
  73. {
  74. int i, rng;
  75. float f, randu, randv, ray_t, hits = 0;
  76. // int Samples = 2;
  77. vector ray_P, ray_R;
  78. normal hit_normal = N;
  79. outNormal = Normal;
  80. float hit_dist;
  81. float normal_blend;
  82. float s_weight = 1/Samples;
  83.  
  84. f = fmod(cellnoise(P*123456.0), 1.0);
  85. rng_seed(rng, int(f * 21374647));
  86.  
  87. if (Mask > 0.5) {
  88. for(i = 0; i < Samples; i++) {
  89. randu = rng_uniform(rng);
  90. randv = rng_uniform(rng);
  91.  
  92. ray_P = P;
  93. ray_R = sample_cos_hemisphere(-N, randu, randv);
  94. ray_t = Distance;
  95.  
  96.  
  97. if (Concave == 1)
  98. { if(trace(ray_P, -ray_R, "maxdist", ray_t)) {
  99. getmessage ("trace", "N", hit_normal);
  100. getmessage ("trace", "hitdist", hit_dist);
  101. normal_blend = 1-(hit_dist/Distance);
  102. outNormal = Normal + (hit_normal*normal_blend);
  103. break;
  104. }
  105. }
  106. if (Convex == 1) {
  107. if(trace(ray_P, ray_R, "maxdist", ray_t)) {
  108. getmessage ("trace", "N", hit_normal);
  109. getmessage ("trace", "hitdist", hit_dist);
  110. normal_blend = 1-(hit_dist/Distance);
  111. if (dot(I, -hit_normal) > 0.0) {
  112. outNormal = Normal - (hit_normal*normal_blend);
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. outNormal = normalize(outNormal);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement