Three.js中文网 Three.js中文网
首页
免费视频
系统课 (opens new window)
  • Three.js基础课程
  • Vue3+Threejs 3D可视化
  • Threejs进阶课程
  • 展厅3D预览漫游
  • Threejs Shader
  • Blender建模基础
  • Three.js基础课程(旧版本) (opens new window)
  • 文章
WebGPU教程
  • WebGL教程
  • WebGL教程(旧版本) (opens new window)
3D案例
  • 本站部署(打开快) (opens new window)
  • 原英文官网文档 (opens new window)
首页
免费视频
系统课 (opens new window)
  • Three.js基础课程
  • Vue3+Threejs 3D可视化
  • Threejs进阶课程
  • 展厅3D预览漫游
  • Threejs Shader
  • Blender建模基础
  • Three.js基础课程(旧版本) (opens new window)
  • 文章
WebGPU教程
  • WebGL教程
  • WebGL教程(旧版本) (opens new window)
3D案例
  • 本站部署(打开快) (opens new window)
  • 原英文官网文档 (opens new window)
Web3D系统课程视频
  • 0.学前说明

  • 1.Three.js快速入门

  • 2.几何体BufferGeometry

  • 3.模型对象、材质

  • 4.层级模型

  • 5.顶点UV坐标、纹理贴图

  • 6.加载外部三维模型(gltf)

  • 7.PBR材质与纹理贴图

  • 8.渲染器和前端UI界面

  • 9.生成曲线、几何体

  • 10.相机基础

  • 11.光源和阴影

  • 12.精灵模型Sprite

  • 13.后处理EffectComposer

  • 14.射线拾取模型

    • 1. 射线Ray
    • 2. Raycaster(射线拾取模型)
    • 3. 屏幕坐标转标准设备坐标
    • 4. Raycaster(鼠标点击选中模型)
    • 5. Canvas尺寸变化(射线坐标计算)
      • 6. 射线拾取层级模型(模型描边)
      • 7. 射线拾取Sprite控制场景
    • 15.场景标注标签信息

    • 16.关键帧动画

    • 17.动画库tween.js

    • Three.js教程
    • 14.射线拾取模型
    郭隆邦
    2023-03-03
    目录

    5. Canvas尺寸变化(射线坐标计算)

    # Canvas尺寸变化(射线坐标计算)

    本节课内容非常简单,就是你鼠标单击canvas画布,用射线拾取模型的时候,有一点要注意,Canvas画布尺寸如果不是固定的,而是变化的,会对坐标变换有影响。

    学习本节课内容之前,可以先回顾下,前面关于threejs Canvas画布布局的讲解。

    1.12. Canvas画布布局和全屏 (opens new window)

    8.1. three.js Canvas画布布局 (opens new window)

    # canvas画布全屏-尺寸跟着浏览器窗口变化

    const width = window.innerWidth;
    const height = window.innerHeight;
    // 画布跟随窗口变化
    window.onresize = function () {
        renderer.setSize(window.innerWidth, window.innerHeight);
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
    };
    

    如果canvas画布尺寸变化了,下面代码中的width、height还是原来的值,这时候射线拾取就有可能无法拾取到模型对象。

    renderer.domElement.addEventListener('click', function (event) {
        const px = event.offsetX;
        const py = event.offsetY;
        //屏幕坐标转WebGL标准设备坐标
        const x = (px / width) * 2 - 1;
        const y = -(py / height) * 2 + 1;
    })
    

    每次触发click事件,都要重新计算canvas画布。

    renderer.domElement.addEventListener('click', function (event) {
        const px = event.offsetX;
        const py = event.offsetY;
        //屏幕坐标转WebGL标准设备坐标
        const x = (px / window.innerWidth) * 2 - 1;
        const y = -(py / window.innerHeight) * 2 + 1;
    })
    

    特殊情况,canvas画布和body左上角重合,可以用clientX、clientY替换offsetX、offsetY

    renderer.domElement.addEventListener('click', function (event) {
        const px = event.clientX;
        const py = event.clientY;
        //屏幕坐标转WebGL标准设备坐标
        const x = (px / window.innerWidth) * 2 - 1;
        const y = -(py / window.innerHeight) * 2 + 1;
    })
    

    # canvas局部布局-尺寸随窗口变化

    // 画布跟随窗口变化
    window.onresize = function () {
        const width = window.innerWidth - 200; //canvas画布宽度
        const height = window.innerHeight - 60; //canvas画布高度
        renderer.setSize(width, height);
        camera.aspect = width / height;
        camera.updateProjectionMatrix();
        document.getElementById('left').style.height = height + 'px';
    };
    
    renderer.domElement.addEventListener('click', function (event) {
        const width = window.innerWidth-200;
        const height = window.innerHeight-60;
        //屏幕坐标转WebGL标准设备坐标
        const x = (px / width) * 2 - 1;
        const y = -(py / height) * 2 + 1;
    })
    
    4. Raycaster(鼠标点击选中模型)
    6. 射线拾取层级模型(模型描边)

    ← 4. Raycaster(鼠标点击选中模型) 6. 射线拾取层级模型(模型描边)→

    Theme by Vdoing | Copyright © 2016-2025 豫ICP备16004767号-2
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式