beep

OSL flakes for Octane

Nov 7th, 2017
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. shader
  2. flakes
  3. (
  4. float flake_scale = 10.0 [[ string description = "Smaller values zoom into the flake map, larger values zoom out" ]],
  5. float flake_size = 0.5 [[ string description = "Relative size of the flakes" ]],
  6. float flake_size_variance = 0.0 [[ string description = "0.0 makes all flakes the same size, 1.0 assigns random size between 0 and the given flake size" ]],
  7. float flake_normal_orientation = 0.0 [[ string description = "Blend between the flake normals (0.0) and the surface normal (1.0)" ]],
  8. point proj = point(u, v, 0) [[string label = "Projection"]],
  9. output color result = 1.0
  10. )
  11. {
  12. float safe_flake_size_variance = clamp(flake_size_variance, 0.1, 1.0);
  13. vector cellCenters[9] = {
  14. vector( 0.5, 0.5, 0.0),
  15. vector( 1.5, 0.5, 0.0),
  16. vector( 1.5, 1.5, 0.0),
  17. vector( 0.5, 1.5, 0.0),
  18. vector(-0.5, 1.5, 0.0),
  19. vector(-0.5, 0.5, 0.0),
  20. vector(-0.5, -0.5, 0.0),
  21. vector( 0.5, -0.5, 0.0),
  22. vector( 1.5, -0.5, 0.0)
  23. };
  24.  
  25. //point position = vector(u, v, 0.0);
  26. point position = flake_scale * proj;
  27.  
  28. point base = floor(position);
  29.  
  30. point nearestCell = point(0.0, 0.0, 1.0);
  31. int nearestCellIndex = -1;
  32. for(int cellIndex = 0; cellIndex < 9; ++cellIndex) {
  33. point cellCenter = base + cellCenters[cellIndex];
  34.  
  35. vector centerOffset = cellnoise(cellCenter) * 2.0 - 1.0;
  36. centerOffset[2] *= safe_flake_size_variance;
  37. centerOffset = normalize(centerOffset);
  38.  
  39. cellCenter += 0.5 * centerOffset;
  40. float cellDistance = distance(position, cellCenter);
  41. if(cellDistance < flake_size && cellCenter[2] < nearestCell[2]) {
  42. nearestCell = cellCenter;
  43. nearestCellIndex = cellIndex;
  44. }
  45. }
  46.  
  47. result = color(0.5, 0.5, 1.0);
  48.  
  49. if (nearestCellIndex != -1) {
  50. vector randomNormal = cellnoise(base + cellCenters[nearestCellIndex] + vector(0.0, 0.0, 1.5));
  51. randomNormal = 2.0 * randomNormal - 1.0;
  52. randomNormal = faceforward(randomNormal, I, randomNormal);
  53. randomNormal = normalize(mix(randomNormal, vector(0.0, 0.0, 1.0), flake_normal_orientation));
  54.  
  55. result = color(0.5*randomNormal[0]+0.5, 0.5*randomNormal[1]+0.5, randomNormal[2]);
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment