【OpenGL】Shader实例分析(一)-Wave

本文深入分析了OpenGL Shader的使用,特别是针对Wave效果的实现。通过ComputeScreenPos转换三维坐标,结合fmod和step函数创建背景虚线网格,再利用lerp函数绘制波纹效果,展示了Shader在图形渲染中的魅力。
摘要由CSDN通过智能技术生成

转发请保持地址:http://blog.csdn.net/stalendp/article/details/21993227

这篇文章主要分析一个Shader,从而感受shader的魅力,并学习相关shader的函数的用法。

先看Shader运行的效果:


下面是代码:

Shader "shadertoy/Waves" {  //see https://www.shadertoy.com/view/4dsGzH

	CGINCLUDE  

		#include "UnityCG.cginc"              
		#pragma target 3.0  
		struct vertOut {  
			float4 pos:SV_POSITION;  
			float4 srcPos; 
		};

		vertOut vert(appdata_base v) {
			vertOut o;
			o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
			o.srcPos = ComputeScreenPos(o.pos);
			return o;
		}

		fixed4 frag(vertOut i) : COLOR0 {

			fixed3 COLOR1 = fixed3(0.0,0.0,0.3);
			fixed3 COLOR2 = fixed3(0.5,0.0,0.0);
			float BLOCK_WIDTH = 0.03;

			float2 uv = (i.srcPos.xy/i.srcPos.w);

			// To create the BG pattern
			fixed3 final_color = fixed3(1.0);
			fixed3 bg_color = fixed3(0.0);
			fixed3 wave_color = fixed3(0.0);

			float c1 = fmod(uv.x, 2.0* BLOCK_WIDTH);
			c1 = step(BLOCK_WIDTH, c1);
			float c2 = fmod(uv.y, 2.0* BLOCK_WIDTH);
			c2 = step(BLOCK_WIDTH, c2);
			bg_color = lerp(uv.x * COLOR1, uv.y * COLOR2, c1*c2
  • 8
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值