| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <template>
- <div
- class="PieD3"
- @pointerleave="enableAutoplay && play()"
- @pointermove="pause"
- >
- <VciD3
- :before-init-qt="beforeInitQt"
- @ready="ready"
- />
- <slot></slot>
- </div>
- </template>
- <script>
- // power by visual-ceiling, at 2024/7/4 上午11:45
- import VciD3 from "./VciD3.vue";
- import { mergeDeep } from "@vci/helper/src/object";
- import { PluginAdapt } from "./../src/plugins/PluginAdapt";
- import { PluginEvents } from "./../src/plugins/PluginEvents";
- import { PluginAnimation } from "./../src/plugins/PluginAnimation";
- import { ExtrudeGeometry, Mesh, MeshStandardMaterial, Path, Shape } from "three/webgpu";
- import { createDirectionLight } from "./../src/creator/lights/createDirectionLight";
- import { degToRad } from "three/src/math/MathUtils";
- import Thing from "./../src/core/Thing";
- import { sumArray } from "@vci/helper/src/array";
- import { createAmbientLight } from "./../src/creator/lights/createAmbientLight";
- import { createOrthographicCamera } from "./../src/creator/cameras/createOrthographicCamera";
- import { PluginFns } from "./../src/plugins/PluginFns";
- import { ThingEvents } from "./../src/events/ThingEvents";
- import { remove, Tween } from "@tweenjs/tween.js";
- import { PluginRendererWebGPU } from "./../src/plugins/PluginRendererWebGPU";
- import { PluginAdjustment } from "./../src/plugins/PluginAdjustment";
- const PI2 = Math.PI * 2;
- export default {
- name: "PieD3",
- components: { VciD3 },
- props: {
- dataPie: {
- type: Array,
- default: null
- },
- colors: {
- type: Array,
- default: () => ["#ffa861", "#4294ff", "#34f36a", "#fd3a84"]
- },
- enableAutoplay: {
- type: Boolean,
- default: false
- }
- },
- data() {
- return {
- isReady: false,
- autoplay: {
- index: 0,
- inter: 0
- }
- };
- },
- watch: {
- dataPie: {
- immediate: true,
- handler() {
- this.isReady && this.update();
- }
- }
- },
- beforeDestroy() {
- this.pause();
- },
- methods: {
- beforeInitQt(option) {
- mergeDeep(option, {
- name: "Pie3D",
- debug: false,
- plugins: [
- PluginAdjustment,
- PluginAnimation,
- PluginAdapt,
- PluginEvents,
- PluginFns,
- {
- plugin: PluginRendererWebGPU,
- option: {
- params: { antialias: true }
- }
- }
- ]
- });
- },
- async ready(qt) {
- this.qt = qt;
- qt.camera = createOrthographicCamera({
- qt,
- left: -qt.elWidth / 2 * .4,
- right: qt.elWidth / 2 * .4,
- top: qt.elHeight / 2,
- bottom: -qt.elHeight / 2
- });
- qt.camera.position.set(0, 80, 100);
- qt.camera.zoom = .9;
- qt.camera.lookAt(0, 0, 0);
- qt.camera.updateProjectionMatrix();
- // await createEnvHDR(qt, `${resourceRootAliyun}/textures/env-light.hdr`);
- // qt.scene.background = null;
- createAmbientLight({ qt, intensity: 1 });
- createDirectionLight({ qt, intensity: 10, position: [-50, 50, 50] });
- this.isReady = true;
- this.update();
- qt.plugin("PluginAnimation").animationIn();
- },
- createPieItem(angle = Math.PI * .25, radius = [32, 50]) {
- const { qt } = this;
- const shape = new Shape();
- if(Array.isArray(radius)) {
- const pathInner = new Path();
- pathInner.arc(0, 0, radius[0], 0, angle, true);
- const pathOuter = new Path();
- pathOuter.arc(0, 0, radius[1], 0, angle, true);
- shape.setFromPoints(pathOuter.getPoints(99).concat(pathInner.getPoints(99).reverse()));
- } else {
- shape.moveTo(0, 0);
- shape.absarc(0, 0, radius, 0, angle, true);
- shape.lineTo(0, 0);
- }
- const geo = new ExtrudeGeometry(shape, {
- depth: 18,
- steps: 20,
- bevelEnabled: false
- });
- geo.rotateX(degToRad(-90));
- geo.computeVertexNormals();
- const mtl = new MeshStandardMaterial({
- roughness: .2,
- metalness: .6
- });
- const o3 = new Mesh(geo, mtl);
- const thing = new Thing({
- qt,
- o3
- });
- thing.isFocus = false;
- thing.focus = async () => {
- thing.isFocus = true;
- qt.things.forEach(t => t !== thing && t.blur());
- this.autoplay.index = qt.things.indexOf(thing);
- this.$emit("focus", {
- index: this.autoplay.index,
- data: this.dataPie[this.autoplay.index]
- });
- await thing.grow(1.5);
- };
- thing.blur = async () => {
- thing.isFocus = false;
- await thing.grow(1);
- };
- thing.grow = async (grow, duration = 100, delay = 0, immediate = false) => {
- thing.tw.grow && remove(thing.tw.grow);
- if(immediate) thing.scale = [1, grow, 1];
- else await new Promise(resolve => thing.tw.grow = new Tween(thing.scale)
- .to([1, grow, 1], duration)
- .delay(delay)
- .onUpdate(e => thing.scale = e)
- .onComplete(resolve)
- .start());
- };
- thing.addEventListener(ThingEvents.Click, () => {
- if(thing.isFocus) {
- thing.blur();
- this.$emit("blur");
- } else thing.focus();
- });
- return thing;
- },
- async update() {
- const { colors, dataPie, qt } = this;
- qt.things.length > 0 && await new Promise(resolve => qt.things.forEach(thing => thing.fadeOut({ duration: 200 }).then(thing => {
- thing.destroy();
- resolve();
- })));
- if(dataPie && dataPie.length > 0) {
- const amount = sumArray(dataPie, "value");
- this.markAngle = degToRad(90);
- dataPie.forEach((v, i) => {
- const percent = v.value / amount;
- const angle = percent * PI2;
- const thing = this.createPieItem(-angle);
- thing.o3.material.color.set(colors[i % colors.length]);
- thing.rotation = [0, this.markAngle, 0];
- thing.hide().fadeIn();
- thing.grow(0, 0, 0, true);
- thing.grow(1, 350, i * 175);
- this.markAngle -= angle;
- });
- }
- this.enableAutoplay ? this.play() : this.pause();
- },
- play() {
- clearTimeout(this.autoplay.inter);
- this.autoplay.index++;
- if(this.autoplay.index >= this.dataPie.length) this.autoplay.index = 0;
- this.qt.things[this.autoplay.index].focus();
- this.autoplay.inter = setTimeout(() => this.play(), 2e3);
- },
- pause() {
- clearTimeout(this.autoplay.inter);
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- .PieD3 {
- width: 100%;
- height: 100%;
- }
- </style>
|