Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add intensity parameter #6

Merged
merged 2 commits into from
Mar 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/shaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var pickSrc = glslify('../shaders/pick.glsl')
exports.createShader = function(gl) {
var shader = createShader(gl, vertSrc, fragSrc, null, [
{name: 'uv', type: 'vec4'},
{name: 'f', type: 'vec2'},
{name: 'f', type: 'vec3'},
{name: 'normal', type: 'vec3'}
])
shader.attributes.uv.location = 0
Expand All @@ -20,7 +20,7 @@ exports.createShader = function(gl) {
exports.createPickShader = function(gl) {
var shader = createShader(gl, vertSrc, pickSrc, null, [
{name: 'uv', type: 'vec4'},
{name: 'f', type: 'vec2'},
{name: 'f', type: 'vec3'},
{name: 'normal', type: 'vec3'}
])
shader.attributes.uv.location = 0
Expand Down
2 changes: 1 addition & 1 deletion shaders/contour-vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {
clipPosition.z = clipPosition.z + zOffset;

gl_Position = clipPosition;
value = dataCoordinate.z;
value = uv.z;
kill = -1.0;
worldCoordinate = dataCoordinate;
planeCoordinate = uv.zw;
Expand Down
3 changes: 1 addition & 2 deletions shaders/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ void main() {
float specular = beckmann(L, V, N, roughness);
float diffuse = min(kambient + kdiffuse * max(dot(N, L), 0.0), 1.0);

float interpValue = (value - lowerBound.z) / (upperBound.z - lowerBound.z);
vec4 surfaceColor = texture2D(colormap, vec2(interpValue, interpValue));
vec4 surfaceColor = texture2D(colormap, vec2(value, value));
vec4 litColor = surfaceColor.a * vec4(diffuse * surfaceColor.rgb + kspecular * vec3(1,1,1) * specular, 1.0);

gl_FragColor = mix(litColor, contourColor, contourTint) * opacity;
Expand Down
8 changes: 4 additions & 4 deletions shaders/vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
precision mediump float;

attribute vec4 uv;
attribute vec2 f;
attribute vec3 f;
attribute vec3 normal;

uniform mat4 model, view, projection, inverseModel;
Expand All @@ -17,14 +17,14 @@ void main() {
vec4 worldPosition = model * vec4(worldCoordinate, 1.0);
vec4 clipPosition = projection * view * worldPosition;
gl_Position = clipPosition;
value = f.x;
kill = f.y;
value = f.z;
planeCoordinate = uv.xy;

//Lighting geometry parameters
vec4 cameraCoordinate = view * worldPosition;
cameraCoordinate.xyz /= cameraCoordinate.w;
lightDirection = lightPosition - cameraCoordinate.xyz;
eyeDirection = eyePosition - cameraCoordinate.xyz;
surfaceNormal = normalize((vec4(normal,0) * inverseModel).xyz);
}
}
29 changes: 25 additions & 4 deletions surface.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var createContourShader = shaders.createContourShader
var createPickShader = shaders.createPickShader
var createPickContourShader = shaders.createPickContourShader

var SURFACE_VERTEX_SIZE = 4 * (4 + 2 + 3)
var SURFACE_VERTEX_SIZE = 4 * (4 + 3 + 3)

var IDENTITY = [
1, 0, 0, 0,
Expand Down Expand Up @@ -892,8 +892,10 @@ proto.update = function(params) {
//Initialize surface
var lo = [ Infinity, Infinity, Infinity]
var hi = [-Infinity,-Infinity,-Infinity]
var lo_intensity = Infinity
var hi_intensity = -Infinity
var count = (shape[0]-1) * (shape[1]-1) * 6
var tverts = pool.mallocFloat(bits.nextPow2(9*count))
var tverts = pool.mallocFloat(bits.nextPow2(10*count))
var tptr = 0
var fptr = 0
var vertexCount = 0
Expand All @@ -919,32 +921,51 @@ proto.update = function(params) {
var tx = this._field[0].get(r+1, c+1)
var ty = this._field[1].get(r+1, c+1)
var f = this._field[2].get(r+1, c+1)
var vf = f
var nx = normals.get(r+1, c+1, 0)
var ny = normals.get(r+1, c+1, 1)
var nz = normals.get(r+1, c+1, 2)

if(params.intensity) {
vf = params.intensity.get(r, c)
}

tverts[tptr++] = r
tverts[tptr++] = c
tverts[tptr++] = tx
tverts[tptr++] = ty
tverts[tptr++] = f
tverts[tptr++] = 0
tverts[tptr++] = vf
tverts[tptr++] = nx
tverts[tptr++] = ny
tverts[tptr++] = nz

lo[0] = Math.min(lo[0], tx)
lo[1] = Math.min(lo[1], ty)
lo[2] = Math.min(lo[2], f)
lo_intensity = Math.min(lo_intensity, vf)

hi[0] = Math.max(hi[0], tx)
hi[1] = Math.max(hi[1], ty)
hi[2] = Math.max(hi[2], f)
hi_intensity = Math.max(hi_intensity, vf)

vertexCount += 1
}
}
}

if(params.intensityBounds) {
lo_intensity = +params.intensityBounds[0]
hi_intensity = +params.intensityBounds[1]
}

//Scale all vertex intensities
for(var i=6; i<tptr; i+=10) {
tverts[i] = (tverts[i] - lo_intensity) / (hi_intensity - lo_intensity)
}

this._vertexCount = vertexCount
this._coordinateBuffer.update(tverts.subarray(0,tptr))
pool.freeFloat(tverts)
Expand Down Expand Up @@ -1219,15 +1240,15 @@ function createSurfacePlot(params) {
offset: 0
},
{ buffer: coordinateBuffer,
size: 2,
size: 3,
stride: SURFACE_VERTEX_SIZE,
offset: 16
},
{
buffer: coordinateBuffer,
size: 3,
stride: SURFACE_VERTEX_SIZE,
offset: 24
offset: 28
}
])

Expand Down