CubeTextureNode.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. import { ExpressionNode } from '../core/ExpressionNode.js';
  8. function CubeTextureNode( value, uv, bias ) {
  9. InputNode.call( this, 'v4', { shared: true } );
  10. this.value = value;
  11. this.uv = uv || new ReflectNode();
  12. this.bias = bias;
  13. }
  14. CubeTextureNode.prototype = Object.create( InputNode.prototype );
  15. CubeTextureNode.prototype.constructor = CubeTextureNode;
  16. CubeTextureNode.prototype.nodeType = "CubeTexture";
  17. CubeTextureNode.prototype.getTexture = function ( builder, output ) {
  18. return InputNode.prototype.generate.call( this, builder, output, this.value.uuid, 'tc' );
  19. };
  20. CubeTextureNode.prototype.generate = function ( builder, output ) {
  21. if ( output === 'samplerCube' ) {
  22. return this.getTexture( builder, output );
  23. }
  24. var cubetex = this.getTexture( builder, output );
  25. var uv = this.uv.build( builder, 'v3' );
  26. var bias = this.bias ? this.bias.build( builder, 'f' ) : undefined;
  27. if ( bias === undefined && builder.context.bias ) {
  28. bias = new builder.context.bias( this ).build( builder, 'f' );
  29. }
  30. var code;
  31. if ( bias ) code = 'texCubeBias( ' + cubetex + ', ' + uv + ', ' + bias + ' )';
  32. else code = 'texCube( ' + cubetex + ', ' + uv + ' )';
  33. // add a custom context for fix incompatibility with the core
  34. // include ColorSpace function only for vertex shader (in fragment shader color space functions is added automatically by core)
  35. // this should be removed in the future
  36. // context.include =: is used to include or not functions if used FunctionNode
  37. // context.ignoreCache =: not create variables temp nodeT0..9 to optimize the code
  38. var context = { include: builder.isShader( 'vertex' ), ignoreCache: true };
  39. var outputType = this.getType( builder );
  40. builder.addContext( context );
  41. this.colorSpace = this.colorSpace || new ColorSpaceNode( new ExpressionNode( '', outputType ) );
  42. this.colorSpace.fromEncoding( builder.getTextureEncodingFromMap( this.value ) );
  43. this.colorSpace.input.parse( code );
  44. code = this.colorSpace.build( builder, outputType );
  45. // end custom context
  46. builder.removeContext();
  47. return builder.format( code, outputType, output );
  48. };
  49. CubeTextureNode.prototype.copy = function ( source ) {
  50. InputNode.prototype.copy.call( this, source );
  51. if ( source.value ) this.value = source.value;
  52. this.uv = source.uv;
  53. if ( source.bias ) this.bias = source.bias;
  54. return this;
  55. };
  56. CubeTextureNode.prototype.toJSON = function ( meta ) {
  57. var data = this.getJSONNode( meta );
  58. if ( ! data ) {
  59. data = this.createJSONNode( meta );
  60. data.value = this.value.uuid;
  61. data.uv = this.uv.toJSON( meta ).uuid;
  62. if ( this.bias ) data.bias = this.bias.toJSON( meta ).uuid;
  63. }
  64. return data;
  65. };
  66. export { CubeTextureNode };