Preview.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package hrt.shgraph.nodes;
  2. import h3d.scene.Mesh;
  3. using hxsl.Ast;
  4. @name("Preview")
  5. @description("Preview node, just to debug :)")
  6. @group("Output")
  7. @width(100)
  8. @noheader()
  9. class Preview extends ShaderNode {
  10. @input("Input") var input = SType.Vec4;
  11. public var variable : TVar;
  12. override public function build(key : String) : TExpr {
  13. return {
  14. p : null,
  15. t : TVoid,
  16. e : TBinop(OpAssign, {
  17. e: TVar(variable),
  18. p: null,
  19. t: variable.type
  20. }, input.getVar(variable.type))
  21. };
  22. }
  23. #if editor
  24. public var shaderGraph : ShaderGraph;
  25. var nodePreview : js.jquery.JQuery;
  26. var cube : Mesh;
  27. var scene : hide.comp.Scene;
  28. var currentShaderPreview : hxsl.DynamicShader;
  29. var currentShaderDef : hrt.prefab.ContextShared.ShaderDef;
  30. public var config : hide.Config;
  31. override public function getPropertiesHTML(width : Float) : Array<hide.Element> {
  32. var elements = super.getPropertiesHTML(width);
  33. for (c in ShaderNode.availableVariables) {
  34. if (c.name == "pixelColor") {
  35. this.variable = c;
  36. }
  37. }
  38. if (this.variable == null) {
  39. throw ShaderException.t("The preview is not available", this.id);
  40. }
  41. var element = new hide.Element('<div style="width: 100px; height: 100px"><div class="preview-parent" top="-10px" ><div class="node-preview" style="height: 100px" ></div></div></div>');
  42. nodePreview = element.find(".node-preview");
  43. scene = new hide.comp.Scene(config, null, nodePreview);
  44. scene.onReady = function() {
  45. var prim = new h3d.prim.Cube();
  46. prim.addUVs();
  47. prim.addNormals();
  48. cube = new Mesh(prim, scene.s3d);
  49. scene.s3d.camera.pos = new h3d.Vector(0.5, 3.4, 0.5);
  50. scene.s3d.camera.target = new h3d.Vector(0.5, 0.5, 0.5);
  51. var light = new h3d.scene.pbr.DirLight(scene.s3d.camera.target.sub(scene.s3d.camera.pos), scene.s3d);
  52. light.setPosition(scene.s3d.camera.pos.x, scene.s3d.camera.pos.y, scene.s3d.camera.pos.z);
  53. scene.s3d.camera.zoom = 1;
  54. scene.init();
  55. onMove();
  56. computeOutputs();
  57. };
  58. elements.push(element);
  59. return elements;
  60. }
  61. public function onMove(?x : Float, ?y : Float, zoom : Float = 1.) {
  62. // var top : Float;
  63. // var left : Float;
  64. // var parent = nodePreview.parent();
  65. // if (x != null && y != null) {
  66. // left = x;
  67. // top = y;
  68. // } else {
  69. // var offsetWindow = nodePreview.closest(".heaps-scene").offset();
  70. // var offset = nodePreview.closest("foreignObject").offset();
  71. // if (offsetWindow == null || offset == null) return;
  72. // top = offset.top - offsetWindow.top - 32;
  73. // left = offset.left - offsetWindow.left;
  74. // }
  75. // nodePreview.closest(".prop-group").attr("transform", 'translate(0, -5)');
  76. nodePreview.closest(".properties-group").children().first().css("fill", "#000");
  77. // parent.css("top", top/zoom + 17);
  78. // parent.css("left", left/zoom);
  79. // parent.css("zoom", zoom);
  80. }
  81. function onResize() {
  82. if( cube == null ) return;
  83. }
  84. override public function computeOutputs() {
  85. if (currentShaderPreview != null) {
  86. for (m in cube.getMaterials()) {
  87. m.mainPass.removeShader(currentShaderPreview);
  88. }
  89. currentShaderPreview = null;
  90. }
  91. if (scene == null || input == null || input.isEmpty()) return;
  92. if (@:privateAccess scene.window == null)
  93. return;
  94. scene.setCurrent();
  95. var shader : hxsl.DynamicShader = null;
  96. try {
  97. var shaderGraphDef = shaderGraph.compile(this);
  98. shader = new hxsl.DynamicShader(shaderGraphDef.shader);
  99. for (init in shaderGraphDef.inits) {
  100. setParamValue(init.variable, init.value, shader);
  101. }
  102. for (m in cube.getMaterials()) {
  103. m.mainPass.addShader(shader);
  104. }
  105. currentShaderPreview = shader;
  106. currentShaderDef = shaderGraphDef;
  107. } catch(e : Dynamic) {
  108. if (shader != null) {
  109. for (m in cube.getMaterials()) {
  110. m.mainPass.removeShader(shader);
  111. }
  112. }
  113. }
  114. }
  115. public function setParamValueByName(varName : String, value : Dynamic) {
  116. if (currentShaderDef == null) return;
  117. for (init in currentShaderDef.inits) {
  118. if (init.variable.name == varName) {
  119. setParamValue(init.variable, value, currentShaderPreview);
  120. return;
  121. }
  122. }
  123. }
  124. public function setParamValue(variable : TVar, value : Dynamic, shader : hxsl.DynamicShader) {
  125. scene.setCurrent();
  126. try {
  127. switch (variable.type) {
  128. case TSampler2D:
  129. shader.setParamValue(variable, scene.loadTexture("", value));
  130. case TVec(size, _):
  131. shader.setParamValue(variable, h3d.Vector.fromArray(value));
  132. default:
  133. shader.setParamValue(variable, value);
  134. }
  135. } catch (e : Dynamic) {
  136. // variable not used
  137. }
  138. }
  139. #end
  140. }