debug-view-runtime-control.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import { Color, Canvas, UITransform, instantiate, math, Toggle, TextureCube, _decorator, Component, Button, labelAssembler, game, director, Node, Scene, renderer, CameraComponent, Label, ForwardPipeline, RichText } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('internal.DebugViewRuntimeControl')
  4. export class DebugViewRuntimeControl extends Component {
  5. @property(Node)
  6. compositeModeToggle: Node | null = null;
  7. @property(Node)
  8. singleModeToggle: Node | null = null;
  9. @property(Node)
  10. EnableAllCompositeModeButton: Node | null = null;
  11. _single: number = 0;
  12. private strSingle: string[] = [
  13. 'No Single Debug',
  14. 'Vertex Color',
  15. 'Vertex Normal',
  16. 'Vertex Tangent',
  17. 'World Position',
  18. 'Vertex Mirror',
  19. 'Face Side',
  20. 'UV0',
  21. 'UV1',
  22. 'UV Lightmap',
  23. 'Project Depth',
  24. 'Linear Depth',
  25. 'Fragment Normal',
  26. 'Fragment Tangent',
  27. 'Fragment Binormal',
  28. 'Base Color',
  29. 'Diffuse Color',
  30. 'Specular Color',
  31. 'Transparency',
  32. 'Metallic',
  33. 'Roughness',
  34. 'Specular Intensity',
  35. 'IOR',
  36. 'Direct Diffuse',
  37. 'Direct Specular',
  38. 'Direct All',
  39. 'Env Diffuse',
  40. 'Env Specular',
  41. 'Env All',
  42. 'Emissive',
  43. 'Light Map',
  44. 'Shadow',
  45. 'AO',
  46. 'Fresnel',
  47. 'Direct Transmit Diffuse',
  48. 'Direct Transmit Specular',
  49. 'Env Transmit Diffuse',
  50. 'Env Transmit Specular',
  51. 'Transmit All',
  52. 'Direct Internal Specular',
  53. 'Env Internal Specular',
  54. 'Internal All',
  55. 'Fog',
  56. ];
  57. private strComposite: string[] = [
  58. 'Direct Diffuse',
  59. 'Direct Specular',
  60. 'Env Diffuse',
  61. 'Env Specular',
  62. 'Emissive',
  63. 'Light Map',
  64. 'Shadow',
  65. 'AO',
  66. 'Normal Map',
  67. 'Fog',
  68. 'Tone Mapping',
  69. 'Gamma Correction',
  70. 'Fresnel',
  71. 'Transmit Diffuse',
  72. 'Transmit Specular',
  73. 'Internal Specular',
  74. 'TT',
  75. ];
  76. private strMisc: string[] = [
  77. 'CSM Layer Coloration',
  78. 'Lighting With Albedo',
  79. ];
  80. private compositeModeToggleList: Node[] = [];
  81. private singleModeToggleList: Node[] = [];
  82. private miscModeToggleList: Node[] = [];
  83. private textComponentList: RichText[] = [];
  84. private labelComponentList: Label[] = [];
  85. private textContentList: string[] = [];
  86. private hideButtonLabel: Label;
  87. start() {
  88. // get canvas resolution
  89. const canvas = this.node.parent.getComponent(Canvas);
  90. if (!canvas) {
  91. console.error('debug-view-runtime-control should be child of Canvas');
  92. return;
  93. }
  94. const uiTransform = this.node.parent.getComponent(UITransform);
  95. const halfScreenWidth = uiTransform.width * 0.5;
  96. const halfScreenHeight = uiTransform.height * 0.5;
  97. let x = -halfScreenWidth + halfScreenWidth * 0.1, y = halfScreenHeight - halfScreenHeight * 0.1;
  98. const width = 200, height = 20;
  99. // new nodes
  100. const miscNode = this.node.getChildByName('MiscMode');
  101. const buttonNode = instantiate(miscNode);
  102. buttonNode.parent = this.node;
  103. buttonNode.name = 'Buttons';
  104. const titleNode = instantiate(miscNode);
  105. titleNode.parent = this.node;
  106. titleNode.name = 'Titles';
  107. // title
  108. for (let i = 0; i < 2; i++) {
  109. const newLabel = instantiate(this.EnableAllCompositeModeButton.getChildByName('Label'));
  110. newLabel.setPosition(x + (i > 0 ? 50 + width * 2 : 150), y, 0.0);
  111. newLabel.setScale(0.75, 0.75, 0.75);
  112. newLabel.parent = titleNode;
  113. const labelComponent = newLabel.getComponent(Label);
  114. labelComponent.string = i ? '----------Composite Mode----------' : '----------Single Mode----------';
  115. labelComponent.color = Color.WHITE;
  116. labelComponent.overflow = 0;
  117. this.labelComponentList[this.labelComponentList.length] = labelComponent;
  118. }
  119. y -= height;
  120. // single
  121. let currentRow = 0;
  122. for (let i = 0; i < this.strSingle.length; i++, currentRow++) {
  123. if (i === this.strSingle.length >> 1) {
  124. x += width;
  125. currentRow = 0;
  126. }
  127. const newNode = i ? instantiate(this.singleModeToggle) : this.singleModeToggle;
  128. newNode.setPosition(x, y - height * currentRow, 0.0);
  129. newNode.setScale(0.5, 0.5, 0.5);
  130. newNode.parent = this.singleModeToggle.parent;
  131. const textComponent = newNode.getComponentInChildren(RichText);
  132. textComponent.string = this.strSingle[i];
  133. this.textComponentList[this.textComponentList.length] = textComponent;
  134. this.textContentList[this.textContentList.length] = textComponent.string;
  135. newNode.on(Toggle.EventType.TOGGLE, this.toggleSingleMode, this);
  136. this.singleModeToggleList[i] = newNode;
  137. }
  138. x += width;
  139. // buttons
  140. this.EnableAllCompositeModeButton.setPosition(x + 15, y, 0.0);
  141. this.EnableAllCompositeModeButton.setScale(0.5, 0.5, 0.5);
  142. this.EnableAllCompositeModeButton.on(Button.EventType.CLICK, this.enableAllCompositeMode, this);
  143. this.EnableAllCompositeModeButton.parent = buttonNode;
  144. let labelComponent = this.EnableAllCompositeModeButton.getComponentInChildren(Label);
  145. this.labelComponentList[this.labelComponentList.length] = labelComponent;
  146. const changeColorButton = instantiate(this.EnableAllCompositeModeButton);
  147. changeColorButton.setPosition(x + 90, y, 0.0);
  148. changeColorButton.setScale(0.5, 0.5, 0.5);
  149. changeColorButton.on(Button.EventType.CLICK, this.changeTextColor, this);
  150. changeColorButton.parent = buttonNode;
  151. labelComponent = changeColorButton.getComponentInChildren(Label);
  152. labelComponent.string = 'TextColor';
  153. this.labelComponentList[this.labelComponentList.length] = labelComponent;
  154. const HideButton = instantiate(this.EnableAllCompositeModeButton);
  155. HideButton.setPosition(x + 200, y, 0.0);
  156. HideButton.setScale(0.5, 0.5, 0.5);
  157. HideButton.on(Button.EventType.CLICK, this.hideUI, this);
  158. HideButton.parent = this.node.parent;
  159. labelComponent = HideButton.getComponentInChildren(Label);
  160. labelComponent.string = 'Hide UI';
  161. this.labelComponentList[this.labelComponentList.length] = labelComponent;
  162. this.hideButtonLabel = labelComponent;
  163. // misc
  164. y -= 40;
  165. for (let i = 0; i < this.strMisc.length; i++) {
  166. const newNode = instantiate(this.compositeModeToggle);
  167. newNode.setPosition(x, y - height * i, 0.0);
  168. newNode.setScale(0.5, 0.5, 0.5);
  169. newNode.parent = miscNode;
  170. const textComponent = newNode.getComponentInChildren(RichText);
  171. textComponent.string = this.strMisc[i];
  172. this.textComponentList[this.textComponentList.length] = textComponent;
  173. this.textContentList[this.textContentList.length] = textComponent.string;
  174. const toggleComponent = newNode.getComponent(Toggle);
  175. toggleComponent.isChecked = i ? true : false;
  176. newNode.on(Toggle.EventType.TOGGLE, i ? this.toggleLightingWithAlbedo : this.toggleCSMColoration, this);
  177. this.miscModeToggleList[i] = newNode;
  178. }
  179. // composite
  180. y -= 150;
  181. for (let i = 0; i < this.strComposite.length; i++) {
  182. const newNode = i ? instantiate(this.compositeModeToggle) : this.compositeModeToggle;
  183. newNode.setPosition(x, y - height * i, 0.0);
  184. newNode.setScale(0.5, 0.5, 0.5);
  185. newNode.parent = this.compositeModeToggle.parent;
  186. const textComponent = newNode.getComponentInChildren(RichText);
  187. textComponent.string = this.strComposite[i];
  188. this.textComponentList[this.textComponentList.length] = textComponent;
  189. this.textContentList[this.textContentList.length] = textComponent.string;
  190. newNode.on(Toggle.EventType.TOGGLE, this.toggleCompositeMode, this);
  191. this.compositeModeToggleList[i] = newNode;
  192. }
  193. }
  194. isTextMatched(textUI, textDescription) : boolean {
  195. let tempText = new String(textUI);
  196. const findIndex = tempText.search('>');
  197. if (findIndex === -1) {
  198. return textUI === textDescription;
  199. } else {
  200. tempText = tempText.substr(findIndex + 1);
  201. tempText = tempText.substr(0, tempText.search('<'));
  202. return tempText === textDescription;
  203. }
  204. }
  205. toggleSingleMode(toggle: Toggle) {
  206. const debugView = director.root!.debugView;
  207. const textComponent = toggle.getComponentInChildren(RichText);
  208. for (let i = 0; i < this.strSingle.length; i++) {
  209. if (this.isTextMatched(textComponent.string, this.strSingle[i])) {
  210. debugView.singleMode = i;
  211. }
  212. }
  213. }
  214. toggleCompositeMode(toggle: Toggle) {
  215. const debugView = director.root!.debugView;
  216. const textComponent = toggle.getComponentInChildren(RichText);
  217. for (let i = 0; i < this.strComposite.length; i++) {
  218. if (this.isTextMatched(textComponent.string, this.strComposite[i])) {
  219. debugView.enableCompositeMode(i, toggle.isChecked);
  220. }
  221. }
  222. }
  223. toggleLightingWithAlbedo(toggle: Toggle) {
  224. const debugView = director.root!.debugView;
  225. debugView.lightingWithAlbedo = toggle.isChecked;
  226. }
  227. toggleCSMColoration(toggle: Toggle) {
  228. const debugView = director.root!.debugView;
  229. debugView.csmLayerColoration = toggle.isChecked;
  230. }
  231. enableAllCompositeMode(button: Button) {
  232. const debugView = director.root!.debugView;
  233. debugView.enableAllCompositeMode(true);
  234. for (let i = 0; i < this.compositeModeToggleList.length; i++) {
  235. const toggleComponent = this.compositeModeToggleList[i].getComponent(Toggle);
  236. toggleComponent.isChecked = true;
  237. }
  238. let toggleComponent = this.miscModeToggleList[0].getComponent(Toggle);
  239. toggleComponent.isChecked = false;
  240. debugView.csmLayerColoration = false;
  241. toggleComponent = this.miscModeToggleList[1].getComponent(Toggle);
  242. toggleComponent.isChecked = true;
  243. debugView.lightingWithAlbedo = true;
  244. }
  245. hideUI(button: Button) {
  246. const titleNode = this.node.getChildByName('Titles');
  247. const activeValue = !titleNode.active;
  248. this.singleModeToggleList[0].parent.active = activeValue;
  249. this.miscModeToggleList[0].parent.active = activeValue;
  250. this.compositeModeToggleList[0].parent.active = activeValue;
  251. this.EnableAllCompositeModeButton.parent.active = activeValue;
  252. titleNode.active = activeValue;
  253. this.hideButtonLabel.string = activeValue ? 'Hide UI' : 'Show UI';
  254. }
  255. private _currentColorIndex = 0;
  256. private strColor: string[] = [
  257. '<color=#ffffff>',
  258. '<color=#000000>',
  259. '<color=#ff0000>',
  260. '<color=#00ff00>',
  261. '<color=#0000ff>',
  262. ];
  263. private color: Color[] = [
  264. Color.WHITE,
  265. Color.BLACK,
  266. Color.RED,
  267. Color.GREEN,
  268. Color.BLUE,
  269. ];
  270. changeTextColor(button: Button) {
  271. this._currentColorIndex++;
  272. if (this._currentColorIndex >= this.strColor.length) {
  273. this._currentColorIndex = 0;
  274. }
  275. for (let i = 0; i < this.textComponentList.length; i++) {
  276. this.textComponentList[i].string = this.strColor[this._currentColorIndex] + this.textContentList[i] + '</color>';
  277. }
  278. for (let i = 0; i < this.labelComponentList.length; i++) {
  279. this.labelComponentList[i].color = this.color[this._currentColorIndex];
  280. }
  281. }
  282. onLoad() {
  283. }
  284. update(deltaTime: number) {
  285. }
  286. }