好久没有写文章了,赶紧补几篇。最近研究了一个玻璃折射的效果(用在钻石上),虽然没有达到最满意的效果,还是先分享出来,待以后有更好的想法再补充。

先看效果吧:

这里面有两个效果,左边是unity的免费插件Gem Shader,右边的是我自己实现的,我将分别介绍这两个效果的实现方法。

一、知识补充:

钻石的图片:

两个shader都使用了CubeMap...

Shader "FX/Diamond"
{Properties {_Color ("Color", Color) = (1,1,1,1)_ReflectTex ("Reflection Texture", Cube) = "dummy.jpg" {TexGen CubeReflect}_RefractTex ("Refraction Texture", Cube) = "dummy.jpg" {TexGen CubeReflect}}    SubShader {Tags {"Queue" = "Transparent"}// First pass - here we render the backfaces of the diamonds. Since those diamonds are more-or-less// convex objects, this is effectively rendering the inside of themPass {Color (0,0,0,0)Offset  -1, -1Cull FrontZWrite OffSetTexture [_RefractTex] {constantColor [_Color]combine texture * constant, primary}SetTexture [_ReflectTex] {combine previous, previous +- texture}}// Second pass - here we render the front faces of the diamonds.Pass {Fog { Color (0,0,0,0)}ZWrite onBlend One OneSetTexture [_RefractTex] {constantColor [_Color]combine texture * constant}SetTexture [_ReflectTex] {combine texture + previous, previous +- texture}}}// Older cards. Here we remove the bright specular highlightSubShader {// First pass - here we render the backfaces of the diamonds. Since those diamonds are more-or-less// convex objects, this is effectively rendering the inside of themPass {Color (0,0,0,0)Cull FrontSetTexture [_RefractTex] {constantColor [_Color]combine texture * constant, primary}}// Second pass - here we render the front faces of the diamonds.Pass {Fog { Color (0,0,0,0)}ZWrite offBlend One OneSetTexture [_RefractTex] {constantColor [_Color]combine texture * constant}}}// Ancient cards without cubemapping support// We could use a 2D refletction texture, but the chances of getting one of these cards are slim, so we won't bother.SubShader {Pass {Color [_Color]}}
}
Shader "stalendp/myDiamondShader" {Properties {_Color ("Color", Color) = (1,1,1,1)_ReflectTex ("Reflection Texture", Cube) = "" { }_RefractTex ("Refraction Texture", Cube) = "" { }} CGINCLUDE  #include "UnityCG.cginc"             fixed4 _Color;  samplerCUBE _RefractTex;    samplerCUBE _ReflectTex;       struct v2f {      half4 pos:SV_POSITION;      half3 Reflect : TEXCOORD0;     half3 RefractR : TEXCOORD1;half3 RefractG : TEXCOORD2;half3 RefractB : TEXCOORD3;half Ratio : TEXCOORD4;};    v2f vert(appdata_full v) {    float EtaR = 0.65;float EtaG = 0.67;float EtaB = 0.69;float FresnelPower = 5.0;float F = ((1.0-EtaG) * (1.0-EtaG)) / ((1.0+EtaG) * (1.0+EtaG));float3 i = -normalize(ObjSpaceViewDir(v.vertex)); float3 n = normalize(v.normal); // ??????v2f o;o.Ratio = F + (1.0 - F) * pow((1.0 - dot(-i, n)), FresnelPower) ;o.RefractR = refract(i, n, EtaR);o.RefractG = refract(i, n, EtaG);o.RefractB = refract(i, n, EtaB);o.Reflect = reflect(i, n);o.pos = mul (UNITY_MATRIX_MVP, v.vertex);   return o;    }    fixed4 frag(v2f i) : COLOR0 {  float3 refractColor, reflectColor;refractColor.r = float3(texCUBE(_RefractTex, i.RefractR)).r;refractColor.g = float3(texCUBE(_RefractTex, i.RefractG)).g;refractColor.b = float3(texCUBE(_RefractTex, i.RefractB)).b;reflectColor = float3(texCUBE(_ReflectTex, i.Reflect));return  2*_Color * pow(fixed4(lerp( _Color * refractColor * 5, reflectColor * 2, i.Ratio), 1), 1.8);}    ENDCG      SubShader {     Pass {      CGPROGRAM      #pragma vertex vert      #pragma fragment frag      #pragma fragmentoption ARB_precision_hint_fastest       ENDCG      }  }  FallBack Off
}  

关键词: diamond、crystal、glass、refraction、cubeMap、钻石、水晶、玻璃、折射
参考资料:

1. CubeMap技术相关:http://docs.unity3d.com/Manual/SL-ImplementingTexGen.html

2.[GPU Gems]Environment Mapping Techniques:http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter07.html

3.《OpenGL Shading Language 3rd edition》14.1 Refraction :  https://www.scss.tcd.ie/Michael.Manzke/CS7055/GLSL/GLSL-3rdEd-refraction.pdf

4.[GPU Gems]Generic Refraction Simulation:http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter19.html

5. 某个大学的ppt: http://users.ece.gatech.edu/lanterma/mpg08/mpglecture13f08_4up.pdf

6. Fresnel原理:http://www.3drender.com/glossary/fresneleffect.htm

7. AMD关于diamond算法的文章:http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2012/10/Gosselin_Gem.pdf

8. CG Shaders - Cubemap Refraction: http://www.artisticexperiments.com/cg-shaders/cg-shaders-cubemap-refraction

9. 水滴折射效果:http://wiki.unity3d.com/index.php?title=RefractionEffect

【OpenGL】Shader实例分析(十)- 钻石效果相关推荐

  1. android 4实例分析,OpenGL Shader实例分析(4)闪光效果

    本文实例为大家分享了opengl shader实例闪光效果的具体代码,供大家参考,具体内容如下 在游戏中,当战斗结束后,对一些获取的宝贝需要进行闪光处理.这篇文章介绍一个进行闪光处理的shader,运 ...

  2. android+动态光圈效果,OpenGL Shader实例分析(8)彩色光圈效果

    本文实例为大家分享了opengl实现彩色光圈效果的具体代码,供大家参考,具体内容如下 研究了一个彩色光圈效果,感觉挺不错的,分享给大家,效果如下: 代码如下: shader "shadert ...

  3. android雪花飘落效果,【OpenGL】Shader实例分析(七)- 雪花飘落效果

    研究了一个雪花飘落效果.感觉挺不错的.分享给大家,效果例如以下: 代码例如以下: Shader "shadertoy/Flakes" { // https://www.shader ...

  4. 【OpenGL】Shader实例分析(六)- 卡牌特效

    转发请保持地址:http://blog.csdn.net/stalendp/article/details/30989295 本文将介绍怎么通过alpha通道来隐藏信息.并实现卡牌特效. 执行效果例如 ...

  5. 创意进度条设计-12个优秀作品分享【附实例分析】

    本文分享了12个创意进度条设计,多为Dribble上的作品,点击图片即可查看原作品,同时附上了案例分析-蜗牛进度条的设计步骤,希望能对你有帮助. 什么叫进度条? 进度条即计算机在处理任务时,实时的,以 ...

  6. 使用OpenGL Shader实现放大镜效果

    使用OpenGL Shader实现放大镜效果 2014年3月16日renjihe发表评论阅读评论 周末闲来无事,想玩玩OpenGL Shader,想想就实现一个放大镜效果的Shader吧. 着色器可以 ...

  7. OpenGL(十九)——Qt OpenGL波动纹理(旗子的飘动效果)

    OpenGL(十九)--Qt OpenGL波动纹理(旗子的飘动效果) 一.场景 在日常的项目中,我们经常会实现波动的一些纹理效果,比如飘动的旗子,水的波纹,地图上某一点的波浪圈圈等...,本篇介绍波动 ...

  8. 【程序设计基础】第九、十、十一章 综合实例分析 递归

    实例分析: 闰年算法变化 1 resule:=0 2 if (y mod 400=0) or ((y mod 4=0) and (y mod 100<>0)) 3 then result: ...

  9. OpenGL shader normals法线贴图的实例

    OpenGL shader normals法线贴图 先上图,再解答. 完整主要的源代码 源代码剖析 先上图,再解答. 完整主要的源代码 #include <glad/glad.h> #in ...

最新文章

  1. rsyslog日志管理+LogAnalyzer
  2. leetcode算法题--扑克牌中的顺子
  3. 原来,07年我把自己给和谐了
  4. 认识FPGA触发器的亚稳态
  5. .NET Core UI框架Avalonia
  6. 来自学长同学分享的学习方法
  7. 全栈工程师已经过时?
  8. Weblogic的classpath设置
  9. 巴菲特2021年致股东信披露重仓股名单
  10. SQL AZURE数据导入导出,云计算体验之四
  11. 如何在邮件中加入html代码,如何在电子邮件正文中嵌入HTML文件
  12. Ubuntu设置截图的快捷键
  13. Linux考试题(带答案)
  14. 海思Hi3798MV310机顶盒芯片Datasheet-基本信息
  15. VisualStudio2019 安装时下载不动或者显示下载失败
  16. 时间的流逝,流逝了谁的无奈?
  17. Neodynamic Barcode Professional for Windows Forms 14.0
  18. AD9361使用概述
  19. 苹果新功能惹众怒,4000多家组织和个人签署公开信 敦促苹果放弃“儿童安全”功能
  20. 动手学深度学习(二十二)——GoogLeNet:CNN经典模型(五)

热门文章

  1. Decentraland元宇宙时装周,穿戴式NFT的兴起
  2. 随笔-年薪百万就从小事做起
  3. git 局域网 两台电脑之间同步 (不用安装Java和Gitblit和OpenSSH)
  4. 项目管理大师-哈罗德.科兹纳的管理名言
  5. word 标题自动编号、按章节给图片设置题注、给图片添加对应的文字交叉引用
  6. 使命召唤15显示服务器封锁,使命召唤15NAT连接失败 使命召唤15NAT连接解决方法...
  7. 目前最全的手机号码运营商正则
  8. u盘文件看得见却打不开_为什么u盘里的文件打不开_u盘文件看得见却打不开怎么修复-win7之家...
  9. Axure钢笔工具使用技巧
  10. java sso单点登录源码_Java单点登录系统 sso源码下载