【OpenGL】Shader技巧集合

这篇文章将收集unity中使用shader的相关技巧和特效,会不断地更新内容。关于在Unity中使用shader的介绍,请参考《【OpenGL】使用Unity来学习OpenGL

常用的内置uniform

iResolution =》_ScreenParams

iGlobalTime => _Time.y

glFragCoord => float4 sp:WPOS  // 需要 #pragma target 3.0, 另外的方式请见下面

vec2 => float2

mix => lerp

mod => fmod

texture2D => tex2D

textureCube => texCUBE

mat2=>float2x2

fract=>frac

========

关于glFragCoord, 可以使用另外一种方式计算(支持3.0之前的)参考官方例子

o.scrPos = ComputeScreenPos(o.pos);

float2 wcoord = (i.scrPos.xy/i.scrPos.w);

-------

float2 wcoord = sp.xy/_ScreenParams.xy;


关于数学的Shader:https://www.shadertoy.com/view/ldlSD2    https://www.shadertoy.com/view/ldlSWj


很好的一个教程:http://ogldev.atspace.co.uk/index.html


Deferred Shading 原理: http://ogldev.atspace.co.uk/www/tutorial35/tutorial35.html 


关于Stencil Buffer 的理解:http://www.cnblogs.com/mikewolf2002/archive/2012/05/15/2500867.html

更多文章:1)http://docs.unity3d.com/Manual/SL-Stencil.html

2) http://answers.unity3d.com/questions/590800/how-to-cullrender-to-through-a-window.html


Stencil Shadow Volume : http://ogldev.atspace.co.uk/www/tutorial40/tutorial40.html

http://en.wikipedia.org/wiki/Shadow_volume


镜面反射的实现原理:

ftp://ftp.sgi.com/sgi/opengl/contrib/blythe/advanced99/notes/node158.html

其它镜面反射:

http://en.wikibooks.org/wiki/Cg_Programming/Unity/Mirrors


在unity cg中可以使用[HideInInspector]来隐藏uniform属性,这样就可以用作自定义常量。

Physically Based Rendering:   Tutorial: Physically Based Rendering, And you can too!

边缘检测:1) http://www.codeproject.com/Articles/94817/Pixel-Shader-for-Edge-Detection-and-Cartoon-Effect

2) http://coding-experiments.blogspot.hk/2010/06/edge-detection.html

3) http://en.wikipedia.org/wiki/Edge_detection

Cg函数表:http://http.developer.nvidia.com/CgTutorial/cg_tutorial_appendix_e.html

heat effect : http://forum.unity3d.com/threads/50132-Heat-Distortion,   http://www.cnblogs.com/geoffyange/archive/2013/06/06/3122570.html

skin shading in unity: http://www.altdevblogaday.com/2011/12/31/skin-shading-in-unity3d/

http://http.developer.nvidia.com/GPUGems3/gpugems3_ch14.html

http://gamedev.stackexchange.com/questions/31308/algorithm-for-creating-spheres

RenderMan University: http://renderman.pixar.com/view/renderman-university

一些shader的例子:

Shader "shaderToy/LolCrap" {
	Properties {
		_MainTex ("image", 2D) = "white" {}
		_NoiseTex("noise", 2D) = "bump" {}
		_percent("percent", Range(-0.3, 1)) = 0
		_DefColor ("defalutColor", COLOR)  = ( 0, .8, .4, 1)
	}
	
	CGINCLUDE
        #include "UnityCG.cginc"           
      	#pragma target 3.0  
        #pragma glsl    
		          
		float mod289(float x) {
		    return x - floor(x * (1.0 / 289.0)) * 289.0;
		}

		float4 mod289(float4 x) {
		    return x - floor(x * (1.0 / 289.0)) * 289.0;
		}

		float4 perm(float4 x) {
		    return mod289(((x * 34.0) + 1.0) * x);
		}

		float noise3d(float3 p) {
		    float3 a = floor(p);
		    float3 d = p - a;
		    d = d * d * (3.0 - 2.0 * d);

		    float4 b = a.xxyy + float4(0.0, 1.0, 0.0, 1.0);
		    float4 k1 = perm(b.xyxy);
		    float4 k2 = perm(k1.xyxy + b.zzww);

		    float4 c = k2 + a.zzzz;
		    float4 k3 = perm(c);
		    float4 k4 = perm(c + 1.0);

		    float4 o1 = frac(k3 * (1.0 / 41.0));
		    float4 o2 = frac(k4 * (1.0 / 41.0));

		    float4 o3 = o2 * d.z + o1 * (1.0 - d.z);
		    float2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x);

		    return o4.y * d.y + o4.x * (1.0 - d.y);
		}

		struct v2f {    
            half4 pos:SV_POSITION;    
            half4 uv : TEXCOORD0;   
        };  

 		v2f vert(appdata_base v) {
            v2f o;  
            o.pos = mul (UNITY_MATRIX_MVP, v.vertex);  
            return o;  
        }
  
        fixed4 frag(float4 sp:WPOS) : COLOR0 {
		        
		    float2 uv = 2.0 * sp.xy / _ScreenParams.xy - 1.0;
			
		    float3 water[4];
		    float3 fire[4];

		    float3x3 r = float3x3(0.36, 0.48, -0.8, -0.8, 0.60, 0.0, 0.48, 0.64, 0.60);
		    float3 p_pos = mul(float3(uv * float2(16.0, 9.0), 0.0), r);
		    float3 p_time = mul(float3(0.0, 0.0, _Time.y * 2.0), r);

		//    /* Noise sampling points for water */
		    water[0] = p_pos / 2.0 + p_time;
		    water[1] = p_pos / 4.0 + p_time;
		    water[2] = p_pos / 8.0 + p_time;
		    water[3] = p
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值