multi-lod-group.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. const { trackEventWithTimer } = require('../../utils/metrics');
  3. const { getMessageProtocolScene } = require('../../utils/prop');
  4. exports.template = `
  5. <div>
  6. <ui-prop ref="multi-lod-dump" type="dump"></ui-prop>
  7. <ui-prop>
  8. <ui-label slot="label" value="Object Size"></ui-label>
  9. <div class="object-size-content" slot="content">
  10. <ui-num-input min="0" step="0.01"
  11. :invalid="multiObjectSizeInvalid"
  12. :value="multiObjectSizeInvalid && dump.value && dump.value.objectSize ? null : dump.value.objectSize.values[0]"
  13. @confirm="onMultiObjectSizeConfirm($event)"
  14. ></ui-num-input>
  15. <!-- <ui-button @confirm="resetMultiObjectSize">
  16. <ui-label value="Reset Object Size"></ui-label>
  17. </ui-button> -->
  18. </div>
  19. </ui-prop>
  20. <template v-for="(screenSize, index) in multiLODs">
  21. <ui-prop resize resize-group="screen-size-group"
  22. :key="index"
  23. :message="multiLODsErr[index]"
  24. :class="multiLODsErr[index] ? 'warn' : ''"
  25. >
  26. <ui-label slot="label"
  27. :value="handleMultiScreenSize(index)"
  28. ></ui-label>
  29. <ui-num-input slot="content"
  30. :invalid="screenSize === 'invalid'"
  31. :value="screenSize === 'invalid' ? 0 : Editor.Utils.Math.multi(screenSize, 100)"
  32. @confirm.stop="onMultiScreenSizeConfirm($event, index)"
  33. @change.stop
  34. ></ui-num-input>
  35. </ui-prop>
  36. </template>
  37. </div>
  38. `;
  39. exports.props = ['dump'];
  40. exports.watch = {
  41. dump: {
  42. immediate: true,
  43. deep: true,
  44. handler() {
  45. const that = this;
  46. that.refresh();
  47. },
  48. },
  49. };
  50. exports.data = function() {
  51. return {
  52. multiLen: 0,
  53. multiObjectSizeInvalid: false,
  54. multiLODs: [],
  55. multiLODsErr: [],
  56. };
  57. };
  58. exports.methods = {
  59. refresh() {
  60. const that = this;
  61. that.multiLODs = [];
  62. that.multiLODsErr = [];
  63. if (that.dump.value) {
  64. that.multiObjectSizeInvalid = that.dump.value.objectSize.values.some((val) => val !== that.dump.value.objectSize.values[0]);
  65. const multiLodGroups = that.dump.value.LODs.values;
  66. that.multiLen = multiLodGroups.reduce((pre, current) => {
  67. return pre < current.length ? pre : current.length;
  68. }, multiLodGroups[0].length);
  69. const multiLods = [];
  70. for (let i = 0; i < that.multiLen; i++) {
  71. let multiScreenSizeInvalid = false;
  72. for (let j = 1; j < multiLodGroups.length; j++) {
  73. if (
  74. !multiLodGroups[0][i] || !multiLodGroups[j][i] ||
  75. multiLodGroups[j][i].value.screenUsagePercentage.value !== multiLodGroups[0][i].value.screenUsagePercentage.value
  76. ) {
  77. multiScreenSizeInvalid = true;
  78. break;
  79. }
  80. }
  81. multiLods[i] = multiScreenSizeInvalid ? 'invalid' : multiLodGroups[0][i].value.screenUsagePercentage.value;
  82. }
  83. that.multiLODs = multiLods;
  84. }
  85. },
  86. onMultiObjectSizeConfirm(event) {
  87. const that = this;
  88. that.dump.value.objectSize.values = that.dump.value.objectSize.values.fill(event.target.value);
  89. that.multiObjectSizeInvalid = false;
  90. that.updateDump(that.dump.value.objectSize);
  91. },
  92. onMultiScreenSizeConfirm(event, index) {
  93. const that = this;
  94. const multiLODs = that.dump.value.LODs.values;
  95. const value = Editor.Utils.Math.divide(event.target.value, 100);
  96. if (checkMultiScreenSize(multiLODs, value, index)) {
  97. multiLODs.forEach((lod) => {
  98. if (lod[index]) {
  99. lod[index].value.screenUsagePercentage.value = value;
  100. }
  101. });
  102. that.updateDump(that.dump.value.LODs);
  103. trackEventWithTimer('LOD', 'A100011');
  104. that.$set(that.multiLODsErr, index, '');
  105. } else {
  106. that.$set(that.multiLODsErr, index, 'Input out of range, please check and manually modify the value');
  107. }
  108. },
  109. resetMultiObjectSize() {
  110. const that = this;
  111. that.dump.value.uuid.values.forEach((uuid) => {
  112. Editor.Message.send(getMessageProtocolScene(that.$el), 'execute-component-method', {
  113. uuid: uuid,
  114. name: 'resetObjectSize',
  115. args: [],
  116. });
  117. });
  118. trackEventWithTimer('LOD', 'A100010');
  119. },
  120. updateDump(dump) {
  121. const that = this;
  122. that.$refs['multi-lod-dump'].dump = dump;
  123. that.$refs['multi-lod-dump'].dispatch('change-dump');
  124. that.$refs['multi-lod-dump'].dispatch('confirm-dump');
  125. },
  126. handleMultiScreenSize(index) {
  127. return `LOD ${index} Transition (% Screen Ratio)`;
  128. },
  129. };
  130. function checkMultiScreenSize(multiLODs, value, index) {
  131. for (let i = 0; i < multiLODs.length; i++) {
  132. const multiLods = multiLODs[i];
  133. if (multiLods[index + 1] && multiLods[index + 1].value.screenUsagePercentage.value >= value) {
  134. return false;
  135. }
  136. if (multiLods[index - 1] && multiLods[index - 1].value.screenUsagePercentage.value <= value) {
  137. return false;
  138. }
  139. }
  140. return true;
  141. }