suerprisePlus
2024-07-08 6f393bea8db5898684b573b8bebc64890fcc9183
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
139
140
141
142
143
<template>
    <div id="threeBox" class="appBox"></div>
</template>
 
<script>
import * as THREE from "three";
import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader.js";
import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader.js";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
export default {
    props: {
        someData: {
            type: String,
            default: "",
        }
    },
    data() {
        return {
            scene: null,
            camera: null,
            mesh: null,
            renderer: null,
            object: null,
        }
    },
    mounted() {
 
    },
    methods: {
        setRender() {
            this.renderer.render(this.scene, this.camera);
        },
        setMapThreeStart() {
            const threeBox = document.getElementById("threeBox");
            const width = threeBox.offsetWidth;
            const height = threeBox.offsetHeight;
            // 创建场景
            this.scene = new THREE.Scene();
            // const axesHelper = new THREE.AxesHelper(200);
            // this.scene.add(axesHelper);
            // 创建相机
            this.camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 10000);
            // 根据需要设置相机位置具体值
            this.camera.position.set(0.5, 0.5, 0.5);
            this.camera.lookAt(0, 0, 0);
            this.scene.add(this.camera);
            // 添加光源
            const ambientLight = new THREE.AmbientLight(0xffffff, 0.4);
            this.scene.add(ambientLight);
 
            const pointLight = new THREE.PointLight(0xffffff, 0.8);
 
            this.camera.add(pointLight);
 
            const ambientLight1 = new THREE.HemisphereLight("#Ffffff", "#000000", 1);
            this.scene.add(ambientLight1);
 
            // 创建渲染器
            this.renderer = new THREE.WebGLRenderer({ antialias: false });
 
            this.renderer.setClearColor("rgb(248,248,255)", 1.0);
            this.renderer.setSize(width, height);
            threeBox.appendChild(this.renderer.domElement);
            this.setRender();
 
            this.clock = new THREE.Clock();
            this.addOrbitControls();
        },
        addOrbitControls() {
            // 设置相机控件轨道控制器OrbitControls
            const controls = new OrbitControls(this.camera, this.renderer.domElement);
            // 如果OrbitControls改变了相机参数,重新调用渲染器渲染三维场景
            const that = this;
            controls.addEventListener("change", function () {
                that.setRender();
            }); //监听鼠标、键盘事件
        },
        replaceLast6Chars(res, replacement) {
            if (!res) return
            return res.slice(0, -3) + replacement.padEnd(3, '');
        },
        setMapThree(res) {
            var that = this;
            var mtlLoader = new MTLLoader();
            const objUrl = res;
            var mtlUrl = res;
            if (res.indexOf('.obj') > -1) {
                mtlUrl = this.replaceLast6Chars(res, 'mtl')
            }
            mtlLoader.load(mtlUrl, (material) => {
                console.log(material);
                material.preload();
                var objLoader = new OBJLoader();
                //设置当前加载的纹理
                objLoader
                    .setMaterials(material)
                    .load(objUrl, (item) => {
                        item.position.set(0, 0, 0);
                        //
                        //
                        item.traverse(function (obj) {
                            if (obj.isMesh) {
                                obj.castShadow = true; //阴影
                                obj.receiveShadow = true; //接受别人投的阴影
                            }
                        });
 
                        that.scene.add(item);
                        that.object = item;
                        that.setRender();
                    });
            });
        },
        setRender() {
            this.renderer.render(this.scene, this.camera);
        },
    },
    watch: {
        someData(n, o) {
            if (n) {
                if (!this.scene) {
                    this.setMapThreeStart();
                }
                this.$nextTick(() => {
                    this.setMapThree(n);
                })
                // 
            }
        }
    }
}
</script>
 
<style scoped lang="scss">
.appBox {
    // width: 100%;
    // height: 100%;
    width: 616px;
    height: 379px;
    position: absolute;
}
</style>