• threejs的阴影


    案例说明:

    第一:定义一个平面用来接收阴影的

    第二:定义一个物体用来产生阴影的

    第三:

    代码

    1. //添加平行光
    2. const directionalLight = new THREE.DirectionalLight(0xffffff)
    3. directionalLight.position.set(1, 1, 1) //设置光的位置
    4. directionalLight.castShadow = true //********
    5. directionalLight.shadow.mapSize.width = 1024 //默认是512
    6. directionalLight.shadow.mapSize.height = 1024 //默认是512
    7. scene.add(directionalLight)//把平行光添加进场景
    8. //定义一个平面(用来接收阴影)
    9. const planeG = new THREE.PlaneGeometry(4, 4)
    10. const planeM = new THREE.MeshStanderdMaterial({ color: 0xcccccc })
    11. const plane = new THREE.Mesh(planeG, planeM)
    12. plane.rotation.x = -0.5 * Math.PI //让平面旋转90度
    13. plane.receiveShadow = true //接收阴影 //********
    14. scene.add(plane) //添加场景
    15. //定义一个球体(被照物体)
    16. const sphereG = new THREE.SphereGeometry(0.5)//定义一个球半径为0.5
    17. const sphereM = new THREE.MeshStandardMaterial({
    18. color: 0xffff00,
    19. })
    20. const sphere = new THREE.Mesh(sphereG, sphereM)
    21. sphere.position .y = 0.5
    22. sphere.castShadow = true //光线照在物体上产生阴影 //********
    23. scene.add(sphere)
    24. //渲染阴影
    25. renderer.shadowMap.enabled = true //渲染阴影 //********

    效果图:

    这个的阴影比较粗糙

    需要光滑就要给光设置

    directionalLight.shadow.mapSize.width = 1024 //默认是512 
    directionalLight.shadow.mapSize.height = 1024 //默认是512

    让球动起来

    1. const clock = new THREE.Clock()
    2. tick()
    3. function tick() {
    4. const time = clock.getElapsedTime()
    5. //Math.sin(time) 取值范围是-1到1之间
    6. //Math.abs()是取绝对值
    7. sphere.position.y = Math.abs(Math.sin(time))
    8. requestAnimationFrame(tick)
    9. renderer.render(scene, camere)
    10. }

  • 相关阅读:
    Hudi核心概念二:表和查询类型
    JavaScript奇淫技巧:20行代码,实现屏幕录像
    含泪整理Redis相关面试题大全
    图书馆防盗的窍门
    Git操作流程
    基于IDEA进行Maven工程构建
    最新前端vue项目打包放到gitHub上部署步骤
    Go vs Java vs C# 语法对比
    Sysweld笔记:利用稳态算法加速算法模拟焊接过程的残余应力
    基于Python的Django开发接口框架搭建
  • 原文地址:https://blog.csdn.net/qq_52421092/article/details/126906248