three.js (四)离散层次细节level of details

本文介绍了一种在三维场景中使用LOD(Level of Detail)技术来优化大规模地形绘制的方法。通过根据摄像机距离动态调整地形块的细节级别,实现高效渲染。具体包括如何计算地形块的细节等级、如何构建不同细节级别的地形块以及如何在每帧更新时判断是否需要重新设置地形细节。

LOD 处理比较大的外部地面场景中比较有用, 一般用于绘制地形。 首先通过可视体的切割删除不用的地形块,接着通过LOD 对照相机不同距离的地形块进行层次细节调整。

这里采用最简单的LOD 方法。

首先地形有n*n 的块构成, 这些块共同构成一个大平面;

首先根据每个块到照相机的距离 计算细节层次, 

例如假设由5*5 个块构成地形, 每个块1*1大小, 有4个细节层次, 

当块距离照相机 小于2 层次 0

距离小于4  层次 1

小于6 层次 2

其它层次 3

首先构造一个Object3D 作为整个地面的代表。

myGame.Earth = function(){
    THREE.Object3D.call(this);
    this.curPatches = [];
    this.patches = [];
    this.width = 5;
    this.height = 5;
    this.patch_width = 1;
    this.patch_height = 1;
    for(var i = 0; i < this.height; i++)
    {
        for(var j = 0; j < this.width; j++)
        {
            this.patches.push(1);//distance ---> detail 0 1*1
        }
    }
};


myGame.Earth.prototype = new THREE.Object3D();


var earth = new myGame.Earth();

earth中的每一个块是 一个Mesh 对象,底层的几何体是一个PlaneGeometry

curPatches 用于存放当前组成earth的块, 当细节层次需要改变的时候这些块将被丢弃,而重新构造新的块。

patches用于存储当前块的细节层次。


关键的setDetails 函数用于调节块的细节, 首先计算所有块到照相机的距离 得到细节层次; 接着删除旧的所有平面, 接着再构建新的块加入到场景中。 


myGame.Earth.prototype.setDetails = function(camera){
    var diff = new THREE.Vector3();
    var standard = this.patch_width;
    var pos = new THREE.Vector3();
    for(var i = 0; i < this.height; i++)
    {
        for(var j = 0; j < this.width; j++)
        {
            pos.set(-this.width/2*this.patch_width+j*this.patch_width+this.patch_width/2, 
                    0, 
                    -this.height/2*this.patch_height+i*this.patch_height+this.patch_height/2); 

            var dist = diff.sub(camera.position, pos).length();
            if(dist < 2*standard)
                this.patches[i*this.width+j] = 0;
            else if(dist < 4*standard)
                this.patches[i*this.width+j] = 1;
            else if(dist < 6*standard)
                this.patches[i*this.width+j] = 2;
            else
                this.patches[i*this.width+j] = 3;
        }
    }
    for(var i = 0; i < this.curPatches.length; i++)
    {
        this.remove(this.curPatches[i]);
    }
    var mat = new THREE.MeshBasicMaterial({color:0xff0000, wireframe:true});
    for(var i = 0; i < this.patches.length; i++)
    {
        var pl;
        var detail = this.patches[i];
        console.log(detail);
        if(detail == 0)
            pl = new THREE.PlaneGeometry(this.patch_width, this.patch_height, 10, 10);
        else if(detail == 1)
            pl = new THREE.PlaneGeometry(this.patch_width, this.patch_height, 5, 5);
        else if(detail == 2)
            pl = new THREE.PlaneGeometry(this.patch_width, this.patch_height, 2, 2);
        else
            pl = new THREE.PlaneGeometry(this.patch_width, this.patch_height, 1, 1);
        var obj = new THREE.Mesh(pl, mat);
        obj.position.set(-this.width/2*this.patch_width+i%this.width*this.patch_width+this.patch_width/2, 
                        -this.height/2*this.patch_height+ (this.height-~~(i/this.width))*this.patch_height+this.patch_height/2,
                        0);
        this.curPatches.push(obj);
        this.add(obj);
    }
};


在每帧更新的时候, 通过检测照相机新旧位置的距离差, 如果足够大 则更新整个场景的块。

function animate()
{
    requestAnimationFrame(animate);
    controls.update(clock.getDelta());
    var vec = new THREE.Vector3();
    var dist = vec.sub(camera.position, camera.oldPosition).length();
    if(dist > earth.patch_width)
    {
        camera.oldPosition.copy(camera.position);
        earth.setDetails(camera);
    }
    render();
}



You are an experienced AI developer specializing in designing and implementing progressive application prototypes utilizing advanced AI assistants such as Google’s Gemini Assistant. Your expertise is sought to conceptualize and develop five distinct versions of a sketch-to-image application, each incrementally increasing in complexity and functionality. Please provide detailed prompts for each version, ensuring to include: - Clear definition of core features and enhancements introduced at each progressive stage, specifying how complexity grows (e.g., from basic image generation to integration of contextual understanding or user customization). - Target platform considerations (web, mobile, desktop) and intended user demographics or use cases to tailor functionality appropriately. - Integration points and expected interactions with Gemini Assistant, highlighting how its capabilities are leveraged at each complexity level. - Technical specifications or input formats required to facilitate prompt engineering for reliable and scalable AI-driven image synthesis. - Suggested prompt structures or templates that enable reproducible and efficient app behavior aligned with the outlined features. Leverage your deep knowledge of AI application design, prompt engineering, and user experience optimization to create a comprehensive framework that guides the systematic development of these five app versions. Your contribution should enable the creation of robust, user-centric sketch-to-image tools that progressively harness Gemini Assistant’s evolving capabilities.
最新发布
10-03
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值