# SPDX-FileCopyrightText: 2011-2019 Disney Enterprises, Inc.
# SPDX-License-Identifier: LicenseRef-Apache-2.0
# SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
# SPDX-License-Identifier: GPL-3.0-or-later



# Texture Instancer

# Adrienne Othon  Dec 23, 2004

# A user specified texture is placed at the center of each grid cell.
# Texture scale and jitter (offset from cell center) may be specified.  
# If the texture scale is large enough to extend the texture outside it's
# cell, then the overlap areas will be layered, using a predetermined
# order.  It is necessary to specify an alpha image for the layering
# for work properly on any pattern, so the pattern need not be rectangular.




#------------------------------------------------------
# grid xform params:
$c1=8;
$c2= -6 ;  #-10;
$scale = 1;    #size of the grid
$scaley = 1;     #scale the grid along y (vertical)
$scalez = 1;     #scale the grid along z (horizontal)
$angle = -45;   #degrees

#scale (size) of the texture.  compute coordinate within this cell
$scaletex = 1.3;
$scaletexy = 1;
$scaletexz = 1.6;
$jitter = 0;

$translatey = 0;  #-5.0,5.0
$translatez = 0;  #-5.0,5.0
####

## noise (warp)
$noiseamount =  0;
$noisefreq = 2;




#deg -> rad
$angle=$angle*3.14159/180;

$cos = cos($angle);
$sin = sin($angle);

$P1 = ($P + [0,-$translatey,$translatez])/[1,$scaley,$scalez]/$scale;

#rotate, to create staggered, brick-type grid
$P1 = [0, $cos*$P1[1] + $sin*$P1[2], -$sin*$P1[1]+$cos*$P1[2]];



# add noise to P1 (add warp):
$P1 = $P1 + $noiseamount*cnoise($P1*$noisefreq);



##............. translation jitter (no rotation jitter implemented here), scale the texture ..............

$frac = $P1 - floor($P1);

# i needa little function to do the following:
$frac1 = ($frac - [.5,.5,.5])/$scaletex;
$frac1y = [0,$cos,-$sin];   # get axes, [rot matrix] * [0,1,0]
$frac1z = [0,$sin,$cos];   # get axes,   [rot matrix] * [0,0,1]
$frac1y = dot($frac1y,$frac1)*$frac1y;   # project onto axes
$frac1z = dot($frac1z,$frac1)*$frac1z;   # project onto axes
$frac1y = $frac1y / $scaletexy;   #scale along axes
$frac1z = $frac1z / $scaletexz;   #scale along axes
$frac1 = $frac1y + $frac1z + [.5,.5,.5] + $jitter*(ccellnoise($P1)-[.5,.5,.5]);   #combine components, .5 center

$y = $frac1[1];
$z = $frac1[2];
$test = ($y < 0 || $y > 1 || $z < 0 || $z > 1);

#map("/home/fahome/aothon/misc/fishscale_1.tif",$z,$y)


$color = $test ? [0,0,0] : map("/home/fahome/aothon/misc/scale.tif",$z,$y);
$alpha = $test ? [0,0,0] : map("/home/fahome/aothon/misc/scale-alpha.tif",$z,$y);



#compute coord within adjacent cells:

# i needa little function to do the following:
$neighbourcell = [0,0,-1];
$frac1 = ($frac - $neighbourcell - [.5,.5,.5])/$scaletex;
$frac1y = [0,$cos,-$sin];   # get axes, [rot matrix] * [0,1,0]
$frac1z = [0,$sin,$cos];   # get axes,   [rot matrix] * [0,0,1]
$frac1y = ($frac1y[0]*$frac1[0] + $frac1y[1]*$frac1[1] + $frac1y[2]*$frac1[2])*$frac1y;   # project onto axes
$frac1z = ($frac1z[0]*$frac1[0] + $frac1z[1]*$frac1[1] + $frac1z[2]*$frac1[2])*$frac1z;   # project onto axes
$frac1y = $frac1y / $scaletexy;   #scale along axes
$frac1z = $frac1z / $scaletexz;   #scale along axes
$frac1 = $frac1y + $frac1z + [.5,.5,.5] + $jitter*(ccellnoise($P1 + $neighbourcell)-[.5,.5,.5]);   #combine components, .5 center

$y = $frac1[1];
$z = $frac1[2];
$test = ($y < 0 || $y > 1 || $z < 0 || $z > 1);
$color1 = $test ? [0,0,0] : map("/home/fahome/aothon/misc/scale.tif",$z,$y);
$alpha1 = $test  ? [0,0,0] : map("/home/fahome/aothon/misc/scale-alpha.tif",$z,$y);

# i needa little function to do the following:
$neighbourcell = [0,1,0];
$frac1 = ($frac - $neighbourcell - [.5,.5,.5])/$scaletex;
$frac1y = [0,$cos,-$sin];   # get axes, [rot matrix] * [0,1,0]
$frac1z = [0,$sin,$cos];   # get axes,   [rot matrix] * [0,0,1]
$frac1y = ($frac1y[0]*$frac1[0] + $frac1y[1]*$frac1[1] + $frac1y[2]*$frac1[2])*$frac1y;   # project onto axes
$frac1z = ($frac1z[0]*$frac1[0] + $frac1z[1]*$frac1[1] + $frac1z[2]*$frac1[2])*$frac1z;   # project onto axes
$frac1y = $frac1y / $scaletexy;   #scale along axes
$frac1z = $frac1z / $scaletexz;   #scale along axes
$frac1 = $frac1y + $frac1z + [.5,.5,.5] + $jitter*(ccellnoise($P1 + $neighbourcell)-[.5,.5,.5]);   #combine components, .5 center

$y = $frac1[1];
$z = $frac1[2];
$test = ($y < 0 || $y > 1 || $z < 0 || $z > 1);
$color2 = $test ? [0,0,0] : map("/home/fahome/aothon/misc/scale.tif",$z,$y);
$alpha2 = $test ? [0,0,0] : map("/home/fahome/aothon/misc/scale-alpha.tif",$z,$y);


# i needa little function to do the following:
$neighbourcell = [0,1,-1];
$frac1 = ($frac - $neighbourcell - [.5,.5,.5])/$scaletex;
$frac1y = [0,$cos,-$sin];   # get axes, [rot matrix] * [0,1,0]
$frac1z = [0,$sin,$cos];   # get axes,   [rot matrix] * [0,0,1]
$frac1y = ($frac1y[0]*$frac1[0] + $frac1y[1]*$frac1[1] + $frac1y[2]*$frac1[2])*$frac1y;   # project onto axes
$frac1z = ($frac1z[0]*$frac1[0] + $frac1z[1]*$frac1[1] + $frac1z[2]*$frac1[2])*$frac1z;   # project onto axes
$frac1y = $frac1y / $scaletexy;   #scale along axes
$frac1z = $frac1z / $scaletexz;   #scale along axes
$frac1 = $frac1y + $frac1z + [.5,.5,.5] + $jitter*(ccellnoise($P1 + $neighbourcell)-[.5,.5,.5]);   #combine components, .5 center

$y = $frac1[1];
$z = $frac1[2];
$test = ($y < 0 || $y > 1 || $z < 0 || $z > 1);
$color3 = $test ? [0,0,0] : map("/home/fahome/aothon/misc/scale.tif",$z,$y);
$alpha3 = $test ? [0,0,0] : map("/home/fahome/aothon/misc/scale-alpha.tif",$z,$y);


#comp em, over black
#$color3*$alpha3 + (1-$alpha3)*($color2*$alpha2 + (1-$alpha2)*($color1*$alpha1 + (1-$alpha1)*$color*$alpha))
mix(mix(mix(mix([0,0,0],$color,$alpha),$color1,$alpha1),$color2,$alpha2),$color3,$alpha3)
