IfCondition.hx 1.5 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 && !falseVar.isEmpty())
  13. return ShaderType.checkCompatibilities(type, ShaderType.getSType(falseVar.getType()));
  14. if (key == "falseVar" && trueVar != null && !trueVar.isEmpty())
  15. return ShaderType.checkCompatibilities(type, ShaderType.getSType(trueVar.getType()));
  16. return true;
  17. }
  18. override public function computeOutputs() {
  19. if (trueVar != null && !trueVar.isEmpty() && falseVar != null && !falseVar.isEmpty())
  20. addOutput("output", trueVar.getVar(falseVar.getType()).t);
  21. else if (trueVar != null && !trueVar.isEmpty())
  22. addOutput("output", trueVar.getType());
  23. else if (falseVar != null && !falseVar.isEmpty())
  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. }