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

Rainbow shader #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
153 changes: 153 additions & 0 deletions shaders/rainbow.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#version 150
// Copyright 2015, Christopher J. Foster and the other displaz contributors.
// Use of this code is governed by the BSD-style license found in LICENSE.txt

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 modelViewProjectionMatrix;

//------------------------------------------------------------------------------
#if defined(VERTEX_SHADER)

uniform int colorMode = 0; //# uiname=Colour Mode; enum=Z|Intensity
uniform float valueMultiplier = 1.000;//# uiname=Value Multiplier; min=0; max=100; scaling=linear; speed=0.1
uniform float delta = 0.500;//# uiname=Value Delta; min=0; max=1; scaling=linear; speed=0.1
uniform float radiusMultiplier = 0.01;//# uiname=Radius Multiplier; min=0.00001; max=100
uniform float trimRadius = 1000000;
uniform float minPointSize = 0;
uniform float maxPointSize = 400.0;
// Point size multiplier to get from a width in projected coordinates to the
// number of pixels across as required for gl_PointSize
uniform float pointPixelScale = 0;
uniform vec3 cursorPos = vec3(0);
uniform int fileNumber = 0;

in vec3 position;
in vec3 color;
in float intensity;
in float markersize;
in int markershape;

flat out float modifiedPointRadius;
flat out float pointScreenSize;
flat out vec3 pointColor;
flat out int markerShape2;

vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

vec3 tonemap(float x)
{
float h = clamp(fract(x + delta), 0.0, 1.0);
return hsv2rgb(vec3(h, 1.0, 1.0));
}

void main()
{
vec4 p = modelViewProjectionMatrix * vec4(position,1.0);
float r = length(position - cursorPos);
modifiedPointRadius = radiusMultiplier * step(r, trimRadius);
if (markersize != 0) // Default == 0 for in attributes. TODO: this isn't good in this case - what to do about it?
modifiedPointRadius *= markersize;
pointScreenSize = clamp(2*pointPixelScale*modifiedPointRadius / p.w, minPointSize, maxPointSize);
markerShape2 = 0;//markershape;
// Ensure zero size points are discarded. The actual minimum point size is
// hardware and driver dependent, so set the markerShape2 to discarded for
// good measure.
if (pointScreenSize <= 0)
{
pointScreenSize = 0;
markerShape2 = -1;
}
else if (pointScreenSize < 1)
{
// Clamp to minimum size of 1 to avoid aliasing with some drivers
pointScreenSize = 1;
}
// Compute vertex color
float v;
if (colorMode == 0)
v = position.z * 2.0;
else
v = intensity / 255.0;
pointColor = tonemap(v * valueMultiplier);
//pointColor = vec3(1.0,1.0,1.0);
//pointScreenSize = pointScreenSize / 5.0;
gl_PointSize = pointScreenSize;
gl_Position = p;
}


//------------------------------------------------------------------------------
#elif defined(FRAGMENT_SHADER)

uniform float markerWidth = 0.3;

flat in float modifiedPointRadius;
flat in float pointScreenSize;
flat in vec3 pointColor;
flat in int markerShape2;

out vec4 fragColor;

// Limit at which the point is rendered as a small square for antialiasing
// rather than using a specific marker shape
const float pointScreenSizeLimit = 2;
const float sqrt2 = 1.414213562;

void main()
{
if (markerShape2 < 0) // markerShape2 == -1: discarded.
discard;
// (markerShape2 == 1: Square shape)
# ifndef BROKEN_GL_FRAG_COORD
gl_FragDepth = gl_FragCoord.z;
# endif
if (markerShape2 != 1 && pointScreenSize > pointScreenSizeLimit)
{
float w = markerWidth;
if (pointScreenSize < 2*pointScreenSizeLimit)
{
// smoothly turn on the markers as we get close enough to see them
w = mix(1, w, pointScreenSize/pointScreenSizeLimit - 1);
}
vec2 p = 2*(gl_PointCoord - 0.5);
if (markerShape2 == 0) // shape: .
{
float r = length(p);
if (r > 1)
discard;
# ifndef BROKEN_GL_FRAG_COORD
gl_FragDepth += projectionMatrix[3][2] * gl_FragCoord.w*gl_FragCoord.w
// TODO: Why is the factor of 0.5 required here?
* 0.5*modifiedPointRadius*sqrt(1-r*r);
# endif
}
else if (markerShape2 == 2) // shape: o
{
float r = length(p);
if (r > 1 || r < 1 - w)
discard;
}
else if (markerShape2 == 3) // shape: x
{
w *= 0.5*sqrt2;
if (abs(p.x + p.y) > w && abs(p.x - p.y) > w)
discard;
}
else if (markerShape2 == 4) // shape: +
{
w *= 0.5;
if (abs(p.x) > w && abs(p.y) > w)
discard;
}
}
fragColor = vec4(pointColor, 1);
}

#endif