/* This file downloaded from Highend3d.com '' '' Highend3d.com File Information: '' '' Script Name: particleHWShader v1.1 '' Author: Bjorn Henriksson & Fredrik Averpil '' Last Updated: November 10, 2005 '' Update/Change this file at: '' http://www.highend3d.com/maya/downloads/mel_scripts/rendering/misc/3766.html '' '' History: '' particleHWShader v1.0 on May 9, 2005 by Bjorn Henriksson & Fredrik Averpil '' '' Please do not alter any information above this line '' it is generated dynamically by Highend3d.com and will '' be changed automatically on any updates. */ global proc particleHWShader() { global vector $attributeSelection= <<0, 0, 0>>; if ( (`window -exists particleHWShader`) == true ) deleteUI particleHWShader; window -title "particleHWShader" -wh 360 250 -menuBar 1 particleHWShader; menu -l "Help" file; menuItem -l "Web tutorial" -c "tutorial ()" tutorial; menuItem -l "About" -c "aboutWindow ()" about; columnLayout -adjustableColumn true; textFieldGrp -cw 1 100 -label "Particle Name" -text "particle1" particleName; textFieldGrp -cw 1 100 -label "Camera Name" -text "camera1" camName; setParent..;setParent ..; int $Red, $Green, $Blue; frameLayout -collapsable true -label "Render stats"; rowColumnLayout -numberOfColumns 3; columnLayout; text -label "R"; string $collection1 = `radioCollection`; radioButton -label "Depth" -onc "$Red= 0"; radioButton -label "DepthWithFocus" -onc "$Red= 1"; radioButton -label "Height" -onc "$Red= 2"; radioButton -label "Normalized Age" -onc "$Red= 3"; radioButton -label "White" -onc "$Red= 4"; setParent ..; columnLayout; text -label "G"; string $collection2 = `radioCollection`; radioButton -label "Depth" -onc "$Green= 0"; radioButton -label "DepthWithFocus" -onc "$Green= 1"; radioButton -label "Height" -onc "$Green= 2"; radioButton -label "Normalized Age" -onc "$Green= 3"; radioButton -label "White" -onc "$Green= 4"; setParent ..; columnLayout; text -label "B"; string $collection3 = `radioCollection`; radioButton -label "Depth" -onc "$Blue= 0"; radioButton -label "DepthWithFocus" -onc "$Blue= 1"; radioButton -label "Height" -onc "$Blue= 2"; radioButton -label "Normalized Age" -onc "$Blue= 3"; radioButton -label "White" -onc "$Blue= 4"; setParent ..; setParent ..; setParent ..; print $collection2; columnLayout -adjustableColumn true; button -l "Go" -w 100 -al "center" -c ("string $particleName =`textFieldGrp -q -text particleName`;" + "string $camName=`textFieldGrp -q -text camName`; "+ "createParticleHWShaderExpression($Red, $Green, $Blue,$camName,$particleName)"); setParent ..; showWindow particleHWShader; } particleHWShader; //------------------------------------------------------------------------- // about - author info //------------------------------------------------------------------------- global proc aboutWindow () { if (`window -exists aboutWin`) deleteUI aboutWin; window -wh 350 120 -mxb off aboutWin; columnLayout -adjustableColumn on aboutCol; scrollField -wordWrap true -w 210 -h 120 -text "particleHWShader v1.0\nUpdated: 2005-05-09\nBy: Björn Henriksson (mail@bhenriksson.se) & Fredrik Averpil (fredrik@averpil.com)" -editable false aboutScroll; showWindow aboutWin; } //------------------------------------------------------------------------- // tutorial - connect to web for tutorial // //------------------------------------------------------------------------- global proc tutorial () { system("load " + "http://www.averpil.com/fredrik/index.php?page=tutorials&id=2" ); } //------------------------------------------------------------------------- // createParticleHWShaderExpression - the real deal! // //------------------------------------------------------------------------- global proc createParticleHWShaderExpression(int $Red, int $Green, int $Blue, string $camName, string $particleName){ string $particleShape[] = `listRelatives -s $particleName`; string $camShape[] = `listRelatives -s $camName`; string $rgbPPAtrib[]; string $expr; int $attribChooser[3]; $attribChooser[0] = $Red; $attribChooser[1] = $Green; $attribChooser[2] = $Blue; int $i=0; float $focusPosition[3] ={0.0 , 0.0 , 0.0}; float $roofPos[3] ={0.0 , 1.0 , 0.0}; float $floorPos[3] ={0.0 , 0.0 , 0.0}; float $focusDist; float $focusMaxDist= 5; float $focusDecayRate=1; int $depthCalc=0; float $focusRange =0; //_____________________________________declarations______________ // delete existing locators if(`objExists "focusPoint1"`){ $focusPosition[0] = `getAttr focusPoint1.translateX`; $focusPosition[1] = `getAttr focusPoint1.translateY`; $focusPosition[2] = `getAttr focusPoint1.translateZ`; delete focusPoint1; } if(`objExists "roofLoc1"`){ $roofPos[0] = `getAttr roofLoc1.translateX`; $roofPos[1] = `getAttr roofLoc1.translateY`; $roofPos[2] = `getAttr roofLoc1.translateZ`; delete roofLoc1; } if(`objExists "floorLoc1"`){ $floorPos[0] = `getAttr floorLoc1.translateX`; $floorPos[1] = `getAttr floorLoc1.translateY`; $floorPos[2] = `getAttr floorLoc1.translateZ`; delete floorLoc1; } if (! `attributeExists "rgbPP" $particleShape[0]`){ addAttr -ln "rgbPP" -dt vectorArray $particleShape[0]; addAttr -ln "rgbPP0" -dt vectorArray $particleShape[0]; } if(getApplicationVersionAsFloat() >= 6.0) { $expr += (`dynExpression -q -rad $particleShape[0]`) + "\r\r\n//particleHWShader\r\r\n"; // Maya 6.5 Compatible } else { $expr += (`dynExpression -q -r $particleShape[0]`)+ "\r\n//particleHWShader\r\r\n"; // Maya 5 Compatible } print $attribChooser; //DepthWithFocusCalc________________ if($attribChooser[0]==0||$attribChooser[1]==0||$attribChooser[2]==0){ $depthCalc=1; if (! `attributeExists "distanceToCamPP" $particleShape[0]`){ addAttr -ln distanceToCamPP -dt doubleArray $particleShape[0]; addAttr -ln distanceToCamPP0 -dt doubleArray $particleShape[0]; } $expr += "// CAMERA DISTANCE \r\n"+ "float $clipDist = "+ $camShape[0] +".farClipPlane-" + $camShape[0] +".nearClipPlane;\r\n"+ "vector $camPos = <<" + $camName + ".translateX, " +$camName +".translateY,"+ $camName +".translateZ>>;\r\n"+ "" + $particleShape[0] + ".distanceToCamPP = mag(" + $particleShape[0] + ".worldPosition-$camPos);\r\n"+ "float $outValueDepth = (" + $particleShape[0] + ".distanceToCamPP)/$clipDist;\r\n"; } //DepthCalculation____________________ if($attribChooser[0]==1||$attribChooser[1]==1||$attribChooser[2]==1){ if($depthCalc==0){ //tests if the depths has been calculated if (! `attributeExists "distanceToCamPP" $particleShape[0]`){ addAttr -ln distanceToCamPP -dt doubleArray $particleShape[0]; addAttr -ln distanceToCamPP0 -dt doubleArray $particleShape[0]; } $expr += "// CAMERA DISTANCE \r\n"+ "float $clipDist = "+ $camShape[0] +".farClipPlane-" + $camShape[0] +".nearClipPlane;\r\n"+ "vector $camPos = <<" + $camName + ".translateX, " +$camName +".translateY,"+ $camName +".translateZ>>;\r\n"+ "" + $particleShape[0] + ".distanceToCamPP = mag(" + $particleShape[0] + ".worldPosition-$camPos);\r\n"; } rename `spaceLocator -p 0 0 0` focusPoint1; move $focusPosition[0] $focusPosition[1] $focusPosition[2] focusPoint1; addAttr -ln focusDecayRate -at double |focusPoint1; setAttr -e -keyable true focusPoint1.focusDecayRate; setAttr focusPoint1.focusDecayRate $focusDecayRate; addAttr -ln focusMaxDist -at double |focusPoint1; setAttr -e -keyable true focusPoint1.focusMaxDist; setAttr focusPoint1.focusMaxDist $focusMaxDist; addAttr -ln focusRange -at double |focusPoint1; setAttr -e -keyable true focusPoint1.focusRange; setAttr focusPoint1.focusRange $focusRange; $expr += "vector $focPos =<>;\r\n"+ "float $focDist= mag($focPos-$camPos);\r\n"+ "float $outValueFocusDepth;\r\n"+ "if(" + $particleShape[0] + ".distanceToCamPP - $focDist <-focusPoint1.focusRange)\r\n" + "$outValueFocusDepth = clamp(0, 1, 1 - pow(abs(" + $particleShape[0] + ".distanceToCamPP - $focDist + focusPoint1.focusRange / 2)/ focusPoint1.focusMaxDist, focusPoint1.focusDecayRate ) ); \r\n"+ "else if(" + $particleShape[0] + ".distanceToCamPP - $focDist >focusPoint1.focusRange)\r\n"+ "$outValueFocusDepth = clamp(0, 1, 1 - pow(abs(" + $particleShape[0] + ".distanceToCamPP - $focDist - focusPoint1.focusRange / 2)/ focusPoint1.focusMaxDist, focusPoint1.focusDecayRate ) )\r\n;"+ "else\r\n"+ "$outValueFocusDepth = 1;\r\n"; } //HeightCalculation if($attribChooser[0]==2||$attribChooser[1]==2||$attribChooser[2]==2){ if(! `objExists "roofLoc1"`){ rename `spaceLocator -p 0 0 0` roofLoc1; move $roofPos[0] $roofPos[1] $roofPos[2] roofLoc1; } if(! `objExists "floorLoc1"`){ rename `spaceLocator -p 0 0 0` floorLoc1; move $floorPos[0] $floorPos[1] $floorPos[2] floorLoc1; } $expr+= "// HEIGHT DISTANCE (RED)\r\n"+ "vector $particlePos ="+ $particleShape[0] + ".worldPosition;\r\n"+ "float $floorLocation = floorLoc1.translateY;\r\n"+ "float $outValueHeight = 1-($particlePos.y - $floorLocation) / (roofLoc1.translateY - $floorLocation);\r\n"; } //NormalizedCalculation if($attribChooser[0]==3||$attribChooser[1]==3||$attribChooser[2]==3){ $expr += "float $outValueAge=age/finalLifespanPP;\r\n"; warning "Make sure particle lifespan is not set to \"live forever\"!"; } //build the rgbPP vector $i=0; while($i<3){ if($attribChooser[$i] ==0) $rgbPPAtrib[$i]="$outValueDepth"; else if($attribChooser[$i] ==1) $rgbPPAtrib[$i]="$outValueFocusDepth"; else if($attribChooser[$i] ==2) $rgbPPAtrib[$i]="$outValueHeight"; else if($attribChooser[$i] ==3) $rgbPPAtrib[$i]="$outValueAge"; else if($attribChooser[$i] ==4) $rgbPPAtrib[$i]="1"; //nothing=1 to make the particle visible in that channel $i+=1; } $expr+= "// COLOR CHANNEL PLACEMENT \r\n"+ $particleShape[0] + ".rgbPP = <<"+ $rgbPPAtrib[0]+","+ $rgbPPAtrib[1]+","+ $rgbPPAtrib[2]+">>;"; if (getApplicationVersionAsFloat() >= 6.0) dynExpression -rad -c -s $expr $particleShape[0]; // Maya 6.5 Compatible else dynExpression -r -c -s $expr $particleShape[0]; // Maya 5 Compatible }