国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用ShaderToy來處理VR畸變

2019-11-06 09:52:34
字體:
來源:轉載
供稿:網友

ShaderToy

因為前兩周一直在尋找處理VR反畸變的方法,發現了這個工具,它是一個調試渲染很棒的工具,直接在線輸入代碼,即時顯示你的渲染效果,一直沒來得及記錄下來,畸變已經處理差不多了,現在來把這個工具的處理畸變的方法記錄下來. ShaderToy是一個計算機圖形方面的在線的學習,交流平臺.在這個平臺上使用GLSL語言(語法跟C很像,如果你學習過C語言,操作它基本沒什么問題)就能畫出很漂亮的渲染效果.你可以在官網上看到人分享的鏈接(包含源碼). 這里分享兩個ShaderToy上調試可能需要的鏈接:

GLSL4.5的語法規范ShaderToy使用教程(我最初也是看這個教程,來學習的)

VR畸變算法

至于為什么會產生畸變?畸變是什么?如果修正畸變?這些問題查看之前發的幾篇關于畸變的文章吧. 這里直接上反畸變主要算法:

//這里假設畸變中心的坐標是(0.5,0.5).//計算的公式來自這個視頻:https://www.youtube.com/watch?v=B7qrgrrHry0&feature=youtu.be和Wikipedia的畸變校正算法:https://en.wikipedia.org/wiki/Distortion_(optics)#Software_correctionrr = sqrt((fU - 0.5f)*(fU - 0.5f) + (fV - 0.5f)*(fV - 0.5f));r2 = rr * (1 + K1*(rr*rr) + K2*(rr*rr*rr*rr));theta = atan2(fU-0.5f, fV-0.5f);//圖像經過畸變后會明顯的變小,需要在這里乘以放大系數,來充滿整個屏幕.hX = sin(theta)*r2*1.0;hY = cos(theta)*r2*1.0;

使用ShaderToy處理畸變

渲染效果,已經上傳ShaderToy上:barrel&pincushion distortion 代碼如下:

void mainImage( out vec4 fragColor, in vec2 fragCoord ) { //If you have any questions, you can send me Email:helenhololens@Gmail.com //get source_uv vec2 source_uv = fragCoord.xy / iResolution.xy; //assume your distortion center coordinate is (0.5,0.5),you can use your distortion center instead. vec2 distortion_center = vec2(0.5,0.5); //Define algorithm dependent variables float distortion_x,distortion_y,rr,r2,theta; //define distortion coefficient K1 and K2 ,In most cases we can only adjust K1. then K2 parameters can be adjusted more perfect Effect //iGlobalTime is used for Real-time change. //K1 < 0 is pincushion distortion //K1 >=0 is barrel distortion float distortion_k1 = 1.0 * sin(iGlobalTime*0.5),distortion_k2 = 0.5; vec2 dest_uv; //--------------------------Algorithm Start---------------------------------------- //The formula is derived from this video:https://www.youtube.com/watch?v=B7qrgrrHry0&feature=youtu.be //and Distortion correction algorithm for Wikipedia:https://en.wikipedia.org/wiki/Distortion_(optics)#Software_correction rr = sqrt((source_uv.x - distortion_center.x)*(source_uv.x - distortion_center.x) + (source_uv.y - distortion_center.y)*(source_uv.y - distortion_center.y)); r2 = rr * (1.0 + distortion_k1*(rr*rr) + distortion_k2*(rr*rr*rr*rr)); theta = atan(source_uv.x - distortion_center.x, source_uv.y - distortion_center.y); distortion_x = sin(theta) * r2 * 1.0;//1.0 is scale factor distortion_y = cos(theta) * r2 * 1.0;//1.0 is scale factor dest_uv.x = distortion_x + 0.5; dest_uv.y = distortion_y + 0.5; //--------------------------Algorithm End------------------------------------------ //Get texture from Channel0,and set dest_uv. fragColor = vec4( texture( iChannel0, dest_uv).r, texture( iChannel0,dest_uv).g,texture( iChannel0,dest_uv).b, 1. );}

上面的代碼中畸變參數K1,K2是畸變調節的參數,想要效果好就根據畸變算法所示那樣,疊加到Kn,多數情況下調整K1是能夠滿足需求的.”float distortion_k1 = 1.0 * sin(iGlobalTime*0.5)”這里的”iGlobalTime”是一個全局的時間變量,是為了讓畫面有動態效果,如果只想要做反畸變,可以把K1直接設置為0.5(這個值在項目中是要根據光學鏡片的畸變來調整de,有一些輔助工具可以幫助獲得這個值),就能看到”桶”型的反畸變效果: 這里寫圖片描述 原圖如下: 這里寫圖片描述 代碼其他部分,已經添加注釋了,如果項目中需要用到反畸變,可以將上面的代碼移植到自己所在的平臺上,我這里在openVR上調試后,效果還是可以的.

PS:最后,ShaderToy這工具真的很棒,純用數學算法就能制作出很精美的動畫,如果開放紋理上傳功能就更完美了.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 徐水县| 沭阳县| 青州市| 祁连县| 武安市| 武冈市| 武威市| 荔浦县| 射洪县| 玛纳斯县| 华安县| 博湖县| 湾仔区| 桑日县| 维西| 江油市| 高密市| 镇安县| 明溪县| 罗平县| 灯塔市| 洛阳市| 郧西县| 嵩明县| 崇左市| 江源县| 乐陵市| 石屏县| 清河县| 玛纳斯县| 梨树县| 如皋市| 涟水县| 辰溪县| 巧家县| 营口市| 长乐市| 潞西市| 古蔺县| 凤冈县| 丹寨县|