- Three.js 教程
- Three.js - 首页
- Three.js - 简介
- Three.js - 安装
- Three.js - Hello Cube 应用
- Three.js - 渲染器和响应性
- Three.js - 响应式设计
- Three.js - 调试和统计信息
- Three.js - 相机
- Three.js - 控件
- Three.js - 光照与阴影
- Three.js - 几何体
- Three.js - 材质
- Three.js - 纹理
- Three.js - 绘制线条
- Three.js - 动画
- Three.js - 创建文本
- Three.js - 加载 3D 模型
- Three.js - 库和插件
- Three.js 有用资源
- Three.js - 快速指南
- Three.js - 有用资源
- Three.js - 讨论
Three.js - 统计信息
统计信息
统计信息在大型应用中发挥着重要作用。假设您正在创建一个包含许多对象和动画的大型 Three.js 项目。监控代码的性能(如 fps(每秒帧数)、分配的内存等)非常重要。Three.js 的创建者还创建了一个小型 JavaScript 库 Stats.js 来监控渲染。
安装
像任何其他库一样,您可以简单地通过三种方式之一将其添加到您的项目中,如前所述。
您可以从GitHub下载它并将其导入到您的 HTML 页面中。或者,您可以将 CDN 链接添加到 HTML 页面中。
<script src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r17/Stats.min.js"></script>
如果您使用的是节点应用程序,请安装npm 包并将其导入到您的项目中。
npm install stats.js
或者
yarn add stats.js import * as stats from 'stats.js'
功能
您可以使用 Stats.js 监控以下属性。
- FPS - 在上一秒中渲染的帧数 (0)。
- MS - 渲染一帧所需的毫秒数 (1)。
- MB - 已分配内存的兆字节 (2)(使用 --enable-precise-memoryinfo 运行 Chrome)
- 自定义 - 您可以定义要监控的内容 - 用户定义的面板支持 (3)。
它是如何工作的?
如果您正在监控帧速率,它会计算在上一秒内更新被调用的次数并显示该值。如果您正在跟踪渲染时间,它只会显示调用和更新函数之间的时间。
用法
您可以通过几个简单的步骤将此功能添加到您的代码中。
创建 stats 对象并使用 DOM 将其添加到 HTML 页面中。
const stats = new Stats() stats.showPanel(1) // 0: fps, 1: ms, 2: mb, 3+: custom document.body.appendChild(stats.dom)
注意 - 您可以使用 showPanel() 显示您想要的面板。默认情况下,Stats.js 显示 fps 面板,您可以通过单击面板在面板之间切换。
选择要监控的代码。
stats.begin() // monitored code goes here // in our case the render function renderer.render(scene, camera) stats.end()
如果您使用动画,则应在渲染每一帧时更新统计信息。
function animate() {
requestAnimationFrame(render)
// our animations
renderer.render(scene, camera)
stats.update()
}
示例
查看此工作示例。
stats.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Three.js - Stats.js</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: -applesystem, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
html,
body {
height: 100vh;
width: 100vw;
}
#threejs-container {
position: block;
width: 100%;
height: 100%;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.js"></script>
<script src="http://mrdoob.github.io/stats.js/build/stats.min.js"></script>
</head>
<body>
<div id="threejs-container"></div>
<script type="module">
// Adding stats panel to moniter application statistics
const gui = new dat.GUI()
const stats = new Stats()
//stats.showPanel(0)
//stats.showPanel(1)
document.body.appendChild(stats.dom)
// width, height
let width = window.innerWidth
let height = window.innerHeight
// scene
const scene = new THREE.Scene()
scene.background = new THREE.Color(0x262626)
// camera
const camera = new THREE.PerspectiveCamera(30, width / height, 0.1, 100)
camera.position.set(0, 0, 10)
// cube
const geometry = new THREE.BoxGeometry(1, 1, 1)
const material = new THREE.MeshBasicMaterial({
color: 0xffffff,
wireframe: true
})
const cube = new THREE.Mesh(geometry, material)
scene.add(cube)
// responsiveness
window.addEventListener('resize', () => {
width = window.innerWidth
height = window.innerHeight
camera.aspect = width / height
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
renderer.render(scene, camera)
})
// renderer
const renderer = new THREE.WebGL1Renderer()
renderer.setSize(width, height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
// animation
function animate() {
requestAnimationFrame(animate)
cube.rotation.x += 0.005
cube.rotation.y += 0.01
renderer.render(scene, camera)
stats.update()
}
// rendering the scene
const container = document.querySelector('#threejs-container')
container.append(renderer.domElement)
stats.begin()
renderer.render(scene, camera)
stats.end()
animate()
</script>
</body>
</html>
输出
threejs_debug_and_stats.htm
广告