TextureCubeNode.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. import { TextureCubeUVNode } from './TextureCubeUVNode.js';
  6. import { ColorSpaceNode } from '../utils/ColorSpaceNode.js';
  7. function TextureCubeNode( value, uv ) {
  8. TempNode.call( this, 'v4' );
  9. this.value = value;
  10. this.uv = uv || new TextureCubeUVNode();
  11. }
  12. TextureCubeNode.prototype = Object.create( TempNode.prototype );
  13. TextureCubeNode.prototype.constructor = TextureCubeNode;
  14. TextureCubeNode.prototype.nodeType = "TextureCube";
  15. TextureCubeNode.prototype.generate = function ( builder, output ) {
  16. if ( builder.isShader( 'fragment' ) ) {
  17. var uv_10 = this.uv.build( builder ) + '.uv_10',
  18. uv_20 = this.uv.build( builder ) + '.uv_20',
  19. t = this.uv.build( builder ) + '.t';
  20. var texture = this.value && this.value.value;
  21. var format = texture && texture.encoding || THREE.LinearEncoding;
  22. var decoding = ColorSpaceNode.prototype.getDecodingMethod(format);
  23. function decode(input) {
  24. return decoding[0] + '( ' + input +
  25. (decoding[1] !== undefined ? ', ' + decoding[1] : '') +
  26. ' )';
  27. }
  28. var color10 = 'texture2D( ' + this.value.build( builder, 'sampler2D' ) + ', ' + uv_10 + ' )';
  29. color10 = decode(color10);
  30. var color20 = 'texture2D( ' + this.value.build( builder, 'sampler2D' ) + ', ' + uv_20 + ' )';
  31. color20 = decode(color20);
  32. return builder.format( 'vec4( mix( ' + color10 + ', ' + color20 + ', ' + t + ' ).rgb, 1.0 )', this.getType( builder ), output );
  33. } else {
  34. console.warn( "THREE.TextureCubeNode is not compatible with " + builder.shader + " shader." );
  35. return builder.format( 'vec4( 0.0 )', this.getType( builder ), output );
  36. }
  37. };
  38. TextureCubeNode.prototype.toJSON = function ( meta ) {
  39. var data = this.getJSONNode( meta );
  40. if ( ! data ) {
  41. data = this.createJSONNode( meta );
  42. data.uv = this.uv.toJSON( meta ).uuid;
  43. data.textureSize = this.textureSize.toJSON( meta ).uuid;
  44. data.blinnExponentToRoughness = this.blinnExponentToRoughness.toJSON( meta ).uuid;
  45. if ( this.roughness ) data.roughness = this.roughness.toJSON( meta ).uuid;
  46. }
  47. return data;
  48. };
  49. export { TextureCubeNode };