ShaderNode.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package hrt.shgraph;
  2. using hxsl.Ast;
  3. typedef InputInfo = { name : String, type : ShaderType.SType, hasProperty : Bool, isRequired : Bool, ?ids : Array<Int> };
  4. typedef OutputInfo = { name : String, type : ShaderType.SType, ?id : Int };
  5. @:autoBuild(hrt.shgraph.ParseFieldsMacro.build())
  6. @:keepSub
  7. class ShaderNode {
  8. public var id : Int;
  9. static var availableVariables = [
  10. {
  11. parent: null,
  12. id: 0,
  13. kind: Global,
  14. name: "pixelColor",
  15. type: TVec(4, VFloat)
  16. }];
  17. var inputs : Map<String, NodeVar> = [];
  18. var outputs : Map<String, TVar> = [];
  19. public var outputCompiled : Map<String, Bool> = []; // todo: put with outputs variable
  20. public function setId(id : Int) {
  21. this.id = id;
  22. }
  23. public function setInput(key : String, s : NodeVar) {
  24. if (s == null)
  25. inputs.remove(key);
  26. else
  27. inputs.set(key, s);
  28. }
  29. public function getInput(key : String) : NodeVar {
  30. return inputs.get(key);
  31. }
  32. public function getInputsKey() {
  33. return [for (k in inputs.keys()) k ];
  34. }
  35. public function getInputs() {
  36. return [for (k in inputs.keys()) inputs.get(k) ];
  37. }
  38. public function hasInputs() {
  39. return inputs.keys().hasNext();
  40. }
  41. function addOutput(key : String, t : Type) {
  42. outputs.set(key, { parent: null,
  43. id: 0,
  44. kind: Local,
  45. name: "output_" + id + "_" + key,
  46. type: t
  47. });
  48. }
  49. function removeOutput(key : String) {
  50. outputs.remove(key);
  51. }
  52. function addOutputTvar(tVar : TVar) {
  53. outputs.set(tVar.name, tVar);
  54. }
  55. public function computeOutputs() : Void {}
  56. public function getOutput(key : String) : TVar {
  57. return outputs.get(key);
  58. }
  59. public function getOutputType(key : String) : Type {
  60. var output = getOutput(key);
  61. if (output == null)
  62. return null;
  63. return output.type;
  64. }
  65. public function getOutputTExpr(key : String) : TExpr {
  66. var o = getOutput(key);
  67. if (o == null)
  68. return null;
  69. return {
  70. e: TVar(o),
  71. p: null,
  72. t: o.type
  73. };
  74. }
  75. public function build(key : String) : TExpr {
  76. throw "Build function not implemented";
  77. }
  78. public function checkTypeAndCompatibilyInput(key : String, type : ShaderType.SType) : Bool {
  79. var infoKey = getInputInfo(key).type;
  80. if (infoKey != null && !(ShaderType.checkConversion(type, infoKey))) {
  81. return false;
  82. }
  83. return checkValidityInput(key, type);
  84. }
  85. public function checkValidityInput(key : String, type : ShaderType.SType) : Bool {
  86. return true;
  87. }
  88. public function getInputInfoKeys() : Array<String> {
  89. return [];
  90. }
  91. public function getInputInfo(key : String) : InputInfo {
  92. return null;
  93. }
  94. public function getOutputInfoKeys() : Array<String> {
  95. return [];
  96. }
  97. public function getOutputInfo(key : String) : OutputInfo {
  98. return null;
  99. }
  100. public function loadProperties(props : Dynamic) {
  101. var fields = Reflect.fields(props);
  102. for (f in fields) {
  103. Reflect.setField(this, f, Reflect.field(props, f));
  104. }
  105. }
  106. public function savePropertiesNode() : Dynamic {
  107. var parameters = saveProperties();
  108. var thisClass = std.Type.getClass(this);
  109. var fields = std.Type.getInstanceFields(thisClass);
  110. var metas = haxe.rtti.Meta.getFields(thisClass);
  111. var metaSuperClass = haxe.rtti.Meta.getFields(std.Type.getSuperClass(thisClass));
  112. for (f in fields) {
  113. var m = Reflect.field(metas, f);
  114. if (m == null) {
  115. m = Reflect.field(metaSuperClass, f);
  116. if (m == null)
  117. continue;
  118. }
  119. if (Reflect.hasField(m, "prop")) {
  120. var metaData : Array<String> = Reflect.field(m, "prop");
  121. if (metaData != null && metaData.length >= 1 && metaData[0] == "macro") {
  122. Reflect.setField(parameters, f, Reflect.getProperty(this, f));
  123. }
  124. }
  125. }
  126. return parameters;
  127. }
  128. public function saveProperties() : Dynamic {
  129. var parameters = {};
  130. var thisClass = std.Type.getClass(this);
  131. var fields = std.Type.getInstanceFields(thisClass);
  132. var metas = haxe.rtti.Meta.getFields(thisClass);
  133. var metaSuperClass = haxe.rtti.Meta.getFields(std.Type.getSuperClass(thisClass));
  134. for (f in fields) {
  135. var m = Reflect.field(metas, f);
  136. if (m == null) {
  137. m = Reflect.field(metaSuperClass, f);
  138. if (m == null)
  139. continue;
  140. }
  141. if (Reflect.hasField(m, "prop")) {
  142. var metaData : Array<String> = Reflect.field(m, "prop");
  143. if (metaData == null || metaData.length == 0 || metaData[0] != "macro") {
  144. Reflect.setField(parameters, f, Reflect.getProperty(this, f));
  145. }
  146. }
  147. }
  148. return parameters;
  149. }
  150. #if editor
  151. public function getPropertiesHTML(width : Float) : Array<hide.Element> {
  152. return [];
  153. }
  154. static public var registeredNodes = new Map<String, Class<ShaderNode>>();
  155. static public function register(key : String, cl : Class<ShaderNode>) : Bool {
  156. registeredNodes.set(key, cl);
  157. return true;
  158. }
  159. #end
  160. }