BumpNode.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.BumpNode = function ( value, coord, scale ) {
  5. THREE.TempNode.call( this, 'v3' );
  6. this.value = value;
  7. this.coord = coord || new THREE.UVNode();
  8. this.scale = scale || new THREE.Vector2Node( 1, 1 );
  9. };
  10. THREE.BumpNode.fBumpToNormal = new THREE.FunctionNode( [
  11. "vec3 bumpToNormal( sampler2D bumpMap, vec2 uv, vec2 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.x ), .5 + ( dBy * scale.y ), 1.0 );",
  18. "}"
  19. ].join( "\n" ), null, { derivatives: true } );
  20. THREE.BumpNode.prototype = Object.create( THREE.TempNode.prototype );
  21. THREE.BumpNode.prototype.constructor = THREE.BumpNode;
  22. THREE.BumpNode.prototype.nodeType = "Bump";
  23. THREE.BumpNode.prototype.generate = function ( builder, output ) {
  24. var material = builder.material, func = THREE.BumpNode.fBumpToNormal;
  25. builder.include( func );
  26. if ( builder.isShader( 'fragment' ) ) {
  27. return builder.format( func.name + '(' + this.value.build( builder, 'sampler2D' ) + ',' +
  28. this.coord.build( builder, 'v2' ) + ',' +
  29. this.scale.build( builder, 'v2' ) + ')', this.getType( builder ), output );
  30. } else {
  31. console.warn( "THREE.BumpNode is not compatible with " + builder.shader + " shader." );
  32. return builder.format( 'vec3( 0.0 )', this.getType( builder ), output );
  33. }
  34. };
  35. THREE.BumpNode.prototype.toJSON = function ( meta ) {
  36. var data = this.getJSONNode( meta );
  37. if ( ! data ) {
  38. data = this.createJSONNode( meta );
  39. data.value = this.value.toJSON( meta ).uuid;
  40. data.coord = this.coord.toJSON( meta ).uuid;
  41. data.scale = this.scale.toJSON( meta ).uuid;
  42. }
  43. return data;
  44. };