|
フラグメントシェーダー ブーリアン演算 smooth min関数
ブーリアン演算のmin関数をsmooth min関数にすると
接合部をなめらかにすることができます。min関数と比べてみてください。
https://www.iquilezles.org/www/articles/smin/smin.htm
DIM ST(2),C(3)
ASK BITMAP SIZE XSIZE,YSIZE
SET COLOR MODE "NATIVE"
SET POINT STYLE 1
SET WINDOW -XSIZE/MIN(XSIZE,YSIZE),XSIZE/MIN(XSIZE,YSIZE),-YSIZE/MIN(XSIZE,YSIZE),YSIZE/MIN(XSIZE,YSIZE)
FOR YY=0 TO YSIZE-1
FOR XX=0 TO XSIZE-1
LET X=(XX*2-XSIZE)/MIN(XSIZE,YSIZE)
LET Y=(YY*2-YSIZE)/MIN(XSIZE,YSIZE)
CALL VEC2(ST,X,Y)
!IF ABS(DISTANCE(ST))<.01 THEN
IF DISTANCE(ST)<0 THEN
CALL VEC3(C,1,0,0)
ELSE
CALL VEC3(C,0,0,0)
END IF
CALL SETCOLOR(C)
PLOT POINTS:X,Y
NEXT XX
NEXT YY
END
EXTERNAL FUNCTION CIRCLE(P(),B(),R)
DIM PP(2)
MAT PP=P-B
LET CIRCLE=LENGTH(PP)-R
END FUNCTION
EXTERNAL FUNCTION DISTANCE(P())
DIM P1(2),P2(2)
CALL VEC2(P1,-.25,0)
CALL VEC2(P2,.25,0)
LET DISTANCE=SMIN(CIRCLE(P,P1,.6),CIRCLE(P,P2,.4))
!LET DISTANCE=SMIN2(CIRCLE(P,P1,.6),CIRCLE(P,P2,.4))
!LET DISTANCE=SMIN3(CIRCLE(P,P1,.6),CIRCLE(P,P2,.4))
!LET DISTANCE=SMIN4(CIRCLE(P,P1,.6),CIRCLE(P,P2,.4))
!LET DISTANCE=SMIN5(CIRCLE(P,P1,.6),CIRCLE(P,P2,.4))
END FUNCTION
EXTERNAL FUNCTION SMIN(A,B) !exponential smooth min
LET K=32
LET RES=EXP(-K*A)+EXP(-K*B)
LET SMIN=-LOG(RES)/K
END FUNCTION
EXTERNAL FUNCTION SMIN2(A,B) !polynomial smooth min
LET K=.1
LET H=CLAMP(.5+.5*(B-A)/K,0,1)
LET SMIN2=MIX(B,A,H)-K*H*(1-H)
END FUNCTION
EXTERNAL FUNCTION SMIN3(A,B) !polynomial smooth min
LET K=.1
LET H=MAX(K-ABS(A-B),0)/K
LET SMIN3=MIN(A,B)-H*H*K/4
END FUNCTION
EXTERNAL FUNCTION SMIN4(A,B) !polynomial smooth min
LET K=.1
LET H=MAX(K-ABS(A-B),0)/K
LET SMIN4=MIN(A,B)-H*H*H*K/6
END FUNCTION
EXTERNAL FUNCTION SMIN5(A,B) !root smooth min
LET K=.01
LET H=A-B
LET SMIN5=.5*((A+B)-SQR(H*H+K))
END FUNCTION
!EXTERNAL FUNCTION SMAX(A,B)
!LET SMAX=A+B-SMIN(A,B)
!END FUNCTION
EXTERNAL SUB SETCOLOR(COL())
SET COLOR COLORINDEX(CLAMP(COL(1),0,1),CLAMP(COL(2),0,1),CLAMP(COL(3),0,1))
END SUB
EXTERNAL SUB VEC2(A(),X,Y)
LET A(1)=X
LET A(2)=Y
END SUB
EXTERNAL SUB VEC3(A(),X,Y,Z)
LET A(1)=X
LET A(2)=Y
LET A(3)=Z
END SUB
EXTERNAL FUNCTION CLAMP(X,A,B)
LET CLAMP=MIN(B,MAX(X,A))
END FUNCTION
EXTERNAL FUNCTION LENGTH(A())
LET LENGTH=SQR(A(1)^2+A(2)^2)
END FUNCTION
EXTERNAL FUNCTION MIX(X,Y,A)
LET MIX=X*(1-A)+Y*A
END FUNCTION
|
|