LuminanceNode.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { TempNode } from '../core/TempNode.js';
  2. import { ConstNode } from '../core/ConstNode.js';
  3. import { FunctionNode } from '../core/FunctionNode.js';
  4. class LuminanceNode extends TempNode {
  5. constructor( rgb ) {
  6. super( 'f' );
  7. this.rgb = rgb;
  8. }
  9. generate( builder, output ) {
  10. const luminance = builder.include( LuminanceNode.Nodes.luminance );
  11. return builder.format( luminance + '( ' + this.rgb.build( builder, 'v3' ) + ' )', this.getType( builder ), output );
  12. }
  13. copy( source ) {
  14. super.copy( source );
  15. this.rgb = source.rgb;
  16. return this;
  17. }
  18. toJSON( meta ) {
  19. let data = this.getJSONNode( meta );
  20. if ( ! data ) {
  21. data = this.createJSONNode( meta );
  22. data.rgb = this.rgb.toJSON( meta ).uuid;
  23. }
  24. return data;
  25. }
  26. }
  27. LuminanceNode.Nodes = ( function () {
  28. const LUMA = new ConstNode( 'vec3 LUMA vec3( 0.2125, 0.7154, 0.0721 )' );
  29. // Algorithm from Chapter 10 of Graphics Shaders
  30. const luminance = new FunctionNode( /* glsl */`
  31. float luminance( vec3 rgb ) {
  32. return dot( rgb, LUMA );
  33. }`
  34. , [ LUMA ] );
  35. return {
  36. LUMA: LUMA,
  37. luminance: luminance
  38. };
  39. } )();
  40. LuminanceNode.prototype.nodeType = 'Luminance';
  41. export { LuminanceNode };