LuminanceNode.js 1.4 KB

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