VarNode.js 1.1 KB

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