Skip to content

Commit 13cfc40

Browse files
authored
Merge pull request #16 from gl-vis/3353-light-reversed-ranges
gl-mesh3d - Light position and shading fix
2 parents ac59ea4 + 33a7e74 commit 13cfc40

File tree

5 files changed

+38
-30
lines changed

5 files changed

+38
-30
lines changed

lib/triangle-fragment.glsl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#extension GL_OES_standard_derivatives : enable
22

3-
precision highp float;
3+
precision mediump float;
44

55
#pragma glslify: cookTorrance = require(glsl-specular-cook-torrance)
6-
#pragma glslify: faceNormal = require('glsl-face-normal')
6+
//#pragma glslify: beckmann = require(glsl-specular-beckmann) // used in gl-surface3d
7+
78
#pragma glslify: outOfRange = require(glsl-out-of-range)
89

910
uniform vec3 clipBounds[2];
@@ -29,13 +30,13 @@ void main() {
2930
vec3 L = normalize(f_lightDirection);
3031
vec3 V = normalize(f_eyeDirection);
3132

32-
vec3 normal = faceNormal(f_data);
33-
34-
if (dot(N, normal) < 0.0) {
33+
if(gl_FrontFacing) {
3534
N = -N;
3635
}
3736

3837
float specular = cookTorrance(L, V, N, roughness, fresnel);
38+
//float specular = max(beckmann(L, V, N, roughness), 0.); // used in gl-surface3d
39+
3940
float diffuse = min(kambient + kdiffuse * max(dot(N, L), 0.0), 1.0);
4041

4142
vec4 surfaceColor = f_color * texture2D(texture, f_uv);

lib/triangle-vertex.glsl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
precision highp float;
1+
precision mediump float;
22

33
attribute vec3 position, normal;
44
attribute vec4 color;
55
attribute vec2 uv;
66

77
uniform mat4 model
88
, view
9-
, projection;
9+
, projection
10+
, inverseModel;
1011
uniform vec3 eyePosition
1112
, lightPosition;
1213

@@ -17,14 +18,21 @@ varying vec3 f_normal
1718
varying vec4 f_color;
1819
varying vec2 f_uv;
1920

21+
vec4 project(vec3 p) {
22+
return projection * view * model * vec4(p, 1.0);
23+
}
24+
2025
void main() {
21-
vec4 m_position = model * vec4(position, 1.0);
22-
vec4 t_position = view * m_position;
23-
gl_Position = projection * t_position;
26+
gl_Position = project(position);
27+
28+
//Lighting geometry parameters
29+
vec4 cameraCoordinate = view * vec4(position , 1.0);
30+
cameraCoordinate.xyz /= cameraCoordinate.w;
31+
f_lightDirection = lightPosition - cameraCoordinate.xyz;
32+
f_eyeDirection = eyePosition - cameraCoordinate.xyz;
33+
f_normal = normalize((vec4(normal,0) * inverseModel).xyz);
34+
2435
f_color = color;
25-
f_normal = normal;
2636
f_data = position;
27-
f_eyeDirection = eyePosition - position;
28-
f_lightDirection = lightPosition - position;
2937
f_uv = uv;
3038
}

mesh.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var pickShader = shaders.pickShader
2424
var pointPickShader = shaders.pointPickShader
2525
var contourShader = shaders.contourShader
2626

27-
var identityMatrix = [
27+
var IDENTITY = [
2828
1,0,0,0,
2929
0,1,0,0,
3030
0,0,1,0,
@@ -121,9 +121,9 @@ function SimplicialMesh(gl
121121

122122
this.opacity = 1.0
123123

124-
this._model = identityMatrix
125-
this._view = identityMatrix
126-
this._projection = identityMatrix
124+
this._model = IDENTITY
125+
this._view = IDENTITY
126+
this._projection = IDENTITY
127127
this._resolution = [1,1]
128128
}
129129

@@ -513,7 +513,7 @@ fill_loop:
513513
}
514514

515515
for(var j=0; j<3; ++j) {
516-
var v = cell[j]
516+
var v = cell[2 - j]
517517

518518
var p = positions[v]
519519
tPos.push(p[0], p[1], p[2])
@@ -595,9 +595,9 @@ fill_loop:
595595
proto.drawTransparent = proto.draw = function(params) {
596596
params = params || {}
597597
var gl = this.gl
598-
var model = params.model || identityMatrix
599-
var view = params.view || identityMatrix
600-
var projection = params.projection || identityMatrix
598+
var model = params.model || IDENTITY
599+
var view = params.view || IDENTITY
600+
var projection = params.projection || IDENTITY
601601

602602
var clipBounds = [[-1e6,-1e6,-1e6],[1e6,1e6,1e6]]
603603
for(var i=0; i<3; ++i) {
@@ -609,6 +609,7 @@ proto.drawTransparent = proto.draw = function(params) {
609609
model: model,
610610
view: view,
611611
projection: projection,
612+
inverseModel: IDENTITY.slice(),
612613

613614
clipBounds: clipBounds,
614615

@@ -628,6 +629,10 @@ proto.drawTransparent = proto.draw = function(params) {
628629
texture: 0
629630
}
630631

632+
uniforms.inverseModel = invert(uniforms.inverseModel, uniforms.model)
633+
634+
gl.disable(gl.CULL_FACE)
635+
631636
this.texture.bind(0)
632637

633638
var invCameraMatrix = new Array(16)
@@ -698,9 +703,9 @@ proto.drawPick = function(params) {
698703

699704
var gl = this.gl
700705

701-
var model = params.model || identityMatrix
702-
var view = params.view || identityMatrix
703-
var projection = params.projection || identityMatrix
706+
var model = params.model || IDENTITY
707+
var view = params.view || IDENTITY
708+
var projection = params.projection || IDENTITY
704709

705710
var clipBounds = [[-1e6,-1e6,-1e6],[1e6,1e6,1e6]]
706711
for(var i=0; i<3; ++i) {

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
"gl-shader": "^4.2.1",
1515
"gl-texture2d": "^2.0.8",
1616
"gl-vao": "^1.1.3",
17-
"glsl-face-normal": "^1.0.2",
1817
"glsl-out-of-range": "^1.0.4",
1918
"glsl-specular-cook-torrance": "^2.0.1",
2019
"glslify": "^7.0.0",

0 commit comments

Comments
 (0)