Hexagon - 图1

    1. /*
    2. * Hexagon.osl by Michel J. Anders (c)2012
    3. * from https://github.com/sambler/osl-shaders
    4. *
    5. * license: cc-by-sa
    6. *
    7. * original script from -
    8. * http://blenderthings.blogspot.co.uk/2012/11/a-hexagon-osl-shader.html
    9. * Modified 1/4/2020 by Saul Espinosa for Redshift 3D
    10. */
    11. #define A 0.86602540378443864676372317075294 // sqrt(3)/2
    12. #define A2 (2*A)
    13. #define A4 (4*A)
    14. #define SY (1/A)
    15. shader MAhexagon
    16. [[ string help = "Generates an RGB Hexagon Pattern",
    17. string label = "Hexagons" ]]
    18. (
    19. vector Vector = 0,
    20. color Diffuse_Color1 = color(0.2, 0.8, 0.2),
    21. color Diffuse_Color2 = color(0.8, 0.2, 0.2),
    22. color Diffuse_Color3 = color(0.2, 0.2, 0.8),
    23. output color Color = 0,
    24. output int Index = 1,
    25. output float Distance = 0)
    26. {
    27. // calculate the color
    28. color colors[3] = {Diffuse_Color1,
    29. Diffuse_Color2,
    30. Diffuse_Color3};
    31. // we warp the grid so that two adjacent equilateral triangles
    32. // are mapped to two triangles that fit in a square
    33. float syc = Vector[1] * SY;
    34. float sxc = Vector[0] + 0.5 * syc;
    35. int ind[18] = {1,1,3,3,3,1, 2,2,2,3,3,3, 1,2,2,2,1,1};
    36. int iy = int(mod(syc,3.0));
    37. int ix = int(mod(sxc,3.0));
    38. ix = iy * 6 + ix * 2 + ( mod(sxc,1.0) > mod(syc,1.0) );
    39. Index = ind[ix];
    40. Color = colors[Index-1];
    41. // calculate the distance to the center of the hexagon
    42. float sx = mod(Vector[0],3);
    43. float sy = mod(Vector[1]+0.75,A4);
    44. // map everthing to a single quadrant
    45. if ( sx > 1.5 ) sx = 3 - sx;
    46. if ( sy > A2 ) sy = A4 - sy;
    47. // the distance were interested in is the distance to
    48. // the *closest* center point
    49. float d1 = distance(vector(sx,sy,0),vector(1.5,A2,0));
    50. float d2 = distance(vector(sx,sy,0),vector(0,A,0));
    51. float d6 = distance(vector(sx,sy,0),vector(1.5,0,0));
    52. Distance = min(min(d1,d2), d6);
    53. }