Skip to content

Commit

Permalink
added invertedLight global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
jemmybutton committed Jul 2, 2023
1 parent 085007e commit 64b5a63
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
15 changes: 14 additions & 1 deletion fiziko.mkiv
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ input fiziko.mp;

\startdocument[
author=Sergey Slyusarev,
title={\type{fiziko}\par v.0.2.0 package for \MetaPost},
title={\type{fiziko}\par v.0.2.1 package for \MetaPost},
abstract={This document describes a bunch of macros provided by the \quotation{fiziko} library for \MetaPost.},
license={This document is distributed under the CC-BY-SA 4.0 license\par\symbol[cc][by]~\symbol[cc][sa]},
url={https://github.com/jemmybutton/fiziko},
Expand Down Expand Up @@ -77,6 +77,19 @@ This variable controls direction from which light falls on shaded objects. It’
lightDirection := (-1/8pi, 1/8pi);
\stopMP

\subsection{lightDirection}
This variable determines whether the light is inverted. This comes in handy when you need to have your shaded objects white on black. It's of \type{boolean} type. Default value is false:

\def\BufferName{lightDirection}
\startbuffer[\BufferName]
draw sphere.c(1cm);
invertedLight := true;
fill ((unitsquare shifted (-1/2,-1/2)) scaled 3/2cm) shifted (3/2cm, 0) withcolor black;
draw sphere.c(1cm) shifted (3/2cm, 0) withcolor white;
\stopbuffer
\typebuffer[\BufferName] \processMPbuffer[\BufferName]


\section{\quotation{Lower level} macros}
Currently, algorithms are quite stupid and will produce decent results only in certain simple circumstances.

Expand Down
40 changes: 31 additions & 9 deletions fiziko.mp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% fiziko 0.2.0
% fiziko 0.2.1
% MetaPost library for physics textbook illustrations
% Copyright 2022 Sergey Slyusarev
% Copyright 2023 Sergey Slyusarev
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -358,9 +358,10 @@ enddef;
%

vardef offsetPathGenerate (expr p, q, i) =
save returnPath, c, d, pl, ps;
save returnPath, c, d, a, pl, ps;
path returnPath, pl[];
pair c[], d[];
numeric a[];
c1 := precontrol i of p;
c2 := point i of p;
c3 := postcontrol i of p;
Expand Down Expand Up @@ -401,8 +402,18 @@ vardef offsetPathGenerate (expr p, q, i) =
and (abs(c1-c4)>0)
and (abs(direction i of q) > 0):
c6 := c4 shifted (unitvector(c4 - c3) rotated 90 scaled ypart(point i + 1 of q));
c7 := (c2 - c1) scaled (abs(c5-c6)/abs(c1-c4)) rotated angle(direction i of q) shifted c5;
c8 := (c3 - c4) scaled (abs(c5-c6)/abs(c1-c4)) rotated angle(direction i + 1 of q) shifted c6;
%if abs(direction i of q) > 0:
a1 := angle(direction i of q);
%else:
% a1 := angle((point (i + 1/10) of q) - (point (i - 1/10) of q));
%fi;
%if abs(direction i + 1 of q) > 0:
a2 := angle(direction i + 1 of q);
%else:
% a2 := angle((point (i + 1 + 1/10) of q) - (point (i + 1 - 1/10) of q));
%fi;
c7 := (c2 - c1) scaled (abs(c5-c6)/abs(c1-c4)) rotated a1 shifted c5;
c8 := (c3 - c4) scaled (abs(c5-c6)/abs(c1-c4)) rotated a2 shifted c6;
returnPath := returnPath .. controls c7 and c8 .. offsetPathGenerate (p, q, i + 1);
else:
returnPath := returnPath -- offsetPathGenerate (p, q, i + 1);
Expand Down Expand Up @@ -1027,6 +1038,12 @@ enddef;
% Once we get two angles at some point of some surface, we can compute light intensity there.
%

% In case you need to have your shaded objects white-on-black,
% you can just switch invertedLight invertedLight to 'true'

boolean invertedLight;
invertedLight := false;

vardef normalVectorToLightness (expr normalVector, d, q) =
save returnValue, shiftedShadowPath;
path shiftedShadowPath;
Expand All @@ -1045,10 +1062,15 @@ vardef normalVectorToLightness (expr normalVector, d, q) =
returnValue := lightnessPP(returnValue);
fi;
if returnValue > 1:
1
else:
returnValue
fi
returnValue := 1;
fi;
if returnValue < 0:
returnValue := 0;
fi;
if invertedLight:
returnValue := 1 - returnValue;
fi;
returnValue
enddef;

vardef lightnessPP (expr v) =
Expand Down
21 changes: 19 additions & 2 deletions fiziko.tex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}

\author{Sergey Slyusarev}
\title{``fiziko'' v. 0.2.0 package for \METAPOST}
\title{``fiziko'' v. 0.2.1 package for \METAPOST}

\begin{document}
\maketitle
Expand Down Expand Up @@ -53,11 +53,28 @@ \subsection{minStrokeWidth}

\subsection{lightDirection}
This variable controls direction from which light falls on shaded objects. It's of \texttt{pair} type and is set in radians. Default direction is top-left:

\begin{lstlisting}
lightDirection := (-1/8pi, 1/8pi);
\end{lstlisting}


\subsection{invertedLight}
This variable determines whether the light is inverted. This comes in handy when you need to have your shaded objects white on black. It's of \texttt{boolean} type. Default value is false:

\begin{lstlisting}
draw sphere.s(1cm);
invertedLight := true;
fill ((unitsquare shifted (-1/2,-1/2)) scaled 3/2cm) shifted (3/2cm, 0) withcolor black;
draw sphere.s(1cm) shifted (3/2cm, 0) withcolor white;
\end{lstlisting}

\begin{mplibcode}
draw sphere.c(1cm);
invertedLight := true;
fill ((unitsquare shifted (-1/2,-1/2)) scaled 3/2cm) shifted (3/2cm, 0) withcolor black;
draw sphere.c(1cm) shifted (3/2cm, 0) withcolor white;
\end{mplibcode}

\section{``Lower level'' macros}
Currently, algorithms are quite stupid and will produce decent results only in certain simple circumstances.

Expand Down

0 comments on commit 64b5a63

Please sign in to comment.