VarNode.js 974 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { Node } from './Node.js';
  2. class VarNode extends Node {
  3. constructor( type, value ) {
  4. super( type );
  5. this.value = value;
  6. }
  7. getType( builder ) {
  8. return builder.getTypeByFormat( this.type );
  9. }
  10. generate( builder, output ) {
  11. const varying = builder.getVar( this.uuid, this.type );
  12. if ( this.value && builder.isShader( 'vertex' ) ) {
  13. builder.addNodeCode( varying.name + ' = ' + this.value.build( builder, this.getType( builder ) ) + ';' );
  14. }
  15. return builder.format( varying.name, this.getType( builder ), output );
  16. }
  17. copy( source ) {
  18. super.copy( source );
  19. this.type = source.type;
  20. this.value = source.value;
  21. return this;
  22. }
  23. toJSON( meta ) {
  24. let data = this.getJSONNode( meta );
  25. if ( ! data ) {
  26. data = this.createJSONNode( meta );
  27. data.type = this.type;
  28. if ( this.value ) data.value = this.value.toJSON( meta ).uuid;
  29. }
  30. return data;
  31. }
  32. }
  33. VarNode.prototype.nodeType = 'Var';
  34. export { VarNode };