如何正确更改相机视图以在 Three.js 中前后移动到场景中? [英] How to properly change the camera view to move forward and backward into a scene in Three.js?

查看:42
本文介绍了如何正确更改相机视图以在 Three.js 中前后移动到场景中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过移动透视相机使我可以在three.js中以以下方式移动:

  1. 按下一个键或按钮,将相机向前移动到我正在看的方向"
  2. 按下一个键或按钮,将相机向下移动到我正在看的方向下方"
  3. 点击一个键或按钮来调整相机,使fov"具有不同的值
  4. 按下按键或按钮来倾斜相机,这样我就可以旋转,就像在相机能够看到我左右的位置一样旋转

以下是我当前的代码.根据我所看到的,文档中的 PerspectiveCamera 似乎没有任何像setFov"或类似的方法,简单的camera.fov ="似乎没有像相机后的网格那样有任何效果已初始化.那么我如何才能正确地执行上述操作?:

<头><title>示例 01.03 - 材料和灯光</title><script type="text/javascript" src="../libs/three.js"></script><script type="text/javascript" src="../libs/jquery-1.9.0.js"></script><script type="text/javascript" src="../libs/stats.js"></script><风格>身体{/* 将边距设置为 0,溢出设置为隐藏,以全屏显示 */边距:0;溢出:隐藏;}</风格><身体><div id="统计输出">

<!-- 将保存输出的 Div --><div id="WebGL-输出">

<!-- 运行我们 Three.js 示例的 Javascript 代码 --><script type="text/javascript">//加载完所有内容后,我们运行 Three.js 的东西.$(函数(){var stats = initStats();//创建一个场景,该场景将包含我们所有的元素,例如对象、相机和灯光.var scene = new THREE.Scene();//创建一个相机,它定义了我们正在看的地方.var camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);//创建渲染并设置大小var renderer = new THREE.WebGLRenderer();renderer.setClearColorHex(0xEEEEEE, 1.0);renderer.setSize(window.innerWidth, window.innerHeight);renderer.shadowMapEnabled = true;//创建地平面var planeGeometry = new THREE.PlaneGeometry(60,20,1,1);var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});var plane = new THREE.Mesh(planeGeometry,planeMaterial);平面.receiveShadow = true;//旋转和定位平面plane.rotation.x=-0.5*Math.PI;平面.位置.x=15平面.位置.y=0平面.位置.z=0//将飞机添加到场景中场景.添加(平面);//创建一个立方体var cubeGeometry = new THREE.CubeGeometry(4,4,4);var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);立方体.castShadow = true;//放置立方体立方体.位置.x=-4;立方体.位置.y=3;立方体.位置.z=0;//将立方体添加到场景中场景.添加(立方体);var sphereGeometry = new THREE.SphereGeometry(4,20,20);var sphereMaterial = new THREE.MeshLambertMaterial({color: 0x7777ff});var sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);//定位球体sphere.position.x=20;sphere.position.y=0;sphere.position.z=2;sphere.castShadow=true;//将球体添加到场景中场景添加(球体);//将相机定位并指向场景的中心camera.position.x = -30;相机位置 y = 40;相机.位置.z = 30;相机.看(场景.位置);//添加微妙的环境照明varambientLight = new THREE.AmbientLight(0x0c0c0c);场景.添加(环境光);//为阴影添加聚光灯var spotLight = new THREE.SpotLight(0xffffff);spotLight.position.set( -40, 60, -10 );spotLight.castShadow = true;场景添加(聚光灯);//将渲染器的输出添加到 html 元素$("#WebGL-output").append(renderer.domElement);//调用渲染函数无功步骤=0;使成为();函数渲染(){stats.update();//绕其轴旋转立方体立方体.旋转.x += 0.02;立方体.旋转.y += 0.02;立方体.旋转.z += 0.02;//上下弹跳球体步长+=0.04;sphere.position.x = 20+( 10*(Math.cos(step)));sphere.position.y = 2 +( 10*Math.abs(Math.sin(step)));//使用 requestAnimationFrame 渲染请求动画帧(渲染);renderer.render(场景,相机);}函数 initStats() {var stats = new Stats();stats.setMode(0);//0: fps, 1: 毫秒//左上对齐stats.domElement.style.position = '绝对';stats.domElement.style.left = '0px';stats.domElement.style.top = '0px';$("#Stats-output").append(stats.domElement);返回统计数据;}});</html>

解决方案

我相信您只需要了解相机矩阵的组件即可实现您想要的.三.js相机"是经典线性代数类型的4x4矩阵.<代码>4x4 矩阵 = [a b c de f g hi j k l无声]3x3 内部矩阵实际上就是相机的方向:<代码>相机方向 = [a b ce f g我 j k]意思是向量 { a, b, c } 是从相机的 pov 指向相机右侧的方向向量.矢量 { e, f, g } 是从相机的角度向上的方向矢量.而向量 { i, j, k } 是指向摄像机朝向的方向向量.

这 3 个向量共同构成了相机的方向.

通过像这样将左手握在您面前,可以进一步想象它们:每个手指都指向相机视角的正方向,它们同样等于定向相机所需的旋转因子.

其他非旋转分量 { n, m, o } 是相机的 x, y, z 位置.

要向相机的方向移动,请将向量 { i, j, k } 添加到 { n, m, o }.

要向右移动,从相机的视角,将向量 { a, b, c } 添加到 { n, m, o }.

要向上移动,从相机的 pov,将向量 { e, f, g } 添加到 { n, m, o }.

而且,当然,向相反方向移动会添加这些方向向量的负值.

I want to make it such that I can move around in the following manner in three.js by moving the perspectivecamera:

  1. "hit a key or button to move the camera forward into the direction im looking at"
  2. "hit a key or button to move the camera downward below the direction im looking at"
  3. "hit a key or button to pitch the camera such that the "fov" is of a different value
  4. "hit keys or buttons to pitch the camera such that i am rotating as if pivoted at the place the camera is to be able to see whats left and right of me

The following is my current code. Based on what I am seeing, it appears that PerspectiveCamera from the docs does not appear to have any methods like "setFov" or anything like that, simply "camera.fov = " does not appear to have any effect like the meshes after the camera has been initialized. So how would I properly be able to do the above?:

<!DOCTYPE html>

<html>

<head>
    <title>Example 01.03 - Materials and light</title>
    <script type="text/javascript" src="../libs/three.js"></script>
    <script type="text/javascript" src="../libs/jquery-1.9.0.js"></script>
    <script type="text/javascript" src="../libs/stats.js"></script>
    <style>
        body{
            /* set margin to 0 and overflow to hidden, to go fullscreen */
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>

<div id="Stats-output">
</div>
<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>

<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">

    // once everything is loaded, we run our Three.js stuff.
    $(function () {

        var stats = initStats();

        // 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);

        // create a render and set the size
        var renderer = new THREE.WebGLRenderer();

        renderer.setClearColorHex(0xEEEEEE, 1.0);
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.shadowMapEnabled = true;

        // create the ground plane
        var planeGeometry = new THREE.PlaneGeometry(60,20,1,1);
        var planeMaterial =    new THREE.MeshLambertMaterial({color: 0xffffff});
        var plane = new THREE.Mesh(planeGeometry,planeMaterial);
        plane.receiveShadow  = true;

        // rotate and position the plane
        plane.rotation.x=-0.5*Math.PI;
        plane.position.x=15
        plane.position.y=0
        plane.position.z=0

        // add the plane to the scene
        scene.add(plane);

        // create a cube
        var cubeGeometry = new THREE.CubeGeometry(4,4,4);
        var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});
        var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
        cube.castShadow = true;

        // position the cube
        cube.position.x=-4;
        cube.position.y=3;
        cube.position.z=0;

        // add the cube to the scene
        scene.add(cube);

        var sphereGeometry = new THREE.SphereGeometry(4,20,20);
        var sphereMaterial = new THREE.MeshLambertMaterial({color: 0x7777ff});
        var sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);

        // position the sphere
        sphere.position.x=20;
        sphere.position.y=0;
        sphere.position.z=2;
        sphere.castShadow=true;

        // add the sphere to the scene
        scene.add(sphere);

        // position and point the camera to the center of the scene
        camera.position.x = -30;
        camera.position.y = 40;
        camera.position.z = 30;
        camera.lookAt(scene.position);

        // add subtle ambient lighting
        var ambientLight = new THREE.AmbientLight(0x0c0c0c);
        scene.add(ambientLight);

        // add spotlight for the shadows
        var spotLight = new THREE.SpotLight( 0xffffff );
        spotLight.position.set( -40, 60, -10 );
        spotLight.castShadow = true;
        scene.add( spotLight );

        // add the output of the renderer to the html element
        $("#WebGL-output").append(renderer.domElement);

        // call the render function
        var step=0;
        render();

        function render() {
            stats.update();
            // rotate the cube around its axes
            cube.rotation.x += 0.02;
            cube.rotation.y += 0.02;
            cube.rotation.z += 0.02;

            // bounce the sphere up and down
            step+=0.04;
            sphere.position.x = 20+( 10*(Math.cos(step)));
            sphere.position.y = 2 +( 10*Math.abs(Math.sin(step)));

            // render using requestAnimationFrame
            requestAnimationFrame(render);
            renderer.render(scene, camera);
        }

        function initStats() {

            var stats = new Stats();

            stats.setMode(0); // 0: fps, 1: ms

            // Align top-left
            stats.domElement.style.position = 'absolute';
            stats.domElement.style.left = '0px';
            stats.domElement.style.top = '0px';

            $("#Stats-output").append( stats.domElement );

            return stats;
        }
    });



</script>
</body>
</html>

解决方案

I believe you simply need to understand the components of the camera's matrix to realize what you want. The three.js "camera" is a 4x4 matrix of the classic linear algebra type. 4x4 matrix = [a b c d e f g h i j k l n m o p] The 3x3 inner matrix is literally the orientation of your camera: camera orientation = [a b c e f g i j k] Meaning the vector { a, b, c } is the direction vector pointing to the camera's right, from the camera's pov. The vector { e, f, g } is the direction vector pointing up, from the camera's point of view. And the vector { i, j, k } is the director vector pointing in the direction the camera is facing.

Together these 3 vectors compose the orientation of the camera.

They can be further envisioned by holding your left hand in front of you like this: Each finger is pointing in the positive direction of the camera's pov, and they likewise equal the rotation factors necessary to orient the camera.

And the other, non-rotation components { n, m, o } is the x, y, z position of the camera.

To move in the direction of the camera, add the vector { i, j, k } to { n, m, o }.

To move to the right, from the camera's pov, add the vector { a, b, c } to { n, m, o }.

To move up, from the camera's pov, add the vector { e, f, g } to { n, m, o }.

And, of course, to move in the opposite directions add the negatives of those orientation vectors.

这篇关于如何正确更改相机视图以在 Three.js 中前后移动到场景中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆