vue3+three完整代码如下:
<template> </template> <script setup> import * as THREE from 'three'; import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js" import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js" import { OutlinePass } from "three/examples/jsm/postprocessing/OutlinePass.js" import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass.js" import { FXAAShader } from "three/examples/jsm/shaders/FXAAShader.js" import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'; const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); const renderer = new THREE.WebGLRenderer(); renderer.setSize( window.innerWidth, window.innerHeight ); document.body.appendChild( renderer.domElement ); const geometry = new THREE.BoxGeometry(); const material = new THREE.MeshBasicMaterial( { color: 0xffffff } ); const cube = new THREE.Mesh( geometry, material ); const cube2 = cube.clone() cube2.position.set(2,2,1) scene.add( cube ); scene.add( cube2 ); console.log(scene, 'scene') camera.position.z = 5; // 轨道控制器 const controls = new OrbitControls( camera, renderer.domElement ); // 后处理效果代码 let composer = null let outlinePass = null let renderPass = null //高亮显示模型(呼吸灯) function outlineObj (selectedObjects) { // 创建一个EffectComposer(效果组合器)对象,然后在该对象上添加后期处理通道。 composer = new EffectComposer(renderer) // 新建一个场景通道 为了覆盖到原理来的场景上 renderPass = new RenderPass(scene, camera) composer.addPass(renderPass); // 物体边缘发光通道 outlinePass = new OutlinePass(new THREE.Vector2(window.innerWidth, window.innerHeight), scene, camera, selectedObjects) outlinePass.selectedObjects = selectedObjects outlinePass.edgeStrength = 10.0 // 边框的亮度 outlinePass.edgeGlow = 1// 光晕[0,1] outlinePass.usePatternTexture = false // 是否使用父级的材质 outlinePass.edgeThickness = 1.0 // 边框宽度 outlinePass.downSampleRatio = 1 // 边框弯曲度 outlinePass.pulsePeriod = 5 // 呼吸闪烁的速度 outlinePass.visibleEdgeColor.set(parseInt(0x00ff00)) // 呼吸显示的颜色 outlinePass.hiddenEdgeColor = new THREE.Color(0, 0, 0) // 呼吸消失的颜色 outlinePass.clear = true composer.addPass(outlinePass) // 自定义的着色器通道 作为参数 var effectFXAA = new ShaderPass(FXAAShader) effectFXAA.uniforms.resolution.value.set(1 / window.innerWidth, 1 / window.innerHeight) effectFXAA.renderToScreen = true composer.addPass(effectFXAA) } outlineObj([cube]) function animate() { requestAnimationFrame( animate ); renderer.render( scene, camera ); controls.update(); if (composer) { composer.render() } } animate(); </script>