index.vue
3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<template>
<div class="go-content-box">
<!-- fix: 预览三维图像需加上preserveDrawingBuffer: true -->
<div v-if="supportWebGL">
<vue3dLoader
ref="vue3dLoaderRef"
:webGLRendererOptions="webGLRendererOptions"
:backgroundColor="backgroundColor"
:backgroundAlpha="backgroundAlpha"
:height="h"
:width="w"
:filePath="dataset"
:autoPlay="autoPlay"
:enableDamping="enableDamping"
:outputEncoding="outputEncoding"
:clearScene="clearScene"
:dampingFactor="dampingFactor"
:labels="labels"
@process="onProcess"
@load="onLoad"
@click="onClick"
/>
<div v-show="show" class="process">
<span>拼命加载中...</span>
<n-progress status="success" type="line" :percentage="process" :indicator-placement="'inside'" processing />
</div>
</div>
<div v-else>您的浏览器不支持WebGL!</div>
</div>
</template>
<script setup lang="ts">
import { PropType, toRefs, ref, onMounted, nextTick } from 'vue'
import { CreateComponentType } from '@/packages/index.d'
import { vue3dLoader } from 'vue-3d-loader'
const props = defineProps({
chartConfig: {
type: Object as PropType<CreateComponentType>,
required: true
}
})
const vue3dLoaderRef = ref(null)
//threejs配置
const webGLRendererOptions = {
alpha: true, // 透明
antialias: true, // 抗锯齿
preserveDrawingBuffer: true //缩略图生效需开启
}
//标注示例
const labels = ref()
labels.value = [
// text label
{
text: 'co2浓度:44',
position: { x: 0, y: 18, z: 0 },
scale: { x: 11.5, y: 10.8, z: 30.5 },
textStyle: {
fontFamily: 'Arial',
fontSize: 12,
fontWeight: 600,
lineHeight: 1,
color: '#ffffff',
borderWidth: 8,
borderRadius: 0,
borderColor: '#000000',
backgroundColor: 'rgba(0,0,0,1)'
},
sid: 3 // 自定义标识,可有可无
}
]
//三维模型加载
const show = ref(true)
const onLoad = async () => {
//加载完成
await nextTick()
show.value = false
}
//三维模型进度条
const process = ref(0)
const onProcess = (event: any) => {
// 拼命加载中,加载进度
process.value = Math.floor(event.loaded / event.total) * 100
}
const onClick = (event: any) => {
console.log(event)
}
//判断浏览器是否支持WebGL
const supportWebGL = ref(true)
const detectWebGLContext = () => {
let canvas = document.createElement('canvas')
let gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl')
if (gl && gl instanceof WebGLRenderingContext) {
supportWebGL.value = true
} else {
supportWebGL.value = false
}
}
onMounted(() => {
detectWebGLContext()
console.log(`实例`, vue3dLoaderRef.value)
})
const { w, h } = toRefs(props.chartConfig.attr)
const {
backgroundColor,
backgroundAlpha,
autoPlay,
enableDamping,
dataset,
outputEncoding,
clearScene,
dampingFactor,
} = toRefs(props.chartConfig.option) as any
</script>
<style lang="scss" scoped>
.go-content-box {
height: 100%;
position: relative;
.process {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}
</style>