Skip to content

Commit d0b8243

Browse files
committed
added support for color attribute and a color shader
1 parent 7246b2f commit d0b8243

5 files changed

Lines changed: 63 additions & 9 deletions

File tree

lib/src/mesh.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Mesh extends Node {
1313
Texture texture;
1414
Texture texture2;
1515

16-
Buffer verticesBuffer, textureCoordBuffer, normalsBuffer, vertexIndexBuffer;
16+
Buffer verticesBuffer, colorsBuffer, textureCoordBuffer, normalsBuffer, vertexIndexBuffer;
1717

1818
int numItems;
1919

@@ -27,6 +27,12 @@ class Mesh extends Node {
2727
gl.bindBuffer(ARRAY_BUFFER, verticesBuffer);
2828
gl.bufferDataTyped(ARRAY_BUFFER, meshData.vertices as Float32List, STATIC_DRAW);
2929

30+
if( meshData.colors != null ) {
31+
colorsBuffer = gl.createBuffer();
32+
gl.bindBuffer(ARRAY_BUFFER, colorsBuffer);
33+
gl.bufferDataTyped(ARRAY_BUFFER, meshData.colors as Float32List, STATIC_DRAW);
34+
}
35+
3036
if( meshData.textureCoords != null ) {
3137
textureCoordBuffer = gl.createBuffer();
3238
gl.bindBuffer(ARRAY_BUFFER, textureCoordBuffer);
@@ -52,6 +58,9 @@ class Mesh extends Node {
5258

5359
void clearData() {
5460
gl.deleteBuffer( verticesBuffer);
61+
if( colorsBuffer != null ) {
62+
gl.deleteBuffer( colorsBuffer);
63+
}
5564
if( textureCoordBuffer != null ) {
5665
gl.deleteBuffer( textureCoordBuffer);
5766
}
@@ -85,6 +94,11 @@ class Mesh extends Node {
8594
gl.bindBuffer(ARRAY_BUFFER, verticesBuffer);
8695
gl.vertexAttribPointer(program.vertexPositionAttribute, 3, FLOAT, false, 0, 0);
8796

97+
if( program.shaderObject.colorsAttribute != null) {
98+
gl.bindBuffer(ARRAY_BUFFER, colorsBuffer);
99+
gl.vertexAttribPointer(program.colorsAttribute, 3, FLOAT, false, 0, 0);
100+
}
101+
88102
if( program.shaderObject.textureCoordinatesAttribute != null) {
89103
gl.bindBuffer(ARRAY_BUFFER, textureCoordBuffer);
90104
gl.vertexAttribPointer(program.textureCoordAttribute, 2, FLOAT, false, 0, 0);

lib/src/mesh_data.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,32 @@ part of chronosgl;
33
class MeshData {
44

55
List<double> vertices;
6+
List<double> colors;
67
List<double> normals;
78
List<double> textureCoords;
89
List<int> vertexIndices;
910
Texture texture;
1011
Texture texture2;
1112

12-
MeshData({this.vertices, this.textureCoords, this.normals, this.vertexIndices, this.texture, this.texture2});
13+
MeshData({this.vertices, this.colors, this.textureCoords, this.normals, this.vertexIndices, this.texture, this.texture2});
1314

1415
MeshData.empty() {
1516
vertices = new List<double>();
17+
colors = new List<double>();
1618
textureCoords = new List<double>();
1719
normals = new List<double>();
1820
vertexIndices = new List<int>();
1921
}
2022

2123
void optimize() {
2224
if (!(vertices is Float32List)) vertices = new Float32List.fromList(vertices);
23-
if ( textureCoords == null) textureCoords = new List<double>();
24-
if (!(textureCoords is Float32List)) textureCoords = new Float32List.fromList(textureCoords);
25-
if ( normals == null) normals = new List<double>();
26-
if (!(normals is Float32List)) normals = new Float32List.fromList(normals);
25+
26+
if ( colors != null && !(colors is Float32List)) colors = new Float32List.fromList(colors);
27+
28+
if ( textureCoords != null && !(textureCoords is Float32List)) textureCoords = new Float32List.fromList(textureCoords);
29+
30+
if ( normals == null && !(normals is Float32List)) normals = new Float32List.fromList(normals);
31+
2732
if (!(vertexIndices is Uint16List)) vertexIndices = new Uint16List.fromList(vertexIndices);
2833
}
2934

lib/src/shader/basic_shader.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ ShaderObject createTexturedShader() {
1717
return generateShader(shaderObject);
1818
}
1919

20+
ShaderObject createColorShader() {
21+
ShaderObject shaderObject = new ShaderObject("Color");
22+
shaderObject.vertexPositionAttribute = "aVertexPosition";
23+
shaderObject.colorsAttribute = "aColor";
24+
shaderObject.modelViewMatrixUniform = "uMVMatrix";
25+
shaderObject.perpectiveMatrixUniform = "uPMatrix";
26+
shaderObject.fragmentShaderBody = "gl_FragColor = vec4( vaColor, 1.0 );";
27+
return generateShader(shaderObject);
28+
}
29+
2030
ShaderObject createLightShader() {
2131
ShaderObject shaderObject = new ShaderObject("Light");
2232

lib/src/shader/generate_shader.dart

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,30 @@ ShaderObject generateShader( ShaderObject shaderObject) {
1111
uniform mat4 ${shaderObject.perpectiveMatrixUniform};
1212
""";
1313

14-
if( shaderObject.vertexPositionAttribute != null) {
14+
if( shaderObject.textureCoordinatesAttribute != null) {
1515
vs += """
1616
attribute vec2 ${shaderObject.textureCoordinatesAttribute};
1717
varying vec2 v${shaderObject.textureCoordinatesAttribute};
1818
""";
1919
}
20+
21+
if( shaderObject.colorsAttribute != null) {
22+
vs += """
23+
attribute vec3 ${shaderObject.colorsAttribute};
24+
varying vec3 v${shaderObject.colorsAttribute};
25+
""";
26+
}
2027

2128
vs += shaderObject.vertexShaderHeader+"\n";
2229
vs += "void main(void) {\n";
2330
vs += "gl_Position = ${shaderObject.perpectiveMatrixUniform} * ${shaderObject.modelViewMatrixUniform} * vec4(${shaderObject.vertexPositionAttribute}, 1.0);\n";
2431

25-
if( shaderObject.vertexPositionAttribute != null) {
32+
if( shaderObject.textureCoordinatesAttribute != null) {
2633
vs += "v${shaderObject.textureCoordinatesAttribute} = ${shaderObject.textureCoordinatesAttribute};\n";
2734
}
35+
if( shaderObject.colorsAttribute != null) {
36+
vs += "v${shaderObject.colorsAttribute} = ${shaderObject.colorsAttribute};\n";
37+
}
2838
vs += shaderObject.vertexShaderBody;
2939

3040
vs += "}\n";
@@ -35,10 +45,14 @@ ShaderObject generateShader( ShaderObject shaderObject) {
3545

3646
String fs = "precision mediump float;\n";
3747

38-
if( shaderObject.vertexPositionAttribute != null) {
48+
if( shaderObject.textureCoordinatesAttribute != null) {
3949
fs += "varying vec2 v${shaderObject.textureCoordinatesAttribute};\n";
4050
}
4151

52+
if( shaderObject.colorsAttribute != null) {
53+
fs += "varying vec3 v${shaderObject.colorsAttribute};\n";
54+
}
55+
4256
if( shaderObject.textureSamplerUniform != null) {
4357
fs += "uniform sampler2D ${shaderObject.textureSamplerUniform};\n";
4458
}

lib/src/shader_program.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class ShaderObject {
66
String vertexShader;
77
String fragmentShader;
88
String vertexPositionAttribute;
9+
String colorsAttribute;
910
String textureCoordinatesAttribute;
1011
String normalAttribute;
1112
String transformationMatrixUniform;
@@ -30,6 +31,7 @@ class ShaderProgram implements Drawable {
3031
RenderingContext gl;
3132
Program program;
3233
int vertexPositionAttribute;
34+
int colorsAttribute;
3335
int textureCoordAttribute;
3436
int normalAttribute;
3537
UniformLocation pMatrixUniform;
@@ -58,6 +60,9 @@ class ShaderProgram implements Drawable {
5860

5961
vertexPositionAttribute = gl.getAttribLocation(program, shaderObject.vertexPositionAttribute);
6062

63+
if( shaderObject.colorsAttribute != null)
64+
colorsAttribute = gl.getAttribLocation(program, shaderObject.colorsAttribute);
65+
6166
if( shaderObject.textureCoordinatesAttribute != null)
6267
textureCoordAttribute = gl.getAttribLocation(program, shaderObject.textureCoordinatesAttribute);
6368

@@ -148,6 +153,9 @@ class ShaderProgram implements Drawable {
148153

149154
gl.useProgram(program);
150155
gl.enableVertexAttribArray(vertexPositionAttribute);
156+
157+
if( shaderObject.colorsAttribute != null)
158+
gl.enableVertexAttribArray(colorsAttribute);
151159
if( shaderObject.textureCoordinatesAttribute != null)
152160
gl.enableVertexAttribArray(textureCoordAttribute);
153161
if( shaderObject.normalAttribute != null)
@@ -186,6 +194,9 @@ class ShaderProgram implements Drawable {
186194

187195
drawObjects(objects);
188196
gl.disableVertexAttribArray(vertexPositionAttribute);
197+
198+
if( shaderObject.colorsAttribute != null)
199+
gl.disableVertexAttribArray(colorsAttribute);
189200
if( shaderObject.textureCoordinatesAttribute != null)
190201
gl.disableVertexAttribArray(textureCoordAttribute);
191202
if( shaderObject.normalAttribute != null)

0 commit comments

Comments
 (0)