React+Three.js——PerspectiveCamera透视相机camera参数以及属性值探索
因项目问题,对webgl进行了探索,当进行到3d相机时,对camera的up,position属性有部分难以理解的地方,因此做下了记录。
代码如下:
import React, {Component} from 'react';
import * as Three from "three";
const {Vector3} = Three;
let scene, camera, renderer, container, width, height, light;
class Lesson3 extends Component {
initThree = () => {
container = document.getElementById('lesson3map');
width = container.clientWidth;
height = container.clientHeight;
width = width > 1440 ? 1440 : width;
height = height > 600 ? 600 : height;
renderer = new Three.WebGLRenderer({
antialias: true
});
renderer.setSize(width, height);
container.appendChild(renderer.domElement);
renderer.setClearColor(0xffffff, 1.0)
};
initCamera = () => {
camera = new Three.PerspectiveCamera(45, width / height, 1, 10000);
camera.position.set(0, 2000, 0);
// camera.up.set(0, 0, 0);
camera.lookAt(0, 0, 0);
};
initScene = () => {
scene = new Three.Scene();
};
initLight = () => {
light = new Three.DirectionalLight(0xff0000, 1.0, 0);
light.position.set(100, 100, 200);
scene.add(light);
};
initObject = () => {
const geometry = new Three.Geometry();
const p1 = new Vector3(-400, 0, 0);
const p2 = new Vector3(400, 0, 0);
geometry.vertices.push(p1, p2);
for (let i = 0; i < 21; i++) {
const line = new Three.Line(geometry, new Three.LineBasicMaterial({color: 0x000000, opacity: 0.2}));
line.position.z = (i * 40) - 400;
scene.add(line);
const lineV = new Three.Line(geometry, new Three.LineBasicMaterial({color: 0x000000, opacity: 0.2}));
lineV.position.x = (i * 40) - 400;
lineV.rotation.y = 90 * Math.PI / 180;
scene.add(lineV);
}
};
tRender = () => {
renderer.clear();
renderer.render(scene, camera);
requestAnimationFrame(this.tRender);
};
draw = () => {
this.initThree();
this.initCamera();
this.initScene();
this.initLight();
this.initObject();
this.tRender();
};
componentDidMount() {
setTimeout(() => {
this.draw();
});
}
render() {
return (
<div id="lesson3map" style={{width: '100%', height: '100vh'}}/>
)
}
}
export default Lesson3;
initObject:可以看出这是一个在xz二维坐标轴上的20*20方块,x方向为-400到400,z方向也为-400到400。
initCamera:
PerspectiveCamera(fov?: number, aspect?: number, near?: number, far?: number)有四个参数,我做一下简单介绍,详情可自行查阅相关资料。
- fov:眼球张开的角度,0°时相当于闭眼。
- aspect:可视区域横纵比。
- near:眼睛能看到的最近垂直距离。
- far:眼睛能看到的最远垂直距离。
camera.position.set(0, 2000, 0);
// camera.up.set(0, 0, 0);
camera.lookAt(0, 0, 0);
camera.position:设置相机的摆放位置。
camera.lookAt:设置相机望向哪里。
从相机设置可以看出,我们是在y轴上高度为2000的位置,望向原点(0,0,0),因此,观察到的将是一个正对我们的正方形20*20格子图。



为了便于理解,我们假设在y轴上俯视原点,我们将看到一个x正方向向右,z正方向像下的坐标系。此处牵扯到camera中up这个属性的设定,此属性表示我们以哪个方向作为图的上方。由于z轴正方向是向下,因此,此图的up方向为z轴负方向,即可写为(0,0,-1)。
此时,调整camera.position中的y轴位置,会改变观察到的方块大小。相机往左移动时,x值变小;相机往右x值变大;相机往下z值变大;相机往上z值变小,。
因此,我们调整参数为:
camera.position.set(-2000, 2000, 0);
camera.up.set(0, 0, -1);
camera.lookAt(0, 0, 0);
相机此刻在往左移动,由相机位置望向原点,会有一个45°的角度,将会看到一个左边近,右边远的侧身图:

分别调整参数,将会得到其他三种图以作参考。
// 左侧观察
// camera.position.set(-2000, 2000, 0);
// 右侧观察
camera.position.set(2000, 2000, 0);
// 上侧观察
// camera.position.set(0, 2000, -2000);
// 下侧观察
// camera.position.set(0, 2000, 2000);



于此,相信大家对camera透视相机的position,up参数有一定了解了吧,动手试验一下吧。有问题可以留言!
React+Three.js——PerspectiveCamera透视相机camera参数以及属性值探索的更多相关文章
- 原生js使用getComputedStyle方法获取CSS内部属性值
在对网页进行调试的过程中,经常会用到js来获取元素的CSS样式, 1.下面的方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),而无法获取定义在<style ...
- js中所有函数的参数(按值和按引用)都是按值传递的,怎么理解?
我觉着我可能对这块有点误解,所以单独开个博说下自己的理解,当然是研究后的正解了. 1,参数传递是基本类型,看个例子: function addTen(num){ num += 10; return n ...
- react为按钮绑定点击事件和修改属性值
注意点:1.事件名称由react提供,所以事件名首字母大写.比如onClick,onMouseOver. 2.为事件提供的处理函数,格式必须是onClick={function},没有小括号. 3.绑 ...
- 【web开发--js学习】functionName 如果是一个属性值,函数将不会被调用
<html> <head> <meta http-equiv="Content-Type" Content="text/html; char ...
- JS 取Json数据中对象特定属性值
解析JSON JSON 数据 var str = '[{"a": "1","b": "2"}, {"a&quo ...
- html中设置data-*属性值 并在js中进行获取属性值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Three.js学习(相机,场景,渲染,形状)
相机分为透视相机和正交相机(还有第三人称相机不介绍). var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window. ...
- three.js学习3_相机相关
Three.Camera Camera是所有相机的抽象基类, 在构建新摄像机时,应始终继承此类. 常见的相机有两种类型: PerspectiveCamera(透视摄像机)或者 Orthographic ...
- 【REACT NATIVE 系列教程之十二】REACT NATIVE(JS/ES)与IOS(OBJECT-C)交互通信
http://blog.csdn.net/xiaominghimi/article/details/51586492 一用到跨平台的引擎必然要有引擎与各平台原生进行交互通信的需要.那么Himi先讲解R ...
随机推荐
- 查看iptables状态-重启
iptables 所在目录 /etc/sysconfig/iptables service iptables status 查看iptables状态 service iptables restart ...
- $.ajax()所有参数详解
原文:https://www.cnblogs.com/everest33Tong/p/6159700.html [一]$.ajax()所有参数详解 url: 要求为String类型的参数,(默认为当前 ...
- BlangenOA项目展示(附源码)
1. 登录界面 1.1表单校验 1.2信息有误 1.3正在登录 2.桌面 3.用户管理 3.1添加 3.2删除 3.3编辑 3.4设置用户角色 3.5设置用户权限 4.角色管理 5.权限管理(菜单 ...
- TCP中的seq
TCP连接中传送的字节流中的每个字节都按顺序编号,第一个字节的编号由本地随机产生 seq其实就是这个报文段中的第一个字节的数据编号. 例如,一段报文的序号字段值是 200 ,而携带的数据共有100字段 ...
- webservice简单例子
1.添加web服务. /// <summary> /// demo 的摘要说明 /// </summary> [WebService(Namespace = "htt ...
- “全栈2019”Java第二十八章:数组详解(上篇)
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- POJ - 1251A - Jungle Roads 利用最小生成树
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...
- Windows IIS ASP.NET Core中创建和使用HTTPS自签名证书
为什么要用Https就不说了. 第一步:创建自签名的证书.在Windows下开启PowerShell,将以下文字粘贴进去: # setup certificate properties includi ...
- Codeforces 464E. The Classic Problem
题目大意 给定一张$n$个点, $m$条边的无向图,求$S$ 到$T$的最短路,其中边权都是$2^k$的形式$n,m,k<=10^5$,结果对$10^9+7$取模 题解 大佬好厉害 跑一边dij ...
- iOS 设备定位功能可用的判断
if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] ...