ConstNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. THREE.ConstNode = function ( src, useDefine ) {
  5. THREE.TempNode.call( this );
  6. this.eval( src || THREE.ConstNode.PI, useDefine );
  7. };
  8. THREE.ConstNode.PI = 'PI';
  9. THREE.ConstNode.PI2 = 'PI2';
  10. THREE.ConstNode.RECIPROCAL_PI = 'RECIPROCAL_PI';
  11. THREE.ConstNode.RECIPROCAL_PI2 = 'RECIPROCAL_PI2';
  12. THREE.ConstNode.LOG2 = 'LOG2';
  13. THREE.ConstNode.EPSILON = 'EPSILON';
  14. THREE.ConstNode.prototype = Object.create( THREE.TempNode.prototype );
  15. THREE.ConstNode.prototype.constructor = THREE.ConstNode;
  16. THREE.ConstNode.prototype.nodeType = "Const";
  17. THREE.ConstNode.prototype.getType = function ( builder ) {
  18. return builder.getTypeByFormat( this.type );
  19. };
  20. THREE.ConstNode.prototype.eval = function ( src, useDefine ) {
  21. src = ( src || '' ).trim();
  22. var name, type, value = "";
  23. var rDeclaration = /^([a-z_0-9]+)\s([a-z_0-9]+)\s?\=?\s?(.*?)(\;|$)/i;
  24. var match = src.match( rDeclaration );
  25. this.useDefine = useDefine;
  26. if ( match && match.length > 1 ) {
  27. type = match[ 1 ];
  28. name = match[ 2 ];
  29. value = match[ 3 ];
  30. } else {
  31. name = src;
  32. type = 'fv1';
  33. }
  34. this.name = name;
  35. this.type = type;
  36. this.value = value;
  37. };
  38. THREE.ConstNode.prototype.build = function ( builder, output ) {
  39. if ( output === 'source' ) {
  40. if ( this.value ) {
  41. if ( this.useDefine ) {
  42. return '#define ' + this.name + ' ' + this.value;
  43. }
  44. return 'const ' + this.type + ' ' + this.name + ' = ' + this.value + ';';
  45. }
  46. } else {
  47. builder.include( this );
  48. return builder.format( this.name, this.getType( builder ), output );
  49. }
  50. };
  51. THREE.ConstNode.prototype.generate = function ( builder, output ) {
  52. return builder.format( this.name, this.getType( builder ), output );
  53. };
  54. THREE.ConstNode.prototype.toJSON = function ( meta ) {
  55. var data = this.getJSONNode( meta );
  56. if ( ! data ) {
  57. data = this.createJSONNode( meta );
  58. data.name = this.name;
  59. data.out = this.type;
  60. if ( this.value ) data.value = this.value;
  61. if ( data.useDefine === true ) data.useDefine = true;
  62. }
  63. return data;
  64. };