ShaderOutput.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 : String = "_sg_out_color";
  9. var components = [X, Y, Z, W];
  10. public var generatePreview = false;
  11. public function new(variable = "_sg_out_color") {
  12. this.variable = variable;
  13. }
  14. var inputs : Array<ShaderNode.InputInfo>;
  15. override public function getInputs() : Array<ShaderNode.InputInfo> {
  16. if (inputs == null) {
  17. var global = availableOutputs[variable].g;
  18. var info = Variables.Globals[global];
  19. inputs = [{name: "input", type: ShaderGraph.typeToSgType(info.type)}];
  20. }
  21. return inputs;
  22. }
  23. override public function generate(ctx: NodeGenContext) {
  24. var out = ctx.getInput(0, SgHxslVar.ShaderDefInput.Const(getDef("input", 0.0)));
  25. ctx.setGlobalOutput(availableOutputs[variable].g, out);
  26. ctx.addPreview(out);
  27. }
  28. override public function getAliases(name: String, group: String, description: String) {
  29. var aliases = super.getAliases(name, group, description);
  30. for (key => output in hrt.shgraph.ShaderOutput.availableOutputs) {
  31. aliases.push({
  32. nameSearch : name + " - " + output.display,
  33. group: group,
  34. description: description,
  35. args: [key],
  36. });
  37. }
  38. return aliases;
  39. }
  40. public static var availableOutputs : Map<String, {display: String, g: Variables.Global}> = [
  41. "_sg_out_color" => {display: "Pixel Color", g:SGPixelColor},
  42. "_sg_out_alpha" => {display: "Alpha", g:SGPixelAlpha},
  43. "relativePosition" => {display: "Position (Object Space)", g:RelativePosition},
  44. "transformedPosition" => {display: "Position (World Space)", g:TransformedPosition},
  45. "projectedPosition" => {display: "Position (View Space)", g:ProjectedPosition},
  46. "calculatedUV" => { display: "UV", g:CalculatedUV},
  47. "transformedNormal" => { display: "Normal (World Space)", g:TransformedNormal},
  48. "metalness" => {display: "Metalness", g: Metalness},
  49. "roughness" => {display: "Roughness", g: Roughness},
  50. "emissive" => {display: "Emissive", g: Emissive},
  51. "occlusion" => {display: "Occlusion", g: Occlusion},
  52. ];
  53. override function loadProperties(props:Dynamic) {
  54. super.loadProperties(props);
  55. var ivar = availableOutputs.get(this.variable);
  56. if (ivar == null) {
  57. for (k => v in availableOutputs) {
  58. variable = k;
  59. break;
  60. }
  61. }
  62. }
  63. #if editor
  64. override public function getPropertiesHTML(width : Float) : Array<hide.Element> {
  65. var elements = super.getPropertiesHTML(width);
  66. var element = new hide.Element('<div style="width: 110px; height: 30px"></div>');
  67. element.append(new hide.Element('<select id="variable"></select>'));
  68. if (this.variable == null) {
  69. variable = "__sg_out_color";
  70. }
  71. var input = element.children("select");
  72. var selectingDefault = false;
  73. for (k => c in ShaderOutput.availableOutputs) {
  74. input.append(new hide.Element('<option value="${k}">${c.display}</option>'));
  75. }
  76. input.val(variable);
  77. input.on("change", function(e) {
  78. variable = input.val();
  79. inputs = null;
  80. requestRecompile();
  81. });
  82. elements.push(element);
  83. return elements;
  84. }
  85. #end
  86. }