three.js+vue3点光源不生效的另外一个可能

问题

在尝试3D项目的时候,明明按照教程一步步来的,可是点光源就是不生效,模型就是黑色。

搜索一番都是说改模型材质。MeshLambertMaterial 早就改成这个材质了,还是不行。

还有说是光源位置问题,也尝试过移动位置,但是不起作用。

解决:

后来发现,我安装的three.js版本:^0.160.0,目前的最新版,于是尝试降级,我选择了

^0.150.1 版本,模型瞬间就有光了!

demo:

最后附上demo源码:

<template>
  <div class="contain" ref="containerRef"></div>
</template>

<script setup lang='ts'>
import * as THREE from 'three'
import { ref, reactive, onMounted } from 'vue'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

const containerRef = ref<HTMLDivElement>()
// 创建场景
const scene = new THREE.Scene()
// 创建照相机
const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000)
// 设置摄像机位置
camera.position.set(-30, 40, 30)
// 设置摄像机朝向(看哪)
camera.lookAt(scene.position)
// webgl渲染器
const renderer = new THREE.WebGLRenderer()
renderer.setClearColor(new THREE.Color(0xeeeeee))
renderer.setSize(window.innerWidth, window.innerHeight)
// 可以渲染阴影
renderer.shadowMap.enabled = true

// 光源
function point() {
  let point = new THREE.SpotLight(0xffffff) //设置灯光的颜色
  point.position.set(80, 100, 80) //点光源位置
  point.angle = Math.PI / 10 //设置投影的程度
  point.shadow.mapSize.set(1024, 1024)
  scene.add(point)
  point.castShadow = true //灯光投影
}

// 地面
function plane() {
  // 添加地面
  const planeGeometry = new THREE.PlaneGeometry(60, 20)
  // 添加材质
  const meshBasicMaterial = new THREE.MeshLambertMaterial({ color: 0x999999 })
  const plane = new THREE.Mesh(planeGeometry, meshBasicMaterial)
  // 接受阴影
  plane.receiveShadow = true
  // 调整位置
  plane.rotation.x = -0.5 * Math.PI //放平
  plane.position.x = 15
  plane.position.y = 0
  plane.position.z = 0
  scene.add(plane)
}

// 立方体
function cube() {
  // 添加几何体
  const cubeGeometry = new THREE.BoxGeometry(4, 4, 4)
  const cubeMaterial = new THREE.MeshLambertMaterial({ color: 0xff0000 })
  const cube = new THREE.Mesh(cubeGeometry, cubeMaterial)
  cube.position.set(2, 2, 2)
  scene.add(cube)
  cube.castShadow = true
}

// 球
function sphere() {
  // 球体
  const sphereGeometry = new THREE.SphereGeometry(4)
  const sphereMaterial = new THREE.MeshLambertMaterial({ color: 0x7777ff, wireframe: true })
  const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial)
  sphere.castShadow = true
  sphere.position.x = 20
  sphere.position.y = 4
  sphere.position.z = 0
  scene.add(sphere)
}

// 球
function sphere2() {
  // 球体
  const sphereGeometry = new THREE.SphereGeometry(2)
  const sphereMaterial = new THREE.MeshLambertMaterial({ color: 0x7777ff })
  const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial)
  //sphere.translateX(10)
  /* sphere.position.x = 10
  sphere.position.y = 2
  sphere.position.z = 0 */
  sphere.position.set(10, 2, 2)
  sphere.castShadow = true
  scene.add(sphere)
}

// 灯泡
function lookAtGeom() {
  // 添加圆球
  const lookAtGeom = new THREE.SphereGeometry(0.5)
  const lookAtMesh = new THREE.Mesh(
    lookAtGeom,
    new THREE.MeshBasicMaterial({
      color: 'red'
    })
  )
  lookAtMesh.position.set(10, 10, -10)
  scene.add(lookAtMesh)
  // 添加点光源
  const pointLight = new THREE.PointLight('red')
  pointLight.distance = 100
  pointLight.position.copy(lookAtMesh.position)
  scene.add(pointLight)
}

plane()
cube()
sphere()
point()
sphere2()
lookAtGeom()

// 环境光
const light = new THREE.AmbientLight(0xffffff, 0.1)
light.position.set(0, 0, 0)
scene.add(light)

// 添加坐标系
const axes = new THREE.AxesHelper(20)
scene.add(axes)

let animloop = () => {
  render()
  const controls = new OrbitControls(camera, renderer.domElement) //创建控件对象
  controls.addEventListener('change', render) //监听鼠标、键盘事件
}
let render = () => {
  renderer.render(scene, camera) //执行渲染操作
}
animloop()

onMounted(() => {
  containerRef.value?.appendChild(renderer.domElement)
  renderer.render(scene, camera)
})
</script>

### 如何在 Vue 中集成和使用 Three.js 为了在基于 Vue 的项目中集成并有效使用 Three.js,可以按照以下方法操作: #### 安装依赖项 首先,在 Vue 项目环境中安装 Three.js 库。可以通过 npm 或 yarn 来完成此过程: ```bash npm install three --save ``` 或者如果更倾向于使用 yarn,则运行如下命令: ```bash yarn add three ``` 这一步骤确保了 Three.js 成功引入到项目的 `node_modules` 文件夹中[^1]。 #### 创建组件结构 创建一个新的 Vue 组件来封装 Three.js 渲染逻辑。例如,命名为 `ThreeScene.vue` 并设置其基本模板结构: ```vue <template> <div ref="threeContainer" class="three-container"></div> </template> <script> import * as THREE from &#39;three&#39;; export default { name: &#39;ThreeScene&#39;, data() { return { scene: null, camera: null, renderer: null, mesh: null, }; }, mounted() { this.init(); this.animate(); }, methods: { init() { const container = this.$refs.threeContainer; // 初始化场景 this.scene = new THREE.Scene(); // 设置相机参数 this.camera = new THREE.PerspectiveCamera( 75, container.clientWidth / container.clientHeight, 0.1, 1000 ); // 配置渲染器 this.renderer = new THREE.WebGLRenderer({ antialias: true }); this.renderer.setSize(container.clientWidth, container.clientHeight); container.appendChild(this.renderer.domElement); // 添加几何体与材质 const geometry = new THREE.BoxGeometry(1, 1, 1); const material = new THREE.MeshBasicMaterial({ color: &#39;#ff8c00&#39; }); this.mesh = new THREE.Mesh(geometry, material); this.scene.add(this.mesh); // 调整相机位置 this.camera.position.z = 5; }, animate() { requestAnimationFrame(this.animate.bind(this)); this.mesh.rotation.x += 0.01; this.mesh.rotation.y += 0.01; this.render(); }, render() { this.renderer.render(this.scene, this.camera); } } }; </script> <style scoped> .three-container { width: 100%; height: 100vh; /* 占满整个视口高度 */ } </style> ``` 上述代码展示了如何初始化一个简单的三维场景,并通过旋转立方体实现动态效果。注意这里的关键部分在于正确配置 WebGL 渲染器以及绑定动画帧回调函数[^2]。 #### 使用自定义组件 最后,在应用的主要布局文件(如 App.vue)或其他父级组件中导入刚刚创建好的 `ThreeScene` 组件即可: ```vue <template> <div id="app"> <h1>Vue & Three.js Integration Example</h1> <ThreeScene /> </div> </template> <script> import ThreeScene from &#39;./components/ThreeScene.vue&#39;; export default { components: { ThreeScene } }; </script> ``` 这样就完成了基础的 VueThree.js 结合案例。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值