ShaderOutput.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package hrt.shgraph;
  2. using hxsl.Ast;
  3. @name("Outputs")
  4. @description("Parameters outputs, it's dynamic")
  5. @group("Output")
  6. @color("#A90707")
  7. class ShaderOutput extends ShaderNode {
  8. @prop("Variable") public var variable : TVar;
  9. var components = [X, Y, Z, W];
  10. override function getShaderDef(domain: ShaderGraph.Domain):hrt.shgraph.ShaderGraph.ShaderNodeDef {
  11. var pos : Position = {file: "", min: 0, max: 0};
  12. var inVar : TVar = {name: "input", id:0, type: this.variable.type, kind: Param, qualifiers: []};
  13. var output : TVar = {name: variable.name, id:1, type: this.variable.type, kind: Local, qualifiers: []};
  14. var finalExpr : TExpr = {e: TBinop(OpAssign, {e:TVar(output), p:pos, t:output.type}, {e: TVar(inVar), p: pos, t: output.type}), p: pos, t: output.type};
  15. //var param = getParameter(inputNode.parameterId);
  16. //inits.push({variable: inVar, value: param.defaultValue});
  17. return {expr: finalExpr, inVars: [{v: inVar, internal: false}], outVars:[{v: output, internal: true}], externVars: [], inits: []};
  18. }
  19. /*override public function checkValidityInput(key : String, type : hxsl.Ast.Type) : Bool {
  20. return ShaderType.checkConversion(type, variable.type);
  21. }*/
  22. // override public function build(key : String) : TExpr {
  23. // return {
  24. // p : null,
  25. // t : TVoid,
  26. // e : TBinop(OpAssign, {
  27. // e: TVar(variable),
  28. // p: null,
  29. // t: variable.type
  30. // }, input.getVar(variable.type))
  31. // };
  32. // }
  33. static var availableOutputs = [
  34. {
  35. parent: null,
  36. id: 0,
  37. kind: Var,
  38. name: "calculatedUV",
  39. type: TVec(2, VFloat)
  40. },
  41. {
  42. parent: null,
  43. id: 0,
  44. kind: Var,
  45. name: "transformedNormal",
  46. type: TVec(3, VFloat)
  47. },
  48. {
  49. parent: null,
  50. id: 0,
  51. kind: Var,
  52. name: "metalnessValue",
  53. type: TFloat
  54. },
  55. {
  56. parent: null,
  57. id: 0,
  58. kind: Var,
  59. name: "roughnessValue",
  60. type: TFloat
  61. },
  62. {
  63. parent: null,
  64. id: 0,
  65. kind: Var,
  66. name: "emissiveValue",
  67. type: TFloat
  68. }
  69. ];
  70. override public function loadProperties(props : Dynamic) {
  71. var type: Type;
  72. if(props.type != null) {
  73. var args = [];
  74. if(props.type == "TVec") {
  75. args.push(props.vecSize);
  76. args.push(VecType.createByName(props.vecType));
  77. }
  78. type = hxsl.Ast.Type.createByName(props.type, args);
  79. }
  80. else @:deprecated {
  81. var paramVariable : Array<Dynamic> = Reflect.field(props, "variable");
  82. if( paramVariable[0] == null)
  83. return;
  84. for (c in ShaderNode.availableVariables) {
  85. if (c.name == paramVariable[0]) {
  86. this.variable = c;
  87. return;
  88. }
  89. }
  90. for (c in ShaderOutput.availableOutputs) {
  91. if (c.name == paramVariable[0]) {
  92. this.variable = c;
  93. return;
  94. }
  95. }
  96. type = haxe.EnumTools.createByName(Type, paramVariable[1], paramVariable[2]);
  97. }
  98. this.variable = {
  99. parent: null,
  100. id: 0,
  101. kind: Local,
  102. name: props.name,
  103. type: type,
  104. };
  105. }
  106. override public function saveProperties() : Dynamic {
  107. if (this.variable == null) {
  108. this.variable = ShaderNode.availableVariables[0];
  109. }
  110. var parameters : Dynamic = {
  111. name: variable.name,
  112. type: variable.type.getName(),
  113. };
  114. switch variable.type {
  115. case TVec(size, t):
  116. parameters.vecSize = size;
  117. parameters.vecType = t.getName();
  118. default:
  119. }
  120. return parameters;
  121. }
  122. #if editor
  123. override public function getPropertiesHTML(width : Float) : Array<hide.Element> {
  124. var elements = super.getPropertiesHTML(width);
  125. var element = new hide.Element('<div style="width: 110px; height: 70px"></div>');
  126. element.append(new hide.Element('<select id="variable"></select>'));
  127. if (this.variable == null) {
  128. this.variable = ShaderNode.availableVariables[0];
  129. }
  130. var input = element.children("select");
  131. var indexOption = 0;
  132. var selectingDefault = false;
  133. for (c in ShaderNode.availableVariables) {
  134. input.append(new hide.Element('<option value="${indexOption}">${c.name}</option>'));
  135. if (this.variable.name == c.name) {
  136. input.val(indexOption);
  137. selectingDefault = true;
  138. }
  139. indexOption++;
  140. }
  141. for (c in ShaderOutput.availableOutputs) {
  142. input.append(new hide.Element('<option value="${indexOption}">${c.name}</option>'));
  143. if (this.variable.name == c.name) {
  144. input.val(indexOption);
  145. selectingDefault = true;
  146. }
  147. indexOption++;
  148. }
  149. var maxIndex = indexOption;
  150. input.append(new hide.Element('<option value="${maxIndex}">Other...</option>'));
  151. var initialName : String = null;
  152. var initialType : Type = null;
  153. if( !selectingDefault ) {
  154. input.val(maxIndex);
  155. initialName = this.variable.name;
  156. initialType = this.variable.type;
  157. }
  158. var customVarChooser = new CustomVarChooser(element, initialName, initialType, function(val) {
  159. this.variable = val;
  160. });
  161. if( !selectingDefault )
  162. customVarChooser.show();
  163. else
  164. customVarChooser.hide();
  165. input.on("change", function(e) {
  166. var value = input.val();
  167. if (value < ShaderNode.availableVariables.length) {
  168. this.variable = ShaderNode.availableVariables[value];
  169. } else if (value < maxIndex) {
  170. this.variable = ShaderOutput.availableOutputs[value-ShaderNode.availableVariables.length];
  171. }
  172. if (value == maxIndex) {
  173. customVarChooser.show();
  174. if (customVarChooser.variable != null) {
  175. this.variable = customVarChooser.variable;
  176. }
  177. } else {
  178. customVarChooser.hide();
  179. }
  180. });
  181. elements.push(element);
  182. return elements;
  183. }
  184. #end
  185. }