OK, The following is my code :
<template>
<div ref=“demo1”></div>
</template>
<script>
import ServerLayout from ‘./js/ServerLayout’;
import LoadingData from “./js/LoadingData”;
export default {
name: ‘Three’,
data: () => ({
controls: {
scene: null,
camera: null,
renderer: null,
loading : false,
// rotationSpeed: 0.02
}
}),
created () {
this.$nextTick(() => {
// this.init();
});
},
mounted () {
window.THREE = require(‘three’);
require(‘three/examples/js/controls/OrbitControls’);
require(‘three/examples/js/loaders/MTLLoader’);
require(‘three/examples/js/loaders/OBJLoader’);
require(‘three/examples/js/loaders/STLLoader’);
require(‘three/examples/js/loaders/GLTFLoader’);
require(‘three/src/loaders/TextureLoader’);
require(‘three/examples/js/controls/MapControls’);
require(‘three/examples/js/controls/TransformControls’);
require(‘three/examples/js/controls/FirstPersonControls’);
require(‘three/examples/js/controls/DragControls’);
require(‘three/examples/js/controls/TrackballControls’);
this.$nextTick(function () {
this.init();
});
},
methods: {
init () {
this.initMesh();
},
initMesh () {
this.scene = new THREE.Scene();
this.scene.background = new THREE.Color(0xcccccc);
this.renderer = new THREE.WebGLRenderer({antialias: true });
this.renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(this.renderer.domElement);
this.camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 10000);
this.camera.lookAt(0,200,200);
this.camera.position.x = 500;
this.camera.position.y = 400;
this.camera.position.z = 400;
let light1 = new THREE.DirectionalLight(0xffffff, 0.95);
light1.position.set(100,100,60);
this.scene.add(light1);
var sphere=new THREE.SphereGeometry(20,20,20);
var sphereMaterial=new THREE.MeshLambertMaterial({color:0xff0000});
var spereMesh=new THREE.Mesh(sphere,sphereMaterial);
spereMesh.position.x =0;
spereMesh.position.y = 200;
spereMesh.position.z = 200;
this.scene.add(spereMesh);
this.loadPlane(this.scene); // add plane
this.$refs.demo1.append(this.renderer.domElement);
this.renderScene();
let controls = new THREE.MapControls(this.camera,this.renderer.domElement);
controls.addEventListener(‘change’,this.renderer);
},
renderScene () {
let {controls, scene, camera, renderer} = this;
requestAnimationFrame(this.renderScene);
renderer.render(scene, camera);
},
loadPlane (scene) {
let length = 3000;
let geometry1 = new THREE.Geometry();
let lineMaterial = new THREE.LineDashedMaterial({color: 0x777777});
geometry1.vertices.push(new THREE.Vector3(-length / 2, 0, 0));
geometry1.vertices.push(new THREE.Vector3(length / 2, 0, 0));
for (let i = 0; i <= length / 10; i++) {
let lineX = new THREE.Line(geometry1, lineMaterial);
lineX.position.z = (i * 10) - length / 2;
scene.add(lineX);
let lineY = new THREE.Line(geometry1, lineMaterial);
lineY.rotation.y = 0.5 Math.PI;
lineY.position.x = (i 10) - length / 2;
scene.add(lineY);
}
let axes = new THREE.AxisHelper(20);
scene.add(axes);
}
}
};
</script>