BumpMapNode.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.BumpMapNode = function ( value, scale ) {
  5. THREE.TempNode.call( this, 'v3' );
  6. this.value = value;
  7. this.scale = scale || new THREE.FloatNode( 1 );
  8. this.toNormalMap = false;
  9. };
  10. THREE.BumpMapNode.fBumpToNormal = new THREE.FunctionNode( [
  11. "vec3 bumpToNormal( sampler2D bumpMap, vec2 uv, float scale ) {",
  12. " vec2 dSTdx = dFdx( uv );",
  13. " vec2 dSTdy = dFdy( uv );",
  14. " float Hll = texture2D( bumpMap, uv ).x;",
  15. " float dBx = texture2D( bumpMap, uv + dSTdx ).x - Hll;",
  16. " float dBy = texture2D( bumpMap, uv + dSTdy ).x - Hll;",
  17. " return vec3( .5 - ( dBx * scale ), .5 - ( dBy * scale ), 1.0 );",
  18. "}"
  19. ].join( "\n" ), null, { derivatives: true } );
  20. THREE.BumpMapNode.prototype = Object.create( THREE.TempNode.prototype );
  21. THREE.BumpMapNode.prototype.constructor = THREE.BumpMapNode;
  22. THREE.BumpMapNode.prototype.nodeType = "BumpMap";
  23. THREE.BumpMapNode.prototype.generate = function ( builder, output ) {
  24. if ( builder.isShader( 'fragment' ) ) {
  25. if ( this.toNormalMap ) {
  26. builder.include( THREE.BumpMapNode.fBumpToNormal );
  27. return builder.format( THREE.BumpMapNode.fBumpToNormal.name + '( ' + this.value.build( builder, 'sampler2D' ) + ', ' +
  28. this.value.coord.build( builder, 'v2' ) + ', ' +
  29. this.scale.build( builder, 'fv1' ) + ' )', this.getType( builder ), output );
  30. } else {
  31. builder.include( 'dHdxy_fwd' );
  32. builder.include( 'perturbNormalArb' );
  33. this.normal = this.normal || new THREE.NormalNode( THREE.NormalNode.VIEW );
  34. this.position = this.position || new THREE.PositionNode( THREE.NormalNode.VIEW );
  35. var derivativeOfHeightCode = 'dHdxy_fwd( ' + this.value.build( builder, 'sampler2D' ) + ', ' +
  36. this.value.coord.build( builder, 'v2' ) + ', ' +
  37. this.scale.build( builder, 'fv1' ) + ' )';
  38. return builder.format( 'perturbNormalArb( -' + this.position.build( builder, 'v3' ) + ', ' +
  39. this.normal.build( builder, 'v3' ) + ', ' +
  40. derivativeOfHeightCode + ' )', this.getType( builder ), output );
  41. }
  42. } else {
  43. console.warn( "THREE.BumpMapNode is not compatible with " + builder.shader + " shader." );
  44. return builder.format( 'vec3( 0.0 )', this.getType( builder ), output );
  45. }
  46. };
  47. THREE.BumpMapNode.prototype.copy = function ( source ) {
  48. THREE.GLNode.prototype.copy.call( this, source );
  49. this.value = source.value;
  50. this.scale = source.scale;
  51. };
  52. THREE.BumpMapNode.prototype.toJSON = function ( meta ) {
  53. var data = this.getJSONNode( meta );
  54. if ( ! data ) {
  55. data = this.createJSONNode( meta );
  56. data.value = this.value.toJSON( meta ).uuid;
  57. data.scale = this.scale.toJSON( meta ).uuid;
  58. }
  59. return data;
  60. };