IfCondition.hx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package hrt.shgraph.nodes;
  2. using hxsl.Ast;
  3. @name("If")
  4. @description("Return the correct input according to the condition")
  5. @group("Condition")
  6. class IfCondition extends ShaderNode {
  7. @input("condition") var condition = SType.Bool;
  8. @input("true") var trueVar = SType.Variant;
  9. @input("false") var falseVar = SType.Variant;
  10. @output() var output = SType.Variant;
  11. override public function checkValidityInput(key : String, type : ShaderType.SType) : Bool {
  12. if (key == "trueVar" && falseVar != null)
  13. return ShaderType.checkCompatibilities(type, ShaderType.getType(falseVar.getType()));
  14. if (key == "falseVar" && trueVar != null)
  15. return ShaderType.checkCompatibilities(type, ShaderType.getType(trueVar.getType()));
  16. return true;
  17. }
  18. override public function createOutputs() {
  19. if (trueVar != null && falseVar != null)
  20. addOutput("output", trueVar.getVar(falseVar.getType()).t);
  21. else if (trueVar != null)
  22. addOutput("output", trueVar.getType());
  23. else if (falseVar != null)
  24. addOutput("output", falseVar.getType());
  25. else
  26. removeOutput("output");
  27. }
  28. override public function build(key : String) : TExpr {
  29. return {
  30. p : null,
  31. t: output.type,
  32. e : TBinop(OpAssign, {
  33. e: TVar(output),
  34. p: null,
  35. t: output.type
  36. }, {
  37. e: TIf( condition.getVar(),
  38. trueVar.getVar(falseVar.getType()),
  39. falseVar.getVar(trueVar.getType())),
  40. p: null,
  41. t: output.type
  42. })
  43. };
  44. }
  45. }