BumpNode.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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.generate = function( builder, output ) {
  23. var material = builder.material, func = THREE.BumpNode.fBumpToNormal;
  24. builder.include( func );
  25. if ( builder.isShader( 'fragment' ) ) {
  26. return builder.format( func.name + '(' + this.value.build( builder, 'sampler2D' ) + ',' +
  27. this.coord.build( builder, 'v2' ) + ',' +
  28. this.scale.build( builder, 'v2' ) + ')', this.getType( builder ), output );
  29. } else {
  30. console.warn( "THREE.BumpNode is not compatible with " + builder.shader + " shader." );
  31. return builder.format( 'vec3( 0.0 )', this.getType( builder ), output );
  32. }
  33. };