NormalNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. import { NodeLib } from '../core/NodeLib.js';
  6. function NormalNode( scope ) {
  7. TempNode.call( this, 'v3' );
  8. this.scope = scope || NormalNode.VIEW;
  9. }
  10. NormalNode.LOCAL = 'local';
  11. NormalNode.WORLD = 'world';
  12. NormalNode.VIEW = 'view';
  13. NormalNode.prototype = Object.create( TempNode.prototype );
  14. NormalNode.prototype.constructor = NormalNode;
  15. NormalNode.prototype.nodeType = "Normal";
  16. NormalNode.prototype.getShared = function () {
  17. // if shared is false, TempNode will not create temp variable (for optimization)
  18. return this.scope === NormalNode.WORLD;
  19. };
  20. NormalNode.prototype.build = function ( builder, output, uuid, ns ) {
  21. var contextNormal = builder.context[ this.scope + 'Normal' ];
  22. if ( contextNormal ) {
  23. return contextNormal.build( builder, output, uuid, ns );
  24. }
  25. return TempNode.prototype.build.call( this, builder, output, uuid );
  26. };
  27. NormalNode.prototype.generate = function ( builder, output ) {
  28. var result;
  29. switch ( this.scope ) {
  30. case NormalNode.VIEW:
  31. if ( builder.isShader( 'vertex' ) ) result = 'transformedNormal';
  32. else result = 'geometryNormal';
  33. break;
  34. case NormalNode.LOCAL:
  35. if ( builder.isShader( 'vertex' ) ) {
  36. result = 'objectNormal';
  37. } else {
  38. builder.requires.normal = true;
  39. result = 'vObjectNormal';
  40. }
  41. break;
  42. case NormalNode.WORLD:
  43. if ( builder.isShader( 'vertex' ) ) {
  44. result = 'inverseTransformDirection( transformedNormal, viewMatrix ).xyz';
  45. } else {
  46. builder.requires.worldNormal = true;
  47. result = 'vWNormal';
  48. }
  49. break;
  50. }
  51. return builder.format( result, this.getType( builder ), output );
  52. };
  53. NormalNode.prototype.copy = function ( source ) {
  54. TempNode.prototype.copy.call( this, source );
  55. this.scope = source.scope;
  56. return this;
  57. };
  58. NormalNode.prototype.toJSON = function ( meta ) {
  59. var data = this.getJSONNode( meta );
  60. if ( ! data ) {
  61. data = this.createJSONNode( meta );
  62. data.scope = this.scope;
  63. }
  64. return data;
  65. };
  66. NodeLib.addKeyword( 'viewNormal', function () {
  67. return new NormalNode( NormalNode.VIEW );
  68. } );
  69. NodeLib.addKeyword( 'localNormal', function () {
  70. return new NormalNode( NormalNode.NORMAL );
  71. } );
  72. NodeLib.addKeyword( 'worldNormal', function () {
  73. return new NormalNode( NormalNode.WORLD );
  74. } );
  75. export { NormalNode };