-
Notifications
You must be signed in to change notification settings - Fork 24
/
BasicRender.as
89 lines (64 loc) · 2.63 KB
/
BasicRender.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package {
import com.barliesque.agal.EasierAGAL;
import flash.display3D.Context3DProgramType;
import flash.display3D.Context3DVertexBufferFormat;
import flash.display3D.IndexBuffer3D;
import flash.display3D.VertexBuffer3D;
import flash.geom.Matrix3D;
/**
* A simple test shader that accepts xyz vertex data with rgb vertex colors.
* @author David Barlia, david@barliesque.com
*/
public class BasicRender extends EasierAGAL {
private var vertexBuffer:VertexBuffer3D;
private var indexBuffer:IndexBuffer3D;
/// x, y, z, r, g, b
public const DATA32_PER_VERTEX:uint = 6;
public function BasicRender() {
// Passing true here means we'll assemble the shader in debug mode
super(true);
}
override protected function _vertexShader():void {
comment("Apply a 4x4 matrix to transform vertices to clip-space");
multiply4x4(OUTPUT, ATTRIBUTE[0], CONST[0]);
comment("Pass vertex color to fragment shader");
move(VARYING[0], ATTRIBUTE[1]);
}
override protected function _fragmentShader():void {
// Output the interpolated vertex color for this pixel
move(OUTPUT, VARYING[0]);
}
public function setGeometry(vertices:Vector.<Number>, indices:Vector.<uint>):void {
// Upload vertex data
if (vertexBuffer != null) vertexBuffer.dispose();
vertexBuffer = context.createVertexBuffer(vertices.length / DATA32_PER_VERTEX, DATA32_PER_VERTEX);
vertexBuffer.uploadFromVector(vertices, 0, vertices.length / DATA32_PER_VERTEX);
// Upload polygon data (vertex indices)
if (indexBuffer != null) indexBuffer.dispose();
indexBuffer = context.createIndexBuffer(indices.length);
indexBuffer.uploadFromVector(indices, 0, indices.length);
}
public function render(viewMatrix:Matrix3D):void {
// Tell the 3D context that this is the current shader program to be rendered
context.setProgram(program);
// Set ATTRIBUTE Registers to point at vertex data
context.setVertexBufferAt(0, vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3); // xyz
context.setVertexBufferAt(1, vertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_3); // rgb
// Pass viewMatrix into constant registers
context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, viewMatrix);
// Render the shader!
context.drawTriangles(indexBuffer);
}
override public function dispose():void {
if (vertexBuffer) {
vertexBuffer.dispose();
vertexBuffer = null;
}
if (indexBuffer) {
indexBuffer.dispose();
indexBuffer = null;
}
super.dispose();
}
}
}