RemapNode.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { TempNode } from '../core/TempNode.js';
  2. import { FunctionNode } from '../core/FunctionNode.js';
  3. const REMAP_SRC = `
  4. float remap( float value, float inLow, float inHigh, float outLow, float outHigh ) {
  5. float x = ( value - inLow ) / ( inHigh - inLow );
  6. return outLow + ( outHigh - outLow ) * x;
  7. }
  8. vec2 remap( vec2 value, vec2 inLow, vec2 inHigh, vec2 outLow, vec2 outHigh ) {
  9. return vec2(
  10. remap( value.x, inLow.x, inHigh.x, outLow.x, outHigh.x ),
  11. remap( value.y, inLow.y, inHigh.y, outLow.y, outHigh.y )
  12. );
  13. }
  14. vec2 remap( vec2 value, float inLow, float inHigh, float outLow, float outHigh ) {
  15. return vec2(
  16. remap( value.x, inLow, inHigh, outLow, outHigh ),
  17. remap( value.y, inLow, inHigh, outLow, outHigh )
  18. );
  19. }
  20. vec3 remap( vec3 value, vec3 inLow, vec3 inHigh, vec3 outLow, vec3 outHigh ) {
  21. return vec3(
  22. remap( value.x, inLow.x, inHigh.x, outLow.x, outHigh.x ),
  23. remap( value.y, inLow.y, inHigh.y, outLow.y, outHigh.y ),
  24. remap( value.z, inLow.z, inHigh.z, outLow.z, outHigh.z )
  25. );
  26. }
  27. vec3 remap( vec3 value, float inLow, float inHigh, float outLow, float outHigh ) {
  28. return vec3(
  29. remap( value.x, inLow, inHigh, outLow, outHigh ),
  30. remap( value.y, inLow, inHigh, outLow, outHigh ),
  31. remap( value.z, inLow, inHigh, outLow, outHigh )
  32. );
  33. }
  34. vec4 remap( vec4 value, vec4 inLow, vec4 inHigh, vec4 outLow, vec4 outHigh ) {
  35. return vec4(
  36. remap( value.x, inLow.x, inHigh.x, outLow.x, outHigh.x ),
  37. remap( value.y, inLow.y, inHigh.y, outLow.y, outHigh.y ),
  38. remap( value.z, inLow.z, inHigh.z, outLow.z, outHigh.z ),
  39. remap( value.w, inLow.w, inHigh.w, outLow.w, outHigh.w )
  40. );
  41. }
  42. vec4 remap( vec4 value, float inLow, float inHigh, float outLow, float outHigh ) {
  43. return vec4(
  44. remap( value.x, inLow, inHigh, outLow, outHigh ),
  45. remap( value.y, inLow, inHigh, outLow, outHigh ),
  46. remap( value.z, inLow, inHigh, outLow, outHigh ),
  47. remap( value.w, inLow, inHigh, outLow, outHigh )
  48. );
  49. }
  50. `.trim();
  51. class RemapNode extends TempNode {
  52. constructor( value, inLow, inHigh, outLow, outHigh ) {
  53. super( 'f' );
  54. this.value = value;
  55. this.inLow = inLow;
  56. this.inHigh = inHigh;
  57. this.outLow = outLow;
  58. this.outHigh = outHigh;
  59. }
  60. generate( builder, output ) {
  61. const remap = builder.include( RemapNode.Nodes.remap );
  62. return builder.format( remap + '( ' + [
  63. this.value.build( builder ),
  64. this.inLow.build( builder ),
  65. this.inHigh.build( builder ),
  66. this.outLow.build( builder ),
  67. this.outHigh.build( builder ),
  68. ].join( ', ' ) + ' )', this.getType( builder ), output );
  69. }
  70. getType( builder ) {
  71. return this.value.getType( builder );
  72. }
  73. copy( source ) {
  74. super.copy( source );
  75. this.value = source.value;
  76. this.inLow = source.inLow;
  77. this.inHigh = source.inHigh;
  78. this.outLow = source.outLow;
  79. this.outHigh = source.outHigh;
  80. }
  81. toJSON( meta ) {
  82. let data = this.getJSONNode( meta );
  83. if ( ! data ) {
  84. data = this.createJSONNode( meta );
  85. data.value = this.value.toJSON( meta ).uuid;
  86. data.inLow = this.inLow.toJSON( meta ).uuid;
  87. data.inHigh = this.inHigh.toJSON( meta ).uuid;
  88. data.outLow = this.outLow.toJSON( meta ).uuid;
  89. data.outHigh = this.outHigh.toJSON( meta ).uuid;
  90. }
  91. return data;
  92. }
  93. }
  94. RemapNode.prototype.nodeType = 'Remap';
  95. RemapNode.Nodes = ( function () {
  96. return {
  97. remap: new FunctionNode( REMAP_SRC )
  98. };
  99. } )();
  100. export { RemapNode };