2
0

ShaderGlobalInput.hx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package hrt.shgraph;
  2. using hxsl.Ast;
  3. @name("Global")
  4. @description("Global Inputs")
  5. @group("Property")
  6. @color("#0e8826")
  7. class ShaderGlobalInput extends ShaderNode {
  8. @prop("Variable") public var variableIdx : Int = 0;
  9. static public var globalInputs : Array<{display: String, g: Variables.Global}> =
  10. [
  11. {display: "Time", g: Time},
  12. {display: "Pixel Size", g: PixelSize},
  13. {display: "Camera Global Position", g: CameraPosition},
  14. ];
  15. public function new(idx: Null<Int>) {
  16. variableIdx = idx ?? variableIdx;
  17. }
  18. var outputs : Array<ShaderNode.OutputInfo>;
  19. override public function getOutputs() {
  20. if (outputs == null) {
  21. var global = globalInputs[variableIdx].g;
  22. var info = Variables.Globals[global];
  23. outputs = [{name: "output", type: ShaderGraph.typeToSgType(info.type)}];
  24. }
  25. return outputs;
  26. }
  27. override function generate(ctx: NodeGenContext) {
  28. var input = ctx.getGlobalInput(globalInputs[variableIdx].g);
  29. ctx.setOutput(0, input);
  30. ctx.addPreview(input);
  31. }
  32. override public function getAliases(name: String, group: String, description: String) {
  33. var aliases = super.getAliases(name, group, description);
  34. for (i => input in globalInputs) {
  35. aliases.push({
  36. nameSearch : name + " - " + input.display,
  37. group: group,
  38. description: description,
  39. args: [i],
  40. });
  41. }
  42. return aliases;
  43. }
  44. #if editor
  45. override public function getPropertiesHTML(width : Float) : Array<hide.Element> {
  46. var elements = [];
  47. var element = new hide.Element('<div style="width: 120px; height: 30px"></div>');
  48. element.append(new hide.Element('<select id="variable"></select>'));
  49. var input = element.children("select");
  50. for (indexOption => c in ShaderGlobalInput.globalInputs) {
  51. var name = c.display;
  52. input.append(new hide.Element('<option value="${indexOption}">${name}</option>'));
  53. if (this.variableIdx == indexOption) {
  54. input.val(indexOption);
  55. }
  56. }
  57. input.on("change", function(e) {
  58. var value = input.val();
  59. outputs = null;
  60. this.variableIdx = value;
  61. requestRecompile();
  62. });
  63. elements.push(element);
  64. return elements;
  65. }
  66. #end
  67. }