
#declare ConcreteTexture = texture{ pigment{ BeigeMixPig } normal{ bumps bump_size 0.1 scale 0.1 } }

//Slopes beside water plane
#declare SlopesWidth = 2;
#declare SlopesHeight = SlopesWidth;
#declare CanalWidth = 12;
#declare WestSlopesXCoord = -172;
#declare SouthSlopesZCoord = -80;
#declare NorthEastCorner = < 10, 0, 10 >;
#declare BlockCornerPosition = < -CanalWidth - SlopesWidth - 4, SlopesHeight, -CanalWidth - SlopesWidth - 4 >;

#declare PtA = < WestSlopesXCoord, SlopesHeight, SlopesWidth >;
#declare PtB = < WestSlopesXCoord, 0, 0 >;
#declare PtC = < 0, 0, 0 >;
#declare PtD = < SlopesWidth, SlopesHeight, SlopesWidth >;
#declare PtE = < SlopesWidth, SlopesHeight, SouthSlopesZCoord >;
#declare PtF = < 0, 0, SouthSlopesZCoord >;
#declare PtG = < -CanalWidth, 0, -CanalWidth >;
#declare PtH = PtG + < -SlopesWidth, SlopesHeight, -SlopesWidth >;
#declare PtI = < WestSlopesXCoord, 0, -CanalWidth >;
#declare PtJ = PtI + < 0, SlopesHeight, -SlopesWidth >;
#declare PtK = < -CanalWidth, 0, SouthSlopesZCoord >;
#declare PtL = PtK + < -SlopesWidth, SlopesHeight, 0 >;

#declare BeigeSlopes =
mesh{
	triangle{ PtA, PtB, PtC }
	triangle{ PtA, PtC, PtD }
	triangle{ PtC, PtD, PtF }
	triangle{ PtF, PtD, PtE }
	triangle{ PtG, PtI, PtH }
	triangle{ PtI, PtJ, PtH }
	triangle{ PtG, PtH, PtL }
	triangle{ PtG, PtL, PtK }
	texture{ ConcreteTexture }
}

#declare SouthWestConcreteBlocks =
box{< -CanalWidth -SlopesWidth, SlopesHeight, -CanalWidth -SlopesWidth >,
	< WestSlopesXCoord, 0,SouthSlopesZCoord > texture{ ConcreteTexture } }

#declare NorthEastConcreteBlocks =
union{
	box{ PtD, < NorthEastCorner.x, 0, SouthSlopesZCoord > }//East part
	box{ PtA, NorthEastCorner }//North part
	texture{ ConcreteTexture } 
}

#declare WaterPlane =
box{ PtC, < WestSlopesXCoord, -100, SouthSlopesZCoord > 
	texture{ T_mediumWater 
		normal{ bumps 0.5 scale < 0.5, 0.25, 0.25 > * 0.25 turbulence 0.6 }
		finish{ specular 0.01 roughness 0.01 } scale 4 
	} 
}

#declare RustColor = color srgb < 139 / 255, 103 / 255, 60 / 255 >;
#declare BarrierMetalColor = color srgb < 212 / 255, 122 / 255, 35 / 255 >;
#declare BarrierPigment = pigment{ granite color_map{ 
											[ 0 BarrierMetalColor ]
											[ 0.49 BarrierMetalColor ]
											[ 0.49 RustColor ]
											[ 0.51 RustColor ]
											[ 0.51 BarrierMetalColor ]
											[ 1.0 BarrierMetalColor ]
										}
							}
							
#declare BarrierTexture = texture{ pigment{ BarrierPigment } }

#declare BarrierRadius = 0.05;
#declare BarrierDeltaWithSlope = 0.4;
#declare BarrierHighness = 0.45;
#declare BarrierPylonSpace = 1;

#declare BackBarrier = 
union{
	cylinder{ < WestSlopesXCoord, PtD.y + BarrierHighness, PtD.z + BarrierDeltaWithSlope >,
				PtD + < BarrierDeltaWithSlope, BarrierHighness, BarrierDeltaWithSlope >, 
				BarrierRadius 
	}
	sphere{ PtD + < BarrierDeltaWithSlope, BarrierHighness, BarrierDeltaWithSlope >, BarrierRadius }
	cylinder{ PtD + < BarrierDeltaWithSlope, BarrierHighness, BarrierDeltaWithSlope >,
				< PtD.x + BarrierDeltaWithSlope, PtD.y + BarrierHighness, SouthSlopesZCoord >, BarrierRadius }
	texture{ BarrierTexture }
}

#declare CornerBarrier =
union{
	cylinder{ < WestSlopesXCoord, PtH.y + BarrierHighness, PtH.z - BarrierDeltaWithSlope >,
		< PtH.x - BarrierDeltaWithSlope, PtH.y + BarrierHighness, PtH.z - BarrierDeltaWithSlope >, BarrierRadius 
	}
	sphere{ PtH + < -BarrierDeltaWithSlope, BarrierHighness, -BarrierDeltaWithSlope >, BarrierRadius }
	cylinder{ PtH + < -BarrierDeltaWithSlope, BarrierHighness, -BarrierDeltaWithSlope >, 
		< PtH.x - BarrierDeltaWithSlope, PtH.y + BarrierHighness, SouthSlopesZCoord >,
		BarrierRadius }
	texture{ BarrierTexture }
}

#macro MakeOnXBarrierPylon(XStart, XEnd, ZPosition, Space)
	#for( It, XStart, XEnd, Space )
		cylinder{ < It, SlopesWidth, ZPosition >, < It, SlopesWidth + BarrierHighness, ZPosition >, BarrierRadius texture{ BarrierTexture } }
	#end
#end

#macro MakeOnZBarrierPylon(ZStart, ZEnd, XPosition, Space)
	#for( It, ZStart, ZEnd, Space )
		cylinder{ < XPosition, SlopesWidth, It >, < XPosition, SlopesWidth + BarrierHighness, It >, BarrierRadius texture{ BarrierTexture } }
	#end
#end

#declare SpecularMediaPlaceSeed = seed( 234 );
#declare MediaSphereShineColorSeed = seed( 567 );
#declare MediaSphereRadius = 0.02;

#macro MakeWaterSpecularMedia(Transform, GreenComponentsValue)
	sphere{ 0, MediaSphereRadius 
		hollow no_shadow
		interior{
			media{ emission 16 intervals 2 samples 1, 30 
				density{ color srgb < 0, GreenComponentsValue, 1 > }
				//density{ color srgb < 1, 1, 1 > }
			}
		}
		transform{ Transform }
		texture{ pigment{ color srgbt 1 } finish{ specular 0.1 roughness 0.1 } }
	}
#end

#macro PlaceWaterMediaSpeculars(Min2DPlanePos, Max2DPlanePos, YPos, Spacing)
	#local MediaSphereCount = 0;
	#for( ItX, Min2DPlanePos.x, Max2DPlanePos.x, Spacing )
		#for( ItZ, Min2DPlanePos.y, Max2DPlanePos.y, Spacing )
			MakeWaterSpecularMedia(
				transform{ translate < ItX + SetWithAffineFunction( 0, 1, -Spacing / 3, Spacing / 3, rand( SpecularMediaPlaceSeed ) ),
							YPos, 
							ItZ + SetWithAffineFunction( 0, 1, -Spacing / 3, Spacing / 3, rand( SpecularMediaPlaceSeed ) ) > },
				SetWithAffineFunction( 0, 1, 0.75, 0.95, rand( MediaSphereShineColorSeed ) )
			)
			#local MediaSphereCount = MediaSphereCount + 1;
		#end
	#end
	#debug concat("Placed ", str(MediaSphereCount, 0, 0 ), " media spheres.\n")
#end

//Pipe beetween plant block
#declare PipeColor = color srgb < 82 / 255, 67 / 255, 45 / 255 >;
#declare PipeTexture = texture{ pigment{ PipeColor } finish{ specular 0.01 roughness 0.01 } }
#declare PipeRadius = 0.25;
#declare PipeYPos = PipeRadius * 2;
#declare PlantBlockPipe =
cylinder{ BlockCornerPosition + < 0, PipeYPos, 0 >, BlockCornerPosition + < -10, PipeYPos, 0 >, PipeRadius texture{ PipeTexture } }

#declare PlantBlockPipeCenter = ( max_extent( PlantBlockPipe ) + min_extent( PlantBlockPipe ) ) / 2;

#declare PipeSupport =
prism{ linear_sweep linear_spline -0.25, 0.25, 7,
	< PipeYPos, 0 >, 
	< PipeYPos, PipeYPos * 2 * 2 / 3 >, 
	< PipeYPos * 2 / 3, PipeYPos * 2 >, 
	< -PipeYPos * 2 / 3, PipeYPos * 2 >,
	< -PipeYPos, PipeYPos * 2 * 2 / 3 >, 
	< -PipeYPos, 0 >, 
	< PipeYPos, 0 > 
	rotate -90 * x
	rotate 90 * y 
	texture{ ConcreteTexture }
}