Skip to content

Commit 88a5c93

Browse files
committed
added helpful docs to the vector cross and dot functions.
added createPlane2ColorShader to improve edge detection.
1 parent 1ce5f6c commit 88a5c93

3 files changed

Lines changed: 73 additions & 5 deletions

File tree

lib/chronosgl.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ class ChronosGL
7272
// fix a bug in current chrome v.27
7373
_canvas.onDragStart.listen((HTML.MouseEvent event){ event.preventDefault(); });
7474

75-
// TODO: add resize handler
7675
_canvas.width = _canvas.clientWidth;
7776
_canvas.height = _canvas.clientHeight;
7877
_aspect = _canvas.clientWidth / _canvas.clientHeight;

lib/src/shader_lib.dart

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,85 @@ class ShaderLib {
128128
uniform mat4 uMVMatrix;
129129
uniform mat4 uPMatrix;
130130
131-
varying vec3 vNormal;
131+
varying vec3 vColor;
132132
133133
void main(void) {
134134
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
135-
vNormal = aNormal;
135+
vColor=normalize( aNormal / 2.0 + vec3(0.5) );
136136
}
137137
""";
138138

139139
shaderObject.fragmentShader = """
140140
precision mediump float;
141141
142-
varying vec3 vNormal;
142+
varying vec3 vColor;
143+
144+
void main(void) {
145+
gl_FragColor = vec4( vColor, 1.0 );
146+
}
147+
""";
148+
149+
shaderObject.vertexPositionAttribute = "aVertexPosition";
150+
shaderObject.normalAttribute = "aNormal";
151+
shaderObject.modelViewMatrixUniform = "uMVMatrix";
152+
shaderObject.perpectiveMatrixUniform = "uPMatrix";
153+
154+
return shaderObject;
155+
}
156+
157+
ShaderObject createPlane2ColorShader() {
158+
ShaderObject shaderObject = new ShaderObject();
159+
160+
shaderObject.vertexShader = """
161+
precision mediump float;
162+
163+
attribute vec3 aVertexPosition;
164+
attribute vec3 aNormal;
165+
166+
uniform mat4 uMVMatrix;
167+
uniform mat4 uPMatrix;
168+
169+
varying vec3 vColor;
143170
171+
vec3 HSV2RGB( vec3 hsv )
172+
{
173+
hsv.x = mod( 100.0 + hsv.x, 1.0 ); // Ensure [0,1[
174+
175+
float HueSlice = 6.0 * hsv.x; // In [0,6[
176+
float HueSliceInteger = floor( HueSlice );
177+
float HueSliceInterpolant = HueSlice - HueSliceInteger; // In [0,1[ for each hue slice
178+
179+
vec3 TempRGB = vec3( hsv.z * (1.0 - hsv.y),
180+
hsv.z * (1.0 - hsv.y * HueSliceInterpolant),
181+
hsv.z * (1.0 - hsv.y * (1.0 - HueSliceInterpolant)) );
182+
183+
float IsOddSlice = mod( HueSliceInteger, 2.0 ); // 0 if even (slices 0, 2, 4), 1 if odd (slices 1, 3, 5)
184+
float ThreeSliceSelector = 0.5 * (HueSliceInteger - IsOddSlice); // (0, 1, 2) corresponding to slices (0, 2, 4) and (1, 3, 5)
185+
186+
vec3 ScrollingRGBForEvenSlices = vec3( hsv.z, TempRGB.zx ); // (V, Temp Blue, Temp Red) for even slices (0, 2, 4)
187+
vec3 ScrollingRGBForOddSlices = vec3( TempRGB.y, hsv.z, TempRGB.x ); // (Temp Green, V, Temp Red) for odd slices (1, 3, 5)
188+
vec3 ScrollingRGB = mix( ScrollingRGBForEvenSlices, ScrollingRGBForOddSlices, IsOddSlice );
189+
190+
float IsNotFirstSlice = clamp( ThreeSliceSelector, 0.0,1.0 ); // 1 if NOT the first slice (true for slices 1 and 2)
191+
float IsNotSecondSlice = clamp( ThreeSliceSelector-1.0, 0.0,1. ); // 1 if NOT the first or second slice (true only for slice 2)
192+
193+
return mix( ScrollingRGB.xyz, mix( ScrollingRGB.zxy, ScrollingRGB.yzx, IsNotSecondSlice ), IsNotFirstSlice ); // Make the RGB rotate right depending on final slice index
194+
}
195+
196+
void main(void) {
197+
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
198+
float d=dot( aVertexPosition, aNormal);
199+
vec3 hsv = vec3(d,1,1);
200+
vColor=HSV2RGB(hsv);
201+
}
202+
""";
203+
204+
shaderObject.fragmentShader = """
205+
precision mediump float;
206+
207+
varying vec3 vColor;
144208
void main(void) {
145-
gl_FragColor = vec4( normalize( vNormal / 2.0 + vec3(0.5) ), 1.0 );
209+
gl_FragColor = vec4( vColor, 1.0 );
146210
}
147211
""";
148212

lib/src/vector.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ class Vector {
120120
return this;
121121
}
122122

123+
// The cross product results in a vector perpendicular to the two input vectors
124+
// The result's magnitude is equal to the magnitudes of the two inputs multiplied together and then multiplied by the sine of the angle between the inputs.
125+
// Or in other words the result's magnitude is equal to the area of the parallelogram that the two input vectors span.
123126
Vector cross( Vector vec2) {
124127
return cross2( this, vec2);
125128
}
@@ -134,6 +137,8 @@ class Vector {
134137
return this;
135138
}
136139

140+
// The dot product is a float value equal to the magnitudes of the two vectors multiplied together and then multiplied by the cosine of the angle between them.
141+
// For normalized vectors Dot returns 1 if they point in exactly the same direction, -1 if they point in completely opposite directions and zero if the vectors are perpendicular.
137142
double dot( Vector vec2) {
138143
return array[0] * vec2[0] + array[1] * vec2[1] + array[2] * vec2[2];
139144
}

0 commit comments

Comments
 (0)