关于three.js中的矩阵更新

简介: 关于three.js中的矩阵更新

关于three.js中的矩阵更新

目录

1. 概述

使用如下代码绘制一个面:

'use strict';
function init() {
    //console.log("Using Three.js version: " + THREE.REVISION);   
    // create a scene, that will hold all our elements such as objects, cameras and lights.
    var scene = new THREE.Scene();
    // create a camera, which defines where we're looking at.
    var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
    // position and point the camera to the center of the scene
    camera.position.set(0, 0, 100);   //相机的位置
    camera.up.set(0, 1, 0);         //相机以哪个方向为上方
    camera.lookAt(new THREE.Vector3(1, 2, 3));          //相机看向哪个坐标。
    console.log(camera.matrixWorldInverse);
 
    // create a render and set the size
    var renderer = new THREE.WebGLRenderer();
    renderer.setClearColor(new THREE.Color(0x000000));
    renderer.setSize(window.innerWidth, window.innerHeight);
    // add the output of the renderer to the html element
    document.getElementById("webgl-output").appendChild(renderer.domElement);
    
    // create the ground plane
    var planeGeometry = new THREE.PlaneGeometry(60, 20);
    var planeMaterial = new THREE.MeshBasicMaterial({
        color: 0xAAAAAA
    });
    var plane = new THREE.Mesh(planeGeometry, planeMaterial);
    // add the plane to the scene
    scene.add(plane);
    // rotate and position the plane    
    plane.position.set(15, 8, -10);
    plane.rotation.x = THREE.Math.degToRad(30);
    plane.rotation.y = THREE.Math.degToRad(45);
    plane.rotation.z = THREE.Math.degToRad(60);
    console.log(plane.matrixWorld);
 
    renderer.render(scene, camera);
}

打印输出的视图矩阵和模型矩阵如下:

而去掉最后的渲染语句:

renderer.render(scene, camera);

之后,打印输出的视图矩阵和模型矩阵如下:

可以发现两者的输出结果并不一致,这其实涉及到three.js中矩阵更新的问题。

2. 详解

three.js中的Mesh和Camera都继承自Object3D,Object3D提供了更新图形矩阵的接口:

在分别设置Mesh和camera的图形变换参数之后,需要调用updateMatrixWorld()才能保证图形矩阵正确:

camera.updateMatrixWorld(true);
plane.updateMatrixWorld(true);

但是在调用renderer.render之后,three.js就会使得矩阵自动进行更新。所以除非必要,模型矩阵和视图矩阵可以不用显示更新。而console.log是异步操作,所以会出现打印信息是正常的现象。如果是单步调式模式,如果不调用updateMatrixWorld(),显示的就会是初始化的矩阵信息。

除此之外,Camera的投影矩阵也值得注意。PerspectiveCamera提供了更新投影矩阵的接口:

文档很明确的说明了,在改变Camera的投影参数之后,必须调用一次updateProjectionMatrix才能使Camera的效果生效。

分类: three.js

标签: 矩阵变换 , three.js



相关文章
|
2月前
|
存储 JavaScript vr&ar
three.js中的矩阵变换(模型视图投影变换)
three.js中的矩阵变换(模型视图投影变换)
62 1
|
5月前
|
机器学习/深度学习 JavaScript Python
GEE机器学习——混淆矩阵Classifier.confusionMatrix()和errorMatrix()和exlain()的用法(js和python代码)
GEE机器学习——混淆矩阵Classifier.confusionMatrix()和errorMatrix()和exlain()的用法(js和python代码)
165 0
|
机器学习/深度学习
Three.js-任意平面的镜像矩阵
1. 什么是镜像变换 直接看下面这张图: 这张图很好的诠释了镜像变化,关于y轴的变化,关于x轴的变化。这种关于任意轴的变化,就是镜像了。 2d下的镜像矩阵变化 我们以图像关于Y轴镜像为例子:原图形和结果图形上所有点的都存在的关系就应该是  x = -x,  也就是都只有x发生变化。这种通用的变化其实可以用矩阵表示,2D空间中的点其实可以用[x,y ] 表示。对角线的两个1就是关于那个轴对称: 这些都是关于x轴、 y轴的对称, 如果说关于2d平面的任意一条直线呢,当然有人已经帮我们推导出来了如下图:(数学证明我就不给出了,有兴趣的可以自行百度,本篇文章注重3d镜像矩证的推导) 3d 图形下关
Three.js-任意平面的镜像矩阵
|
JavaScript
剑指Offer——顺时针打印矩阵(JS实现)
剑指Offer——顺时针打印矩阵(JS实现)
302 0
剑指Offer——顺时针打印矩阵(JS实现)
|
JavaScript
剑指Offer——矩阵中的路径(JS实现)
剑指Offer——矩阵中的路径(JS实现)
268 0
剑指Offer——矩阵中的路径(JS实现)
|
容器
【D3.js 学习总结】16、D3布局-矩阵图
# d3.layout.treemap() ![](https://img.alicdn.com/tps/TB1y7ZDKVXXXXXDXpXXXXXXXXXX-500-313.png) #### 矩阵图(Treemap)的API说明 * treemap.children - 取得或设置孩子访问器。 * treemap.links - 计算树节点中的父子链接。 * treem
2340 0
|
3月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的客户关系管理系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的客户关系管理系统附带文章源码部署视频讲解等
83 2
|
3月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的小区物流配送系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的小区物流配送系统附带文章源码部署视频讲解等
102 4
|
3月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的宠物援助平台附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的宠物援助平台附带文章源码部署视频讲解等
76 4
|
3月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的宠物交易平台附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的宠物交易平台附带文章源码部署视频讲解等
66 4