本文将介绍怎么用Shader来绘制一个跳动的心脏。这里会涉及到一些数学知识。先看效果图:
为若羌等地区用户提供了全套网页设计制作服务,及若羌网站建设行业解决方案。主营业务为成都网站建设、成都网站设计、若羌网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!源代码如下:
// Created by inigo quilez - iq/2013 // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. // changed by stalendp@gmail.com Shader "shadertoy/Heart" { // see https://www.shadertoy.com/view/XsfGRn 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); return o; } fixed4 frag(float4 sp:WPOS) : COLOR0 { float2 p = (2.0*sp.xy - _ScreenParams.xy) / min(_ScreenParams.y, _ScreenParams.x); p.y -= 0.25; //background color float3 bcol = float3(1.0,0.8,0.7-0.07*p.y) * (1.0 - 0.25*length(p)); // animate float tt = fmod(_Time.y, 1.5)/1.5; float ss = pow(tt,.2) * 0.5 + 0.5; ss -= ss*0.2*sin(tt*6.2831*3.0)*exp(-tt*4.0); p *= float2(0.5, 1.5) + ss * float2(0.5, -0.5); // shape float a = atan2(p.x,p.y)/3.141593; float r = length(p); float h = abs(a); float d = (13.0*h - 22.0*h*h + 10.0*h*h*h)/(6.0-5.0*h); // color float s = 1.0-0.5*clamp(r/d,0.0,1.0); s = 0.75 + 0.75*p.x; s *= 1.0-0.25*r; s = 0.5 + 0.6*s; s *= 0.5+0.5*pow( 1.0-clamp(r/d, 0.0, 1.0 ), 0.1 ); float3 hcol = float3(1.0,0.5*r,0.3)*s; float3 col = lerp( bcol, hcol, smoothstep( -0.01, 0.01, d-r) ); return float4(col,1.0); } ENDCG SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest ENDCG } } FallBack Off }