Home Reference Source

cables_dev/cables/src/libs/cgl/rendertargets/rendertargets.js

  1. import slots_frag from "./slots.frag";
  2. import slots_vert from "./slots.vert";
  3. import slots_head_frag from "./slots_head.frag";
  4. import slots_head_vert from "./slots_head.vert";
  5.  
  6.  
  7. class RenderTargets
  8. {
  9. constructor(cgl)
  10. {
  11. this._numBuffers = 4;
  12. this.asString = "";
  13. this._slots = ["Default", "Normal"];
  14. this._name = "rendertargets" + CABLES.uuid();
  15. this.mod = new CGL.ShaderModifier(cgl, this._name);
  16. // this.updateModules();
  17.  
  18.  
  19. this.mod.onBind = (currentShader) =>
  20. {
  21. // console.log(currentShader);
  22. // currentShader.setDrawBuffers([true, true, true, true]);
  23. };
  24. }
  25.  
  26.  
  27. updateModules()
  28. {
  29. this.mod.removeModule(this._name + "_frag");
  30.  
  31. this.mod.addModule(
  32. {
  33. "priority": 2,
  34. "title": this._name + "_frag",
  35. "name": "MODULE_COLOR",
  36. "srcHeadFrag": slots_head_frag,
  37. "srcBodyFrag": this.getSrcFrag(),
  38. });
  39.  
  40. this.mod.removeModule(this._name + "_vert");
  41.  
  42. this.mod.addModule(
  43. {
  44. "priority": 2,
  45. "title": this._name + "_vert",
  46. "name": "MODULE_VERTEX_POSITION",
  47. "srcHeadVert": slots_head_vert,
  48. "srcBodyVert": slots_vert
  49. });
  50. }
  51.  
  52. getTypes()
  53. {
  54. return ["Default",
  55. "Material Id, Object Id, Instance Id",
  56. "Material Id",
  57. "Object Id",
  58. "Position World",
  59. "Position * ModelView",
  60. "Position Local",
  61. "Position Object",
  62. "Normal",
  63. "Normal World",
  64. "Normal * ModelView",
  65. "OIT Accum",
  66. "OIT Revealage",
  67. "FragCoord.z",
  68. "TexCoord",
  69. "Black",
  70. "0", "1"];
  71. }
  72.  
  73. setNumBuffers(n)
  74. {
  75. this._numBuffers = n;
  76. }
  77.  
  78. getSrcString(type, i)
  79. {
  80. let outcolor = "outColor";
  81. if (i === "")outcolor = "col";
  82.  
  83. if (type == "Normal") return " " + outcolor + i + " = vec4(norm,1.);".endl();
  84. else if (type == "Material Id, Object Id, Instance Id") return " " + outcolor + i + " = vec4(round(materialId),round(objectId),round(instIdx),1.0);".endl();
  85. else if (type == "Default" || type == "Color") return " " + outcolor + i + " = col;".endl();
  86. else if (type == "1") return " " + outcolor + i + " = vec4(1.,1.,1.,1.);".endl();
  87. else if (type == "0") return " " + outcolor + i + " = vec4(0.,0.,0.,0.);".endl();
  88. else if (type == "Black") return " " + outcolor + i + " = vec4(0.,0.,0.,1.);".endl();
  89. else if (type == "TexCoord") return " " + outcolor + i + " = vec4(texCoord,0.,1.);".endl();
  90. else if (type == "Position Local") return " " + outcolor + i + " = vec4(MOD_pos_local,1.);".endl();
  91. else if (type == "Position World") return " " + outcolor + i + " = vec4(MOD_pos_world,1.);".endl();
  92. else if (type == "Position * ModelView") return " " + outcolor + i + " = vec4(MOD_pos_mv,1.);".endl();
  93.  
  94. else if (type == "Position Object") return " " + outcolor + i + " = vec4(MOD_pos_object,1.);".endl();
  95. else if (type == "Normal World") return " " + outcolor + i + " = vec4(MOD_normal_world,1.);".endl();
  96. else if (type == "Normal * ModelView") return " " + outcolor + i + " = vec4(MOD_normal_mv,1.);".endl();
  97. else if (type == "Material Id") return " " + outcolor + i + " = vec4(round(materialId),round(instIdx),0.,1.);".endl();
  98. else if (type == "Object Id") return " " + outcolor + i + " = vec4(objectId,0.,0.,1.);".endl();
  99. else if (type == "FragCoord.z") return " " + outcolor + i + " = vec4(vec3(gl_FragCoord.z),1.);".endl();
  100.  
  101. else if (type.contains("OIT "))
  102. {
  103. let str = ""
  104. .endl() + "#ifndef OIT_WEIGHT"
  105. .endl() + "#define OIT_WEIGHT"
  106. .endl() + " float oitWeight=clamp(pow(min(1.0, col.a * 10.0) + 0.01, 3.0) * 1e8 * pow(1.0 - gl_FragCoord.z * 0.9, 3.0), 1e-2, 3e3);"
  107. // .endl() + " float oitWeight=100.0*exp(gl_FragCoord.z*gl_FragCoord.z);"
  108. .endl() + "#endif"
  109. .endl();
  110.  
  111. if (type == "OIT Revealage") str += " " + outcolor + i + " = vec4(col.a*oitWeight,col.a,1.0,1.0);".endl();
  112. if (type == "OIT Accum") str += ""
  113. .endl() + " " + outcolor + i + " = vec4(col.rgb * col.a * oitWeight, col.a);";
  114.  
  115. return str;
  116. }
  117. }
  118.  
  119. getSrcFrag()
  120. {
  121. let src = slots_frag;
  122.  
  123.  
  124. if (this._slots.length == 1)
  125. {
  126. src += this.getSrcString(this._slots[0], "");
  127. }
  128. else
  129. for (let i = 0; i < this._numBuffers; i++)
  130. {
  131. src += this.getSrcString(this._slots[i], i);
  132. }
  133.  
  134. // console.log(src);
  135. return src;
  136. }
  137.  
  138. update(slots)
  139. {
  140. this._slots = slots;
  141. this._numBuffers = slots.length;
  142. this.asString = "";
  143.  
  144. let hasPosWorld = false;
  145. let hasPosLocal = false;
  146. let hasPosObject = false;
  147. let hasMaterialId = false;
  148. let hasObjectId = false;
  149. let hasNormalModelView = false;
  150. let hasNormalWorld = false;
  151. let hasPosModelView = false;
  152.  
  153.  
  154. for (let i = 0; i < this._numBuffers; i++)
  155. {
  156. hasPosWorld = (slots[i] == "Position World") || hasPosWorld;
  157. hasNormalModelView = (slots[i] == "Normal * ModelView") || hasNormalModelView;
  158. hasPosLocal = (slots[i] == "Position Local") || hasPosLocal;
  159. hasPosModelView = (slots[i] == "Position * ModelView") || hasPosModelView;
  160. hasPosObject = (slots[i] == "Position Object") || hasPosObject;
  161. hasMaterialId = (slots[i].contains("Material Id")) || hasMaterialId;
  162. hasObjectId = (slots[i].contains("Object Id")) || hasObjectId;
  163. hasNormalWorld = (slots[i].contains("Normal World")) || hasNormalWorld;
  164.  
  165. this.asString += slots[i];
  166. if (i != this._numBuffers - 1) this.asString += " | ";
  167. }
  168.  
  169.  
  170. // this.updateModules();
  171. this.updateModules();
  172.  
  173. this.mod.toggleDefine("MOD_UNI_OBJECT_ID", hasObjectId);
  174. this.mod.toggleDefine("MOD_UNI_MATERIAL_ID", hasMaterialId);
  175. this.mod.toggleDefine("MOD_SLOT_POS_MV", hasPosModelView);
  176.  
  177. this.mod.toggleDefine("MOD_SLOT_POS_WORLD", hasPosWorld);
  178. this.mod.toggleDefine("MOD_SLOT_POS_LOCAL", hasPosLocal);
  179. this.mod.toggleDefine("MOD_SLOT_POS_OBJECT", hasPosObject);
  180. this.mod.toggleDefine("MOD_SLOT_POS_NORMAL_MV", hasNormalModelView);
  181. this.mod.toggleDefine("MOD_SLOT_POS_NORMAL_WORLD", hasNormalWorld);
  182. }
  183. }
  184.  
  185. export { RenderTargets };