LuminanceNode.js 1.4 KB

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