PieD3.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <div
  3. class="PieD3"
  4. @pointerleave="enableAutoplay && play()"
  5. @pointermove="pause"
  6. >
  7. <VciD3
  8. :before-init-qt="beforeInitQt"
  9. @ready="ready"
  10. />
  11. <slot></slot>
  12. </div>
  13. </template>
  14. <script>
  15. // power by visual-ceiling, at 2024/7/4 上午11:45
  16. import VciD3 from "./VciD3.vue";
  17. import { mergeDeep } from "@vci/helper/src/object";
  18. import { PluginAdapt } from "./../src/plugins/PluginAdapt";
  19. import { PluginEvents } from "./../src/plugins/PluginEvents";
  20. import { PluginAnimation } from "./../src/plugins/PluginAnimation";
  21. import { ExtrudeGeometry, Mesh, MeshStandardMaterial, Path, Shape } from "three/webgpu";
  22. import { createDirectionLight } from "./../src/creator/lights/createDirectionLight";
  23. import { degToRad } from "three/src/math/MathUtils";
  24. import Thing from "./../src/core/Thing";
  25. import { sumArray } from "@vci/helper/src/array";
  26. import { createAmbientLight } from "./../src/creator/lights/createAmbientLight";
  27. import { createOrthographicCamera } from "./../src/creator/cameras/createOrthographicCamera";
  28. import { PluginFns } from "./../src/plugins/PluginFns";
  29. import { ThingEvents } from "./../src/events/ThingEvents";
  30. import { remove, Tween } from "@tweenjs/tween.js";
  31. import { PluginRendererWebGPU } from "./../src/plugins/PluginRendererWebGPU";
  32. import { PluginAdjustment } from "./../src/plugins/PluginAdjustment";
  33. const PI2 = Math.PI * 2;
  34. export default {
  35. name: "PieD3",
  36. components: { VciD3 },
  37. props: {
  38. dataPie: {
  39. type: Array,
  40. default: null
  41. },
  42. colors: {
  43. type: Array,
  44. default: () => ["#ffa861", "#4294ff", "#34f36a", "#fd3a84"]
  45. },
  46. enableAutoplay: {
  47. type: Boolean,
  48. default: false
  49. }
  50. },
  51. data() {
  52. return {
  53. isReady: false,
  54. autoplay: {
  55. index: 0,
  56. inter: 0
  57. }
  58. };
  59. },
  60. watch: {
  61. dataPie: {
  62. immediate: true,
  63. handler() {
  64. this.isReady && this.update();
  65. }
  66. }
  67. },
  68. beforeDestroy() {
  69. this.pause();
  70. },
  71. methods: {
  72. beforeInitQt(option) {
  73. mergeDeep(option, {
  74. name: "Pie3D",
  75. debug: false,
  76. plugins: [
  77. PluginAdjustment,
  78. PluginAnimation,
  79. PluginAdapt,
  80. PluginEvents,
  81. PluginFns,
  82. {
  83. plugin: PluginRendererWebGPU,
  84. option: {
  85. params: { antialias: true }
  86. }
  87. }
  88. ]
  89. });
  90. },
  91. async ready(qt) {
  92. this.qt = qt;
  93. qt.camera = createOrthographicCamera({
  94. qt,
  95. left: -qt.elWidth / 2 * .4,
  96. right: qt.elWidth / 2 * .4,
  97. top: qt.elHeight / 2,
  98. bottom: -qt.elHeight / 2
  99. });
  100. qt.camera.position.set(0, 80, 100);
  101. qt.camera.zoom = .9;
  102. qt.camera.lookAt(0, 0, 0);
  103. qt.camera.updateProjectionMatrix();
  104. // await createEnvHDR(qt, `${resourceRootAliyun}/textures/env-light.hdr`);
  105. // qt.scene.background = null;
  106. createAmbientLight({ qt, intensity: 1 });
  107. createDirectionLight({ qt, intensity: 10, position: [-50, 50, 50] });
  108. this.isReady = true;
  109. this.update();
  110. qt.plugin("PluginAnimation").animationIn();
  111. },
  112. createPieItem(angle = Math.PI * .25, radius = [32, 50]) {
  113. const { qt } = this;
  114. const shape = new Shape();
  115. if(Array.isArray(radius)) {
  116. const pathInner = new Path();
  117. pathInner.arc(0, 0, radius[0], 0, angle, true);
  118. const pathOuter = new Path();
  119. pathOuter.arc(0, 0, radius[1], 0, angle, true);
  120. shape.setFromPoints(pathOuter.getPoints(99).concat(pathInner.getPoints(99).reverse()));
  121. } else {
  122. shape.moveTo(0, 0);
  123. shape.absarc(0, 0, radius, 0, angle, true);
  124. shape.lineTo(0, 0);
  125. }
  126. const geo = new ExtrudeGeometry(shape, {
  127. depth: 18,
  128. steps: 20,
  129. bevelEnabled: false
  130. });
  131. geo.rotateX(degToRad(-90));
  132. geo.computeVertexNormals();
  133. const mtl = new MeshStandardMaterial({
  134. roughness: .2,
  135. metalness: .6
  136. });
  137. const o3 = new Mesh(geo, mtl);
  138. const thing = new Thing({
  139. qt,
  140. o3
  141. });
  142. thing.isFocus = false;
  143. thing.focus = async () => {
  144. thing.isFocus = true;
  145. qt.things.forEach(t => t !== thing && t.blur());
  146. this.autoplay.index = qt.things.indexOf(thing);
  147. this.$emit("focus", {
  148. index: this.autoplay.index,
  149. data: this.dataPie[this.autoplay.index]
  150. });
  151. await thing.grow(1.5);
  152. };
  153. thing.blur = async () => {
  154. thing.isFocus = false;
  155. await thing.grow(1);
  156. };
  157. thing.grow = async (grow, duration = 100, delay = 0, immediate = false) => {
  158. thing.tw.grow && remove(thing.tw.grow);
  159. if(immediate) thing.scale = [1, grow, 1];
  160. else await new Promise(resolve => thing.tw.grow = new Tween(thing.scale)
  161. .to([1, grow, 1], duration)
  162. .delay(delay)
  163. .onUpdate(e => thing.scale = e)
  164. .onComplete(resolve)
  165. .start());
  166. };
  167. thing.addEventListener(ThingEvents.Click, () => {
  168. if(thing.isFocus) {
  169. thing.blur();
  170. this.$emit("blur");
  171. } else thing.focus();
  172. });
  173. return thing;
  174. },
  175. async update() {
  176. const { colors, dataPie, qt } = this;
  177. qt.things.length > 0 && await new Promise(resolve => qt.things.forEach(thing => thing.fadeOut({ duration: 200 }).then(thing => {
  178. thing.destroy();
  179. resolve();
  180. })));
  181. if(dataPie && dataPie.length > 0) {
  182. const amount = sumArray(dataPie, "value");
  183. this.markAngle = degToRad(90);
  184. dataPie.forEach((v, i) => {
  185. const percent = v.value / amount;
  186. const angle = percent * PI2;
  187. const thing = this.createPieItem(-angle);
  188. thing.o3.material.color.set(colors[i % colors.length]);
  189. thing.rotation = [0, this.markAngle, 0];
  190. thing.hide().fadeIn();
  191. thing.grow(0, 0, 0, true);
  192. thing.grow(1, 350, i * 175);
  193. this.markAngle -= angle;
  194. });
  195. }
  196. this.enableAutoplay ? this.play() : this.pause();
  197. },
  198. play() {
  199. clearTimeout(this.autoplay.inter);
  200. this.autoplay.index++;
  201. if(this.autoplay.index >= this.dataPie.length) this.autoplay.index = 0;
  202. this.qt.things[this.autoplay.index].focus();
  203. this.autoplay.inter = setTimeout(() => this.play(), 2e3);
  204. },
  205. pause() {
  206. clearTimeout(this.autoplay.inter);
  207. }
  208. }
  209. };
  210. </script>
  211. <style lang="scss" scoped>
  212. .PieD3 {
  213. width: 100%;
  214. height: 100%;
  215. }
  216. </style>