TextureNode.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.getTexture = function( builder, output ) {
  14. return THREE.InputNode.prototype.generate.call( this, builder, output, this.value.uuid, 't' );
  15. };
  16. THREE.TextureNode.prototype.generate = function( builder, output ) {
  17. if ( output === 'sampler2D' ) {
  18. return this.getTexture( builder, output );
  19. }
  20. var tex = this.getTexture( builder, output );
  21. var coord = this.coord.build( builder, this.project ? 'v4' : 'v2' );
  22. var bias = this.bias ? this.bias.build( builder, 'fv1' ) : undefined;
  23. if ( bias == undefined && builder.requires.bias ) {
  24. bias = builder.requires.bias.build( builder, 'fv1' );
  25. }
  26. var method, code;
  27. if ( this.project ) method = 'texture2DProj';
  28. else method = bias ? 'tex2DBias' : 'tex2D';
  29. if ( bias ) code = method + '(' + tex + ',' + coord + ',' + bias + ')';
  30. else code = method + '(' + tex + ',' + coord + ')';
  31. if ( builder.isSlot( 'color' ) ) {
  32. code = 'mapTexelToLinear(' + code + ')';
  33. } else if ( builder.isSlot( 'emissive' ) ) {
  34. code = 'emissiveMapTexelToLinear(' + code + ')';
  35. } else if ( builder.isSlot( 'environment' ) ) {
  36. code = 'envMapTexelToLinear(' + code + ')';
  37. }
  38. return builder.format( code, this.type, output );
  39. };