TextureNode.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.TextureNode = function ( value, coord, bias, project ) {
  5. THREE.InputNode.call( this, 'v4', { shared: true } );
  6. this.value = value;
  7. this.coord = coord || new THREE.UVNode();
  8. this.bias = bias;
  9. this.project = project !== undefined ? project : false;
  10. };
  11. THREE.TextureNode.prototype = Object.create( THREE.InputNode.prototype );
  12. THREE.TextureNode.prototype.constructor = THREE.TextureNode;
  13. THREE.TextureNode.prototype.nodeType = "Texture";
  14. THREE.TextureNode.prototype.getTexture = function ( builder, output ) {
  15. return THREE.InputNode.prototype.generate.call( this, builder, output, this.value.uuid, 't' );
  16. };
  17. THREE.TextureNode.prototype.generate = function ( builder, output ) {
  18. if ( output === 'sampler2D' ) {
  19. return this.getTexture( builder, output );
  20. }
  21. var tex = this.getTexture( builder, output );
  22. var coord = this.coord.build( builder, this.project ? 'v4' : 'v2' );
  23. var bias = this.bias ? this.bias.build( builder, 'fv1' ) : undefined;
  24. if ( bias == undefined && builder.requires.bias ) {
  25. bias = new builder.requires.bias( this ).build( builder, 'fv1' );
  26. }
  27. var method, code;
  28. if ( this.project ) method = 'texture2DProj';
  29. else method = bias ? 'tex2DBias' : 'tex2D';
  30. if ( bias ) code = method + '( ' + tex + ', ' + coord + ', ' + bias + ' )';
  31. else code = method + '( ' + tex + ', ' + coord + ' )';
  32. code = builder.getTexelDecodingFunctionFromTexture( code, this.value );
  33. return builder.format( code, this.type, output );
  34. };
  35. THREE.TextureNode.prototype.copy = function ( source ) {
  36. THREE.GLNode.prototype.copy.call( this, source );
  37. if ( source.value ) this.value = source.value;
  38. this.coord = source.coord;
  39. if ( source.bias ) this.bias = source.bias;
  40. if ( source.project !== undefined ) this.project = source.project;
  41. };
  42. THREE.TextureNode.prototype.toJSON = function ( meta ) {
  43. var data = this.getJSONNode( meta );
  44. if ( ! data ) {
  45. data = this.createJSONNode( meta );
  46. if ( this.value ) data.value = this.value.uuid;
  47. data.coord = this.coord.toJSON( meta ).uuid;
  48. data.project = this.project;
  49. if ( this.bias ) data.bias = this.bias.toJSON( meta ).uuid;
  50. }
  51. return data;
  52. };