分享一个曾经做过的3d可视化项目,该项目使用vue2.9作为框架 使用webpack构建前段工程,
项目核心点:vue中集成threejs;高德地图中加载3d模型;threejs操作模型的各种应用。
项目运行效果图:
首页地图+3d管道
点击管道进入管道详情:
3d拾取查看详情:
工程核心代码:
1.packge.json
{
"name": "code",
"description": "gcc-test",
"version": "1.0.0",
"author": "cc.guo",
"license": "MIT",
"private": true,
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"dependencies": {
"array-includes": "^3.0.3",
"axios": "^0.19.0",
"cross-spawn": "^7.0.1",
"element-ui": "^2.9.1",
"jquery": "^3.4.1",
"js-md5": "^0.7.3",
"md5-node": "^1.0.1",
"sass": "^1.23.6",
"three": "^0.104.0",
"three-orbit-controls": "^82.1.0",
"vue": "^2.5.11",
"vue-axios": "^2.1.4",
"vue-router": "^3.0.1"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
],
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-stage-3": "^6.24.1",
"copy-webpack-plugin": "^5.0.3",
"cross-env": "^5.0.5",
"css-loader": "^0.28.7",
"file-loader": "^1.1.4",
"node-sass": "^4.5.3",
"sass-loader": "^6.0.6",
"vue-loader": "^13.0.5",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.11.5"
}
}
2.webpack.config.js 前段工程化配置
const path = require('path')
const webpack = require('webpack')
const CopyWebpackPlugin =require('copy-webpack-plugin')
let Plugins=[];
Plugins.push(new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
THREE:"three",
}))
// Plugins.push(new CopyWebpackPlugin([{
// from: "src/model/",
// to: "model/",
// force: true
// }]))
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
plugins:Plugins,
module: {
rules: [
{
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
loader: 'file-loader'
},
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
],
},
{
test: /\.scss$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader'
],
},
{
test: /\.sass$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': [
'vue-style-loader',
'css-loader',
'sass-loader'
],
'sass': [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
]
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
port: 8082,
host: 'localhost',
// contentBase: '../dist/',//服务器上面所有静态资源的源地址
// publicPath: '/',//服务器访问的时候添加的项目前缀,例如本实例的localhost:9000/ipems/scene1来进行访问场景1
proxy: [{
context: ['/oilpipe_xy'],
target: {
// "host": "10.24.19.246",
"host": "192.168.0.45",
"protocol": 'http:',
"port": 8080
}
}],
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
3.three js核心代码
加载模型
const path = require('path')
const webpack = require('webpack')
const CopyWebpackPlugin =require('copy-webpack-plugin')
let Plugins=[];
Plugins.push(new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
THREE:"three",
}))
// Plugins.push(new CopyWebpackPlugin([{
// from: "src/model/",
// to: "model/",
// force: true
// }]))
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
plugins:Plugins,
module: {
rules: [
{
test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
loader: 'file-loader'
},
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
],
},
{
test: /\.scss$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader'
],
},
{
test: /\.sass$/,
use: [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
loaders: {
// Since sass-loader (weirdly) has SCSS as its default parse mode, we map
// the "scss" and "sass" values for the lang attribute to the right configs here.
// other preprocessors should work out of the box, no loader config like this necessary.
'scss': [
'vue-style-loader',
'css-loader',
'sass-loader'
],
'sass': [
'vue-style-loader',
'css-loader',
'sass-loader?indentedSyntax'
]
}
// other vue-loader options go here
}
},
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.(png|jpg|gif)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']
},
devServer: {
historyApiFallback: true,
port: 8082,
host: 'localhost',
// contentBase: '../dist/',//服务器上面所有静态资源的源地址
// publicPath: '/',//服务器访问的时候添加的项目前缀,例如本实例的localhost:9000/ipems/scene1来进行访问场景1
proxy: [{
context: ['/oilpipe_xy'],
target: {
// "host": "10.24.19.246",
"host": "192.168.0.45",
"protocol": 'http:',
"port": 8080
}
}],
noInfo: true,
overlay: true
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
3d模型元素德拾取
// 获取与射线相交的对象数组
function getIntersects(event) {
event.preventDefault();
console.log("event.clientX:"+event.clientX)
console.log("event.clientY:"+event.clientY)
// 声明 raycaster 和 mouse 变量
let raycaster = new THREE.Raycaster();
let mouse = new THREE.Vector2();
// 通过鼠标点击位置,计算出 raycaster 所需点的位置,以屏幕为中心点,范围 -1 到 1
mouse.x = (event.clientX / container.clientWidth) * 2 - 1;
mouse.y = -(event.clientY / container.clientHeight) * 2 + 1;
//通过鼠标点击的位置(二维坐标)和当前相机的矩阵计算出射线位置
raycaster.setFromCamera(mouse, that.camera);
// 获取与射线相交的对象数组,其中的元素按照距离排序,越近的越靠前
let intersects = raycaster.intersectObjects(getIntersectsMesh(),true);
//返回选中的对象
return intersects;
}
//拼接mesh拾取对象数组
function getIntersectsMesh(){
let taoguanMesh = that.scene.getObjectByName("taoguan");
let handianMesh = that.scene.getObjectByName("handian");
let guanMesh = that.scene.getObjectByName("guan");
let gaibanMesh = that.scene.getObjectByName("gaiban");
let bianhaoMesh = that.scene.getObjectByName("bianhao");
let shexiangtouMesh = that.scene.getObjectByName("shexiangtou");
let yinjibaohuzhuangMesh = that.scene.getObjectByName("yinjibaohuzhuang");
let jiaochaguanMesh = that.scene.getObjectByName("jiaochaguan");
let handianjiheMesh = that.scene.getObjectByName("handianjihe");
let intersectsMesh = [];
if(taoguanMesh && taoguanMesh.children){
intersectsMesh = intersectsMesh.concat(taoguanMesh.children);
}
if(handianMesh && handianMesh.children){
intersectsMesh = intersectsMesh.concat(handianMesh.children);
}
if(guanMesh && guanMesh.children){
intersectsMesh = intersectsMesh.concat(guanMesh.children);
}
if(gaibanMesh && gaibanMesh.children){
intersectsMesh = intersectsMesh.concat(gaibanMesh.children);
}
if(bianhaoMesh && bianhaoMesh.children){
intersectsMesh = intersectsMesh.concat(bianhaoMesh.children);
}
if(shexiangtouMesh && shexiangtouMesh.children){
intersectsMesh = intersectsMesh.concat(shexiangtouMesh.children);
}
if(yinjibaohuzhuangMesh && yinjibaohuzhuangMesh.children){
intersectsMesh = intersectsMesh.concat(yinjibaohuzhuangMesh.children);
}
if(jiaochaguanMesh && jiaochaguanMesh.children){
intersectsMesh = intersectsMesh.concat(jiaochaguanMesh.children);
}
if(handianjiheMesh && handianjiheMesh.children){
intersectsMesh = intersectsMesh.concat(handianjiheMesh.children);
}
return intersectsMesh;
}
带码将在在github上发布由于需要修改部分不合适发布的信息,正在脱敏处理中