-
Notifications
You must be signed in to change notification settings - Fork 0
/
julia.sttf
1 lines (1 loc) · 11.4 KB
/
julia.sttf
1
{"links":[{"end":"RenderOutput","filter":"Linear","slot":0,"start":"Image","wrapMode":"Repeat"}],"metadata":{"Author":"iq","Description":"The normal is computed analytically from the gradient of the Green function instead of estimating it numerically. The ambient occlusion is faked by using the orbit traps. I made this one in 2007: [url]https://www.youtube.com/watch?v=9AX8gNyrSWc[/url]","Name":"Julia - Quaternion 1","ShaderToyURL":"https://www.shadertoy.com/view/MsfGRr"},"nodes":[{"class":"RenderOutput","name":"RenderOutput"},{"class":"GLSLShader","name":"Image","source":"// Copyright Inigo Quilez, 2013 - https://iquilezles.org/\n// I am the sole copyright owner of this Work.\n// You cannot host, display, distribute or share this Work in any form,\n// including physical and digital. You cannot use this Work in any\n// commercial or non-commercial product, website or project. You cannot\n// sell this Work and you cannot mint an NFTs of it.\n// I share this Work for educational purposes, and you can link to it,\n// through an URL, proper attribution and unmodified screenshot, as part\n// of your educational material. If these conditions are too restrictive\n// please contact me and we'll definitely work it out.\n\n// A port of my 2007 demo Kindernoiser: https://www.youtube.com/watch?v=9AX8gNyrSWc (http://www.pouet.net/prod.php?which=32549)\n//\n// Info here:\n// https://iquilezles.org/articles/juliasets3d\n//\n//\n// Related shaders\n//\n// Julia - Quaternion 1 : https://www.shadertoy.com/view/MsfGRr\n// Julia - Quaternion 2 : https://www.shadertoy.com/view/lsl3W2\n// Julia - Quaternion 3 : https://www.shadertoy.com/view/3tsyzl\n\n// antialias level (1, 2, 3...)\n#if HW_PERFORMANCE==1\n#define AA 1\n#else\n#define AA 2 // Set AA to 1 if your machine is too slow\n#endif\n\n\n// Normals computation:\n// 0: numerical gradient of d\n// 1: numerical gradient of G\n// 2: analytic gradient of G\n// 3: analytic gradient of G optimized\n#define METHOD 3\n\nvec4 qsqr( in vec4 a ) // square a quaterion\n{\n return vec4( a.x*a.x - a.y*a.y - a.z*a.z - a.w*a.w,\n 2.0*a.x*a.y,\n 2.0*a.x*a.z,\n 2.0*a.x*a.w );\n}\nvec4 qmul( in vec4 a, in vec4 b)\n{\n return vec4(\n a.x * b.x - a.y * b.y - a.z * b.z - a.w * b.w,\n a.y * b.x + a.x * b.y + a.z * b.w - a.w * b.z, \n a.z * b.x + a.x * b.z + a.w * b.y - a.y * b.w,\n a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y );\n\n}\nvec4 qconj( in vec4 a )\n{\n return vec4( a.x, -a.yzw );\n}\nfloat qlength2( in vec4 q )\n{\n return dot(q,q);\n}\n\nconst int numIterations = 11;\n\nfloat map( in vec3 p, out vec4 oTrap, in vec4 c )\n{\n vec4 z = vec4(p,0.0);\n float md2 = 1.0;\n float mz2 = dot(z,z);\n\n vec4 trap = vec4(abs(z.xyz),dot(z,z));\n\n float n = 1.0;\n for( int i=0; i<numIterations; i++ )\n {\n // dz -> 2·z·dz, meaning |dz| -> 2·|z|·|dz|\n // Now we take the 2.0 out of the loop and do it at the end with an exp2\n md2 *= 4.0*mz2;\n // z -> z^2 + c\n z = qsqr(z) + c; \n\n trap = min( trap, vec4(abs(z.xyz),dot(z,z)) );\n\n mz2 = qlength2(z);\n if(mz2>4.0) break;\n n += 1.0;\n }\n \n oTrap = trap;\n\n return 0.25*sqrt(mz2/md2)*log(mz2); // d = 0.5·|z|·log|z|/|z'|\n}\n\n#if METHOD==0\nvec3 calcNormal( in vec3 p, in vec4 c )\n{\n#if 1\n vec2 e = vec2(1.0,-1.0)*0.5773*0.001;\n vec4 za=vec4(p+e.xyy,0.0); float mz2a=qlength2(za), md2a=1.0;\n vec4 zb=vec4(p+e.yyx,0.0); float mz2b=qlength2(zb), md2b=1.0;\n vec4 zc=vec4(p+e.yxy,0.0); float mz2c=qlength2(zc), md2c=1.0;\n vec4 zd=vec4(p+e.xxx,0.0); float mz2d=qlength2(zd), md2d=1.0;\n \tfor(int i=0; i<numIterations; i++)\n {\n md2a *= mz2a; za = qsqr(za)+c; mz2a = qlength2(za);\n md2b *= mz2b; zb = qsqr(zb)+c; mz2b = qlength2(zb);\n md2c *= mz2c; zc = qsqr(zc)+c; mz2c = qlength2(zc);\n md2d *= mz2d; zd = qsqr(zd)+c; mz2d = qlength2(zd);\n }\n return normalize( e.xyy*sqrt(mz2a/md2a)*log2(mz2a) + \n\t\t\t\t\t e.yyx*sqrt(mz2b/md2b)*log2(mz2b) + \n\t\t\t\t\t e.yxy*sqrt(mz2c/md2c)*log2(mz2c) + \n\t\t\t\t\t e.xxx*sqrt(mz2d/md2d)*log2(mz2d) );\n#else \n const vec2 e = vec2(0.001,0.0);\n vec4 za=vec4(p+e.xyy,0.0); float mz2a=qlength2(za), md2a=1.0;\n vec4 zb=vec4(p-e.xyy,0.0); float mz2b=qlength2(zb), md2b=1.0;\n vec4 zc=vec4(p+e.yxy,0.0); float mz2c=qlength2(zc), md2c=1.0;\n vec4 zd=vec4(p-e.yxy,0.0); float mz2d=qlength2(zd), md2d=1.0;\n vec4 ze=vec4(p+e.yyx,0.0); float mz2e=qlength2(ze), md2e=1.0;\n vec4 zf=vec4(p-e.yyx,0.0); float mz2f=qlength2(zf), md2f=1.0;\n \tfor(int i=0; i<numIterations; i++)\n {\n md2a *= mz2a; za = qsqr(za) + c; mz2a = qlength2(za);\n md2b *= mz2b; zb = qsqr(zb) + c; mz2b = qlength2(zb);\n md2c *= mz2c; zc = qsqr(zc) + c; mz2c = qlength2(zc);\n md2d *= mz2d; zd = qsqr(zd) + c; mz2d = qlength2(zd);\n md2e *= mz2e; ze = qsqr(ze) + c; mz2e = qlength2(ze);\n md2f *= mz2f; zf = qsqr(zf) + c; mz2f = qlength2(zf);\n }\n float da = sqrt(mz2a/md2a)*log2(mz2a);\n float db = sqrt(mz2b/md2b)*log2(mz2b);\n float dc = sqrt(mz2c/md2c)*log2(mz2c);\n float dd = sqrt(mz2d/md2d)*log2(mz2d);\n float de = sqrt(mz2e/md2e)*log2(mz2e);\n float df = sqrt(mz2f/md2f)*log2(mz2f);\n \n return normalize( vec3(da-db,dc-dd,de-df) );\n#endif \n}\n#endif\n\n#if METHOD==1\nvec3 calcNormal( in vec3 p, in vec4 c )\n{\n const vec2 e = vec2(0.001,0.0);\n vec4 za = vec4(p+e.xyy,0.0);\n vec4 zb = vec4(p-e.xyy,0.0);\n vec4 zc = vec4(p+e.yxy,0.0);\n vec4 zd = vec4(p-e.yxy,0.0);\n vec4 ze = vec4(p+e.yyx,0.0);\n vec4 zf = vec4(p-e.yyx,0.0);\n\n \tfor(int i=0; i<numIterations; i++)\n {\n za = qsqr(za) + c; \n zb = qsqr(zb) + c; \n zc = qsqr(zc) + c; \n zd = qsqr(zd) + c; \n ze = qsqr(ze) + c; \n zf = qsqr(zf) + c; \n }\n return normalize( vec3(log2(qlength2(za))-log2(qlength2(zb)),\n log2(qlength2(zc))-log2(qlength2(zd)),\n log2(qlength2(ze))-log2(qlength2(zf))) );\n\n}\n#endif\n\n#if METHOD==2\nvec3 calcNormal( in vec3 p, in vec4 c )\n{\n vec4 z = vec4(p,0.0);\n\n // identity derivative\n mat4x4 J = mat4x4(1,0,0,0, \n 0,1,0,0, \n 0,0,1,0, \n 0,0,0,1 );\n\n \tfor(int i=0; i<numIterations; i++)\n {\n // chain rule of jacobians (removed the 2 factor)\n J = J*mat4x4(z.x, -z.y, -z.z, -z.w, \n z.y, z.x, 0.0, 0.0,\n z.z, 0.0, z.x, 0.0, \n z.w, 0.0, 0.0, z.x);\n\n // z -> z2 + c\n z = qsqr(z) + c; \n \n if(qlength2(z)>4.0) break;\n }\n\n return normalize( (J*z).xyz );\n}\n#endif\n\n#if METHOD==3\nvec3 calcNormal( in vec3 p, in vec4 c )\n{\n vec4 z = vec4(p,0.0);\n\n // identity derivative\n vec4 J0 = vec4(1,0,0,0);\n vec4 J1 = vec4(0,1,0,0);\n vec4 J2 = vec4(0,0,1,0);\n \n \tfor(int i=0; i<numIterations; i++)\n {\n vec4 cz = qconj(z);\n \n // chain rule of jacobians (removed the 2 factor)\n J0 = vec4( dot(J0,cz), dot(J0.xy,z.yx), dot(J0.xz,z.zx), dot(J0.xw,z.wx) );\n J1 = vec4( dot(J1,cz), dot(J1.xy,z.yx), dot(J1.xz,z.zx), dot(J1.xw,z.wx) );\n J2 = vec4( dot(J2,cz), dot(J2.xy,z.yx), dot(J2.xz,z.zx), dot(J2.xw,z.wx) );\n\n // z -> z2 + c\n z = qsqr(z) + c; \n \n if(qlength2(z)>4.0) break;\n }\n \n\tvec3 v = vec3( dot(J0,z), \n dot(J1,z), \n dot(J2,z) );\n\n return normalize( v );\n}\n#endif\n\n\n\n\n\nfloat intersect( in vec3 ro, in vec3 rd, out vec4 res, in vec4 c )\n{\n vec4 tmp;\n float resT = -1.0;\n\tfloat maxd = 10.0;\n float h = 1.0;\n float t = 0.0;\n for( int i=0; i<300; i++ )\n {\n if( h<0.0001||t>maxd ) break;\n\t h = map( ro+rd*t, tmp, c );\n t += h;\n }\n if( t<maxd ) { resT=t; res = tmp; }\n\n\treturn resT;\n}\n\nfloat softshadow( in vec3 ro, in vec3 rd, float mint, float k, in vec4 c )\n{\n float res = 1.0;\n float t = mint;\n for( int i=0; i<64; i++ )\n {\n vec4 kk;\n float h = map(ro + rd*t, kk, c);\n res = min( res, k*h/t );\n if( res<0.001 ) break;\n t += clamp( h, 0.01, 0.5 );\n }\n return clamp(res,0.0,1.0);\n}\n\nvec3 render( in vec3 ro, in vec3 rd, in vec4 c )\n{\n\tconst vec3 sun = vec3( 0.577, 0.577, 0.577 );\n \n\tvec4 tra;\n\tvec3 col;\n float t = intersect( ro, rd, tra, c );\n if( t < 0.0 )\n {\n \tcol = vec3(0.7,0.9,1.0)*(0.7+0.3*rd.y);\n\t\tcol += vec3(0.8,0.7,0.5)*pow( clamp(dot(rd,sun),0.0,1.0), 48.0 );\n\t}\n\telse\n\t{\n vec3 mate = vec3(1.0,0.8,0.7)*0.3;\n\t\t//mate.x = 1.0-10.0*tra.x;\n \n vec3 pos = ro + t*rd;\n vec3 nor = calcNormal( pos, c );\n \n\t\tfloat occ = clamp(2.5*tra.w-0.15,0.0,1.0);\n\t\t\n\n col = vec3(0.0);\n\n // sky\n {\n float co = clamp( dot(-rd,nor), 0.0, 1.0 );\n vec3 ref = reflect( rd, nor );\n //float sha = softshadow( pos+0.0005*nor, ref, 0.001, 4.0, c );\n float sha = occ;\n sha *= smoothstep( -0.1, 0.1, ref.y );\n float fre = 0.1 + 0.9*pow(1.0-co,5.0);\n \n\t\tcol = mate*0.3*vec3(0.8,0.9,1.0)*(0.6+0.4*nor.y)*occ;\n\t\tcol += 2.0*0.3*vec3(0.8,0.9,1.0)*(0.6+0.4*nor.y)*sha*fre;\n }\n\n // sun\n {\n const vec3 lig = sun;\n float dif = clamp( dot( lig, nor ), 0.0, 1.0 );\n float sha = softshadow( pos, lig, 0.001, 64.0, c );\n vec3 hal = normalize( -rd+lig );\n float co = clamp( dot(hal,lig), 0.0, 1.0 );\n float fre = 0.04 + 0.96*pow(1.0-co,5.0);\n float spe = pow(clamp(dot(hal,nor), 0.0, 1.0 ), 32.0 );\n col += mate*3.5*vec3(1.00,0.90,0.70)*dif*sha;\n col += 7.0*3.5*vec3(1.00,0.90,0.70)*spe*dif*sha*fre;\n }\n\n // extra fill\n {\n const vec3 lig = vec3( -0.707, 0.000, -0.707 );\n\t\tfloat dif = clamp(0.5+0.5*dot(lig,nor), 0.0, 1.0 );\n col += mate* 1.5*vec3(0.14,0.14,0.14)*dif*occ;\n }\n \n // fake SSS\n {\n float fre = clamp( 1.+dot(rd,nor), 0.0, 1.0 );\n col += mate* mate*0.6*fre*fre*(0.2+0.8*occ);\n }\n }\n\n\treturn pow( col, vec3(0.4545) );\n}\n\nvoid mainImage( out vec4 fragColor, in vec2 fragCoord )\n{\n // anim\n float time = iTime*.15;\n vec4 c = 0.45*cos( vec4(0.5,3.9,1.4,1.1) + time*vec4(1.2,1.7,1.3,2.5) ) - vec4(0.3,0.0,0.0,0.0);\n\n // camera\n\tfloat r = 1.5+0.15*cos(0.0+0.29*time);\n\tvec3 ro = vec3( r*cos(0.3+0.37*time), \n\t\t\t\t\t0.3 + 0.8*r*cos(1.0+0.33*time), \n\t\t\t\t\t r*cos(2.2+0.31*time) );\n\tvec3 ta = vec3(0.0,0.0,0.0);\n float cr = 0.1*cos(0.1*time);\n \n \n // render\n vec3 col = vec3(0.0);\n for( int j=0; j<AA; j++ )\n for( int i=0; i<AA; i++ )\n {\n vec2 p = (-iResolution.xy + 2.0*(fragCoord + vec2(float(i),float(j))/float(AA))) / iResolution.y;\n\n vec3 cw = normalize(ta-ro);\n vec3 cp = vec3(sin(cr), cos(cr),0.0);\n vec3 cu = normalize(cross(cw,cp));\n vec3 cv = normalize(cross(cu,cw));\n vec3 rd = normalize( p.x*cu + p.y*cv + 2.0*cw );\n\n col += render( ro, rd, c );\n }\n col /= float(AA*AA);\n \n vec2 uv = fragCoord.xy / iResolution.xy;\n\tcol *= 0.7 + 0.3*pow(16.0*uv.x*uv.y*(1.0-uv.x)*(1.0-uv.y),0.25);\n \n\tfragColor = vec4( col, 1.0 );\n}\n","type":"Image"}]}