ShaderInput.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package hrt.shgraph;
  2. using hxsl.Ast;
  3. enum InputKind {
  4. IGlobal(g: Variables.Global);
  5. ICustom(gen:(ctx:NodeGenContext) -> TExpr, t: hxsl.Ast.Type);
  6. }
  7. @name("Inputs")
  8. @description("Shader inputs of Heaps, it's dynamic")
  9. @group("Property")
  10. @color("#0e8826")
  11. class ShaderInput extends ShaderNode {
  12. @prop("Variable") public var variable : String = "pixelColor";
  13. public function new(variable = "pixelColor") {
  14. this.variable = variable;
  15. }
  16. var outputs : Array<ShaderNode.OutputInfo>;
  17. override function getOutputs() {
  18. if (outputs == null) {
  19. var global = availableInputs[variable].k;
  20. var t = switch(global) {
  21. case IGlobal(g):
  22. Variables.Globals[g].type;
  23. case ICustom(_,t):
  24. t;
  25. }
  26. outputs = [{name: "output", type: ShaderGraph.typeToSgType(t)}];
  27. }
  28. return outputs;
  29. }
  30. override function generate(ctx: NodeGenContext) {
  31. var expr = switch(availableInputs[variable].k) {
  32. case IGlobal(g):
  33. ctx.getGlobalInput(g);
  34. case ICustom(gen, _):
  35. gen(ctx);
  36. }
  37. ctx.setOutput(0, expr);
  38. ctx.addPreview(expr);
  39. }
  40. override public function getAliases(name: String, group: String, description: String) {
  41. var aliases = super.getAliases(name, group, description);
  42. for (key => input in hrt.shgraph.ShaderInput.availableInputs) {
  43. aliases.push({
  44. nameSearch : name + " - " + input.display,
  45. group: group,
  46. description: description,
  47. args: [key],
  48. });
  49. }
  50. return aliases;
  51. }
  52. override function loadProperties(props:Dynamic) {
  53. super.loadProperties(props);
  54. var ivar = availableInputs.get(this.variable);
  55. if (ivar == null) {
  56. for (k => v in availableInputs) {
  57. variable = k;
  58. break;
  59. }
  60. }
  61. }
  62. public static var availableInputs : Map<String, {display: String, k: InputKind}> = [
  63. "pixelColor" => {display: "Pixel Color", k: ICustom((ctx:NodeGenContext) -> makeSwizzle(ctx.getGlobalInput(PixelColor), [X,Y,Z]), TVec(3, VFloat))},
  64. "alpha" => {display: "Alpha", k: ICustom((ctx:NodeGenContext) -> makeSwizzle(ctx.getGlobalInput(PixelColor), [W]), TFloat)},
  65. "calculatedUV" => {display: "UV", k: IGlobal(CalculatedUV)},
  66. "relativePosition" => {display: "Position (Object Space)", k: IGlobal(RelativePosition)},
  67. "transformedPosition" => {display: "Position (World Space)", k: IGlobal(TransformedPosition)},
  68. "projectedPosition" => {display: "Position (View Space)", k: IGlobal(ProjectedPosition)},
  69. "emitterPosition" => {display: "Emitter Position (World Space)", k: IGlobal(EmitterPosition)},
  70. "normal" => {display: "Normal (Object Space)", k: IGlobal(Normal)},
  71. "transformedNormal" => {display: "Normal (World Space)", k: IGlobal(TransformedNormal)},
  72. "depth" => {display: "Depth", k: IGlobal(Depth)},
  73. "metalness" => {display: "Metalness", k: IGlobal(Metalness)},
  74. "roughness" => {display: "Roughness", k: IGlobal(Roughness)},
  75. "emissive" => {display: "Emissive", k: IGlobal(Emissive)},
  76. "occlusion" => {display: "Occlusion", k: IGlobal(Occlusion)},
  77. "uv" => {display: "Source UV", k: IGlobal(UV)},
  78. ];
  79. #if editor
  80. override public function getPropertiesHTML(width : Float) : Array<hide.Element> {
  81. var elements = super.getPropertiesHTML(width);
  82. var element = new hide.Element('<div style="width: 120px; height: 30px"></div>');
  83. element.append(new hide.Element('<select id="variable"></select>'));
  84. if (variable == null) {
  85. variable = "position";
  86. }
  87. var input = element.children("select");
  88. var indexOption = 0;
  89. for (k => variable in availableInputs) {
  90. input.append(new hide.Element('<option value="${k}">${variable.display}</option>'));
  91. }
  92. input.val(variable);
  93. input.on("change", function(e) {
  94. variable = input.val();
  95. outputs = null;
  96. requestRecompile();
  97. });
  98. elements.push(element);
  99. return elements;
  100. }
  101. #end
  102. }