自学内容网 自学内容网

学习threejs,添加环境光和点光源

👨‍⚕️ 主页: gis分享者
👨‍⚕️ 感谢各位大佬 点赞👍 收藏⭐ 留言📝 加关注✅!
👨‍⚕️ 收录于专栏:threejs gis工程师



一、🍀前言

本文详细介绍如何基于threejs在三维场景中添加环境光和点光源,亲测可用。希望能帮助到您。一起学习,加油!加油!

二、🍀绘制任意字体模型

1. ☘️实现思路

  • 1、初始化renderer渲染器
  • 2、初始化Scene三维场景
  • 3、初始化camera相机,定义相机位置 camera.position.set,设置相机方向camera.lookAt
  • 4、初始化THREE.AmbientLight环境光,scene场景加入环境光,初始化THREE.PointLight点光源,设置点光源位置,设置设置投掷阴影为true,scene添加点光源
  • 5、初始化gui工具,定义控制灯光和立方体模型位置的函数
  • 6、加载几何模型:创建BoxGeometry立方体、PlaneGeometry地面几何体以及AxisHelper辅助工具坐标系,Scene场景加入以上几何体和工具。
  • 7、加入controls控制,加入stats监控器,监控帧数信息

2. ☘️代码样例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>learn12(添加环境光THREE.AmbientLight和点光源THREE.PointLight)</title>
    <script src="lib/threejs/127/three.js-master/build/three.js"></script>
    <script src="lib/threejs/127/three.js-master/examples/js/controls/OrbitControls.js"></script>
    <script src="lib/threejs/127/three.js-master/examples/js/libs/stats.min.js"></script>
    <script src="lib/threejs/127/three.js-master/examples/js/libs/dat.gui.min.js"></script>
</head>
<style>
    body {
        margin: 0;
    }

    canvas {
        width: 100%;
        height: 100%;
        display: block;
    }
</style>
<body onload="draw()">
</body>
<script>
  var renderer
  var initRender = () => {
    renderer = new THREE.WebGLRenderer({antialias: true})
    renderer.setSize(window.innerWidth, window.innerHeight)
    renderer.shadowMap.enabled = true
    renderer.shadowMap.type = THREE.PCFSoftShadowMap
    document.body.appendChild(renderer.domElement)
  }
  var camera
  var initCamera = () => {
    camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000)
    camera.position.set(0, 40, 100)
    camera.lookAt(new THREE.Vector3(0, 0, 0))
  }
  var scene
  var initScene = () => {
    scene = new THREE.Scene()
  }
  var ambientLight, pointLight
  var initLight = () => {
    ambientLight = new THREE.AmbientLight('#111111')
    scene.add(ambientLight)

    pointLight = new THREE.PointLight('#ffffff')
    pointLight.position.set(15, 30, 10)

    pointLight.castShadow = true
    scene.add(pointLight)
  }
  var gui
  var initGui = () => {
    gui = {
      ambientLight: '#111111', //环境光源
      pointLight: '#ffffff', //点光源
      lightY: 30, //灯光y轴的位置
      distance: 0, //点光源距离
      intensity: 1, //灯光强度
      visible: true //是否可见
    }
    var datGui = new dat.GUI()
    datGui.addColor(gui, 'ambientLight').onChange(e => {
      ambientLight.color = new THREE.Color(e)
    })
    datGui.addColor(gui, 'pointLight').onChange(e => {
      pointLight.color = new THREE.Color(e)
    })
    datGui.add(gui, "lightY", 0, 100)
    datGui.add(gui, 'distance', 0, 100).onChange(function (e) {
      pointLight.distance = e
    })
    datGui.add(gui, 'intensity', 0, 5).onChange(function (e) {
      pointLight.intensity = e
    })
    datGui.add(gui, 'visible').onChange(function (e) {
      pointLight.visible = e
    })
  }
  var cube
  var initModel = () => {
    var helper = new THREE.AxesHelper(10)
    scene.add(helper)

    var cubeGeometry = new THREE.BoxGeometry(10, 10, 10)
    var cubeMaterial = new THREE.MeshLambertMaterial({color: 0x00ffff})
    cube = new THREE.Mesh(cubeGeometry, cubeMaterial)
    cube.position.set(5, 5, -5)
    // cube.position.x = 5
    // cube.position.y = 5
    // cube.position.z = -5
    cube.castShadow = true
    scene.add(cube)

    var planeGeometry = new THREE.PlaneGeometry(100, 100)
    var planeMaterial = new THREE.MeshLambertMaterial({color: 0xaaaaaa})

    var plane = new THREE.Mesh(planeGeometry, planeMaterial)
    plane.rotation.x = -0.5 * Math.PI
    plane.position.y = -0

    plane.receiveShadow = true
    scene.add(plane)
  }

  var stats
  var initStats = () => {
    stats = new Stats()
    document.body.appendChild(stats.dom)
  }
  var controls
  var initControls = () => {
    controls = new THREE.OrbitControls(camera, renderer.domElement)
    // 是否有阻尼或惯性
    controls.enableDamping = true
  }
  // 进行渲染
  var render = () => {
    renderer.render(scene, camera)
  }
  var onWindowResize = () => {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    render()
    renderer.setSize(window.innerWidth, window.innerHeight)
  }

  var animate = () => {
    render()
    stats.update()
    pointLight.position.y = gui.lightY
    controls.update()
    requestAnimationFrame(animate)
  }

  var draw = () => {
    initGui()
    initRender()
    initScene()
    initCamera()
    initLight()
    initModel()
    initControls()
    initStats()

    animate()

    window.onresize = onWindowResize
  }
</script>
</html>

效果如下:
在这里插入图片描述


原文地址:https://blog.csdn.net/qq_28419035/article/details/142564681

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!