unity3d shader之Julia集和Mandelbrot集绘制美丽图案 (二)

130 篇文章 5 订阅
86 篇文章 0 订阅

之前写过一篇Mandelbrot集的代码在这里

这次主要写Julia集的

Mandelbrot 集内的每一个点都对应了一个连通的 Julia 集,Mandelbrot 集合外的点则对应了不连通的 Julia 集

Julia集合可以由下式进行反复迭代得到:f(z) = z^2 + c 

这篇文章(www.matrix67.com/blog/archives/4570)讲的非常详细

迭代次数,C值均可调, 先来看看效果:


代码在此:

Shader "Custom/julia1x" {
		Properties {
		_MainTex ("Noise", 2D) = "white" {}
		_MaxIters("Max Iters", range(0,85)) = 1//迭代次数
		_CR("CR", range(-1,1)) = -0.7//y的C
		_CI("CI", range(-1,1)) = -0.7//x的C
		_TS("TS", range(0,15)) = 1//x缩放
		_CS("CS", range(0,15)) = 1//y缩放
		_S0("S0", range(-3,3)) = 0.5//x偏移
		_T0("T0", range(-3,3)) = 0.5//y偏移
		_Limits("iLimits", range(0,19)) = 5
		_ConColor("ConColor",color)=(1,1,1,1)
		_DivColor1("Diverge Color1",color)=(1,1,1,1)
		_DivColor2("Diverge Color2",color)=(1,1,1,1)
	}
	SubShader {
		pass{
		Tags{"LightMode"="ForwardBase" }
		Cull off
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "UnityCG.cginc"

		float4 _LightColor0;
		int _MaxIters;
		float _TS;
		float _CS;
		float _S0;
		float _T0;
		float _Limits;
		float _CR;
		float _CI;
		float4 _ConColor;
		float4 _DivColor1;
		float4 _DivColor2;
		float4 _MainTex_ST;
		struct v2f {
			float4 pos:SV_POSITION;
			float2 uv_MainTex:TEXCOORD0;
		};

		v2f vert (appdata_full v) {
			v2f o;
			o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
			o.uv_MainTex = TRANSFORM_TEX(v.texcoord,_MainTex);
			return o;
		}
		
		float4 frag(v2f i):COLOR
		{
			float real = i.uv_MainTex.x * _TS + _S0;//x
			float imag = i.uv_MainTex.y * _CS + _T0;//y
			float newr;
			int numIters;

			float4 color = 0;

			for(numIters = 0;numIters < _MaxIters;numIters++)
			{
				float newreal =  real * real;
				float newimag =  imag * imag;
				newr = newreal + newimag;
				if(newr >= _Limits)
					break;
				imag = real * imag * 2;
				real = newreal - newimag;
				real += _CR;
				imag += _CI;
			}

			if(newr<_Limits)
				color = _ConColor;
			if(newr>_Limits)
				color = lerp(_DivColor1,_DivColor2, frac(numIters/_CS));

			return color;
		}
		ENDCG
		}//

	} 
}

                                                                                   -----------------------by wolf96 http://blog.csdn.net/wolf96


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值