CubeTextureNode.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. function CubeTextureNode( value, coord, bias ) {
  7. InputNode.call( this, 'v4', { shared: true } );
  8. this.value = value;
  9. this.coord = coord || new ReflectNode();
  10. this.bias = bias;
  11. };
  12. CubeTextureNode.prototype = Object.create( InputNode.prototype );
  13. CubeTextureNode.prototype.constructor = CubeTextureNode;
  14. CubeTextureNode.prototype.nodeType = "CubeTexture";
  15. CubeTextureNode.prototype.getTexture = function ( builder, output ) {
  16. return InputNode.prototype.generate.call( this, builder, output, this.value.uuid, 'tc' );
  17. };
  18. CubeTextureNode.prototype.generate = function ( builder, output ) {
  19. if ( output === 'samplerCube' ) {
  20. return this.getTexture( builder, output );
  21. }
  22. var cubetex = this.getTexture( builder, output );
  23. var coord = this.coord.build( builder, 'v3' );
  24. var bias = this.bias ? this.bias.build( builder, 'fv1' ) : undefined;
  25. if ( bias === undefined && builder.context.bias ) {
  26. bias = new builder.context.bias( this ).build( builder, 'fv1' );
  27. }
  28. var code;
  29. if ( bias ) code = 'texCubeBias( ' + cubetex + ', ' + coord + ', ' + bias + ' )';
  30. else code = 'texCube( ' + cubetex + ', ' + coord + ' )';
  31. code = builder.getTexelDecodingFunctionFromTexture( code, this.value );
  32. return builder.format( code, this.type, output );
  33. };
  34. CubeTextureNode.prototype.copy = function ( source ) {
  35. InputNode.prototype.copy.call( this, source );
  36. if ( source.value ) this.value = source.value;
  37. this.coord = source.coord;
  38. if ( source.bias ) this.bias = source.bias;
  39. };
  40. CubeTextureNode.prototype.toJSON = function ( meta ) {
  41. var data = this.getJSONNode( meta );
  42. if ( ! data ) {
  43. data = this.createJSONNode( meta );
  44. data.value = this.value.uuid;
  45. data.coord = this.coord.toJSON( meta ).uuid;
  46. if ( this.bias ) data.bias = this.bias.toJSON( meta ).uuid;
  47. }
  48. return data;
  49. };
  50. export { CubeTextureNode };