| import * as THREE from 'three'; |
| import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'; |
| import { DragControls } from 'three/examples/jsm/controls/DragControls'; |
| import { TransformControls } from 'three/examples/jsm/controls/TransformControls'; |
| import { CSS3DRenderer, CSS3DObject } from 'three/examples/jsm/renderers/CSS3DRenderer.js'; |
| export default { |
| components: {}, |
| props: {}, |
| data() { |
| return { |
| container: null, |
| camera: null, |
| scene: null, |
| renderer: null, |
| controls: null, |
| cameraTarget: null, |
| width: null, |
| height: null, |
| lineGroup: null, |
| LineSegmentsGroup: null, |
| PointsGroup: null, |
| top: -100, |
| left: -100, |
| name: '', |
| targetX: null, |
| targetY: null, |
| }; |
| }, |
| mounted() { |
| this.$nextTick(() => { |
| this._initData(); |
| this.animate(); |
| }); |
| }, |
| beforeDestroy() {}, |
| methods: { |
| _initData() { |
| this.container = document.createElement('div'); |
| |
| this.$refs.points.appendChild(this.container); |
| this.width = this.$refs.points.offsetWidth; |
| this.height = this.$refs.points.offsetHeight; |
| |
| this.camera = new THREE.PerspectiveCamera(45, this.width / this.height, 1, 10000); |
| this.camera.position.set(0, 0, 2200); |
| this.cameraTarget = new THREE.Vector3(0, 0, 0); |
| |
| this.controls = new OrbitControls(this.camera, this.container); |
| |
| this.controls.addEventListener('change', this._rendergl, true); |
| this.controls.update(); |
| |
| this.controls.enableRotate = false; |
| |
| |
| this.scene = new THREE.Scene(); |
| this.scene.background = new THREE.Color(0x696969); |
| |
| |
| |
| |
| |
| |
| var axisHelper = new THREE.AxesHelper(2); |
| this.scene.add(axisHelper); |
| |
| |
| let spotLight = new THREE.SpotLight(0xffffff); |
| spotLight.position.set(-100, -100, -100); |
| |
| let spotLight2 = new THREE.SpotLight(0xffffff); |
| spotLight2.position.set(100, 100, 100); |
| |
| this.scene.add(spotLight); |
| this.scene.add(spotLight2); |
| |
| this.scene.add(new THREE.HemisphereLight(0x443333, 0x111122)); |
| |
| |
| this.renderer = new THREE.WebGLRenderer({ antialias: true }); |
| this.renderer.setPixelRatio(window.devicePixelRatio); |
| this.renderer.setSize(this.width, this.height); |
| |
| |
| |
| |
| this.renderer.shadowMap.enabled = true; |
| |
| this.container.appendChild(this.renderer.domElement); |
| |
| |
| window.addEventListener('resize', this.onWindowResize, false); |
| window.addEventListener('mousewheel', this.onMousewheel, true); |
| |
| |
| this._loadLine(); |
| |
| this.raycaster = new THREE.Raycaster(); |
| this.pointer = new THREE.Vector2(); |
| |
| |
| this.container.addEventListener('pointerdown', this.onPointerMove, true); |
| }, |
| onPointerMove(event) { |
| |
| let mainCanvas = document.getElementsByTagName('canvas')[0]; |
| |
| this.pointer.x = ((event.clientX - mainCanvas.getBoundingClientRect().left) / mainCanvas.offsetWidth) * 2 - 1; |
| this.pointer.y = -((event.clientY - mainCanvas.getBoundingClientRect().top) / mainCanvas.offsetHeight) * 2 + 1; |
| |
| var vector = new THREE.Vector3(this.pointer.x, this.pointer.y); |
| vector = vector.unproject(this.camera); |
| var raycaster = new THREE.Raycaster(this.camera.position, vector.sub(this.camera.position).normalize()); |
| var intersects = raycaster.intersectObjects(this.scene.children, true); |
| if (intersects.length > 0) { |
| for (let item of intersects) { |
| if (item.object.type == 'Mesh') { |
| let array = item.object.name.split(','); |
| this.name = array[4]; |
| this.targetX = array[2]; |
| this.targetY = array[3]; |
| this.top = event.offsetY - 50; |
| this.left = event.offsetX - 76; |
| } else { |
| this.top = -100; |
| this.left = -100; |
| } |
| } |
| } else { |
| this.top = -100; |
| this.left = -100; |
| } |
| }, |
| |
| _loadLine() { |
| |
| const material = new THREE.LineBasicMaterial({ color: 0x00ff44 }); |
| |
| const distanceMaterial = new THREE.LineDashedMaterial({ |
| color: 0xdbdbdb, |
| dashSize: 3, |
| gapSize: 1, |
| }); |
| |
| const redMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 }); |
| const greenMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff44 }); |
| const yellowMaterial = new THREE.MeshBasicMaterial({ color: 0xffeb00 }); |
| |
| this.lineGroup = new THREE.Group(); |
| this.LineSegmentsGroup = new THREE.Group(); |
| this.PointsGroup = new THREE.Group(); |
| this.PointsGroup.name = 'Points'; |
| |
| this.breakData.forEach((item, index) => { |
| let points = []; |
| points.push(new THREE.Vector3(item[0], item[1], 1)); |
| points.push(new THREE.Vector3(item[2], item[3], 1)); |
| let geometry = new THREE.BufferGeometry().setFromPoints(points); |
| let line = new THREE.Line(geometry, material); |
| this.PointsGroup.add(line); |
| this.PointsGroup.visible = true; |
| this.scene.add(this.PointsGroup); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| let topPointsGeometry = new THREE.CircleGeometry(2, 32); |
| let Points1 = new THREE.Mesh(topPointsGeometry, yellowMaterial); |
| Points1.position.x = item[0]; |
| Points1.position.y = item[1]; |
| Points1.position.z = 0; |
| Points1.name = item.toString(); |
| this.LineSegmentsGroup.add(Points1); |
| this.LineSegmentsGroup.visible = true; |
| this.scene.add(this.LineSegmentsGroup); |
| |
| |
| let bottomMaterial = this.distance > item[4] ? greenMaterial : redMaterial; |
| let bottomPointsGeometry = new THREE.CircleGeometry(2, 32); |
| let Points2 = new THREE.Mesh(bottomPointsGeometry, bottomMaterial); |
| Points2.position.x = item[2]; |
| Points2.position.y = item[3]; |
| Points2.position.z = 1; |
| Points2.name = item.toString(); |
| this.lineGroup.add(Points2); |
| this.lineGroup.visible = true; |
| this.scene.add(this.lineGroup); |
| }); |
| this.renderer.render(this.scene, this.camera); |
| |
| |
| }, |
| |
| initDragControls() { |
| |
| var transformControls = new TransformControls(this.camera, this.renderer.domElement); |
| this.scene.add(transformControls); |
| |
| |
| var objects = []; |
| for (let i = 0; i < this.scene.children.length; i++) { |
| if (this.scene.children[i].isLine || this.scene.children[i].isLineSegments || this.scene.children[i].isPoints) { |
| objects.push(this.scene.children[i]); |
| } |
| } |
| |
| var dragControls = new DragControls(objects, this.camera, this.renderer.domElement); |
| |
| |
| dragControls.addEventListener('hoveron', function (event) { |
| console.log(event); |
| |
| transformControls.attach(event.object); |
| }); |
| |
| dragControls.addEventListener('dragstart', function (event) { |
| controls.enabled = false; |
| }); |
| |
| dragControls.addEventListener('dragend', function (event) { |
| controls.enabled = true; |
| }); |
| }, |
| |
| onWindowResize() { |
| this.$nextTick(() => { |
| if (this.$refs.points) { |
| this.width = this.$refs.points.offsetWidth; |
| this.height = this.$refs.points.offsetHeight; |
| this.camera.aspect = this.width / this.height; |
| this.camera.updateProjectionMatrix(); |
| this.renderer.setSize(this.width, this.height); |
| } |
| }); |
| }, |
| onMousewheel() { |
| this.top = -100; |
| this.left = -100; |
| }, |
| |
| _rendergl() { |
| this.renderer.render(this.scene, this.camera); |
| }, |
| |
| animate() { |
| requestAnimationFrame(this.animate); |
| this._rendergl(); |
| }, |
| |
| centerModel(group) { |
| |
| |
| |
| var box3 = new THREE.Box3(); |
| |
| |
| box3.expandByObject(group); |
| |
| var center = new THREE.Vector3(); |
| box3.getCenter(center); |
| |
| |
| |
| group.position.x = group.position.x - center.x; |
| |
| group.position.z = group.position.z - center.z; |
| }, |
| }, |
| }; |