Skip to content

Commit 63b1669

Browse files
committed
According to the Dart style guide it is not good to create classes just to wrap static methods like I did in ShaderLib.
And since I am adding shaders all the time it was not feasible anymore anyways. So I moved all createShader function to the top level. ( this is a breaking change, sorry! ) I also added an edge detecting sobel shader and matching example.
1 parent 88a5c93 commit 63b1669

16 files changed

Lines changed: 501 additions & 311 deletions

example/light.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void main() {
3737

3838
chronosGL.animatables['rotate_cube'] = new RotateMesh(m);
3939

40-
ShaderProgram prg = chronosGL.createProgram('Light', chronosGL.getShaderLib().createLightShader());
40+
ShaderProgram prg = chronosGL.createProgram('Light', createLightShader());
4141
prg.add(m);
4242

4343
chronosGL.getUtils().addParticles(2000, partiTex.texture);

example/test_sobel.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:chronosgl/chronosgl.dart';
2+
3+
void main() {
4+
5+
ChronosGL chronosGL = new ChronosGL('#webgl-canvas', useFramebuffer:true, fxShader: createSobelShader(), near: 0.1, far:2520.0);
6+
ShaderProgram prg = chronosGL.createProgram('FixedVertexColor', createPlane2GreyShader());
7+
Camera camera = chronosGL.getCamera();
8+
camera.setPos( 0.0, 0.0, 56.0 );
9+
FlyingCamera fc = new FlyingCamera(camera); // W,A,S,D keys fly
10+
chronosGL.addAnimatable('flyingCamera', fc);
11+
12+
loadObj( "ct_logo.obj").then((MeshData2 md) {
13+
14+
Mesh mesh = md.createMesh();
15+
mesh.rotX(3.14/2);
16+
mesh.rotZ(3.14);
17+
Node n = new Node(mesh);
18+
//n.invert = true;
19+
n.lookAt(new Vector(100.0,0.0,-100.0));
20+
//n.matrix.scale(0.02);
21+
22+
prg.add( mesh);
23+
chronosGL.run();
24+
25+
});
26+
27+
28+
29+
30+
}

example/test_sobel.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<html>
2+
<head>
3+
<title>TestObj</title>
4+
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
5+
</head>
6+
7+
<body marginheight="0" marginwidth="0" bgcolor="black" id="body" >
8+
<canvas id="webgl-canvas" style="border: none; cursor:crosshair; width:100%; height: 100%"></canvas>
9+
10+
<script type="application/dart" src="test_sobel.dart"></script>
11+
<script src="packages/browser/dart.js"></script>
12+
13+
</body>
14+
15+
</html>
16+
17+

example/test_ssao.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import 'package:chronosgl/chronosgl.dart';
22

33
void main() {
44

5-
ChronosGL chronosGL = new ChronosGL('#webgl-canvas', useFramebuffer:true, fxShader: getSSAOShader(), near: 0.1, far:2520.0);
6-
ShaderProgram prg = chronosGL.createProgram('FixedVertexColor', chronosGL.getShaderLib().createFixedVertexColorShader());
5+
ChronosGL chronosGL = new ChronosGL('#webgl-canvas', useFramebuffer:true, fxShader: createSSAOShader(), near: 0.1, far:2520.0);
6+
ShaderProgram prg = chronosGL.createProgram('FixedVertexColor', createFixedVertexColorShader());
77
Camera camera = chronosGL.getCamera();
88
camera.setPos( 0.0, 0.0, 56.0 );
99
FlyingCamera fc = new FlyingCamera(camera); // W,A,S,D keys fly

example/testobj.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'package:chronosgl/chronosgl.dart';
33
void main() {
44

55
ChronosGL chronosGL = new ChronosGL('#webgl-canvas');
6-
ShaderProgram prg = chronosGL.createProgram('FixedVertexColor', chronosGL.getShaderLib().createFixedVertexColorShader());
6+
ShaderProgram prg = chronosGL.createProgram('FixedVertexColor', createFixedVertexColorShader());
77
Camera camera = chronosGL.getCamera();
88
camera.setPos( 0.0, 0.0, 56.0 );
99
FlyingCamera fc = new FlyingCamera(camera); // W,A,S,D keys fly

lib/chronosgl.dart

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ part "src/shapes/torusknot.dart";
2424
part "src/shapes/icosahedron.dart";
2525
part "src/pickray.dart";
2626
part "src/input.dart";
27-
part "src/shader_lib.dart";
2827
part "src/load_obj.dart";
2928
part "src/framebuffer.dart";
29+
part "src/shader/basic_shader.dart";
30+
part "src/shader/plane_shader.dart";
3031
part "src/shader/ssao_shader.dart";
32+
part "src/shader/sobel_shader.dart";
33+
part "src/shader/debug_shader.dart";
3134

3235
abstract class Animatable {
3336
void animate( double elapsed);
@@ -48,7 +51,6 @@ class ChronosGL
4851
Map<String, Animatable> animatables = new Map<String, Animatable>();
4952

5053
ShaderProgram programBasic; // shortcut
51-
ShaderLib _shaderLib;
5254

5355
HTML.CanvasElement _canvas;
5456
double _aspect;
@@ -93,8 +95,7 @@ class ChronosGL
9395

9496
gl.lineWidth(5);
9597

96-
_shaderLib = new ShaderLib();
97-
programBasic = createProgram( 'basic', _shaderLib.createBasicShader());
98+
programBasic = createProgram( 'basic', createBasicShader());
9899

99100
_textureCache = new TextureCache(this);
100101
_camera = new Camera();
@@ -104,7 +105,7 @@ class ChronosGL
104105
fxFramebuffer = new ChronosFramebuffer(gl, _canvas.width, _canvas.height);
105106
fxWall = _utils.getWall( fxFramebuffer.colorTexture, 1);
106107
fxWall.texture2 = fxFramebuffer.depthTexture;
107-
fxProgram = new ShaderProgram(this, fxShader == null ? _shaderLib.createBasicShader() : fxShader, 'fx');
108+
fxProgram = new ShaderProgram(this, fxShader == null ? createBasicShader() : fxShader, 'fx');
108109
fxProgram.add(fxWall);
109110
}
110111

@@ -127,10 +128,6 @@ class ChronosGL
127128
return _camera;
128129
}
129130

130-
ShaderLib getShaderLib() {
131-
return _shaderLib;
132-
}
133-
134131
Utils getUtils() {
135132
return _utils;
136133
}

lib/src/shader/basic_shader.dart

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
part of chronosgl;
2+
3+
ShaderObject createBasicShader() {
4+
ShaderObject shaderObject = new ShaderObject();
5+
6+
// TODO: think about multipying uPMatrix and uMVMatrix in Dart code...
7+
// or maybe cache the result in a static ?
8+
9+
shaderObject.vertexShader = """
10+
precision mediump float;
11+
attribute vec3 aVertexPosition;
12+
attribute vec2 aTextureCoord;
13+
14+
uniform mat4 uMVMatrix;
15+
uniform mat4 uPMatrix;
16+
17+
varying vec2 vTextureCoord;
18+
19+
void main(void) {
20+
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
21+
vTextureCoord = aTextureCoord;
22+
}
23+
""";
24+
25+
shaderObject.fragmentShader = """
26+
precision mediump float;
27+
28+
varying vec2 vTextureCoord;
29+
uniform sampler2D uSampler;
30+
31+
void main(void) {
32+
gl_FragColor = texture2D(uSampler, vTextureCoord);
33+
}
34+
""";
35+
36+
shaderObject.vertexPositionAttribute = "aVertexPosition";
37+
shaderObject.textureCoordinatesAttribute = "aTextureCoord";
38+
shaderObject.modelViewMatrixUniform = "uMVMatrix";
39+
shaderObject.perpectiveMatrixUniform = "uPMatrix";
40+
shaderObject.textureSamplerUniform = "uSampler";
41+
42+
return shaderObject;
43+
}
44+
45+
ShaderObject createLightShader() {
46+
ShaderObject shaderObject = new ShaderObject();
47+
48+
shaderObject.vertexShader = """
49+
precision mediump float;
50+
51+
attribute vec3 aVertexPosition;
52+
attribute vec3 aNormal;
53+
54+
uniform mat4 uMVMatrix;
55+
uniform mat4 uPMatrix;
56+
57+
vec3 lightDir = vec3(1.0,0.0,1.0);
58+
vec3 ambientColor = vec3(0.0,0.0,0.0);
59+
vec3 directionalColor = vec3(1.0,1.0,1.0);
60+
61+
vec3 pointLightLocation = vec3( 40, 0, 100);
62+
63+
varying vec3 vLightWeighting;
64+
varying vec3 vNormal;
65+
66+
void main(void) {
67+
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
68+
vNormal = (uMVMatrix * vec4(aNormal, 0.0)).xyz;
69+
70+
vec3 lightDir = normalize(pointLightLocation - aVertexPosition.xyz);
71+
72+
float directionalLightWeighting = max(dot(vNormal, normalize(lightDir)), 0.0);
73+
vLightWeighting = ambientColor + directionalColor * directionalLightWeighting;
74+
}
75+
""";
76+
77+
shaderObject.fragmentShader = """
78+
precision mediump float;
79+
80+
varying vec3 vLightWeighting;
81+
varying vec3 vNormal;
82+
83+
void main(void) {
84+
//gl_FragColor = vec4( vNormal * vLightWeighting, 1.0 );
85+
gl_FragColor = vec4( vLightWeighting, 1.0 );
86+
}
87+
""";
88+
89+
shaderObject.vertexPositionAttribute = "aVertexPosition";
90+
shaderObject.normalAttribute = "aNormal";
91+
shaderObject.modelViewMatrixUniform = "uMVMatrix";
92+
shaderObject.perpectiveMatrixUniform = "uPMatrix";
93+
94+
return shaderObject;
95+
}
96+
97+
ShaderObject createNormal2ColorShader() {
98+
ShaderObject shaderObject = new ShaderObject();
99+
100+
shaderObject.vertexShader = """
101+
precision mediump float;
102+
103+
attribute vec3 aVertexPosition;
104+
attribute vec3 aNormal;
105+
106+
uniform mat4 uMVMatrix;
107+
uniform mat4 uPMatrix;
108+
109+
varying vec3 vColor;
110+
111+
void main(void) {
112+
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
113+
vColor=normalize( aNormal / 2.0 + vec3(0.5) );
114+
}
115+
""";
116+
117+
shaderObject.fragmentShader = """
118+
precision mediump float;
119+
120+
varying vec3 vColor;
121+
122+
void main(void) {
123+
gl_FragColor = vec4( vColor, 1.0 );
124+
}
125+
""";
126+
127+
shaderObject.vertexPositionAttribute = "aVertexPosition";
128+
shaderObject.normalAttribute = "aNormal";
129+
shaderObject.modelViewMatrixUniform = "uMVMatrix";
130+
shaderObject.perpectiveMatrixUniform = "uPMatrix";
131+
132+
return shaderObject;
133+
}
134+
135+
ShaderObject createPointSpritesShader() {
136+
ShaderObject shaderObject = new ShaderObject();
137+
138+
shaderObject.vertexShader = """
139+
precision mediump float;
140+
attribute vec3 aVertexPosition;
141+
142+
uniform mat4 uMVMatrix;
143+
uniform mat4 uPMatrix;
144+
145+
void main(void) {
146+
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
147+
gl_PointSize = 1000.0/gl_Position.z;
148+
}
149+
""";
150+
151+
shaderObject.fragmentShader = """
152+
precision mediump float;
153+
154+
uniform sampler2D uSampler;
155+
156+
void main(void) {
157+
gl_FragColor = texture2D(uSampler, gl_PointCoord);
158+
gl_FragColor.a = 0.4;
159+
}
160+
""";
161+
162+
shaderObject.vertexPositionAttribute = "aVertexPosition";
163+
shaderObject.modelViewMatrixUniform = "uMVMatrix";
164+
shaderObject.perpectiveMatrixUniform = "uPMatrix";
165+
shaderObject.textureSamplerUniform = "uSampler";
166+
return shaderObject;
167+
}
168+

lib/src/shader/debug_shader.dart

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
part of chronosgl;
2+
3+
ShaderObject createFixedVertexColorShader() {
4+
ShaderObject shaderObject = new ShaderObject();
5+
6+
shaderObject.vertexShader = """
7+
precision mediump float;
8+
attribute vec3 aVertexPosition;
9+
10+
uniform mat4 uMVMatrix;
11+
uniform mat4 uPMatrix;
12+
13+
varying vec3 vColor;
14+
15+
void main(void) {
16+
vColor = vec3( sin(aVertexPosition.x)/2.0+0.5, cos(aVertexPosition.y)/2.0+0.5, sin(aVertexPosition.z)/2.0+0.5);
17+
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
18+
}
19+
""";
20+
21+
shaderObject.fragmentShader = """
22+
precision mediump float;
23+
24+
varying vec3 vColor;
25+
26+
void main(void) {
27+
gl_FragColor = vec4( vColor, 1. );
28+
}
29+
""";
30+
31+
shaderObject.vertexPositionAttribute = "aVertexPosition";
32+
shaderObject.modelViewMatrixUniform = "uMVMatrix";
33+
shaderObject.perpectiveMatrixUniform = "uPMatrix";
34+
35+
return shaderObject;
36+
}
37+
38+
ShaderObject createDepthShader() {
39+
ShaderObject shaderObject = new ShaderObject();
40+
41+
shaderObject.vertexShader = """
42+
precision mediump float;
43+
attribute vec3 aVertexPosition;
44+
attribute vec2 aTextureCoord;
45+
46+
uniform mat4 uMVMatrix;
47+
uniform mat4 uPMatrix;
48+
49+
varying vec2 vTextureCoord;
50+
51+
void main(void) {
52+
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
53+
vTextureCoord = aTextureCoord;
54+
}
55+
""";
56+
57+
shaderObject.fragmentShader = """
58+
precision mediump float;
59+
60+
varying vec2 vTextureCoord;
61+
uniform sampler2D uSampler;
62+
uniform sampler2D depthSampler;
63+
64+
uniform float cameraNear;
65+
uniform float cameraFar;
66+
67+
float linearizeDepth(float z)
68+
{
69+
float n = cameraNear;
70+
float f = cameraFar;
71+
return (2.0 * n) / (f + n - z * (f - n));
72+
}
73+
74+
void main(void) {
75+
vec4 texel = texture2D(depthSampler, vTextureCoord);
76+
gl_FragColor = vec4(vec3(linearizeDepth(texel.x)), 1.0);
77+
}
78+
""";
79+
80+
shaderObject.vertexPositionAttribute = "aVertexPosition";
81+
shaderObject.textureCoordinatesAttribute = "aTextureCoord";
82+
shaderObject.modelViewMatrixUniform = "uMVMatrix";
83+
shaderObject.perpectiveMatrixUniform = "uPMatrix";
84+
shaderObject.textureSamplerUniform = "uSampler";
85+
shaderObject.texture2SamplerUniform = "depthSampler";
86+
shaderObject.cameraNear = "cameraNear";
87+
shaderObject.cameraFar = "cameraFar";
88+
89+
return shaderObject;
90+
}

0 commit comments

Comments
 (0)