依赖
npm i three
引包
import * as THREE from 'three'
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js'
import {OBJLoader} from 'three/examples/jsm/loaders/OBJLoader.js'
import {FBXLoader} from 'three/examples/jsm/loaders/FBXLoader.js'
import {MTLLoader} from 'three/examples/jsm/loaders/MTLLoader.js'
使用
this.krq = new THREE.Object3D();
this.textureLoader = new THREE.TextureLoader();
this.scene = new THREE.Scene();
var urls = [
require('@/assets/images/3d/negz.jpg'),
require('@/assets/images/3d/negz.jpg'),
require('@/assets/images/3d/negz.jpg'),
require('@/assets/images/3d/negz.jpg'),
require('@/assets/images/3d/negz.jpg'),
require('@/assets/images/3d/negz.jpg'),
];
var cubeLoader = new THREE.CubeTextureLoader();
this.scene.background = cubeLoader.load(urls);
this.camera = new THREE.PerspectiveCamera(40,
document.documentElement.clientWidth / document.documentElement.clientHeight, 0.5, 10000);
this.camera.position.set(180, 80, 90);
this.camera.lookAt(this.scene.position);
this.renderer = new THREE.WebGLRenderer({
antialias: true,
logarithmicDepthBuffer: true,
});
this.renderer.setClearColor(new THREE.Color(0x121A39));
this.renderer.setSize(this.canvasWidth, this.canvasHeight);
var alight = new THREE.AmbientLight("#ffffff", 1);
alight.name = "aLight";
this.scene.add(alight);
var _this = this;
_this.camera.updateProjectionMatrix();
if (_this.renderClock != null) clearInterval(_this.renderClock)
_this.renderClock = setInterval(() => {
_this.renderer.render(_this.scene, _this.camera);
}, 100)
orbit = new OrbitControls(_this.camera, _this.renderer.domElement);
orbit.addEventListener("change", (e) => {
_this.renderer.render(_this.scene, _this.camera);
});
this.renderer.domElement.addEventListener('click', function (event) {
const mouse = new THREE.Vector2();
var container = document.querySelector('#dom')
let getBoundingClientRect = container.getBoundingClientRect()
mouse.x = ((event.clientX - getBoundingClientRect.left) / container.offsetWidth) * 2 - 1;
mouse.y = -((event.clientY - getBoundingClientRect.top) / container.offsetHeight) * 2 + 1;
var raycaster = new THREE.Raycaster()
raycaster.params.Line.threshold = 0.01
raycaster.setFromCamera(mouse, _this.camera);
const intersects = raycaster.intersectObjects(_this.scene.children);
if (intersects && intersects.length > 0 && intersects[0].object.userData.form) {
_this.clickModel = intersects[0].object
var firstForm = intersects[0].object.userData.form
}
});
加载图片
let _this = this;
var url = require('@/assets/images/other/xxx.png')
new THREE.TextureLoader().load(url,
(texture) => {
let height = 10;
let width = 10;
const mat = new THREE.MeshBasicMaterial({map: texture, side: THREE.DoubleSide, transparent: true});
const geom = new THREE.PlaneGeometry(width, height);
const mesh = new THREE.Mesh(geom, mat);
var tX = -140
mesh.position.set(-97, 20, tX);
mesh.rotation.y = Math.PI / 2
_this.scene.add(mesh);
}
);
加载obj模型 OBJLoader
var objLoader = new OBJLoader();
let _this = this;
objLoader.load('/static/assets/models/floor/floor_obj.obj', obj => {
var mesh = obj.children[0];
mesh.material = new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load('/static/assets/models/floor/2.png'),
});
mesh.position.set(0, -1, 0);
_this.scene.add(mesh);
});
加载fbx文件 FBXLoader
var _this = this;
var fbxLoader = new FBXLoader()
fbxLoader.load('/static/assets/models/electricbox.fbx', res => {
var mesh = res.children[0]
mesh.material = new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load('/static/assets/models/electricbox_pic.png'),
});
mesh.position.set(0, 0, 0);
_this.scene.add(mesh);
})
同时加载mtl跟obj
var _this = this;
var objLoader2 = new OBJLoader();
var mtlLoader = new MTLLoader();
mtlLoader.load('/static/assets/models/car/rio.mtl',mtl=>{
objLoader2.setMaterials(mtl)
objLoader2.load('/static/assets/models/car/rio.obj', obj => {
var mesh = obj;
mesh.scale.set(1, 1, 1);
mesh.position.set(0, 50, 0);
_this.scene.add(mesh);
})
})
改变模型颜色
模型本型.material.color.set('#DAA569');
TWEEN
npm install @tweenjs/tween.js -S
import TWEEN from "@tweenjs/tween.js";
let mesh = this.tempList[0]
var worldPosition = new THREE.Vector3();
var position = mesh.getWorldPosition(worldPosition)
var positionTarget = new THREE.Vector3(position.x + 1000, position.y + 1000, position.z + 1000);
var pos2 = new THREE.Vector3(orbit.target.x + 500, orbit.target.y + 500, orbit.target.z + 500);
console.log(orbit.target, "SSS")
this.animateCamera(camera2.position, positionTarget, orbit.target, pos2)
animateCamera(current1, target1, current2, target2) {
var tween = new TWEEN.Tween({
x1: current1.x,
y1: current1.y,
z1: current1.z,
x2: current2.x,
y2: current2.y,
z2: current2.z
});
tween.to({
x1: target1.x,
y1: target1.y,
z1: target1.z,
x2: target2.x,
y2: target2.y,
z2: target2.z
}, 1000);
tween.onUpdate(function (e) {
camera2.position.x = e.x1;
camera2.position.y = e.y1;
camera2.position.z = e.z1;
orbit.target.x = e.x2;
orbit.target.y = e.y2;
orbit.target.z = e.z2;
})
tween.easing(TWEEN.Easing.Cubic.InOut);
console.log(tween)
tween.start();
},
startScale(mesh) {
const r = {scale: mesh.scale.x}
var s1 = new TWEEN.Tween(r);
s1.to({scale: 30,}, 1000)
s1.easing(TWEEN.Easing.Sinusoidal.InOut).repeat(Infinity)
s1.onUpdate(function (e) {
mesh.scale.set(e.scale, e.scale, e.scale)
});
s1.onStop(function (e) {
mesh.scale.set(10, 10, 10)
})
s1.start();
}
模型光晕效果
import {FXAAShader} from "three/examples/jsm/shaders/FXAAShader";
import {EffectComposer} from "three/examples/jsm/postprocessing/EffectComposer";
import {RenderPass} from "three/examples/jsm/postprocessing/RenderPass";
import {OutlinePass} from "three/examples/jsm/postprocessing/OutlinePass";
import {ShaderPass} from "three/examples/jsm/postprocessing/ShaderPass";
modelLight(selectedObjects) {
this.composer = new EffectComposer(renderer2)
var renderPass = new RenderPass(scene2, camera2)
this.composer.addPass(renderPass);
var w = this.$refs['threeContainerRef'].offsetWidth
var h = this.$refs['threeContainerRef'].offsetHeight
var outlinePass = new OutlinePass(new THREE.Vector2(w, h), scene2, camera2,
selectedObjects)
outlinePass.selectedObjects = selectedObjects
outlinePass.edgeStrength = 15.0
outlinePass.edgeGlow = 1
outlinePass.usePatternTexture = false
outlinePass.edgeThickness = 1.0
outlinePass.downSampleRatio = 1
outlinePass.pulsePeriod = 1
outlinePass.visibleEdgeColor = new THREE.Color(0, 0, 255)
outlinePass.hiddenEdgeColor = new THREE.Color(0, 0, 0)
outlinePass.clear = true
this.composer.addPass(outlinePass)
var effectFXAA = new ShaderPass(FXAAShader)
effectFXAA.uniforms.resolution.value.set(1 / window.innerWidth, 1 / window.innerHeight)
effectFXAA.renderToScreen = true
this.composer.addPass(effectFXAA)
},
if (_this.composer) _this.composer.render()