DAZ Script "Example Shader Definition" Sample

Todo:
Concepts Covered
Description :

Below is the source to a file that builds the interface between the user and a custom shader. Properties created by this file provide the user with access to widgets in the Surface tab of the User Interface, allowing them to modify the values passed on to the shader at render-time.

The buildShaderProperties function (line #331) in the example below should be the only method (typically) requiring modification from shader to shader.

Several convenience functions (addFloatProperty, addIntProperty, addBoolProperty, addColorProperty, addEnumProperty, addImageProperty, addStringProperty, addFileProperty) are included in the example below to simplfy the code, as well as speed it up.

 
Concepts Covered :
  • DESCRIPTION NEEDED
 
Source : ./samples/shader swapping/scripts/support/DAZ/shaderDefinitions/surface/
00001 // DAZ Studio version 3.0  filetype DAZ Script
00002 /**********************************************************************
00003     File: exampleSurf.dsa
00004 
00005     Copyright © 2002-2009 DAZ Productions. All Rights Reserved.
00006 
00007     This file is part of the DAZ Script Documentation.
00008 
00009     This file may be used only in accordance with the DAZ Script 
00010     license provided with the DAZ Script Documentation.
00011 
00012     The contents of this file may not be disclosed to third parties, 
00013     copied or duplicated in any form, in whole or in part, without the 
00014     prior written permission of DAZ Productions, except as explicitly
00015     allowed in the DAZ Script license.
00016 
00017     See http://www.daz3d.com to contact DAZ Productions or for more 
00018     information about DAZ Script.
00019 **********************************************************************/
00020 /*****************************
00021    Script globals
00022 *****************************
00023 "Shader" - is a transient global variable provided by the interpreter
00024 when this script is called via DzRSLShader::setDefinitionFile(),
00025 that references the current DzRSLShader.
00026 *********************************************************************/
00027 var oOWNER = Shader.getOwner();
00028 
00029 /*********************************************************************/
00030 function addFloatProperty( sPropertyGroup, sPropertyName, sPropertyLabel, sShadVar, fMin, fMax, fDefault, fValue, bClamped, bPercent, bMappable, bMustMap, sShadMapVar, oShadMap )
00031 {
00032     var oProperty;
00033     if( sShadVar == "Os" )
00034     {
00035         oProperty = oOWNER.getOpacityProperty();
00036     }
00037     else
00038     {
00039         oProperty = oOWNER.findProperty( sPropertyName );
00040     }
00041 
00042     if( !oProperty || ( oProperty && oProperty.className() != "DzFloatProperty" ) )
00043     {
00044         oProperty = new DzFloatProperty;
00045         oProperty.name = sPropertyName;
00046     }
00047     oProperty.setLabel( sPropertyLabel );
00048     oProperty.setPath( sPropertyGroup );
00049     oProperty.setMinMax( fMin, fMax );
00050     oProperty.setIsClamped( bClamped );
00051     oProperty.setDefaultValue( fDefault );
00052     oProperty.setDisplayAsPercent( bPercent );
00053     oProperty.setIsMappable( bMappable );
00054     fValue != undefined ? oProperty.setValue( fValue ) : oProperty.setValue( fDefault );
00055     if( sShadVar == "Os" && sShadMapVar != undefined )
00056     {
00057         Shader.addMapProperty( oProperty, sShadMapVar );
00058         if( oShadMap != undefined )
00059         {
00060             oProperty.setMap( oShadMap );
00061         }
00062     }
00063     else if( sShadVar != undefined && sShadMapVar != undefined && bMappable )
00064     {
00065         Shader.addMappableProperty( oProperty, sShadVar, sShadMapVar );
00066         oProperty.setMustHaveMap( bMustMap );
00067         if( oShadMap != undefined )
00068         {
00069             oProperty.setMap( oShadMap );
00070         }
00071     }
00072     else if( sShadVar != "Os" && sShadVar != undefined )
00073     {
00074         Shader.addShaderProperty( oProperty, sShadVar );
00075     }
00076     else
00077     {
00078         oOWNER.addProperty( oProperty );
00079     }
00080 
00081     return oProperty;
00082 }
00083 
00084 /*********************************************************************/
00085 function addIntProperty( sPropertyGroup, sPropertyName, sPropertyLabel, sShadVar, nMin, nMax, nDefault, nValue, bClamped, bMappable, bMustMap, sShadMapVar, oShadMap )
00086 {
00087     var oProperty = oOWNER.findProperty( sPropertyName );
00088     if( !oProperty || ( oProperty && oProperty.className() != "DzIntProperty" ) )
00089     {
00090         oProperty = new DzIntProperty;
00091         oProperty.name = sPropertyName;
00092     }
00093     oProperty.setLabel( sPropertyLabel );
00094     oProperty.setPath( sPropertyGroup );
00095     oProperty.setMinMax( nMin, nMax );
00096     oProperty.setIsClamped( bClamped );
00097     oProperty.setDefaultValue( nDefault );
00098     oProperty.setIsMappable( bMappable );
00099     nValue != undefined ? oProperty.setValue( nValue ) : oProperty.setValue( nDefault );
00100     if( sShadVar != undefined && sShadMapVar != undefined && bMappable )
00101     {
00102         Shader.addMappableProperty( oProperty, sShadVar, sShadMapVar );
00103         oProperty.setMustHaveMap( bMustMap );
00104         if( oShadMap != undefined )
00105         {
00106             oProperty.setMap( oShadMap );
00107         }
00108     }
00109     else if( sShadVar != undefined )
00110     {
00111         Shader.addShaderProperty( oProperty, sShadVar );
00112     }
00113     else
00114     {
00115         oOWNER.addProperty( oProperty );
00116     }
00117 
00118     return oProperty;
00119 }
00120 
00121 /*********************************************************************/
00122 function addBoolProperty( sPropertyGroup, sPropertyName, sPropertyLabel, sShadVar, bDefault, bValue )
00123 {
00124     var oProperty = oOWNER.findProperty( sPropertyName );
00125     if( !oProperty || ( oProperty && oProperty.className() != "DzBoolProperty" ) )
00126     {
00127         oProperty = new DzBoolProperty;
00128         oProperty.name = sPropertyName;
00129     }
00130     oProperty.setLabel( sPropertyLabel );
00131     oProperty.setPath( sPropertyGroup );
00132     oProperty.setDefaultValue( bDefault ? 1 : 0 );
00133     bValue != undefined ? oProperty.setBoolValue( bValue ) : oProperty.setBoolValue( bDefault );
00134     if( sShadVar != undefined )
00135     {
00136         Shader.addShaderProperty( oProperty, sShadVar );
00137     }
00138     else
00139     {
00140         oOWNER.addProperty( oProperty );
00141     }
00142 
00143     return oProperty;
00144 }
00145 
00146 /*********************************************************************/
00147 function addColorProperty( sPropertyGroup, sPropertyName, sPropertyLabel, sShadVar, clrDefault, clrValue, bMappable, bMustMap, sShadMapVar, oShadMap )
00148 {
00149     var oProperty;
00150     if( sShadVar == "Cs" )
00151     {
00152         oProperty = oOWNER.getDiffuseProperty();
00153     }
00154     else
00155     {
00156         oProperty = oOWNER.findProperty( sPropertyName );
00157     }
00158 
00159     if( !oProperty || ( oProperty && oProperty.className() != "DzColorProperty" ) )
00160     {
00161         oProperty = new DzColorProperty;
00162         oProperty.name = sPropertyName;
00163     }
00164     oProperty.setLabel( sPropertyLabel );
00165     oProperty.setPath( sPropertyGroup );
00166     oProperty.setIsMappable( bMappable );
00167     if( clrDefault != undefined )
00168     {
00169         oProperty.setDefaultValue( clrDefault.rgb );
00170     }
00171     
00172     if( clrValue != undefined )
00173     {
00174         oProperty.setColorValue( clrValue );
00175     }
00176     
00177     if( sShadVar == "Cs" && sShadMapVar != undefined )
00178     {
00179         Shader.addMapProperty( oProperty, sShadMapVar );
00180         if( oShadMap != undefined )
00181         {
00182             oProperty.setColorMap( oShadMap );
00183         }
00184     }
00185     else if( sShadVar != undefined && sShadMapVar != undefined && bMappable )
00186     {
00187         Shader.addMappableProperty( oProperty, sShadVar, sShadMapVar );
00188         oProperty.setMustHaveMap( bMustMap );
00189         if( oShadMap != undefined )
00190         {
00191             oProperty.setMap( oShadMap );
00192         }
00193     }
00194     else if( sShadVar != "Cs" && sShadVar != undefined )
00195     {
00196         Shader.addShaderProperty( oProperty, sShadVar );
00197     }
00198     else
00199     {
00200         oOWNER.addProperty( oProperty );
00201     }
00202 
00203     return oProperty;
00204 }
00205 
00206 /*********************************************************************/
00207 function addEnumProperty( sPropertyGroup, sPropertyName, sPropertyLabel, sShadVar, aItems, nValue )
00208 {
00209     var oProperty = oOWNER.findProperty( sPropertyName );
00210     if( !oProperty || ( oProperty && oProperty.className() != "DzEnumProperty" ) )
00211     {
00212         oProperty = new DzEnumProperty;
00213         oProperty.name = sPropertyName;
00214     }
00215     oProperty.setLabel( sPropertyLabel );
00216     oProperty.setPath( sPropertyGroup );
00217     if( aItems.length > 0 )
00218     {
00219         for( var i = 0; i < aItems.length; i++ )
00220         {
00221             oProperty.addItem( aItems[ i ], i );
00222         }
00223     }
00224     
00225     if( nValue != undefined )
00226     {
00227         oProperty.setValue( nValue );
00228     }
00229     
00230     if( sShadVar != undefined )
00231     {
00232         Shader.addShaderProperty( oProperty, sShadVar );
00233     }
00234     else
00235     {
00236         oOWNER.addProperty( oProperty );
00237     }
00238 
00239     return oProperty;
00240 }
00241 
00242 /*********************************************************************/
00243 function addImageProperty( sPropertyGroup, sPropertyName, sPropertyLabel, sShadVar, oValue )
00244 {
00245     var oProperty = oOWNER.findProperty( sPropertyName );
00246     if( !oProperty || ( oProperty && oProperty.className() != "DzImageProperty" ) )
00247     {
00248         oProperty = new DzImageProperty;
00249         oProperty.name = sPropertyName;
00250     }
00251     oProperty.setLabel( sPropertyLabel );
00252     oProperty.setPath( sPropertyGroup );
00253     if( oValue != undefined )
00254     {
00255         oProperty.setValue( oValue );
00256     }
00257     if( sShadVar != undefined )
00258     {
00259         Shader.addShaderProperty( oProperty, sShadVar );
00260     }
00261     else
00262     {
00263         oOWNER.addProperty( oProperty );
00264     }
00265 
00266     return oProperty;
00267 }
00268 
00269 /*********************************************************************/
00270 function addStringProperty( sPropertyGroup, sPropertyName, sPropertyLabel, sShadVar, sValue )
00271 {
00272     var oProperty = oOWNER.findProperty( sPropertyName );
00273     if( !oProperty || ( oProperty && oProperty.className() != "DzStringProperty" ) )
00274     {
00275         oProperty = new DzStringProperty;
00276         oProperty.name = sPropertyName;
00277     }
00278     oProperty.setLabel( sPropertyLabel );
00279     oProperty.setPath( sPropertyGroup );
00280     if( sValue != undefined )
00281     {
00282         oProperty.setValue( sValue );
00283     }
00284     
00285     if( sShadVar != undefined )
00286     {
00287         Shader.addShaderProperty( oProperty, sShadVar );
00288     }
00289     else
00290     {
00291         oOWNER.addProperty( oProperty );
00292     }
00293 
00294     return oProperty;
00295 }
00296 
00297 /*********************************************************************/
00298 function addFileProperty( sPropertyGroup, sPropertyName, sPropertyLabel, sShadVar, sValue, sFilter, nType )
00299 {
00300     var oProperty = oOWNER.findProperty( sPropertyName );
00301     if( !oProperty || ( oProperty && oProperty.className() != "DzFileProperty" ) )
00302     {
00303         oProperty = new DzFileProperty;
00304         oProperty.name = sPropertyName;
00305     }
00306     oProperty.setLabel( sPropertyLabel );
00307     oProperty.setPath( sPropertyGroup );
00308     if( sValue != undefined )
00309     {
00310         oProperty.setValue( sValue );
00311     }
00312     
00313     if( sFilter != undefined )
00314     {
00315         oProperty.setFilter( sFilter );
00316     }
00317     
00318     if( nType != undefined )
00319     {
00320         oProperty.setType( nType );
00321     }
00322     
00323     if( sShadVar != undefined )
00324     {
00325         Shader.addShaderProperty( oProperty, sShadVar );
00326     }
00327     else
00328     {
00329         oOWNER.addProperty( oProperty );
00330     }
00331 
00332     return oProperty;
00333 }
00334 
00335 /*********************************************************************/
00336 function buildShaderProperties()
00337 {
00338     var sPropertyGroup;
00339     var oProperty;
00340     
00341     sPropertyGroup = "Shader Arguments";
00342     
00343     oProperty = addFloatProperty( sPropertyGroup, "Float Arg", "Float Arg (float)", "myFloatProperty", 0, 1, 0.5, undefined, true, true, false, false, undefined, undefined );
00344     oProperty = addIntProperty( sPropertyGroup, "Int Arg", "Int Arg (float)", "myIntProperty", 0, 5, 2, undefined, true, false, false, undefined, undefined );
00345     oProperty = addBoolProperty( sPropertyGroup, "Bool Arg", "Bool Arg (float)", "myBoolProperty", true, undefined );
00346     oProperty = addColorProperty( sPropertyGroup, "Color Arg", "Color Arg (color)", "myColorProperty", new Color( 255, 255, 255 ), undefined, false, false, undefined, undefined );
00347     oProperty = addEnumProperty( sPropertyGroup, "Enum Arg", "Enum Arg (float)", "myEnumProperty", [ "String 0", "String 1", "String 2", "String 3" ], 1 );
00348     oProperty = addImageProperty( sPropertyGroup, "Image Arg", "Image Arg (string)", "myImageProperty", undefined );
00349     oProperty = addStringProperty( sPropertyGroup, "String Arg", "String Arg (string)", "myStringProperty", undefined );
00350     oProperty = addFileProperty( sPropertyGroup, "File Arg", "File Arg (string)", "myFileProperty", undefined, undefined, undefined );
00351     
00352     sPropertyGroup = "Element Attributes";
00353     
00354     oProperty = addFloatProperty( sPropertyGroup, "Float Attrib", "Float Attrib (float)", undefined, 0, 1, 0.5, undefined, true, true, false, false, undefined, undefined );
00355     oProperty = addIntProperty( sPropertyGroup, "Int Attrib", "Int Attrib (float)", undefined, 0, 5, 2, undefined, true, false, false, undefined, undefined );
00356     oProperty = addBoolProperty( sPropertyGroup, "Bool Attrib", "Bool Attrib (float)", undefined, true, undefined );
00357     oProperty = addColorProperty( sPropertyGroup, "Color Attrib", "Color Attrib (color)", undefined, new Color( 255, 255, 255 ), undefined, false, false, undefined, undefined );
00358     oProperty = addEnumProperty( sPropertyGroup, "Enum Attrib", "Enum Attrib (float or string)", undefined, [ "String 0", "String 1", "String 2", "String 3" ], 1 );
00359     oProperty = addImageProperty( sPropertyGroup, "Image Attrib", "Image Attrib (string)", undefined, undefined );
00360     oProperty = addStringProperty( sPropertyGroup, "String Attrib", "String Attrib (string)", undefined, undefined );
00361     oProperty = addFileProperty( sPropertyGroup, "File Attrib", "File Attrib (string)", undefined, undefined, undefined, undefined );
00362 }
00363 
00364 /*********************************************************************/
00365 buildShaderProperties();

Generated on Thu Sep 24 12:21:07 2009

Copyright © 2002 - 2009 DAZ 3D, Inc.