Adobe® Flex® 4 Language Reference
Show Packages and Classes List |  Packages  |  Classes  |  Index  |  Appendixes
flash.display 
Shader 
Packageflash.display
Classpublic class Shader
InheritanceShader Inheritance Object
Subclasses ColorBurnShader, ColorDodgeShader, ColorShader, ExclusionShader, HueShader, LuminosityMaskShader, LuminosityShader, SaturationShader, SoftLightShader

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 10, AIR 1.5

A Shader instance represents a Pixel Bender shader kernel in ActionScript. To use a shader in your application, you create a Shader instance for it. You then use that Shader instance in the appropriate way according to the effect you want to create. For example, to use the shader as a filter, you assign the Shader instance to the shader property of a ShaderFilter object.

A shader defines a function that executes on all the pixels in an image, one pixel at a time. The result of each call to the function is the output color at that pixel coordinate in the image. A shader can specify one or more input images, which are images whose content can be used in determining the output of the function. A shader can also specify one or more parameters, which are input values that can be used in calculating the function output. In a single shader execution, the input and parameter values are constant. The only thing that varies is the coordinate of the pixel whose color is the function result. Shader function calls for multiple output pixel coordinates execute in parallel to improve shader execution performance.

The shader bytecode can be loaded at run time using a URLLoader instance. The following example demonstrates loading a shader bytecode file at run time and linking it to a Shader instance.

     var loader:URLLoader = new URLLoader();
     loader.dataFormat = URLLoaderDataFormat.BINARY;
     loader.addEventListener(Event.COMPLETE, onLoadComplete);
     loader.load(new URLRequest("myShader.pbj"));
      
     var shader:Shader;
      
     function onLoadComplete(event:Event):void {
         // Create a new shader and set the loaded data as its bytecode
         shader = new Shader();
         shader.byteCode = loader.data;
         
         // You can also pass the bytecode to the Shader() constructor like this:
         // shader = new Shader(loader.data);
         
         // do something with the shader
     }
     

You can also embed the shader into the SWF at compile time using the [Embed] metadata tag. The [Embed] metadata tag is only available if you use the Flex SDK to compile the SWF. The [Embed] tag's source parameter points to the shader file, and its mimeType parameter is "application/octet-stream", as in this example:

     [Embed(source="myShader.pbj", mimeType="application/octet-stream)]
     var MyShaderClass:Class;
      
     // ...
     
     // create a new shader and set the embedded shader as its bytecode
     var shaderShader = new Shader();
     shader.byteCode = new MyShaderClass();
      
     // You can also pass the bytecode to the Shader() constructor like this:
     // var shader:Shader = new Shader(new MyShaderClass());
      
     // do something with the shader
     

In either case, you link the raw shader (the URLLoader.data property or an instance of the [Embed] data class) to the Shader instance. As the previous examples demonstrate, you can do this in two ways. You can pass the shader bytecode as an argument to the Shader() constructor. Alternatively, you can set it as the Shader instance's byteCode property.

Once a Shader instance is created, it can be used in one of several ways:

  • A shader fill: The output of the shader is used as a fill for content drawn with the drawing API. Pass the Shader instance as an argument to the Graphics.beginShaderFill() method.
  • A shader filter: The output of the shader is used as a graphic filter applied to a display object. Assign the Shader instance to the shader property of a ShaderFilter instance.
  • A blend mode: The output of the shader is rendered as the blending between two overlapping display objects. Assign the Shader instance to the blendShader property of the upper of the two display objects.
  • Background shader processing: The shader executes in the background, avoiding the possibility of freezing the display, and dispatches an event when processing is complete. Assign the Shader instance to the shader property of a ShaderJob instance.

View the examples

See also



Public Properties
 PropertyDefined By
  byteCode : ByteArray
[write-only] The raw shader bytecode for this Shader instance.
Shader
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  data : ShaderData
Provides access to parameters, input images, and metadata for the Shader instance.
Shader
  precisionHint : String
The precision of math operations performed by the shader.
Shader
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
 MethodDefined By
  
Shader(code:ByteArray = null)
Creates a new Shader instance.
Shader
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
Property Detail

byteCode

property
byteCode:ByteArray  [write-only]

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 10, AIR 1.5

The raw shader bytecode for this Shader instance.



Implementation
    public function set byteCode(value:ByteArray):void

data

property 
data:ShaderData

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 10, AIR 1.5

Provides access to parameters, input images, and metadata for the Shader instance. ShaderParameter objects representing parameters for the shader, ShaderInput objects representing the input images for the shader, and other values representing the shader's metadata are dynamically added as properties of the data property object when the Shader instance is created. Those properties can be used to introspect the shader and to set parameter and input values.

For information about accessing and manipulating the dynamic properties of the data object, see the ShaderData class description.



Implementation
    public function get data():ShaderData
    public function set data(value:ShaderData):void

See also

precisionHint

property 
precisionHint:String

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 10, AIR 1.5

The precision of math operations performed by the shader.

The set of possible values for the precisionHint property is defined by the constants in the ShaderPrecision class.

The default value is ShaderPrecision.FULL. Setting the precision to ShaderPrecision.FAST can speed up math operations at the expense of precision.

Full precision mode (ShaderPrecision.FULL) computes all math operations to the full width of the IEEE 32-bit floating standard and provides consistent behavior on all platforms. In this mode, some math operations such as trigonometric and exponential functions can be slow.

Fast precision mode (ShaderPrecision.FAST) is designed for maximum performance but does not work consistently on different platforms and individual CPU configurations. In many cases, this level of precision is sufficient to create graphic effects without visible artifacts.

The precision mode selection affects the following shader operations. These operations are faster on an Intel processor with the SSE instruction set:

  • sin(x)
  • cos(x)
  • tan(x)
  • asin(x)
  • acos(x)
  • atan(x)
  • atan(x, y)
  • exp(x)
  • exp2(x)
  • log(x)
  • log2(x)
  • pow(x, y)
  • reciprocal(x)
  • sqrt(x)



Implementation
    public function get precisionHint():String
    public function set precisionHint(value:String):void

See also

Constructor Detail

Shader

()Constructor
public function Shader(code:ByteArray = null)

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 10, AIR 1.5

Creates a new Shader instance.

Parameters
code:ByteArray (default = null) — The raw shader bytecode to link to the Shader.
ShaderExample.1.as

The following example loads a shader bytecode file at run time and creates a Shader instance linked to it.

Note that this example assumes there's a shader bytecode file named "donothing.pbj" in the same directory as the output directory for the application. The Pixel Bender source code for the DoNothing shader is available in the ShaderData class example.


package {
    import flash.display.Shader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequest;

    public class LoadedShaderExample extends Sprite {
        
        private var loader:URLLoader;
        
        public function LoadedShaderExample() {
            loader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.BINARY;
            loader.addEventListener(Event.COMPLETE, loadCompleteHandler);
            loader.load(new URLRequest("donothing.pbj"));
        }
        
        private function loadCompleteHandler(event:Event):void {
            var shader:Shader = new Shader();
            shader.byteCode = loader.data;
            
            // do something with the Shader instance
        }
    }
}
ShaderExample.2.as

The following example embeds a shader bytecode file by compiling it into the SWF, and creates a Shader instance linked to it.

Note that this example assumes there's a shader bytecode file named "donothing.pbj" in the same directory as the source code for the application, and that the Flex SDK is used to compile the SWF. The Pixel Bender source code for the DoNothing shader is available in the ShaderData class example.


package {
    import flash.display.Shader;
    import flash.display.Sprite;

    public class EmbeddedShaderExample extends Sprite {
        
        [Embed(source="donothing.pbj", mimeType="application/octet-stream")]
        private static var DoNothingShader:Class;
        
        public function EmbeddedShaderExample() {
            var shader:Shader = new Shader();
            shader.byteCode = new DoNothingShader();
            
            // do something with the Shader instance
        }
    }
}