// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== global proc fitFluidToDensity( string $fluid, float $densityThreshold ) { int $minRes = 8; int $maxRes = 300; //on a side int $maxTotalRes = 180*180*180; float $testDens[]; float $pThreshold = $densityThreshold; float $mThreshold = $densityThreshold; int $px = 0; int $mx = 0; int $py = 0; int $my = 0; int $pz = 0; int $mz = 0; int $growPerStep = 2; int $xRes = getAttr( $fluid + ".resolutionW" ); int $yRes = getAttr( $fluid + ".resolutionH" ); int $zRes = getAttr( $fluid + ".resolutionD" ); float $dens; if( $xRes > $minRes ){ $mx = -1; $px = -1; } if( $yRes > $minRes ){ $my = -1; $py = -1; } if( $zRes > $minRes ){ $mz = -1; $pz = -1; } int $boundX = getAttr( $fluid + ".boundaryX" ); int $boundY = getAttr( $fluid + ".boundaryY" ); int $boundZ = getAttr( $fluid + ".boundaryZ" ); int $changeableX = ($boundX != 4); // wrapping bounds should not expand/contract int $changeableY = ($boundY != 4); // wrapping bounds should not expand/contract int $changeableZ = ($boundZ != 4); // wrapping bounds should not expand/contract int $minX = 0; int $maxX = $xRes -1; if( $boundX == 0 ){ $minX++; $maxX--; } else if( $boundX == 2 ){ $maxX--; } else if( $boundX == 3 ){ $minX++; } int $minY = 0; int $maxY = $yRes -1; if( $boundY == 0 ){ $minY++; $maxY--; } else if( $boundY == 2 ){ $maxY--; } else if( $boundY == 3 ){ $minY++; } int $minZ = 0; int $maxZ = $zRes -1; if( $boundZ == 0 ){ $minZ++; $maxZ--; } else if( $boundZ == 2 ){ $maxZ--; } else if( $boundZ == 3 ){ $minZ++; } if( $changeableX ){ $testDens = `getFluidAttr -xi $minX -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $pThreshold ){ $mx = $growPerStep; break; } } if( $mx == -1 ){ $testDens = `getFluidAttr -xi ($minX + 1) -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $mThreshold ){ $mx = 0; break; } } } $testDens = `getFluidAttr -xi $maxX -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $pThreshold ){ $px = $growPerStep; break; } } if( $px == -1 ){ $testDens = `getFluidAttr -xi ($maxX -1) -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $mThreshold ){ $px = 0; break; } } } } if( $changeableY ){ $testDens = `getFluidAttr -yi $minY -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $pThreshold ){ $my = $growPerStep; break; } } if( $my == -1 ){ $testDens = `getFluidAttr -yi ($minY+1) -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $mThreshold ){ $my = 0; break; } } } $testDens = `getFluidAttr -yi $maxY -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $pThreshold ){ $py = $growPerStep; break; } } if( $py == -1 ){ $testDens = `getFluidAttr -yi ($maxY -1) -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $mThreshold ){ $py = 0; break; } } } } if( $changeableZ ){ $testDens = `getFluidAttr -zi $minZ -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $pThreshold ){ $mz = $growPerStep; break; } } if( $mz == -1 ){ $testDens = `getFluidAttr -zi ($minZ+1) -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $mThreshold ){ $mz = 0; break; } } } $testDens = `getFluidAttr -zi $maxZ -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $pThreshold ){ $pz = $growPerStep; break; } } if( $pz == -1 ){ $testDens = `getFluidAttr -zi ($maxZ-1) -at "density" $fluid`; for( $dens in $testDens ){ if( $dens > $mThreshold ){ $pz = 0; break; } } } } int $atMax = ($xRes * $yRes * $zRes > $maxTotalRes); if( $atMax || $xRes > $maxRes ){ if($px > 0 ) $px = 0; if($mx > 0 ) $mx = 0; } if( $atMax || $yRes > $maxRes ){ if($py > 0 ) $py = 0; if($my > 0 ) $my = 0; } if( $atMax || $zRes > $maxRes ){ if($pz > 0 ) $pz = 0; if($mz > 0 ) $mz = 0; } //print( "extend "+$px+", "+$mx+", "+$py+", "+$my+", "+$pz+", "+$mz+"\n" ); extendFluid -sw $mx -ew $px -sh $my -eh $py -sd $mz -ed $pz $fluid; }