window.runtime property | window.runtime.flash.display3D.Context3D |
Inheritance | Context3D EventDispatcher Object |
Runtime Versions: | 3 |
A rendering context includes a drawing surface and its associated resources and state. When possible, the
rendering context uses the hardware graphics processing unit (GPU). Otherwise, the rendering context uses software.
(If rendering through Context3D is not supported on a platform, the stage3Ds
property of the Stage object contains
an empty list.)
The Context3D rendering context is a programmable pipeline that is very similar to OpenGL ES 2, but is abstracted so that it is compatible with a range of hardware and GPU interfaces. Although designed for 3D graphics, the rendering pipeline does not mandate that the rendering is three dimensional. Thus, you can create a 2D renderer by supplying the appropriate vertex and pixel fragment programs. In both the 3D and 2D cases, the only geometric primitive supported is the triangle.
Get an instance of the Context3D class by calling the requestContext3D()
method of a Stage3D object.
A limited number of Context3D objects can exist per stage; one for each Stage3D in the Stage.stage3Ds
list.
When the context is created, the Stage3D object dispatches a context3DCreate
event. A rendering context can
be destroyed and recreated at any time, such as when another application that uses the GPU gains focus.
Your code should anticipate receiving multiple context3DCreate
events. Position the rendering area on the stage
using the x
and y
properties of the associated Stage3D instance.
When the rendering context is attached to the stage of an HTML window,
the default background of the HTMLLoader object obscures the 3D viewport. To turn off the
default background, set the paintsDefaultBackground
property to
false
:
window.htmlLoader.paintsDefaultBackground = false;
To render and display a scene (after getting a Context3D object), the following steps are typical:
- Configure the main display buffer attributes by calling
configureBackBuffer()
. - Create and initialize your rendering resources, including:
- Vertex and index buffers defining the scene geometry
- Vertex and pixel programs (shaders) for rendering the scene
- Textures
- Render a frame:
- Set the render state as appropriate for an object or collection of objects in the scene.
- Call the
drawTriangles()
method to render a set of triangles. - Change the rendering state for the next group of objects.
- Call
drawTriangles()
to draw the triangles defining the objects. - Repeat until the scene is entirely rendered.
- Call the
present()
method to display the rendered scene on the stage.
The following limits apply to rendering:
Resource limits:
Resource | Number allowed | Total memory |
---|---|---|
Vertex buffers
| 4096 | 256 MB |
Index buffers
| 4096 | 128 MB |
Programs
| 4096 | 16 MB |
Textures
| 4096 | 128 MB¹ |
Cube textures
| 4096 | 256 MB |
AGAL limits: 200 opcodes per program.
Draw call limits: 32,768 drawTriangles()
calls for each present()
call.
¹350 MB is the absolute limit for textures, including the texture memory required for mipmaps. However, many devices do not support this much texture memory. For maximum compatibility, limit texture memory use to 128 MB, or less.
You cannot create Context3D objects with the Context3D constructor. It is constructed and available as a property of a Stage3D instance. The Context3D class can be used on desktop platforms only, both when running in Flash Player and in AIR.
See also
Context3DClearMask
Context3DCompareMode
Context3DProgramType
Context3DRenderMode
Context3DStencilAction
Context3DTextureFormat
Context3DTriangleFace
Context3DVertexBufferFormat
Texture
CubeTexture
IndexBuffer3D
Program3D
Stage3D
VertexBuffer3D
Property | Defined By | ||
---|---|---|---|
constructor : Object
A reference to the class object or constructor function for a given object instance. | Object | ||
driverInfo : String [read-only]
The type of graphics library driver used by this rendering context. | Context3D | ||
enableErrorChecking : Boolean
Specifies whether errors encountered by the renderer are reported to the application. | Context3D | ||
prototype : Object [static]
A reference to the prototype object of a class or function object. | Object |
Method | Defined By | ||
---|---|---|---|
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener
receives notification of an event. | EventDispatcher | ||
clear(red:Number = 0.0, green:Number = 0.0, blue:Number = 0.0, alpha:Number = 1.0, depth:Number = 1.0, stencil:uint = 0, mask:uint = 0xffffffff):void
Clears the color, depth, and stencil buffers associated with this Context3D object and
fills them with the specified values. | Context3D | ||
configureBackBuffer(width:int, height:int, antiAlias:int, enableDepthAndStencil:Boolean = true):void
Sets the viewport dimensions and other attributes of the rendering buffer. | Context3D | ||
createCubeTexture(size:int, format:String, optimizeForRenderToTexture:Boolean):flash.display3D.textures:CubeTexture
Creates a CubeTexture object. | Context3D | ||
createIndexBuffer(numIndices:int):IndexBuffer3D
Creates an IndexBuffer3D object. | Context3D | ||
Creates a Program3D object. | Context3D | ||
createTexture(width:int, height:int, format:String, optimizeForRenderToTexture:Boolean):flash.display3D.textures:Texture
Creates a Texture object. | Context3D | ||
createVertexBuffer(numVertices:int, data32PerVertex:int):VertexBuffer3D
Creates a VertexBuffer3D object. | Context3D | ||
dispatchEvent(event:Event):Boolean
Dispatches an event into the event flow. | EventDispatcher | ||
dispose():void
Frees all resources and internal storage associated with this Context3D. | Context3D | ||
drawToBitmapData(destination:BitmapData):void
Draws the current render buffer to a bitmap. | Context3D | ||
drawTriangles(indexBuffer:IndexBuffer3D, firstIndex:int = 0, numTriangles:int = -1):void
Render the specified triangles using the current buffers and state of this Context3D object. | Context3D | ||
hasEventListener(type:String):Boolean
Checks whether the EventDispatcher object has any listeners registered for a specific type
of event. | EventDispatcher | ||
hasOwnProperty(name:String):Boolean
Indicates whether an object has a specified property defined. | Object | ||
isPrototypeOf(theClass:Object):Boolean
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter. | Object | ||
present():void
Displays the back rendering buffer. | Context3D | ||
propertyIsEnumerable(name:String):Boolean
Indicates whether the specified property exists and is enumerable. | Object | ||
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object. | EventDispatcher | ||
setBlendFactors(sourceFactor:String, destinationFactor:String):void
Specifies the factors used to blend the output color of a drawing operation with the existing color. | Context3D | ||
setColorMask(red:Boolean, green:Boolean, blue:Boolean, alpha:Boolean):void
Sets the mask used when writing colors to the render buffer. | Context3D | ||
setCulling(triangleFaceToCull:String):void
Sets triangle culling mode. | Context3D | ||
setDepthTest(depthMask:Boolean, passCompareMode:String):void
Sets type of comparison used for depth testing. | Context3D | ||
setProgram(program:Program3D):void
Sets vertex and fragment shader programs to use for subsequent rendering. | Context3D | ||
setProgramConstantsFromMatrix(programType:String, firstRegister:int, matrix:Matrix3D, transposedMatrix:Boolean = false):void
Sets constants for use by shader programs using values stored in a Matrix3D. | Context3D | ||
setProgramConstantsFromVector(programType:String, firstRegister:int, data:Vector.<Number>, numRegisters:int = -1):void
Sets the constant inputs for the shader programs. | Context3D | ||
setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void
Sets the availability of a dynamic property for loop operations. | Object | ||
setRenderToBackBuffer():void
Sets the back rendering buffer as the render target. | Context3D | ||
setRenderToTexture(texture:flash.display3D.textures:TextureBase, enableDepthAndStencil:Boolean = false, antiAlias:int = 0, surfaceSelector:int = 0):void
Sets the specified texture as the rendering target. | Context3D | ||
setScissorRectangle(rectangle:Rectangle):void
Sets a scissor rectangle, which is type of drawing mask. | Context3D | ||
setStencilActions(triangleFace:String = "frontAndBack", compareMode:String = "always", actionOnBothPass:String = "keep", actionOnDepthFail:String = "keep", actionOnDepthPassStencilFail:String = "keep"):void
Sets stencil mode and operation. | Context3D | ||
setStencilReferenceValue(referenceValue:uint, readMask:uint = 255, writeMask:uint = 255):void
Sets the stencil comparison value used for stencil tests. | Context3D | ||
setTextureAt(sampler:int, texture:flash.display3D.textures:TextureBase):void
Specifies the texture to use for a texture input register of a fragment program. | Context3D | ||
setVertexBufferAt(index:int, buffer:VertexBuffer3D, bufferOffset:int = 0, format:String = "float4"):void
Specifies which vertex data components correspond to a single vertex shader program input. | Context3D | ||
toLocaleString():String
Returns the string representation of this object, formatted according to locale-specific conventions. | Object | ||
toString():String
Returns the string representation of the specified object. | Object | ||
valueOf():Object
Returns the primitive value of the specified object. | Object | ||
willTrigger(type:String):Boolean
Checks whether an event listener is registered with this EventDispatcher object or any of
its ancestors for the specified event type. | EventDispatcher |
driverInfo | property |
driverInfo:String
[read-only] Runtime Versions: | 3 |
The type of graphics library driver used by this rendering context. Indicates whether the rendering is using software, a DirectX driver, or an OpenGL driver.
enableErrorChecking | property |
enableErrorChecking:Boolean
Runtime Versions: | 3 |
Specifies whether errors encountered by the renderer are reported to the application.
When enableErrorChecking
is true
, the clear()
,
and drawTriangles()
methods are synchronous and can throw errors. When
enableErrorChecking
is false
, the default, the clear()
,
and drawTriangles()
methods are asynchronous and errors are not reported.
Enabling error checking reduces rendering performance. You should only enable error checking when debugging.
See also
clear | () | method |
public function clear(red:Number = 0.0, green:Number = 0.0, blue:Number = 0.0, alpha:Number = 1.0, depth:Number = 1.0, stencil:uint = 0, mask:uint = 0xffffffff):void
Runtime Versions: | 3 |
Clears the color, depth, and stencil buffers associated with this Context3D object and fills them with the specified values.
Set the mask
parameter to specify which buffers to clear. Use the constants defined in the
Context3DClearMask class to set the mask
parameter. Use the bitwise OR operator, "|", to add
multiple buffers to the mask (or use Context3DClearMask.ALL
). When rendering to the back buffer,
the configureBackBuffer()
method must be called before any clear()
calls.
Note: If you specify a parameter value outside the allowed range, Numeric parameter values are silently clamped to the range zero to one.
Likewise, if stencil
is greater than 0xff it is set to 0xff.
Parameters
red:Number (default = 0.0 ) — the red component of the color with which to clear the color buffer, in the range zero to one.
| |
green:Number (default = 0.0 ) — the green component of the color with which to clear the color buffer, in the range zero to one.
| |
blue:Number (default = 0.0 ) — the blue component of the color with which to clear the color buffer, in the range zero to one.
| |
alpha:Number (default = 1.0 ) — the alpha component of the color with which to clear the color buffer, in the range zero to one.
The alpha component is not used for blending. It is written to the buffer alpha directly.
| |
depth:Number (default = 1.0 ) — the value with which to clear the depth buffer, in the range zero to one.
| |
stencil:uint (default = 0 ) — the 8-bit value with which to clear the stencil buffer, in a range of 0x00 to 0xff.
| |
mask:uint (default = 0xffffffff ) — specifies which buffers to clear.
|
Throws
Error — Object Disposed: if this Context3D object has been disposed by a calling dispose() or
because the underlying rendering hardware has been lost.
|
See also
configureBackBuffer | () | method |
public function configureBackBuffer(width:int, height:int, antiAlias:int, enableDepthAndStencil:Boolean = true):void
Runtime Versions: | 3 |
Sets the viewport dimensions and other attributes of the rendering buffer.
Rendering is double-buffered. The back buffer is swapped with the visible,
front buffer when the present()
method is called. The minimum size of the buffer is 50x50 pixels.
Configuring the buffer is a slow operation. Avoid changing the buffer size or attributes during normal
rendering operations.
Parameters
width:int — width in pixels of the buffer.
| |||||||||
height:int — height in pixels of the buffer.
| |||||||||
antiAlias:int — an integer value specifying the requested antialiasing quality.
The value correlates to the number of subsamples used when antialiasing.
Using more subsamples requires more calculations to be performed, although
the relative performance impact depends on the specific rendering hardware. The
type of antialiasing and whether antialiasing is performed at all is dependent on the
device and rendering mode. Antialiasing is not supported at all by the
software rendering context.
| |||||||||
enableDepthAndStencil:Boolean (default = true ) — false indicates no depth or stencil buffer is created,
true creates a depth and a stencil buffer.
|
Throws
Error — Object Disposed: if this Context3D object has been disposed by a calling dispose() or
because the underlying rendering hardware has been lost.
| |
Error — Bad Input Size: The width or height parameter is less than the minimum allowed size.
|
createCubeTexture | () | method |
public function createCubeTexture(size:int, format:String, optimizeForRenderToTexture:Boolean):flash.display3D.textures:CubeTexture
Runtime Versions: | 3 |
Creates a CubeTexture object.
Use a CubeTexture object to upload cube texture bitmaps to the rendering context and to reference a cube texture during rendering. A cube texture consists of six equal-sized, square textures arranged in a cubic topology and are useful for describing environment maps.
You cannot create CubeTexture objects with a CubeTexture constructor; use this method instead. After
creating a CubeTexture object, upload the texture bitmap data using the CubeTexture uploadFromBitmapData()
,
uploadFromByteArray()
, or uploadCompressedTextureFromByteArray()
methods..
Parameters
size:int — the texture edge length in texels
| |
format:String — the texel format, of the Context3DTextureFormat enumerated list
| |
optimizeForRenderToTexture:Boolean — set as true if texture is likely to be used as a render target
|
flash.display3D.textures:CubeTexture |
Throws
Error — Object Disposed: if this Context3D object has been disposed by a calling dispose() or
because the underlying rendering hardware has been lost.
| |
Error — Resource Limit Exceeded: if too many Texture objects are created or the amount of
memory allocated to textures is exceeded.
| |
ArgumentError — Depth Texture Not Implemented: if you attempt to create a depth texture.
| |
ArgumentError — Texture Size Is Zero: if the size
parameter is not greater than zero.
| |
ArgumentError — Texture Not Power Of Two: if the size
parameter is not a power of two.
| |
ArgumentError — Texture Too Big: if the size
parameter is greater than 2048.
| |
Error — Texture Creation Failed: if the CubeTexture object could not be created by the
rendering context (but information about the reason is not available).
|
See also
createIndexBuffer | () | method |
public function createIndexBuffer(numIndices:int):IndexBuffer3D
Runtime Versions: | 3 |
Creates an IndexBuffer3D object.
Use an IndexBuffer3D object to upload a set of triangle indices to the rendering context and to
reference that set of indices for rendering. Each index in the index buffer references a corresponding vertex
in a vertex buffer. Each set of three indices identifies a triangle. Pass the IndexBuffer3D object to the
drawTriangles()
method to render one or more triangles defined in the index buffer.
You cannot create IndexBuffer3D objects with the IndexBuffer3D class constructor; use this method instead. After
creating a IndexBuffer3D object, upload the indices using the IndexBuffer3D uploadFromVector()
or uploadFromByteArray()
methods.
Parameters
numIndices:int — the number of vertices to be stored in the buffer. The maximum number of indices in a single buffer is 524287.
|
IndexBuffer3D |
Throws
Error — Object Disposed: if this Context3D object has been disposed by a calling dispose() or
because the underlying rendering hardware has been lost.
| |
Error — Resource Limit Exceeded: if too many index buffers are created or the amount of
memory allocated to index buffers is exceeded.
|
See also
createProgram | () | method |
public function createProgram():Program3D
Runtime Versions: | 3 |
Creates a Program3D object.
Use a Program3D object to upload shader programs to the rendering context and to reference uploaded programs during rendering. A Program3D object stores two programs, a vertex program and a fragment program (also known as a pixel program). The programs are written in a binary shader assembly language.
You cannot create Program3D objects with a Program3D constructor; use this method instead. After
creating a Program3D object, upload the programs using the Program3D upload()
method.
Program3D |
Throws
Error — Object Disposed: if this Context3D object has been disposed by a calling dispose() or
because the underlying rendering hardware has been lost.
| |
Error — The number of programs exceeds 4096 or the total memory size exceeds 16MB
(use dispose to free Program3D resources).
|
See also
createTexture | () | method |
public function createTexture(width:int, height:int, format:String, optimizeForRenderToTexture:Boolean):flash.display3D.textures:Texture
Runtime Versions: | 3 |
Creates a Texture object.
Use a Texture object to upload texture bitmaps to the rendering context and to reference a texture during rendering.
You cannot create Texture objects with a Texture constructor; use this method instead. After
creating a Texture object, upload the texture bitmaps using the Texture uploadFromBitmapData()
,
uploadFromByteArray()
, or uploadCompressedTextureFromByteArray()
methods.
BitmapData
format.
Floating point textures use a conventional RGBA format.
Parameters
width:int — the texture width in texels.
| |
height:int — the texture height in texels.
| |
format:String — the texel format, of the Context3DTextureFormat enumerated list.
| |
optimizeForRenderToTexture:Boolean — set as true if texture is likely to be used as a render target.
|
flash.display3D.textures:Texture |
Throws
Error — Object Disposed: if this Context3D object has been disposed by a calling dispose() or
because the underlying rendering hardware has been lost.
| |
Error — Resource Limit Exceeded: if too many Texture objects are created or the amount of
memory allocated to textures is exceeded.
| |
ArgumentError — Depth Texture Not Implemented: if you attempt to create a depth texture.
| |
ArgumentError — Texture Size Is Zero: if both the width or height
parameters are not greater than zero.
| |
ArgumentError — Texture Not Power Of Two: if both the width and height
parameters are not a power of two.
| |
ArgumentError — Texture Too Big: if either the width or the height
parameter is greater than 2048.
| |
Error — Texture Creation Failed: if the Texture object could not be created by the
rendering context (but information about the reason is not available).
|
See also
createVertexBuffer | () | method |
public function createVertexBuffer(numVertices:int, data32PerVertex:int):VertexBuffer3D
Runtime Versions: | 3 |
Creates a VertexBuffer3D object.
Use a VertexBuffer3D object to upload a set of vertex data to the rendering context.
A vertex buffer contains the data needed to render each point in the scene geometry.
The data attributes associated with each vertex typically includes position, color, and texture
coordinates and serve as the input to the vertex shader program. Identify the data
values that correspond to one of the inputs of the vertex program using the
setVertexBufferAt()
method. You can specify up to sixty-four 32-bit values
for each vertex. (A shader program can use up to eight inputs.)
You cannot create VertexBuffer3D objects with a VertexBuffer3D constructor; use this method instead. After
creating a VertexBuffer3D object, upload the vertex data using the VertexBuffer3D uploadFromVector()
or uploadFromByteArray()
methods.
Parameters
numVertices:int — the number of vertices to be stored in the buffer. The maximum number of
vertices in a single buffer is 65535.
| |
data32PerVertex:int — the number of 32-bit(4-byte) data values associated with each vertex.
The maximum number of 32-bit data elements per vertex is 64 (or 256 bytes).
Note that only eight attribute registers are
accessible by a vertex shader program at any given time. Use SetVertextBufferAt() to select attributes from within
a vertex buffer.
|
VertexBuffer3D |
Throws
Error — Object Disposed: if this Context3D object has been disposed by a calling dispose() or
because the underlying rendering hardware has been lost.
| |
Error — Resource Limit Exceeded: if too many vertex buffer objects are created or the amount of memory alloted to
vertex buffers is exceeded.
| |
ArgumentError — Buffer Too Big: when numVertices is greater than 0x10000 or data32PerVertex is greater than 64.
| |
ArgumentError — Buffer Has Zero Size: when numVertices is zero or data32PerVertex is zero.
| |
ArgumentError — Buffer Creation Failed: if the VertexBuffer3D object could not be created
by the rendering context (but additional information about the reason is not available).
|
See also
dispose | () | method |
public function dispose():void
Runtime Versions: | 3 |
Frees all resources and internal storage associated with this Context3D.
All index buffers, vertex buffers, textures, and programs that were created through this Context3D are disposed
just as if calling dispose()
on each of them individually.
In addition, the Context3D itself is disposed freeing all temporary buffers and the back buffer.
If you call configureBackBuffer(), clear(), drawTriangles(), createCubeTexture(), createTexture(), createProgram(),
createIndexBuffer(), createVertexBuffer(), or drawToBitmapData() after calling dispose(), the runtime throws an exception.
Warning: calling dispose()
on a Context3D object while the associated Stage3D object still
has a listener for the context3DCreate
event, simulates a device loss. The runtime creates a
new Context3D instance for the Stage3D object and dispatches another context3DCreate
event.
If this is not desired, remove the event listener from the Stage3D object before calling dispose()
.
See also
drawToBitmapData | () | method |
public function drawToBitmapData(destination:BitmapData):void
Runtime Versions: | 3 |
Draws the current render buffer to a bitmap.
The current contents of the back render buffer are copied to a BitmapData object. This is potentially a very slow
operation that can take up to a second. Use with care.
Note that this function does not copy the front render buffer (the one shown on stage), but the buffer being drawn to.
To capture the rendered image as it appears on the stage, call drawToBitmapData()
immediately before you calling present()
.
When the image is drawn, it is not scaled to fit the bitmap. Instead, the contents are clipped to the size of the destination bitmap.
Flash BitmapData objects store colors already multiplied by the alpha component. For example, if the "pure" rgb color components of a pixel are (0x0A, 0x12, 0xBB) and the alpha component is 0x7F (.5), then the pixel is stored in the BitmapData object with the rgba values: (0x05, 0x09, 0x5D, 0x7F). You can set the blend factors so that the colors rendered to the buffer are multiplied by alpha or perform the operation in the fragment shader. The rendering context does not validate that the colors are stored in premultiplied format.
Parameters
destination:BitmapData |
Throws
Error — Object Disposed: if this Context3D object has been disposed by a calling dispose() or
because the underlying rendering hardware has been lost.
|
drawTriangles | () | method |
public function drawTriangles(indexBuffer:IndexBuffer3D, firstIndex:int = 0, numTriangles:int = -1):void
Runtime Versions: | 3 |
Render the specified triangles using the current buffers and state of this Context3D object.
For each triangle, the triangle vertices are processed by the vertex shader program and the triangle surface is processed by the pixel shader program. The output color from the pixel program for each pixel is drawn to the render target depending on the stencil operations, depth test, source and destination alpha, and the current blend mode. The render destination can be the main render buffer or a texture.
If culling is enabled, (with the setCulling()
method), then triangles can be discarded from the
scene before the pixel program is run. If stencil and depth testing are enabled, then output
pixels from the pixel program can be discarded without updating the render destination.
In addition, the pixel program can decide not to output a color for a pixel.
The rendered triangles are not displayed in the viewport until you call the present()
method.
After each present()
call, the clear()
method must be called before the
first drawTriangles()
call or rendering fails.
When enableErrorChecking
is false
, this function returns immediately,
does not wait for results, and throws exceptions only if this Context3D instance has been disposed
or there are too many draw calls. If the rendering context state is invalid
rendering fails silently. When the enableErrorChecking
property is true
,
this function returns after the triangles are drawn and throws exceptions for any drawing errors
or invalid context state.
Parameters
indexBuffer:IndexBuffer3D — a set of vertex indices referencing the vertices to render.
| |
firstIndex:int (default = 0 ) — the index of the first vertex index selected to render. Default 0.
| |
numTriangles:int (default = -1 ) — the number of triangles to render. Each triangle consumes three indices.
Pass -1 to draw all triangles in the index buffer. Default -1.
|
Throws
Error — Object Disposed: if this Context3D object has been disposed by a calling dispose() or
because the underlying rendering hardware has been lost.
| |
Error — If this method is called too many times between calls to present() .
The maximum number of calls is 32,768.
The following errors are only thrown when | |
Error — Need To Clear Before Draw: If the buffer has not been cleared
since the last present() call.
| |
Error — If a valid Program3D object is not set.
| |
Error — No Valid Index Buffer Set: If an IndexBuffer3D object is not set.
| |
Error — Sanity Check On Parameters Failed: when the number of triangles to be drawn or the
firstIndex exceed allowed values.
| |
RangeError — Not Enough Indices In This Buffer: when there aren't enough indices in the buffer to
define the number of triangles to be drawn.
| |
Error — Sample Binds Texture Also Bound To Render: when the render target is a texture and that
texture assigned to a texture input of the current fragment program.
| |
Error — Sample Binds Invalid Texture: an invalid texture is specified as the input to the current
fragment program.
| |
Error — Sampler Format Does Not Match Texture Format: when the texture assigned as the input
to the current fragment program has a different format than that specified for the sampler
register. For example, a 2D texture is assigned to a cube texture sampler.
| |
Error — Sample Binds Undefined Texture: The current fragment program accesses a texture
register that has not been set (using setTextureAt() ).
| |
Error — Same Texture Needs Same Sampler Params: If a texture is used for more than one
sampler register, all of the samplers must have the same settings. For example, you cannot
set one sampler to clamp and another to wrap.
| |
Error — Texture Bound But Not Used: A texture is set as a shader input, but
it is not used.
| |
Error — Stream Is Not Used: A vertex buffer is assigned to a vertex attribute input,
but the vertex program does not reference the corresponding register.
| |
Error — Stream Is Invalid: a VertexBuffer3D object assigned to a vertex program input
is not a valid object.
| |
RangeError — Stream Does Not Have Enough Vertices: A vertex buffer supplying data for drawing
the specified triangles does not have enough data.
| |
RangeError — Stream Vertex Offset Out Of Bounds: The offset specified in a
setVertexBufferAt() call is negative or past the end of the buffer.
| |
Error — Stream Read But Not Set: A vertex attribute used by the current vertex program
is not set (using setVertexBufferAt() ).
|
See also
present | () | method |
public function present():void
Runtime Versions: | 3 |
Displays the back rendering buffer.
Calling the present()
method makes the results of all
rendering operations since the last present()
call visible and starts a new rendering cycle.
After calling present
, you must call clear()
before making another
drawTriangles()
call. Otherwise, this function will alternately clear the render buffer
to yellow and green or, if enableErrorChecking
has been set to true
,
an exception is thrown.
Calling present()
also resets the render target, just like calling
setRenderToBackBuffer()
.
Throws
Error — Need To Clear Before Draw: If the clear() has not been called since
the previous call to present() . (Two consecutive present() calls are not allowed
without calling clear() in between.)
|
setBlendFactors | () | method |
public function setBlendFactors(sourceFactor:String, destinationFactor:String):void
Runtime Versions: | 3 |
Specifies the factors used to blend the output color of a drawing operation with the existing color.
The output (source) color of the pixel shader program is combined with the existing (destination) color at that pixel according to the following formula:
result color = (source color * sourceFactor) + (destination color * destinationFactor)
The destination color is the current color in the render buffer for that pixel. Thus it is the result of the most recent
clear()
call and any intervening drawTriangles()
calls.
Use setBlendFactors()
to set the factors used to multiply the source and destination colors before they are added together.
The default blend factors are, sourceFactor = Context3DBlendFactor.ONE
, and destinationFactor = Context3DBlendFactor.ZERO
, which
results in the source color overwriting the destination color (in other words, no blending of the two colors occurs). For normal
alpha blending, use sourceFactor = Context3DBlendFactor.SOURCE_ALPHA
and destinationFactor = Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA
.
Use the constants defined in the Context3DBlendFactor class to set the parameters of this function.
Parameters
sourceFactor:String — The factor with which to multiply the source color. Defaults to Context3DBlendFactor.ONE .
| |
destinationFactor:String — The factor with which to multiply the destination color. Defaults to Context3DBlendFactor.ZERO .
|
Throws
Error — Invalid Enum: when sourceFactor or destinationFactor
is not one of the recognized values, which are defined in the Context3DBlendFactor class.
|
See also
setColorMask | () | method |
public function setColorMask(red:Boolean, green:Boolean, blue:Boolean, alpha:Boolean):void
Runtime Versions: | 3 |
Sets the mask used when writing colors to the render buffer.
Only color components for which the corresponding color mask parameter is true
are updated
when a color is written to the render buffer. For example, if you call:
setColorMask( true, false, false, false )
, only the red component of a color is written to the
buffer until you change the color mask again. The color mask does not affect the behavior of the
clear()
method.
Parameters
red:Boolean — set false to block changes to the red channel.
| |
green:Boolean — set false to block changes to the green channel.
| |
blue:Boolean — set false to block changes to the blue channel.
| |
alpha:Boolean — set false to block changes to the alpha channel.
|
setCulling | () | method |
public function setCulling(triangleFaceToCull:String):void
Runtime Versions: | 3 |
Sets triangle culling mode.
Triangles may be excluded from the scene early in the rendering pipeline based on their orientation relative to the view plane. Specify vertex order consistently (clockwise or counter-clockwise) as seen from the outside of the model to cull correctly.
Parameters
triangleFaceToCull:String — the culling mode. Use one of the constants defined in the Context3DTriangleFace class.
|
Throws
Error — Invalid Enum Error: when triangleFaceToCull is not one of the values
defined in the Context3DTriangleFace class.
|
See also
setDepthTest | () | method |
public function setDepthTest(depthMask:Boolean, passCompareMode:String):void
Runtime Versions: | 3 |
Sets type of comparison used for depth testing.
The depth of the source pixel output from the pixel shader program is compared to the current
value in the depth buffer. If the comparison evaluates as false
, then the source pixel
is discarded. If true
, then the source pixel is processed by the next step in
the rendering pipeline, the stencil test. In addition, the depth buffer is updated with the depth of the source
pixel, as long as the depthMask
parameter is set to true
.
Sets the test used to compare depth values for source and destination pixels. The source pixel is composited with the destination pixel when the comparison is true. The comparison operator is applied as an infix operator between the source and destination pixel values, in that order.
Parameters
depthMask:Boolean — the destination depth value will be updated from the source pixel when true.
| |
passCompareMode:String — the depth comparison test operation. One of the values of Context3DCompareMode.
|
See also
setProgram | () | method |
setProgramConstantsFromMatrix | () | method |
public function setProgramConstantsFromMatrix(programType:String, firstRegister:int, matrix:Matrix3D, transposedMatrix:Boolean = false):void
Runtime Versions: | 3 |
Sets constants for use by shader programs using values stored in a Matrix3D
.
Use this function to pass a matrix to a shader program. The function sets 4 constant registers used by the vertex or fragment program. The matrix is assigned to registers row by row. The first constant register is assigned the top row of the matrix. You can set 128 registers for a vertex program and 28 for a fragment program.
Parameters
programType:String — The type of shader program, either Context3DProgramType.VERTEX or
Context3DProgramType.FRAGMENT .
| |
firstRegister:int — the index of the first constant register to set. Since a Matrix3D has 16 values,
four registers are set.
| |
matrix:Matrix3D — the matrix containing the constant values.
| |
transposedMatrix:Boolean (default = false ) — if true the matrix entries are copied to registers in
transposed order. The default value is false .
|
Throws
TypeError — Null Pointer Error: when matrix is null.
| |
RangeError — Constant Register Out Of Bounds: when attempting to set more than
the maximum number of shader constant registers.
|
See also
setProgramConstantsFromVector | () | method |
public function setProgramConstantsFromVector(programType:String, firstRegister:int, data:Vector.<Number>, numRegisters:int = -1):void
Runtime Versions: | 3 |
Sets the constant inputs for the shader programs.
Sets an array of constants to be accessed by a vertex or fragment shader program. Constants set in Program3D are accessed within the shader programs as constant registers. Each constant register is comprised of 4 floating point values (x, y, z, w). Therefore every register requires 4 entries in the data Vector. You can set 128 registers for a vertex program and 28 for a fragment program.
Parameters
programType:String — The type of shader program, either Context3DProgramType.VERTEX or
Context3DProgramType.FRAGMENT .
| |
firstRegister:int — the index of the first constant register to set.
| |
data:Vector.<Number> — the floating point constant values. There must be at least numRegisters 4
elements in data .
| |
numRegisters:int (default = -1 ) — the number of constants to set. Specify -1, the default value, to set enough
registers to use all of the available data.
|
Throws
TypeError — Null Pointer Error: when data is null .
| |
RangeError — Constant Register Out Of Bounds: when attempting to set more
than the maximum number of shader constant registers (128 for vertex programs;
28 for fragment programs).
|
See also
setRenderToBackBuffer | () | method |
public function setRenderToBackBuffer():void
Runtime Versions: | 3 |
Sets the back rendering buffer as the render target. Subsequent calls to drawTriangles()
and clear()
methods result in updates to the back buffer. Use this method to resume normal rendering
after using the setRenderToTexture()
method.
setRenderToTexture | () | method |
public function setRenderToTexture(texture:flash.display3D.textures:TextureBase, enableDepthAndStencil:Boolean = false, antiAlias:int = 0, surfaceSelector:int = 0):void
Runtime Versions: | 3 |
Sets the specified texture as the rendering target.
Subsequent calls to drawTriangles()
and clear()
methods update the specified texture instead of the back buffer. Mip maps
are created automatically. Use the setRenderToBackBuffer()
to resume normal rendering
to the back buffer.
The texture must be cleared before drawing and the clear()
method is called
automatically if needed. Calling present()
resets the target to the back buffer.
Parameters
texture:flash.display3D.textures:TextureBase — the target texture to render into. Set to null to resume
rendering to the back buffer (setRenderToBackBuffer() and
present also reset the target to the back buffer).
| |
enableDepthAndStencil:Boolean (default = false ) — if true , depth and stencil testing are available.
If false , all depth and stencil state is ignored for subsequent drawing operations.
| |
antiAlias:int (default = 0 ) — the antialiasing quality. Use 0 to disable antialiasing; higher values improve
antialiasing quality, but require more calculations. The value is currently ignored
by the runtime.
| |
surfaceSelector:int (default = 0 ) — specifies which element of the texture to update. Texture objects have one surface, so you must
specify 0, the default value. CubeTexture objects have six surfaces, so you can specify an integer from 0 through 5.
|
Throws
ArgumentError — for a mismatched surfaceSelector parameter. The value must be 0 for 2D textures and 0..5 for cube maps.
| |
ArgumentError — texture is not derived from the TextureBase class (either Texture or CubeTexture classes).
|
See also
setScissorRectangle | () | method |
public function setScissorRectangle(rectangle:Rectangle):void
Runtime Versions: | 3 |
Sets a scissor rectangle, which is type of drawing mask. The renderer only draws to the area inside the scissor rectangle. Scissoring does not affect clear operations.
Pass null
to turn off scissoring.
Parameters
rectangle:Rectangle — The rectangle in which to draw. Specify the rectangle position and dimensions in pixels.
The coordinate system origin is the top left corner of the viewport, with positive values increasing down
and to the right (the same as the normal Flash display coordinate system).
|
setStencilActions | () | method |
public function setStencilActions(triangleFace:String = "frontAndBack", compareMode:String = "always", actionOnBothPass:String = "keep", actionOnDepthFail:String = "keep", actionOnDepthPassStencilFail:String = "keep"):void
Runtime Versions: | 3 |
Sets stencil mode and operation.
An 8-bit stencil reference value can be associated with each draw call. During rendering, the reference value can be tested against values stored previously in the frame buffer. The result of the test can control the draw action and whether or how the stored stencil value is updated. In addition, depth testing controls whether stencil testing is performed. A failed depth test can also be used to control the action taken on the stencil buffer.
In the pixel processing pipeline, depth testing is performed first. If the depth test fails, a stencil buffer update action can be taken, but no further evaluation of the stencil buffer value can be made. If the depth test passes, then the stencil test is performed. Alternate actions can be taken depending on the outcome of the stencil test.
The stencil reference value is set using setStencilReferenceValue()
.
Parameters
triangleFace:String (default = "frontAndBack ") — the triangle orientations allowed to contribute to the stencil operation. One of Context3DTriangleFace.
| |
compareMode:String (default = "always ") — the test operator used to compare the current stencil reference value and the destination pixel stencil value. Destination
pixel color and depth update is performed when the comparison is true. The stencil actions are performed as requested in the following action
parameters. The comparison operator is applied as an infix operator between the current and destination reference values, in
that order (in pseudocode: if stencilReference OPERATOR stencilBuffer then pass ). Use one of the constants defined in the
Context3DCompareMode class.
| |
actionOnBothPass:String (default = "keep ") — action to be taken when both depth and stencil comparisons pass. Use one of the constants defined in the
Context3DStencilAction class.
| |
actionOnDepthFail:String (default = "keep ") — action to be taken when depth comparison fails. Use one of the constants defined in the Context3DStencilAction class.
| |
actionOnDepthPassStencilFail:String (default = "keep ") — action to be taken when depth comparison passes and the stencil comparison fails. Use one of the constants
defined in the Context3DStencilAction class.
|
Throws
Error — Invalid Enum Error: when triangleFace is not one of the values defined in the Context3DTriangleFace class.
| |
Error — Invalid Enum Error: when compareMode is not one of the values defined in the Context3DCompareMode class.
| |
Error — Invalid Enum Error: when actionOnBothPass , actionOnDepthFail , or actionOnDepthPassStencilFail
is not one of the values defined in the Context3DStencilAction class.
|
See also
setStencilReferenceValue | () | method |
public function setStencilReferenceValue(referenceValue:uint, readMask:uint = 255, writeMask:uint = 255):void
Runtime Versions: | 3 |
Sets the stencil comparison value used for stencil tests.
Only the lower 8 bits of the reference value are used. The stencil buffer value is also 8 bits in length.
Use the readMask
and writeMask
to use the stencil buffer as a bit field.
Parameters
referenceValue:uint — an 8-bit reference value used in reference value comparison tests.
| |
readMask:uint (default = 255 ) — an 8-bit mask for applied to both the current stencil buffer value and the reference value before the comparison.
| |
writeMask:uint (default = 255 ) — an 8-bit mask applied to the reference value before updating the stencil buffer.
|
See also
setTextureAt | () | method |
public function setTextureAt(sampler:int, texture:flash.display3D.textures:TextureBase):void
Runtime Versions: | 3 |
Specifies the texture to use for a texture input register of a fragment program.
A fragment program can read information from up to eight texture objects. Use this function to assign a Texture or CubeTexture object to one of the sampler registers used by the fragment program.
Note: if you change the active fragment program (with setProgram
)
to a shader that uses fewer textures, set the unused registers to null
:
setTextureAt( 7, null );
Parameters
sampler:int — the sampler register index, a value from 0 through 7.
| |
texture:flash.display3D.textures:TextureBase — the texture object to make available, either a Texture or a CubeTexture instance.
|
setVertexBufferAt | () | method |
public function setVertexBufferAt(index:int, buffer:VertexBuffer3D, bufferOffset:int = 0, format:String = "float4"):void
Runtime Versions: | 3 |
Specifies which vertex data components correspond to a single vertex shader program input.
Use the setVertexBufferAt
method to identify which components of the data
defined for each vertex in a VertexBuffer3D buffer belong to which inputs to the vertex program. The
developer of the vertex program determines how much data is needed per vertex. That data is mapped from 1 or more
VertexBuffer3D
stream(s) to the attribute registers of the vertex shader program.
The smallest unit of data consumed by the vertex shader is a 32-bit data. Offsets into the vertex stream are specified in multiples of 32-bits.
As an example, a programmer might define each vertex with the following data:position: x float32 y float32 z float32 color: r unsigned byte g unsigned byte b unsigned byte a unsigned byteAssuming the vertex was defined in a VertexBuffer3D object named
buffer
, it would be assigned to a
vertex shader with the following code:
setVertexBufferAt( 0, buffer, 0, Context3DVertexBufferFormat.FLOAT_3 ); // attribute #0 will contain the position information setVertexBufferAt( 1, buffer, 3, Context3DVertexBufferFormat.BYTES_4 ); // attribute #1 will contain the color information
Parameters
index:int — the index of the attribute register in the vertex shader (0 through 7).
| |
buffer:VertexBuffer3D — the buffer that contains the source vertex data to be fed to the vertex shader.
| |
bufferOffset:int (default = 0 ) — an offset from the start of the data for a single vertex at which to start
reading this attribute. In the example above, the position data has an offset of 0 because it is the
first attribute; color has an offset of 3 because the color attribute follows the three 32-bit position values.
The offset is specified in units of 32 bits.
| |
format:String (default = "float4 ") — a value from the Context3DVertexBufferFormat class specifying the data type of this attribute.
|
Throws
Error — Invalid Enum: when format is not one of the values defined in the
Context3DVertexBufferFormat class.
| |
RangeError — Attribute Register Out Of Bounds: when the index parameter
is outside the range from 0 through 7. (A maximum of eight vertex attribute registers can be used by a shader.)
|
See also
The example uses an ActionScript library, 3Dutils.swf, that contains the AGALMiniAssembler class for compiling AGAL assembly to bytecode and the PerspectiveMatrix3D class for creating a prespective projection.
<html> <head> <title>Spinning cube</title> <script src="AIRAliases.js" type="text/javascript"></script> <script src="3Dutils.swf" type="application/x-shockwave-flash"></script> <script type="text/javascript"> const viewWidth = 320; const viewHeight = 200; const zNear = 1; const zFar = 500; const fov = 45; var stage3D; var renderContext; var indexList; var vertexes; var projection = new window.runtime.com.adobe.utils.PerspectiveMatrix3D(); var model = new air.Matrix3D(); var view = new air.Matrix3D(); var finalTransform = new air.Matrix3D(); //For rotating the cube var pivot = new air.Vector3D(); const VERTEX_SHADER = "m44 op, va0, vc0 \n" + // 4x4 matrix transform "mov v0, va1"; //copy color to varying variable v0 const FRAGMENT_SHADER = "mov oc, v0"; //Set the output color to the value interpolated from the three triangle vertices var vertexAssembly = new window.runtime.com.adobe.utils.AGALMiniAssembler(); var fragmentAssembly = new window.runtime.com.adobe.utils.AGALMiniAssembler(); var programPair; function Context3DExample() { window.htmlLoader.paintsDefaultBackground = false; window.nativeWindow.activate(); stage3D = window.nativeWindow.stage.stage3Ds[0]; stage3D.x = 10; stage3D.y = 10; //Add event listener before requesting the context stage3D.addEventListener( air.Event.CONTEXT3D_CREATE, contextCreated ); stage3D.addEventListener( air.ErrorEvent.ERROR, contextCreationError ); stage3D.requestContext3D( air.Context3DRenderMode.AUTO ); //Compile shaders vertexAssembly.assemble( air.Context3DProgramType.VERTEX, VERTEX_SHADER, false ); fragmentAssembly.assemble( air.Context3DProgramType.FRAGMENT, FRAGMENT_SHADER, false ); } //Note, context3DCreate event can happen at any time, such as when the hardware resources are taken by another process function contextCreated( event ) { renderContext = event.target.context3D; air.trace( "3D driver: " + renderContext.driverInfo ); setupScene(); } function setupScene() { renderContext.enableErrorChecking = true; //Can slow rendering - only turn on when developing/testing renderContext.configureBackBuffer( viewWidth, viewHeight, 2, false ); renderContext.setCulling( air.Context3DTriangleFace.BACK ); //Create vertex index list for the triangles forming a cube var triangles = air.Vector["<uint>"]([ 2,1,0, //front face 3,2,0, 4,7,5, //bottom face 7,6,5, 8,11,9, //back face 9,11,10, 12,15,13, //top face 13,15,14, 16,19,17, //left face 17,19,18, 20,23,21, //right face 21,23,22 ] ); indexList = renderContext.createIndexBuffer( triangles.length ); indexList.uploadFromVector( triangles, 0, triangles.length ); //Create vertexes - cube faces do not share vertexes const dataPerVertex = 6; var vertexData = air.Vector["<Number>"]([ // x,y,z r,g,b format 0,0,0, 1,0,0, //front face 0,1,0, 1,0,0, 1,1,0, 1,0,0, 1,0,0, 1,0,0, 0,0,0, 0,1,0, //bottom face 1,0,0, 0,1,0, 1,0,1, 0,1,0, 0,0,1, 0,1,0, 0,0,1, 1,0,0, //back face 1,0,1, 1,0,0, 1,1,1, 1,0,0, 0,1,1, 1,0,0, 0,1,1, 0,1,0, //top face 1,1,1, 0,1,0, 1,1,0, 0,1,0, 0,1,0, 0,1,0, 0,1,1, 0,0,1, //left face 0,1,0, 0,0,1, 0,0,0, 0,0,1, 0,0,1, 0,0,1, 1,1,0, 0,0,1, //right face 1,1,1, 0,0,1, 1,0,1, 0,0,1, 1,0,0, 0,0,1 ]); vertexes = renderContext.createVertexBuffer( vertexData.length/dataPerVertex, dataPerVertex ); vertexes.uploadFromVector( vertexData, 0, vertexData.length/dataPerVertex ); //Identify vertex data inputs for vertex program renderContext.setVertexBufferAt( 0, vertexes, 0, air.Context3DVertexBufferFormat.FLOAT_3 ); //va0 is position renderContext.setVertexBufferAt( 1, vertexes, 3, air.Context3DVertexBufferFormat.FLOAT_3 ); //va1 is color //Upload programs to render context programPair = renderContext.createProgram(); programPair.upload( vertexAssembly.agalcode, fragmentAssembly.agalcode ); renderContext.setProgram( programPair ); //Set up 3D transforms projection.perspectiveFieldOfViewRH( fov, viewWidth/viewHeight, zNear, zFar ); view.appendTranslation( 0, 0, -2 ); //Move view back model.appendTranslation( -.5, -.5, -.5 ); //center cube on origin window.nativeWindow.stage.addEventListener( air.Event.ENTER_FRAME, render ); } function render( event ) { //Rotate model on each frame model.appendRotation( .5, air.Vector3D.Z_AXIS, pivot ); model.appendRotation( .5, air.Vector3D.Y_AXIS, pivot ); model.appendRotation( .5, air.Vector3D.X_AXIS, pivot ); //Combine transforms finalTransform.identity(); finalTransform.append( model ); finalTransform.append( view ); finalTransform.append( projection ); //Pass the final transform to the vertex shader as program constant, vc0 renderContext.setProgramConstantsFromMatrix( air.Context3DProgramType.VERTEX, 0, finalTransform, true ); //Clear is required before drawTriangles on each frame renderContext.clear( .3,.3,.3 ); //Draw the 12 triangles that make up the cube renderContext.drawTriangles( indexList, 0, 12 ); //Show the frame renderContext.present(); } function contextCreationError( error ) { air.trace( error.errorID + ": " + error.text ); } </script> </head> <body onLoad="Context3DExample()"> <p style="background-color:#FFF; width:100px">3D Example<example</p> </body> </html>
Thu Sep 29 2011, 02:34 AM -07:00