Threejs源码系列- WebGLRenderer (3)projectObject函数

WebGLRender - projectObject函数

projectObject函数,作用是递归遍历场景中的 3D 对象,筛选出需要渲染的对象并将其添加到渲染列表(currentRenderList)中。

js 复制代码
function projectObject( object, camera, groupOrder, sortObjects ) {
 // ...
}

参数说明

  • object:当前处理的 3D 对象(可能是模型、灯光、组等)。
  • camera:当前渲染使用的相机,用于可见性判断和深度计算。
  • groupOrder:当前对象所属组的排序值,用于控制渲染顺序。
  • sortObjects:是否需要计算对象深度以用于排序(影响透明/不透明对象的渲染顺序)。

关键逻辑解析

可见性过滤
js 复制代码
// 对象自身不可见,直接跳过
if (object.visible === false) return; 
// 检查对象层级是否在相机可见层级内
const visible = object.layers.test(camera.layers);
Group类型
js 复制代码
   
    if (object.isGroup) {
        // 更新组排序值,子对象继承该排序
        groupOrder = object.renderOrder; 
    } 
LOD类型
js 复制代码
 if ( object.isLOD ) {
    // 根据相机距离更新显示的细节层级
    if (object.autoUpdate === true) object.update(camera); 
} 
Light类型
js 复制代码
if ( object.isLight ) {
    // 将光源加入渲染状态,用于后续光照计算
    currentRenderState.pushLight( object );
    // 若光源投射阴影,加入阴影列表
    if ( object.castShadow ) {
        currentRenderState.pushShadow( object );
    }
}
Sprite类型
js 复制代码
 if ( object.isSprite ) {
    // 未开启视锥体剔除,或精灵在相机视锥体内
    if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
        if ( sortObjects ) {
            // 计算精灵在屏幕空间的 z 坐标(用于排序)
            _vector4.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
        }
        // 更新精灵的几何数据
        const geometry = objects.update( object );
        const material = object.material;
        if (material.visible) {
            // 将精灵加入渲染列表
            currentRenderList.push(object, geometry, material, groupOrder, _vector4.z,null);
        }
    }
}
Mesh类型、Line类型、Points类型
js 复制代码
// 未开启视锥体剔除,或对象在相机视锥体内
if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
    // 更新几何体数据(如缓冲、绑定等)
    const geometry = objects.update(object); 
    const material = object.material;

    if ( sortObjects ) {
        // 计算对象在屏幕空间的 z 坐标(用于排序)
        if ( object.boundingSphere !== undefined ) {
            // 优先使用对象自身的包围球
            if ( object.boundingSphere === null ) object.computeBoundingSphere();
            _vector4.copy( object.boundingSphere.center );
        } else {
            // 否则使用几何体的包围球
            if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
            _vector4.copy( geometry.boundingSphere.center );
        }

        // 转换到屏幕空间
        _vector4.applyMatrix4( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
    }

    // 处理多材质情况(几何体分组对应不同材质)     
    if ( Array.isArray( material ) ) {

        const groups = geometry.groups;

        for ( let i = 0, l = groups.length; i < l; i ++ ) {

            const group = groups[ i ];
            const groupMaterial = material[ group.materialIndex ];

            if ( groupMaterial && groupMaterial.visible ) {

                currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector4.z, group );

            }

        }

    } else if ( material.visible ) {
        // 单一材质,直接加入渲染列表
        currentRenderList.push( object, geometry, material, groupOrder, _vector4.z, null );
    }
}
处理子元素
js 复制代码
const children = object.children;

for ( let i = 0, l = children.length; i < l; i ++ ) {

    projectObject( children[ i ], camera, groupOrder, sortObjects );

}
相关推荐
一马平川的大草原3 天前
基于Vue+Three.js实现三维油藏模型解析与可视化交互切割操作
vue.js·three.js·三维油藏模型
RulerMike4 天前
three 实现简单机械臂逆运动
前端·ai编程·three.js
程序员阿峰6 天前
前端3D·Three.js一学就会系列:第二 画线
前端·three.js
程序员阿峰7 天前
前端3D·Three.js一学就会系列: 第一个3D网站
前端·three.js
烛阴7 天前
Three.js 场景完全入门指南:让你的 3D 场景不在乱成一团
webgl·three.js
ai超级个体10 天前
前端下午茶:这 3 个网页特效建议收藏(送源码)
前端·three.js·threejs·网页设计·vibe coding·网页灵感·网页分享
codingWhat12 天前
用 three.js 实现 3D 地图
three.js
答案answer16 天前
我的Three.js3D场景编辑器免费开源啦🎉🎉🎉
前端·github·three.js
一拳不是超人17 天前
Three.js一起学-如何通过官方例子高效学习 Three.js?手把手带你“抄”出一个3D动画
前端·webgl·three.js