Three.js之相机、渲染器、光源、动画、性能监测

这篇具有很好参考价值的文章主要介绍了Three.js之相机、渲染器、光源、动画、性能监测。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

参考资料

  • 第一个3D案例—透视投影相机
  • 第一个3D案例—渲染器
  • Canvas画布布局和全屏

知识点

  • 透视投影相机PerspectiveCamera
  • WebGL渲染器WebGLRenderer
  • 辅助观察坐标系AxesHelper
  • 漫反射网格材质MeshLambertMaterial
  • 点光源PointLight
  • 点光源辅助观察PointLightHelper
  • 环境光AmbientLight
  • 平行光DirectionalLight
  • 平行光辅助观察DirectionalLightHelper
  • 相机控件OrbitControls
  • 请求动画帧window.requestAnimationFrame
  • canvas画布宽高度动态变化
  • 性能监控Stats

代码实现

  • 相机、渲染器、光

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Three.js</title>
    </head>
      <body>
      </body>
      <!-- 具体路径配置,你根据自己文件目录设置,我的是课件中源码形式 -->
      <script type="importmap">
        {
          "imports": {
            "three": "./js/three.module.js",
            "three/addons/": "../three.js/examples/jsm/"
          }
        }
      </script>
      <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
    
        const width = 800
        const height = 500
    
        // 场景
        const scene = new THREE.Scene();
        // 几何体
        const geometry = new THREE.BoxGeometry(100, 100, 100);
        // 材质 
        // MeshBasicMaterial:不受光
        // MeshLambertMaterial:受光
        const material = new THREE.MeshLambertMaterial({
          color:0x0000ff,
        });
        // 网格模型:物体
        const mesh = new THREE.Mesh(geometry, material);
        mesh.position.set(0, 0, 0);
        scene.add(mesh);
    
        // 坐标系
        const axes = new THREE.AxesHelper(200);
        scene.add(axes);
    
        // // 点光源
        // const pointLight = new THREE.PointLight( 0xffffff, 1.0, 0, 0);
        // pointLight.position.set(-200, 200, 200 );
        // scene.add( pointLight );
    
        // // 辅助点光源
        // const pointLightHelper = new THREE.PointLightHelper( pointLight, 10 );
        // scene.add( pointLightHelper );
    
        // 环境光
        const ambientLight = new THREE.AmbientLight( 0xffffff, 0.2);
        scene.add( ambientLight );
    
        // 平行光
        const directionalLight = new THREE.DirectionalLight( 0xffffff, 1, 0, 0);
        // directionalLight.position.set(100, 0, 0);
        // directionalLight.position.set(0, 100, 0);
        // directionalLight.position.set(100, 100, 100);
        directionalLight.position.set(100, 60, 50);
        directionalLight.target = mesh;
        scene.add( directionalLight );
    
        // 辅助平行光
        const directionalLightHelper = new THREE.DirectionalLightHelper( directionalLight, 10 );
        scene.add( directionalLightHelper );
    
        // 相机
        const camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000);
        camera.position.set(200, 200, 200);
        camera.lookAt(scene.position);
    
        // 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(width, height);
        renderer.render(scene, camera);
        document.body.appendChild(renderer.domElement);
    
        // 控制器
        const controls = new OrbitControls(camera, renderer.domElement);
        controls.addEventListener('change', () => {
          renderer.render(scene, camera);
        });
      </script>
    </html>
    
  • 动画

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Three.js</title>
    </head>
      <body>
      </body>
      <!-- 具体路径配置,你根据自己文件目录设置,我的是课件中源码形式 -->
      <script type="importmap">
        {
          "imports": {
            "three": "./js/three.module.js",
            "three/addons/": "../three.js/examples/jsm/"
          }
        }
      </script>
      <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
    
        const width = 800
        const height = 500
    
        // 场景
        const scene = new THREE.Scene();
        // 几何体
        const geometry = new THREE.BoxGeometry(100, 100, 100);
        // 材质
        const material = new THREE.MeshBasicMaterial({
          color: 0x00ff00,
          transparent: true,
          opacity: 0.5
        });
        // 网格模型:物体
        const mesh = new THREE.Mesh(geometry, material);
        mesh.position.set(0, 0, 0);
        scene.add(mesh);
    
        // 坐标系
        const axes = new THREE.AxesHelper(200);
        scene.add(axes);
    
        // 相机
        const camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000);
        camera.position.set(200, 200, 200);
        camera.lookAt(scene.position);
    
        // 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(width, height);
        renderer.render(scene, camera);
        document.body.appendChild(renderer.domElement);
    
        // 动画渲染
        // 跟踪时间
        var clock = new THREE.Clock();
        function render() {
          const spt = clock.getDelta() * 1000;
          console.log('🚀 ~ file: animation.html:59 ~ render ~ spt:', spt)
          requestAnimationFrame(render);
          mesh.rotation.y += 0.01;
          renderer.render(scene, camera);
        }
    
        render();
    
        // 控制器
        const controls = new OrbitControls(camera, renderer.domElement);
        controls.addEventListener('change', () => {
          // 因为动画渲染了,所以这里可以省略
          // renderer.render(scene, camera);
        });
      </script>
    </html>
    
  • 画布动态变化

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Three.js</title>
      <style>
        body {
          margin: 0;
          overflow: hidden;
        }
      </style>
    </head>
      <body>
      </body>
      <!-- 具体路径配置,你根据自己文件目录设置,我的是课件中源码形式 -->
      <script type="importmap">
        {
          "imports": {
            "three": "./js/three.module.js"
          }
        }
      </script>
      <script type="module">
        import * as THREE from 'three';
    
        let width = window.innerWidth;
        let height = window.innerHeight;
    
        // 场景
        const scene = new THREE.Scene();
        // 几何体
        const geometry = new THREE.BoxGeometry(100, 100, 100);
        // 材质
        const material = new THREE.MeshBasicMaterial({
          color: 0x00ff00,
          transparent: true,
          opacity: 0.5
        });
        // 网格模型:物体
        const mesh = new THREE.Mesh(geometry, material);
        mesh.position.set(0, 0, 0);
        scene.add(mesh);
    
        // 坐标系
        const axes = new THREE.AxesHelper(200);
        scene.add(axes);
    
        // 相机
        const camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000);
        camera.position.set(200, 200, 200);
        camera.lookAt(scene.position);
    
        // 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(width, height);
        renderer.render(scene, camera);
        document.body.appendChild(renderer.domElement);
    
        // 填满浏览器
        window.onresize = function () {
          width = window.innerWidth;
          height = window.innerHeight;
          renderer.setSize(width, height);
          camera.aspect = width / height;
          camera.updateProjectionMatrix();
        }
      </script>
    </html>
    
  • 性能监控文章来源地址https://www.toymoban.com/news/detail-645771.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Three.js</title>
    </head>
      <body>
      </body>
      <!-- 具体路径配置,你根据自己文件目录设置,我的是课件中源码形式 -->
      <script type="importmap">
        {
          "imports": {
            "three": "./js/three.module.js",
            "three/addons/": "../three.js/examples/jsm/"
          }
        }
      </script>
      <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
        import Stats from 'three/addons/libs/stats.module.js';
    
        const width = 800
        const height = 500
    
        // 场景
        const scene = new THREE.Scene();
        // 几何体
        const geometry = new THREE.BoxGeometry(100, 100, 100);
        // 材质
        const material = new THREE.MeshBasicMaterial({
          color: 0x00ff00,
          transparent: true,
          opacity: 0.5
        });
        // 网格模型:物体
        const mesh = new THREE.Mesh(geometry, material);
        mesh.position.set(0, 0, 0);
        scene.add(mesh);
    
        for (let i = 0; i < 1000; i++) {
          // 几何体
          const geometry = new THREE.BoxGeometry(5, 5, 5);
          // 材质
          const material = new THREE.MeshBasicMaterial({
            color: 0x00ff00,
            transparent: true,
            opacity: 0.5
          });
          // 网格模型:物体
          const mesh = new THREE.Mesh(geometry, material);
          mesh.position.set((Math.random() - 0.5) * 200, (Math.random() - 0.5) * 200, (Math.random() - 0.5) * 200);
          scene.add(mesh);
        }
    
        // 坐标系
        const axes = new THREE.AxesHelper(200);
        scene.add(axes);
    
        // 相机
        const camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000);
        camera.position.set(200, 200, 200);
        camera.lookAt(scene.position);
    
        // 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(width, height);
        renderer.render(scene, camera);
        document.body.appendChild(renderer.domElement);
    
        // 性能监控
        const stats = new Stats()
        document.body.appendChild(stats.domElement);
    
        // 动画渲染
        // 跟踪时间
        var clock = new THREE.Clock();
        function render() {
          stats.update()
          const spt = clock.getDelta() * 1000;
          requestAnimationFrame(render);
          mesh.rotation.y += 0.01;
          renderer.render(scene, camera);
        }
    
        render();
      </script>
    </html>
    

到了这里,关于Three.js之相机、渲染器、光源、动画、性能监测的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Three.js——十五、Box3、相机动画、lookAt()视线方向、管道漫游案例、OrbitControls旋转缩放限制、以及相机控件MapControls

    正投影相机和透视相机的区别 如果都以高处俯视去看整个场景,正投影相机就类似于 2d 的可视化的效果,透视相机就类似于人眼观察效果 调整 left, right, top, bottom 范围大小 如果你想整体预览全部立方体,就需要调整相机的渲染范围,比如设置上下左右的范围。 使用场景:正

    2024年02月04日
    浏览(28)
  • Web3D开发经验分享:基于Three.js的Web3D建模案例

    个人主页: 左本Web3D,更多案例预览请点击==》 在线案例 个人简介:专注Web3D使用ThreeJS实现3D效果技巧和学习案例 💕 💕积跬步以至千里,致敬每个爱学习的你。喜欢的话请三连,有问题请私信或者加微信         随着互联网的快速发展,Web3D技术也越来越成熟,越来越

    2024年02月13日
    浏览(33)
  • WEB 3D技术 three.js 点光源

    本文的话 我们来设置一下点光源 点光源其实最直观的就是可以做萤火虫 也可以做星光 点点的效果 我们可以直接在官网中搜索 Pointlight 大家可以在官网这里看一下 其实 SpotLight 聚关灯中的属性 Pointlight 点光源也有的 我们先编写代码如下 PointLight 创建一个点光源 这里我们给了

    2024年01月19日
    浏览(31)
  • Three.js--》理解光源对物体产生影响的重要性

    上篇文章 前端开发者掌握3d技术不再是梦,初识threejs 作为three.js入门篇讲解了许多内容但是没有深入了解其原理以及实现方法,仅仅只是展示了实现的内容及代码,本篇文章将深入讲解实现效果其背后用到的知识与原理。 目录 使用相机控件轨道控制器 理解光源影响 环境光

    2024年02月03日
    浏览(77)
  • three.js(三):three.js的渲染结构

    概述 three.js 封装了场景、灯光、阴影、材质、纹理和三维算法,不必在直接用WebGL 开发项目,但有的时候会间接用到WebGL,比如自定义着色器。 three.js 在渲染三维场景时,需要创建很多对象,并将它们关联在一起。 下图便是一个基本的three.js 渲染结构 Renderer 渲染器 Renderer

    2024年02月11日
    浏览(25)
  • three.js进阶之动画系统

    我曾在three.js进阶之骨骼绑定文章中提到了AnimationMixer、AnimationAction等内容,其实这些应该属于Three.js的动画系统,本文就系统的介绍一下动画系统(Animation System)。 一般情况下,我们很少会使用three.js的动画系统去手动创建动画——因为这真的很麻烦,更高效便捷的做法还是

    2024年02月02日
    浏览(27)
  • three.js 汽车行驶动画效果

    实现原理是使用TWEEN.Tween实现动画效果 使用Promise编写模型的异步加载方法 参数position是汽车初始位置,参数rotation是汽车初始朝向 调用: 第1个参数是汽车初始位置,第2个参数表示汽车初始朝向:西 参数start是行驶起点位置,参数end是行驶终点位置,参数speed是速度 this.mod

    2024年02月05日
    浏览(32)
  • Three.js初识:渲染立方体、3d字体、修改渲染背景颜色

    用场景对three.js进行渲染:场景、相机、渲染器 场景 透视摄影机 参数解析: fov: 视野角度(FOV)。视野角度就是无论在什么时候,你所能在显示器上看到的场景的范围,它的单位是角度(与弧度区分开)。 aspect: 长宽比(aspect ratio)。 也就是你用一个物体的宽除以它的高的值

    2024年02月07日
    浏览(31)
  • 基于Three.js的WebXR渲染入门

    我不会花太多时间讨论 Three.JS 渲染管道的工作原理,因为它在互联网上有详细记录(例如,此链接)。 我将在下图中列出基础知识,以便更容易理解各个部分的去向。 在我们深入了解 WebXR API 本身之前,您应该知道 WebXR 设备 API Polyfill 可通过两种主要方式帮助开发人员: 如

    2024年02月11日
    浏览(30)
  • 【Three.js】渲染模型卡顿的优化办法

    优化方法是根据chatGPT的回答下,我这里记录一下,有的方法进行了尝试,有的还没有。 可以通过减少面数来优化,也可以使用 LOD技术(Level of Detail) 在不同距离下使用不同的模型细节来优化。 使用LOD技术可以在不同距离下使用不同的模型细节来优化three.js渲染性能,下面是

    2024年02月05日
    浏览(22)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包