CubeTextureNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { InputNode } from '../core/InputNode.js';
  5. import { ReflectNode } from '../accessors/ReflectNode.js';
  6. import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
  7. function CubeTextureNode( value, uv, bias ) {
  8. InputNode.call( this, 'v4', { shared: true } );
  9. this.value = value;
  10. this.uv = uv || new ReflectNode();
  11. this.bias = bias;
  12. }
  13. CubeTextureNode.prototype = Object.create( InputNode.prototype );
  14. CubeTextureNode.prototype.constructor = CubeTextureNode;
  15. CubeTextureNode.prototype.nodeType = "CubeTexture";
  16. CubeTextureNode.prototype.getTexture = function ( builder, output ) {
  17. return InputNode.prototype.generate.call( this, builder, output, this.value.uuid, 'tc' );
  18. };
  19. CubeTextureNode.prototype.generate = function ( builder, output ) {
  20. if ( output === 'samplerCube' ) {
  21. return this.getTexture( builder, output );
  22. }
  23. var cubetex = this.getTexture( builder, output );
  24. var uv = this.uv.build( builder, 'v3' );
  25. var bias = this.bias ? this.bias.build( builder, 'f' ) : undefined;
  26. if ( bias === undefined && builder.context.bias ) {
  27. bias = new builder.context.bias( this ).build( builder, 'f' );
  28. }
  29. var code;
  30. if ( bias ) code = 'texCubeBias( ' + cubetex + ', ' + uv + ', ' + bias + ' )';
  31. else code = 'texCube( ' + cubetex + ', ' + uv + ' )';
  32. // add this context to replace ColorSpaceNode.input to code
  33. builder.addContext( { input: code, encoding: builder.getTextureEncodingFromMap( this.value ), include: builder.isShader( 'vertex' ) } );
  34. this.colorSpace = this.colorSpace || new ColorSpaceNode( this );
  35. code = this.colorSpace.build( builder, this.type );
  36. builder.removeContext();
  37. return builder.format( code, this.type, output );
  38. };
  39. CubeTextureNode.prototype.copy = function ( source ) {
  40. InputNode.prototype.copy.call( this, source );
  41. if ( source.value ) this.value = source.value;
  42. this.uv = source.uv;
  43. if ( source.bias ) this.bias = source.bias;
  44. };
  45. CubeTextureNode.prototype.toJSON = function ( meta ) {
  46. var data = this.getJSONNode( meta );
  47. if ( ! data ) {
  48. data = this.createJSONNode( meta );
  49. data.value = this.value.uuid;
  50. data.uv = this.uv.toJSON( meta ).uuid;
  51. if ( this.bias ) data.bias = this.bias.toJSON( meta ).uuid;
  52. }
  53. return data;
  54. };
  55. export { CubeTextureNode };