TextureNode.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 = builder.requires.bias.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. if ( builder.isSlot( 'color' ) ) {
  33. code = 'mapTexelToLinear(' + code + ')';
  34. } else if ( builder.isSlot( 'emissive' ) ) {
  35. code = 'emissiveMapTexelToLinear(' + code + ')';
  36. } else if ( builder.isSlot( 'environment' ) ) {
  37. code = 'envMapTexelToLinear(' + code + ')';
  38. }
  39. return builder.format( code, this.type, output );
  40. };
  41. THREE.TextureNode.prototype.toJSON = function ( meta ) {
  42. var data = this.getJSONNode( meta );
  43. if ( ! data ) {
  44. data = this.createJSONNode( meta );
  45. if ( this.value ) data.value = this.value.uuid;
  46. data.coord = this.coord.toJSON( meta ).uuid;
  47. data.project = this.project;
  48. if ( this.bias ) data.bias = this.bias.toJSON( meta ).uuid;
  49. }
  50. return data;
  51. };