Skip to content

Commit 77f5ac1

Browse files
committed
Added a light shader example
1 parent 581469b commit 77f5ac1

6 files changed

Lines changed: 104 additions & 6 deletions

File tree

example/icosahedron.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void main() {
1515
FlyingCamera fc = new FlyingCamera(camera); // W,A,S,D keys fly
1616
chronosGL.addAnimatable('flyingCamera', fc);
1717

18-
MeshData md = chronosGL.getUtils().createIcosahedron(3);;
18+
MeshData md = chronosGL.getUtils().createIcosahedron(3);
1919
md.texture = blockTex.texture;
2020
Mesh m = md.createMesh();
2121
chronosGL.programBasic.add(m);

example/light.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import 'package:ChronosGL/chronos_gl.dart';
2+
3+
class RotateMesh extends Animatable {
4+
Mesh m;
5+
RotateMesh(this.m);
6+
7+
void animate(double time) {
8+
m.rollLeft(time*0.001);
9+
m.lookLeft(time*0.001);
10+
}
11+
}
12+
13+
14+
void main() {
15+
16+
ChronosGL chronosGL = new ChronosGL('#webgl-canvas');
17+
18+
Camera camera = chronosGL.getCamera();
19+
TextureCache textureCache = chronosGL.getTextureCache();
20+
TextureWrapper blockTex = textureCache.add("gradient.jpg");
21+
TextureWrapper partiTex = textureCache.add("particle.bmp");
22+
23+
textureCache.loadAllThenExecute(() {
24+
camera.setPos( 0.0, 0.0, 56.0 );
25+
26+
FlyingCamera fc = new FlyingCamera(camera); // W,A,S,D keys fly
27+
chronosGL.addAnimatable('flyingCamera', fc);
28+
29+
MeshData md = chronosGL.getUtils().createCube();
30+
for( int i=0; i<md.vertices.length;i++) {
31+
md.vertices[i] = md.vertices[i]*10;
32+
}
33+
Mesh m = md.createMesh();
34+
m.setPos(0.0, 0.0, -150.0);
35+
m.lookUp(1.0);
36+
m.lookLeft(0.7);
37+
38+
chronosGL.animatables['rotate_cube'] = new RotateMesh(m);
39+
40+
ShaderProgram prg = chronosGL.createProgram('Light', chronosGL.getShaderLib().createLightShader());
41+
prg.add(m);
42+
43+
chronosGL.getUtils().addParticles(2000, partiTex.texture);
44+
45+
chronosGL.run();
46+
});
47+
48+
}

example/light.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>light</title>
6+
</head>
7+
<body style="height:100%; margin: 0; padding: 0">
8+
9+
10+
<canvas id="webgl-canvas" style="border: none; cursor:crosshair; width:100%; height: 100%; display:block;"></canvas>
11+
12+
<script type="application/dart" src="light.dart"></script>
13+
<script src="packages/browser/dart.js"></script>
14+
</body>
15+
</html>

lib/src/shader_lib.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ class ShaderLib {
7878
vec3 ambientColor = vec3(0.0,0.0,0.0);
7979
vec3 directionalColor = vec3(1.0,1.0,1.0);
8080
81-
vec3 pointLightLocation = vec3( 4, 0, 10);
81+
vec3 pointLightLocation = vec3( 40, 0, 100);
8282
8383
varying vec3 vLightWeighting;
8484
varying vec3 vNormal;
8585
8686
void main(void) {
8787
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
88+
vNormal = (uMVMatrix * vec4(aNormal, 0.0)).xyz;
8889
8990
vec3 lightDir = normalize(pointLightLocation - aVertexPosition.xyz);
9091
91-
float directionalLightWeighting = max(dot(aNormal, normalize(lightDir)), 0.0);
92+
float directionalLightWeighting = max(dot(vNormal, normalize(lightDir)), 0.0);
9293
vLightWeighting = ambientColor + directionalColor * directionalLightWeighting;
93-
vNormal = aNormal;
9494
}
9595
""";
9696

lib/src/shapes/cube.dart

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,41 @@ MeshData createCubeInternal( [Texture texture]) {
3737
-1.0, -1.0, 1.0,
3838
-1.0, 1.0, 1.0,
3939
-1.0, 1.0, -1.0];
40+
List<double> normals = [// Front face
41+
0.0, 0.0, 1.0,
42+
0.0, 0.0, 1.0,
43+
0.0, 0.0, 1.0,
44+
0.0, 0.0, 1.0,
45+
46+
// Back face
47+
0.0, 0.0, -1.0,
48+
0.0, 0.0, -1.0,
49+
0.0, 0.0, -1.0,
50+
0.0, 0.0, -1.0,
51+
52+
// Top face
53+
0.0, 1.0, 0.0,
54+
0.0, 1.0, 0.0,
55+
0.0, 1.0, 0.0,
56+
0.0, 1.0, 0.0,
57+
58+
// Bottom face
59+
0.0, -1.0, 0.0,
60+
0.0, -1.0, 0.0,
61+
0.0, -1.0, 0.0,
62+
0.0, -1.0, 0.0,
63+
64+
// Right face
65+
1.0, 0.0, 0.0,
66+
1.0, 0.0, 0.0,
67+
1.0, 0.0, 0.0,
68+
1.0, 0.0, 0.0,
69+
70+
// Left face
71+
-1.0, 0.0, 0.0,
72+
-1.0, 0.0, 0.0,
73+
-1.0, 0.0, 0.0,
74+
-1.0, 0.0, 0.0,];
4075
List<double> uvs = [// Front face
4176
0.0, 0.0,
4277
1.0, 0.0,
@@ -81,6 +116,6 @@ MeshData createCubeInternal( [Texture texture]) {
81116
20, 21, 22, 20, 22, 23 // Left face
82117
];
83118

84-
return new MeshData(vertices : new Float32List.fromList(vertices), textureCoords : new Float32List.fromList(uvs), vertexIndices : new Uint16List.fromList(vertIndices), texture : texture);
119+
return new MeshData(vertices : new Float32List.fromList(vertices), normals : new Float32List.fromList(normals), textureCoords : new Float32List.fromList(uvs), vertexIndices : new Uint16List.fromList(vertIndices), texture : texture);
85120

86121
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: ChronosGL
2-
version: 1.0.2
2+
version: 1.0.3
33
author: rhulha <java4life@gmail.com>
44
description: A simple, minimal and elegant scene graph for WebGL written in Dart
55
homepage: https://github.com/rhulha/ChronosGL

0 commit comments

Comments
 (0)