zoukankan      html  css  js  c++  java
  • 浅谈水体的实现

        温故知新,水面很早就实现了,但没有在这里写过,今天总结一下。
        水的模拟要达到以下效果:水面的折射与反射,水的波动。
        要达到这种效果,需要以下四张纹理:折射纹理(RenderTarget),反射纹理(RenderTarget),Bumpmap,水自己的纹理。
        折射与反射的原理就不具体说了,下面只说实现步骤。

        一、渲染到纹理
        RenderTarget的创建我不多述了,网上查查,我创建的是256X256的纹理,格式为X8R8G8B8。折射纹理比较简单,直接把当前的地形或在水面以下的物体渲染多次,注意,为提高性能,在这次pass中,可以选择带clip的shader(假如用到shader)或者直接使用水面的平面作为一个clipplane(不使用Shader)。
        然后是反射的实现,首先生成一个以水面为反射面的反射矩阵,摄像机的坐标乘与反射矩阵求得新的坐标。然后按渲染流程再走一次,(即计算裁剪空间,加入渲染对列,到最后渲染)。渲染到纹理后,注意,此时的纹理U坐标应该做一个反转操作。如果在shader中修改,必须比较简单就是u = 1.0 - uvCoord.x。 

        二、水的波动

        需要利用一个Bumpmap做作纹理UV值的偏移,下面是HLSL的代码:

        float2 BumpUVCoords = input.uvCoords;
        BumpUVCoords.x += time;
        BumpUVCoords.y -= time;
        float4 dudv_offset = tex2D(BumpMap, BumpUVCoords);//primary_motion + secondary_motion); //, 0.0001, 0.9999));  // + secondary_motion);
        float2 offsets = (2 * dudv_offset.xy - 1) * 0.1;

        //这里的clamp是多余的,明天试试去掉
        float2 lastCoords = input.uvCoords + offsets;  //clamp(input.uvCoords + offsets, 0.00, 4.00);//offsets; //, 0.00, 1.00);
       
        float2 waterUV = input.uvCoords;
        waterUV.x += time;
        waterUV.y -= time;
        float4 colorWater = tex2D(Samp0, waterUV);
     
        output.Color = colorWater;

        clamp(input.outTexProj, 0.0, 1.0);
        float2 texProj = input.outTexProj.xy / input.outTexProj.w;
        texProj.x = 1.0 - texProj.x;
        lastCoords = clamp(texProj + offsets, 0.0, 1.0);

    截图:

  • 相关阅读:
    阿里早期Android加固代码的实现分析
    如何利用C++的time头文件获取系统时间
    Python编写基于socket的非阻塞多人聊天室程序(单线程&多线程)
    Dalvik模式下在Android so库文件.init段、.init_array段构造函数上下断点
    手动绕过百度加固Debug.isDebuggerConnected反调试的方法
    request使用代理
    requests爬取豆瓣热门电视剧
    scrapy-继承默认的user-agent 中间件
    scrapy-下载器中间件 随机切换user_agent
    scrapy 直接在编辑器运行
  • 原文地址:https://www.cnblogs.com/lancidie/p/1987224.html
Copyright © 2011-2022 走看看