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 ...
随机推荐
- ASP.NET程序从IIS6移植到IIS7时出现500.22错误(转)
最可能的原因: • 此应用程序在 system.web/httpModules 节中定义配置. 可尝试的操作: • 将配置迁移到 system.webServer/modules 节.也可 ...
- Windows上编译libpng
下载libpng 1.5.10并解压到[工作目录]/png/libpng-1.5.10 用CMake选择png/libpng-1.5.10目录并Configure: CMAKE_C_FLAGS_DEB ...
- win7下安装numpy
OS: WIN7 32bit Python: 3.4 在以下网站下载: numpy-MKL-1.9.0.win32-py3.4.exe
- DELL PowerEdge R410系统日志满报错
DELL PowerEdge R410系统日志满报错 重启服务器时在自检过程中看到CTRL+E时快速按下CTRL+E进入到远程管理ip地址配置界面
- 用Visual Studio 2015成功编译、发布UMDF驱动到目标机!!
开发工具:Visual Studio 2015企业版 主 机:windows10 X64企业版,主机是安装了Visual Studio 2015的操作系统,主要进行驱动开发和调试. 目 标 ...
- while 小项目练习
# (1) 用双层while 写十行十列小星星 j = 0 while j < 10: #打印一行十个小星星 i = 0 while i <10: print("*", ...
- linux的档案权限和目录配置
Linux一般将档案可存取的身份分为三个类别,分别是 owner/group/others /etc/passwd 账号信息 /etc/shadow 个人密码 /etc/group 组名记录 ...
- 洛谷P5158 【模板】多项式快速插值
题面 传送门 前置芝士 拉格朗日插值,多项式多点求值 题解 首先根据拉格朗日插值公式我们可以暴力\(O(n^2)\)插出这个多项式,然而这显然是\(gg\)的 那么看看怎么优化,先来看一看拉格朗日插值 ...
- php生成N个不重复的随机数实例
思路: 将随机数存入数组,再在数组中去除重复的值,即可生成一定数量的不重复随机数. /* * array unique_rand( int $min, int $max, int $num ) * 生 ...
- ios 字符串处理:截取字符串、匹配字符串、分隔字符串
1.截取字符串 NSString*string =@"sdfsfsfsAdfsdf";string = [string substringToIndex:7];//截取掉下标7之后 ...