|
|
@ -1,6 +1,16 @@ |
|
|
import _ from 'lodash' |
|
|
import _ from 'lodash' |
|
|
import * as THREE from 'three' |
|
|
import * as THREE from 'three' |
|
|
import { AxesHelper, GridHelper, OrthographicCamera, Raycaster, Scene, Vector3, WebGLRenderer } from 'three' |
|
|
import { |
|
|
|
|
|
AxesHelper, |
|
|
|
|
|
GridHelper, |
|
|
|
|
|
Mesh, |
|
|
|
|
|
type Object3D, |
|
|
|
|
|
OrthographicCamera, |
|
|
|
|
|
Raycaster, |
|
|
|
|
|
Scene, |
|
|
|
|
|
Vector3, |
|
|
|
|
|
WebGLRenderer |
|
|
|
|
|
} from 'three' |
|
|
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' |
|
|
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' |
|
|
import EsDragControls from './model2DEditor/EsDragControls' |
|
|
import EsDragControls from './model2DEditor/EsDragControls' |
|
|
import Stats from 'three/examples/jsm/libs/stats.module' |
|
|
import Stats from 'three/examples/jsm/libs/stats.module' |
|
|
@ -23,7 +33,7 @@ export default class Viewport { |
|
|
scene: Scene |
|
|
scene: Scene |
|
|
camera: OrthographicCamera |
|
|
camera: OrthographicCamera |
|
|
renderer: WebGLRenderer |
|
|
renderer: WebGLRenderer |
|
|
axesHelper: AxesHelper |
|
|
axesHelper: GridHelper |
|
|
gridHelper: GridHelper |
|
|
gridHelper: GridHelper |
|
|
statsControls: Stats |
|
|
statsControls: Stats |
|
|
controls: OrbitControls |
|
|
controls: OrbitControls |
|
|
@ -139,30 +149,35 @@ export default class Viewport { |
|
|
this.initMode2DCamera() |
|
|
this.initMode2DCamera() |
|
|
|
|
|
|
|
|
// 辅助线
|
|
|
// 辅助线
|
|
|
this.axesHelper = new THREE.AxesHelper(3) |
|
|
const axesHelper = new THREE.GridHelper(1000, 2) |
|
|
|
|
|
axesHelper.material.color.setHex(0x000000) |
|
|
|
|
|
axesHelper.material.linewidth = 2 |
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
axesHelper.material.vertexColors = false |
|
|
|
|
|
this.axesHelper = axesHelper |
|
|
this.scene.add(this.axesHelper) |
|
|
this.scene.add(this.axesHelper) |
|
|
|
|
|
|
|
|
this.gridHelper = new THREE.GridHelper(500, 500) |
|
|
const gridHelper = new THREE.GridHelper(1000, 1000) |
|
|
const gridHelper = this.gridHelper |
|
|
gridHelper.material.color.setHex(0x999999) |
|
|
gridHelper.material = new THREE.LineBasicMaterial({ |
|
|
gridHelper.material.opacity = 0.8 |
|
|
color: 0x888888, |
|
|
gridHelper.material.transparent = true |
|
|
opacity: 0.8, |
|
|
// @ts-ignore
|
|
|
transparent: true |
|
|
gridHelper.material.vertexColors = false |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
scene.add(gridHelper) |
|
|
this.gridHelper = gridHelper |
|
|
|
|
|
this.scene.add(this.gridHelper) |
|
|
|
|
|
|
|
|
// 光照
|
|
|
// 光照
|
|
|
const ambientLight = new THREE.AmbientLight(0xffffff, 1.5) |
|
|
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8) |
|
|
scene.add(ambientLight) |
|
|
scene.add(ambientLight) |
|
|
|
|
|
|
|
|
const directionalLight = new THREE.DirectionalLight(0xffffff, 1.5) |
|
|
// const directionalLight = new THREE.DirectionalLight(0xffffff, 1.5)
|
|
|
directionalLight.position.set(5, 5, 5).multiplyScalar(3) |
|
|
// directionalLight.position.set(5, 5, 5).multiplyScalar(3)
|
|
|
directionalLight.castShadow = true |
|
|
// directionalLight.castShadow = true
|
|
|
scene.add(directionalLight) |
|
|
// scene.add(directionalLight)
|
|
|
|
|
|
//
|
|
|
const hemisphereLight = new THREE.HemisphereLight(0xffffff, 0x444444, 1) |
|
|
// const hemisphereLight = new THREE.HemisphereLight(0xffffff, 0x444444, 1)
|
|
|
scene.add(hemisphereLight) |
|
|
// scene.add(hemisphereLight)
|
|
|
|
|
|
|
|
|
// 性能监控
|
|
|
// 性能监控
|
|
|
const statsControls = new Stats() |
|
|
const statsControls = new Stats() |
|
|
@ -174,16 +189,6 @@ export default class Viewport { |
|
|
viewerDom.appendChild(statsControls.dom) |
|
|
viewerDom.appendChild(statsControls.dom) |
|
|
$(statsControls.dom).children().css('height', '28px') |
|
|
$(statsControls.dom).children().css('height', '28px') |
|
|
|
|
|
|
|
|
// 创建几何体和材质
|
|
|
|
|
|
const geometry = new THREE.BoxGeometry(1, 1, 1) |
|
|
|
|
|
const material = new THREE.MeshStandardMaterial({ |
|
|
|
|
|
color: 0xcccccc, |
|
|
|
|
|
metalness: 0.9, |
|
|
|
|
|
roughness: 0.1 |
|
|
|
|
|
}) |
|
|
|
|
|
const cube = new THREE.Mesh(geometry, material) |
|
|
|
|
|
scene.add(cube) |
|
|
|
|
|
|
|
|
|
|
|
this.animate() |
|
|
this.animate() |
|
|
|
|
|
|
|
|
// 监听事件
|
|
|
// 监听事件
|
|
|
@ -372,9 +377,9 @@ export default class Viewport { |
|
|
const fadeStartDistance = 15 // 开始淡出的距离
|
|
|
const fadeStartDistance = 15 // 开始淡出的距离
|
|
|
|
|
|
|
|
|
// 计算透明度(0~1)
|
|
|
// 计算透明度(0~1)
|
|
|
let opacity = 1 |
|
|
let opacity = 0.8 |
|
|
if (cameraDistance > fadeStartDistance) { |
|
|
if (cameraDistance > fadeStartDistance) { |
|
|
opacity = 1 - Math.min((cameraDistance - fadeStartDistance) / (maxVisibleDistance - fadeStartDistance), 1) |
|
|
opacity = 0.8 - Math.min((cameraDistance - fadeStartDistance) / (maxVisibleDistance - fadeStartDistance) * 0.8, 0.8) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 修改网格材质透明度
|
|
|
// 修改网格材质透明度
|
|
|
|