Skip to content

Commit

Permalink
SDF and Noise helpers use proxies to solve arg name collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Dec 24, 2024
1 parent ff92fbd commit df5727c
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 75 deletions.
109 changes: 62 additions & 47 deletions ultraviolet/js/src/test/scala/ultraviolet/noiseTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,42 @@ class noiseTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|vec2 def1(in vec2 value){
|vec2 def2(in vec2 value){
| return value-((floor(value*0.0034602077212184668))*289.0);
|}
|vec3 def2(in vec3 value){
|vec3 def3(in vec3 value){
| return value-((floor(value*0.1428571492433548))*7.0);
|}
|vec3 def4(in vec3 value){
|vec3 def5(in vec3 value){
| return value-((floor(value*0.0034602077212184668))*289.0);
|}
|vec3 def3(in vec3 value){
| return def4(((34.0*value)+10.0)*value);
|vec3 def4(in vec3 value){
| return def5(((34.0*value)+10.0)*value);
|}
|vec2 def0(in vec2 p){
|vec2 def1(in vec2 _ptArg){
| float K=0.1428571492433548;
| float Ko=0.4285714328289032;
| float jitter=1.0;
| vec2 Pi=def1(floor(p));
| vec2 Pf=fract(p);
| vec2 Pi=def2(floor(_ptArg));
| vec2 Pf=fract(_ptArg);
| vec3 oi=vec3(-1.0,0.0,1.0);
| vec3 of=vec3(-0.5,0.5,1.5);
| vec3 px=def3(Pi.x+oi);
| vec3 p=def3((px.x+Pi.y)+oi);
| vec3 px=def4(Pi.x+oi);
| vec3 p=def4((px.x+Pi.y)+oi);
| vec3 ox=(fract(p*K))-Ko;
| vec3 oy=((def2(floor(p*K)))*K)-Ko;
| vec3 oy=((def3(floor(p*K)))*K)-Ko;
| vec3 dx=(Pf.x+0.5)+(jitter*ox);
| vec3 dy=(Pf.y-of)+(jitter*oy);
| vec3 d1=(dx*dx)+(dy*dy);
| p=def3((px.y+Pi.y)+oi);
| p=def4((px.y+Pi.y)+oi);
| ox=(fract(p*K))-Ko;
| oy=((def2(floor(p*K)))*K)-Ko;
| oy=((def3(floor(p*K)))*K)-Ko;
| dx=(Pf.x-0.5)+(jitter*ox);
| dy=(Pf.y-of)+(jitter*oy);
| vec3 d2=(dx*dx)+(dy*dy);
| p=def3((px.z+Pi.y)+oi);
| p=def4((px.z+Pi.y)+oi);
| ox=(fract(p*K))-Ko;
| oy=((def2(floor(p*K)))*K)-Ko;
| oy=((def3(floor(p*K)))*K)-Ko;
| dx=(Pf.x-1.5)+(jitter*ox);
| dy=(Pf.y-of)+(jitter*oy);
| vec3 d3=(dx*dx)+(dy*dy);
Expand All @@ -86,6 +86,9 @@ class noiseTests extends munit.FunSuite {
| d1Flip3=vec3(d1Flip3.x,min(d1Flip3.y,d2.x),d1Flip3.z);
| return sqrt(d1Flip3.xy);
|}
|vec2 def0(in vec2 p){
| return def1(p);
|}
|def0(vec2(0.0));
|""".stripMargin.trim
)
Expand All @@ -111,30 +114,30 @@ class noiseTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|vec4 def1(in vec4 value){
|vec4 def2(in vec4 value){
| return value-((floor(value*0.0034602077212184668))*289.0);
|}
|vec4 def3(in vec4 value){
|vec4 def4(in vec4 value){
| return value-((floor(value*0.0034602077212184668))*289.0);
|}
|vec4 def2(in vec4 value){
| return def3(((value*34.0)+10.0)*value);
|vec4 def3(in vec4 value){
| return def4(((value*34.0)+10.0)*value);
|}
|vec4 def4(in vec4 value){
|vec4 def5(in vec4 value){
| return 1.7928428649902344-(0.8537347316741943*value);
|}
|vec2 def5(in vec2 value){
|vec2 def6(in vec2 value){
| return ((value*value)*value)*((value*((value*6.0)-15.0))+10.0);
|}
|float def0(in vec2 p){
| vec4 Pi=floor(p.xyxy)+vec4(0.0,0.0,1.0,1.0);
| vec4 Pf=fract(p.xyxy)-vec4(0.0,0.0,1.0,1.0);
| Pi=def3(Pi);
|float def1(in vec2 _ptArg){
| vec4 Pi=floor(_ptArg.xyxy)+vec4(0.0,0.0,1.0,1.0);
| vec4 Pf=fract(_ptArg.xyxy)-vec4(0.0,0.0,1.0,1.0);
| Pi=def4(Pi);
| vec4 ix=Pi.xzxz;
| vec4 iy=Pi.yyww;
| vec4 fx=Pf.xzxz;
| vec4 fy=Pf.yyww;
| vec4 i=def2(def2(ix)+iy);
| vec4 i=def3(def3(ix)+iy);
| vec4 gx=((fract(i*0.024390242993831635))*2.0)-1.0;
| vec4 gy=abs(gx)-0.5;
| vec4 tx=floor(gx+0.5);
Expand All @@ -143,7 +146,7 @@ class noiseTests extends munit.FunSuite {
| vec2 g10=vec2(gx.y,gy.y);
| vec2 g01=vec2(gx.z,gy.z);
| vec2 g11=vec2(gx.w,gy.w);
| vec4 norm=def4(vec4(dot(g00,g00),dot(g01,g01),dot(g10,g10),dot(g11,g11)));
| vec4 norm=def5(vec4(dot(g00,g00),dot(g01,g01),dot(g10,g10),dot(g11,g11)));
| g00=g00*norm.x;
| g01=g01*norm.y;
| g10=g10*norm.z;
Expand All @@ -152,11 +155,14 @@ class noiseTests extends munit.FunSuite {
| float n10=dot(g10,vec2(fx.y,fy.y));
| float n01=dot(g01,vec2(fx.z,fy.z));
| float n11=dot(g11,vec2(fx.w,fy.w));
| vec2 fade_xy=def5(Pf.xy);
| vec2 fade_xy=def6(Pf.xy);
| vec2 n_x=mix(vec2(n00,n01),vec2(n10,n11),fade_xy.x);
| float n_xy=mix(n_x.x,n_x.y,fade_xy.y);
| return 2.299999952316284*n_xy;
|}
|float def0(in vec2 p){
| return def1(p);
|}
|def0(vec2(0.0));
|""".stripMargin.trim
)
Expand All @@ -182,26 +188,29 @@ class noiseTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|vec2 def1(in vec2 value){
|vec2 def2(in vec2 value){
| vec2 k=vec2(0.3183099031448364,0.36787939071655273);
| vec2 y=(value*k)+k.yx;
| return (-1.0)+(2.0*(fract((16.0*k)*(fract((y.x*y.y)*(y.x+y.y))))));
|}
|vec3 def0(in vec2 p){
| vec2 i=floor(p);
| vec2 f=fract(p);
|vec3 def1(in vec2 _ptArg){
| vec2 i=floor(_ptArg);
| vec2 f=fract(_ptArg);
| vec2 u=(f*f)*(3.0-(2.0*f));
| vec2 du=(6.0*f)*(1.0-f);
| vec2 ga=def1(i+vec2(0.0,0.0));
| vec2 gb=def1(i+vec2(1.0,0.0));
| vec2 gc=def1(i+vec2(0.0,1.0));
| vec2 gd=def1(i+vec2(1.0,1.0));
| vec2 ga=def2(i+vec2(0.0,0.0));
| vec2 gb=def2(i+vec2(1.0,0.0));
| vec2 gc=def2(i+vec2(0.0,1.0));
| vec2 gd=def2(i+vec2(1.0,1.0));
| float va=dot(ga,f-vec2(0.0,0.0));
| float vb=dot(gb,f-vec2(1.0,0.0));
| float vc=dot(gc,f-vec2(0.0,1.0));
| float vd=dot(gd,f-vec2(1.0,1.0));
| return vec3((va+(u.x*(vb-va)))+(u.y*(vc-va))+(u.x*u.y)*(((va-vb)-vc)+vd),(ga+(u.x*(gb-ga)))+(u.y*(gc-ga))+(u.x*u.y)*(((ga-gb)-gc)+gd)+(du*((u.yx*(((va-vb)-vc)+vd))+vec2(vb,vc)-va)));
|}
|vec3 def0(in vec2 p){
| return def1(p);
|}
|def0(vec2(0.0));
|""".stripMargin.trim
)
Expand All @@ -227,19 +236,19 @@ class noiseTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|vec2 def1(in vec2 value){
|vec2 def2(in vec2 value){
| return value-((floor(value*0.0034602077212184668))*289.0);
|}
|vec3 def3(in vec3 value){
|vec3 def4(in vec3 value){
| return value-((floor(value*0.0034602077212184668))*289.0);
|}
|vec3 def2(in vec3 value){
| return def3(((34.0*value)+10.0)*value);
|vec3 def3(in vec3 value){
| return def4(((34.0*value)+10.0)*value);
|}
|float def0(in vec2 p){
|float def1(in vec2 _ptArg){
| vec4 C=vec4(0.21132487058639526,0.3660254180431366,-0.5773502588272095,0.024390242993831635);
| vec2 i=floor(p+dot(p,C.yy));
| vec2 x0=(p-i)+dot(i,C.xx);
| vec2 i=floor(_ptArg+dot(_ptArg,C.yy));
| vec2 x0=(_ptArg-i)+dot(i,C.xx);
| vec2 i1;
| if(x0.x>x0.y){
| i1=vec2(1.0,0.0);
Expand All @@ -248,8 +257,8 @@ class noiseTests extends munit.FunSuite {
| }
| vec4 x12=x0.xyxy+C.xxzz;
| x12=vec4(x12.xy-i1,x12.zw);
| i=def1(i);
| vec3 p=def2(((def2(i.y+vec3(0.0,i1.y,1.0)))+i.x)+vec3(0.0,i1.x,1.0));
| i=def2(i);
| vec3 p=def3(((def3(i.y+vec3(0.0,i1.y,1.0)))+i.x)+vec3(0.0,i1.x,1.0));
| vec3 m=max(0.5-vec3(dot(x0,x0),dot(x12.xy,x12.xy),dot(x12.zw,x12.zw)),0.0);
| m=m*m;
| m=m*m;
Expand All @@ -261,6 +270,9 @@ class noiseTests extends munit.FunSuite {
| vec3 g=vec3((a0.x*x0.x)+(h.x*x0.y),(a0.yz*x12.xz)+(h.yz*x12.yw));
| return 130.0*dot(m,g);
|}
|float def0(in vec2 p){
| return def1(p);
|}
|def0(vec2(0.0));
|""".stripMargin.trim
)
Expand All @@ -285,11 +297,14 @@ class noiseTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|vec3 def0(in vec2 p){
| vec3 a=fract(p.xyx*vec3(123.33999633789062,234.33999633789062,345.6499938964844));
|vec3 def1(in vec2 _ptArg){
| vec3 a=fract(_ptArg.xyx*vec3(123.33999633789062,234.33999633789062,345.6499938964844));
| a=a+(dot(a,a+34.45000076293945));
| return fract(vec3(a.x*a.y,a.y*a.z,a.z*a.x));
|}
|vec3 def0(in vec2 p){
| return def1(p);
|}
|def0(vec2(0.0));
|""".stripMargin.trim
)
Expand Down
52 changes: 35 additions & 17 deletions ultraviolet/js/src/test/scala/ultraviolet/sdfTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ class sdfTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|float def0(in vec2 p,in vec2 b){
| vec2 d=abs(p)-b;
|float def1(in vec2 _ptArg,in vec2 _hsArg){
| vec2 d=abs(_ptArg)-_hsArg;
| return length(max(d,0.0))+min(max(d.x,d.y),0.0);
|}
|float def0(in vec2 p,in vec2 b){
| return def1(p,b);
|}
|def0(vec2(0.5),vec2(1.5));
|""".stripMargin.trim
)
Expand All @@ -65,8 +68,11 @@ class sdfTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|float def1(in vec2 _ptArg,in float _rArg){
| return length(_ptArg)-_rArg;
|}
|float def0(in vec2 p,in float r){
| return length(p)-r;
| return def1(p,r);
|}
|def0(vec2(0.5),1.5);
|""".stripMargin.trim
Expand All @@ -92,13 +98,16 @@ class sdfTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|float def0(in vec2 p,in float r){
|float def1(in vec2 _ptArg,in float _rArg){
| vec3 k=vec3(-0.8660253882408142,0.5,0.5773502588272095);
| vec2 pt=abs(p);
| vec2 pt=abs(_ptArg);
| pt=pt-((2.0*min(dot(k.xy,pt),0.0))*k.xy);
| pt=pt-(vec2(clamp(pt.x,(-k.z)*r,k.z*r),r));
| pt=pt-(vec2(clamp(pt.x,(-k.z)*_rArg,k.z*_rArg),_rArg));
| return length(pt)*sign(pt.y);
|}
|float def0(in vec2 p,in float r){
| return def1(p,r);
|}
|def0(vec2(0.5),1.5);
|""".stripMargin.trim
)
Expand All @@ -123,12 +132,15 @@ class sdfTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|float def0(in vec2 p,in vec2 a,in vec2 b){
| vec2 pa=p-a;
| vec2 ba=b-a;
|float def1(in vec2 _ptArg,in vec2 _aArg,in vec2 _bArg){
| vec2 pa=_ptArg-_aArg;
| vec2 ba=_bArg-_aArg;
| float h=clamp(dot(pa,ba)/dot(ba,ba),0.0,1.0);
| return length(pa-(ba*h));
|}
|float def0(in vec2 p,in vec2 a,in vec2 b){
| return def1(p,a,b);
|}
|def0(vec2(0.5),vec2(0.0),vec2(1.0));
|""".stripMargin.trim
)
Expand All @@ -153,17 +165,20 @@ class sdfTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|float def0(in vec2 p,in float r,in float rf){
|float def1(in vec2 _ptArg,in float _rArg,in float _irArg){
| vec2 k1=vec2(0.80901700258255,-0.5877852439880371);
| vec2 k2=vec2(-k1.x,k1.y);
| vec2 p2=vec2(abs(p.x),-p.y);
| vec2 p2=vec2(abs(_ptArg.x),-_ptArg.y);
| p2=p2-((2.0*max(dot(k1,p2),0.0))*k1);
| p2=p2-((2.0*max(dot(k2,p2),0.0))*k2);
| p2=vec2(abs(p2.x),p2.y-r);
| vec2 ba=(rf*(vec2(-k1.y,k1.x)))-vec2(0.0,1.0);
| float h=clamp(dot(p2,ba)/dot(ba,ba),0.0,r);
| p2=vec2(abs(p2.x),p2.y-_rArg);
| vec2 ba=(_irArg*(vec2(-k1.y,k1.x)))-vec2(0.0,1.0);
| float h=clamp(dot(p2,ba)/dot(ba,ba),0.0,_rArg);
| return (length(p2-(ba*h)))*(sign((p2.y*ba.x)-(p2.x*ba.y)));
|}
|float def0(in vec2 p,in float r,in float rf){
| return def1(p,r,rf);
|}
|def0(vec2(0.5),1.5,1.0);
|""".stripMargin.trim
)
Expand All @@ -188,15 +203,18 @@ class sdfTests extends munit.FunSuite {
assertEquals(
actual,
s"""
|float def0(in vec2 p,in float r){
|float def1(in vec2 _ptArg,in float _rArg){
| float k=sqrt(3.0);
| vec2 pt=vec2(abs(p.x)-r,(-p.y)+(r/k));
| vec2 pt=vec2(abs(_ptArg.x)-_rArg,(-_ptArg.y)+(_rArg/k));
| if((pt.x+(k*pt.y))>0.0){
| pt=(vec2(pt.x-(k*pt.y),((-k)*pt.x)-pt.y))/2.0;
| }
| pt=vec2(pt.x-(clamp(pt.x,-r,0.0)),pt.y);
| pt=vec2(pt.x-(clamp(pt.x,-_rArg,0.0)),pt.y);
| return (-length(pt))*sign(pt.y);
|}
|float def0(in vec2 p,in float r){
| return def1(p,r);
|}
|def0(vec2(0.5),1.5);
|""".stripMargin.trim
)
Expand Down
Loading

0 comments on commit df5727c

Please sign in to comment.