mx_hsv.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Three.js Transpiler
  2. // https://github.com/AcademySoftwareFoundation/MaterialX/blob/main/libraries/stdlib/genglsl/lib/mx_hsv.glsl
  3. import { int, float, vec3, If, tslFn } from '../../shadernode/ShaderNode.js';
  4. import { add, sub, mul } from '../../math/OperatorNode.js';
  5. import { floor, trunc, max, min } from '../../math/MathNode.js';
  6. export const mx_hsvtorgb = /*#__PURE__*/ tslFn( ( [ hsv_immutable ] ) => {
  7. const hsv = vec3( hsv_immutable ).toVar();
  8. const h = float( hsv.x ).toVar();
  9. const s = float( hsv.y ).toVar();
  10. const v = float( hsv.z ).toVar();
  11. If( s.lessThan( 0.0001 ), () => {
  12. return vec3( v, v, v );
  13. } ).else( () => {
  14. h.assign( mul( 6.0, h.sub( floor( h ) ) ) );
  15. const hi = int( trunc( h ) ).toVar();
  16. const f = float( h.sub( float( hi ) ) ).toVar();
  17. const p = float( v.mul( sub( 1.0, s ) ) ).toVar();
  18. const q = float( v.mul( sub( 1.0, s.mul( f ) ) ) ).toVar();
  19. const t = float( v.mul( sub( 1.0, s.mul( sub( 1.0, f ) ) ) ) ).toVar();
  20. If( hi.equal( int( 0 ) ), () => {
  21. return vec3( v, t, p );
  22. } ).elseif( hi.equal( int( 1 ) ), () => {
  23. return vec3( q, v, p );
  24. } ).elseif( hi.equal( int( 2 ) ), () => {
  25. return vec3( p, v, t );
  26. } ).elseif( hi.equal( int( 3 ) ), () => {
  27. return vec3( p, q, v );
  28. } ).elseif( hi.equal( int( 4 ) ), () => {
  29. return vec3( t, p, v );
  30. } );
  31. return vec3( v, p, q );
  32. } );
  33. } ).setLayout( {
  34. name: 'mx_hsvtorgb',
  35. type: 'vec3',
  36. inputs: [
  37. { name: 'hsv', type: 'vec3' }
  38. ]
  39. } );
  40. export const mx_rgbtohsv = /*#__PURE__*/ tslFn( ( [ c_immutable ] ) => {
  41. const c = vec3( c_immutable ).toVar();
  42. const r = float( c.x ).toVar();
  43. const g = float( c.y ).toVar();
  44. const b = float( c.z ).toVar();
  45. const mincomp = float( min( r, min( g, b ) ) ).toVar();
  46. const maxcomp = float( max( r, max( g, b ) ) ).toVar();
  47. const delta = float( maxcomp.sub( mincomp ) ).toVar();
  48. const h = float().toVar(), s = float().toVar(), v = float().toVar();
  49. v.assign( maxcomp );
  50. If( maxcomp.greaterThan( 0.0 ), () => {
  51. s.assign( delta.div( maxcomp ) );
  52. } ).else( () => {
  53. s.assign( 0.0 );
  54. } );
  55. If( s.lessThanEqual( 0.0 ), () => {
  56. h.assign( 0.0 );
  57. } ).else( () => {
  58. If( r.greaterThanEqual( maxcomp ), () => {
  59. h.assign( g.sub( b ).div( delta ) );
  60. } ).elseif( g.greaterThanEqual( maxcomp ), () => {
  61. h.assign( add( 2.0, b.sub( r ).div( delta ) ) );
  62. } ).else( () => {
  63. h.assign( add( 4.0, r.sub( g ).div( delta ) ) );
  64. } );
  65. h.mulAssign( 1.0 / 6.0 );
  66. If( h.lessThan( 0.0 ), () => {
  67. h.addAssign( 1.0 );
  68. } );
  69. } );
  70. return vec3( h, s, v );
  71. } ).setLayout( {
  72. name: 'mx_rgbtohsv',
  73. type: 'vec3',
  74. inputs: [
  75. { name: 'c', type: 'vec3' }
  76. ]
  77. } );