Skip to content

Commit

Permalink
- Fallback to UVWO for fragment STPQ.
Browse files Browse the repository at this point in the history
- Make gamma optional and add in/out for fragment pass.
  • Loading branch information
unconed committed Oct 16, 2015
1 parent 00e951f commit 854ce8e
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Document instanced traits (e.g. `divideX`) and non-standard defaults.
* Support fragment passes on unshaded geometry.
* Added `lineBias` prop to set Z-bias between surface/face and its wireframe.
* Allow passing modified STPQ coordinates from <vertex /> to <fragment /> (see `fragmentcolor.html` example)

0.0.3

Expand Down
7 changes: 5 additions & 2 deletions build/mathbox-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -46720,7 +46720,7 @@ module.exports = {"arrow.position": "uniform float worldUnit;\nuniform float lin
"mesh.fragment.color": "varying vec4 vColor;\n\nvec4 getColor() {\n return vColor;\n}\n",
"mesh.fragment.map": "#ifdef POSITION_STPQ\nvarying vec4 vSTPQ;\n#endif\n#ifdef POSITION_U\nvarying float vU;\n#endif\n#ifdef POSITION_UV\nvarying vec2 vUV;\n#endif\n#ifdef POSITION_UVW\nvarying vec3 vUVW;\n#endif\n#ifdef POSITION_UVWO\nvarying vec4 vUVWO;\n#endif\n\nvec4 getSample(vec4 uvwo, vec4 stpq);\n\nvec4 getMapColor() {\n #ifdef POSITION_STPQ\n vec4 stpq = vSTPQ;\n #else\n vec4 stpq = vec4(0.0);\n #endif\n\n #ifdef POSITION_U\n vec4 uvwo = vec4(vU, 0.0, 0.0, 0.0);\n #endif\n #ifdef POSITION_UV\n vec4 uvwo = vec4(vUV, 0.0, 0.0);\n #endif\n #ifdef POSITION_UVW\n vec4 uvwo = vec4(vUVW, 0.0);\n #endif\n #ifdef POSITION_UVWO\n vec4 uvwo = vec4(vUVWO);\n #endif\n\n return getSample(uvwo, stpq);\n}\n",
"mesh.fragment.mask": "varying float vMask;\n\nfloat ease(float t) {\n t = clamp(t, 0.0, 1.0);\n return t * t * (3.0 - 2.0 * t);\n}\n\nvec4 maskColor() {\n if (vMask <= 0.0) discard;\n return vec4(vec3(1.0), ease(vMask));\n}\n",
"mesh.fragment.material": "#ifdef POSITION_STPQ\nvarying vec4 vSTPQ;\n#endif\n#ifdef POSITION_U\nvarying float vU;\n#endif\n#ifdef POSITION_UV\nvarying vec2 vUV;\n#endif\n#ifdef POSITION_UVW\nvarying vec3 vUVW;\n#endif\n#ifdef POSITION_UVWO\nvarying vec4 vUVWO;\n#endif\n\nvec4 getSample(vec4 rgba, vec4 stpq);\n\nvec4 getMaterialColor(vec4 rgba) {\n #ifdef POSITION_STPQ\n vec4 stpq = vSTPQ;\n #else\n vec4 stpq = vec4(0.0);\n #endif\n\n return getSample(rgba, stpq);\n}\n",
"mesh.fragment.material": "#ifdef POSITION_STPQ\nvarying vec4 vSTPQ;\n#endif\n#ifdef POSITION_U\nvarying float vU;\n#endif\n#ifdef POSITION_UV\nvarying vec2 vUV;\n#endif\n#ifdef POSITION_UVW\nvarying vec3 vUVW;\n#endif\n#ifdef POSITION_UVWO\nvarying vec4 vUVWO;\n#endif\n\nvec4 getSample(vec4 rgba, vec4 stpq);\n\nvec4 getMaterialColor(vec4 rgba) {\n vec4 stpq = vec4(0.0);\n\n #ifdef POSITION_U\n stpq.x = vU;\n #endif\n #ifdef POSITION_UV\n stpq.xy = vUV;\n #endif\n #ifdef POSITION_UVW\n stpq.xyz = vUVW;\n #endif\n #ifdef POSITION_UVWO\n stpq = vUVWO;\n #endif\n\n #ifdef POSITION_STPQ\n stpq = vSTPQ;\n #endif\n\n return getSample(rgba, stpq);\n}\n",
"mesh.fragment.shaded": "varying vec3 vNormal;\nvarying vec3 vLight;\nvarying vec3 vPosition;\n\nvec3 offSpecular(vec3 color) {\n vec3 c = 1.0 - color;\n return 1.0 - c * c;\n}\n\nvec4 getShadedColor(vec4 rgba) {\n \n vec3 color = rgba.xyz;\n vec3 color2 = offSpecular(rgba.xyz);\n\n vec3 normal = normalize(vNormal);\n vec3 light = normalize(vLight);\n vec3 position = normalize(vPosition);\n \n float side = gl_FrontFacing ? -1.0 : 1.0;\n float cosine = side * dot(normal, light);\n float diffuse = mix(max(0.0, cosine), .5 + .5 * cosine, .1);\n \n vec3 halfLight = normalize(light + position);\n\tfloat cosineHalf = max(0.0, side * dot(normal, halfLight));\n\tfloat specular = pow(cosineHalf, 16.0);\n\t\n\treturn vec4(color * (diffuse * .9 + .05) + .25 * color2 * specular, rgba.a);\n}\n",
"mesh.fragment.texture": "",
"mesh.gamma.in": "vec4 getGammaInColor(vec4 rgba) {\n return vec4(rgba.rgb * rgba.rgb, rgba.a);\n}\n",
Expand Down Expand Up @@ -63826,7 +63826,7 @@ Traits = {
},
fragment: {
pass: Types.fragmentPass(),
gamma: Types.bool(true)
gamma: Types.bool(false)
},
transform3: {
position: Types.vec3(),
Expand Down Expand Up @@ -64246,6 +64246,9 @@ Fragment = (function(superClass) {
Fragment.prototype.fragment = function(shader, pass) {
if (this.bind.shader != null) {
if (pass === this.props.pass) {
if (this.props.gamma) {
shader.pipe('mesh.gamma.out');
}
shader.pipe(this.bind.shader.shaderBind());
shader.split();
if (this.props.gamma) {
Expand Down
18 changes: 9 additions & 9 deletions build/mathbox-bundle.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions build/mathbox-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {"arrow.position": "uniform float worldUnit;\nuniform float lin
"mesh.fragment.color": "varying vec4 vColor;\n\nvec4 getColor() {\n return vColor;\n}\n",
"mesh.fragment.map": "#ifdef POSITION_STPQ\nvarying vec4 vSTPQ;\n#endif\n#ifdef POSITION_U\nvarying float vU;\n#endif\n#ifdef POSITION_UV\nvarying vec2 vUV;\n#endif\n#ifdef POSITION_UVW\nvarying vec3 vUVW;\n#endif\n#ifdef POSITION_UVWO\nvarying vec4 vUVWO;\n#endif\n\nvec4 getSample(vec4 uvwo, vec4 stpq);\n\nvec4 getMapColor() {\n #ifdef POSITION_STPQ\n vec4 stpq = vSTPQ;\n #else\n vec4 stpq = vec4(0.0);\n #endif\n\n #ifdef POSITION_U\n vec4 uvwo = vec4(vU, 0.0, 0.0, 0.0);\n #endif\n #ifdef POSITION_UV\n vec4 uvwo = vec4(vUV, 0.0, 0.0);\n #endif\n #ifdef POSITION_UVW\n vec4 uvwo = vec4(vUVW, 0.0);\n #endif\n #ifdef POSITION_UVWO\n vec4 uvwo = vec4(vUVWO);\n #endif\n\n return getSample(uvwo, stpq);\n}\n",
"mesh.fragment.mask": "varying float vMask;\n\nfloat ease(float t) {\n t = clamp(t, 0.0, 1.0);\n return t * t * (3.0 - 2.0 * t);\n}\n\nvec4 maskColor() {\n if (vMask <= 0.0) discard;\n return vec4(vec3(1.0), ease(vMask));\n}\n",
"mesh.fragment.material": "#ifdef POSITION_STPQ\nvarying vec4 vSTPQ;\n#endif\n#ifdef POSITION_U\nvarying float vU;\n#endif\n#ifdef POSITION_UV\nvarying vec2 vUV;\n#endif\n#ifdef POSITION_UVW\nvarying vec3 vUVW;\n#endif\n#ifdef POSITION_UVWO\nvarying vec4 vUVWO;\n#endif\n\nvec4 getSample(vec4 rgba, vec4 stpq);\n\nvec4 getMaterialColor(vec4 rgba) {\n #ifdef POSITION_STPQ\n vec4 stpq = vSTPQ;\n #else\n vec4 stpq = vec4(0.0);\n #endif\n\n return getSample(rgba, stpq);\n}\n",
"mesh.fragment.material": "#ifdef POSITION_STPQ\nvarying vec4 vSTPQ;\n#endif\n#ifdef POSITION_U\nvarying float vU;\n#endif\n#ifdef POSITION_UV\nvarying vec2 vUV;\n#endif\n#ifdef POSITION_UVW\nvarying vec3 vUVW;\n#endif\n#ifdef POSITION_UVWO\nvarying vec4 vUVWO;\n#endif\n\nvec4 getSample(vec4 rgba, vec4 stpq);\n\nvec4 getMaterialColor(vec4 rgba) {\n vec4 stpq = vec4(0.0);\n\n #ifdef POSITION_U\n stpq.x = vU;\n #endif\n #ifdef POSITION_UV\n stpq.xy = vUV;\n #endif\n #ifdef POSITION_UVW\n stpq.xyz = vUVW;\n #endif\n #ifdef POSITION_UVWO\n stpq = vUVWO;\n #endif\n\n #ifdef POSITION_STPQ\n stpq = vSTPQ;\n #endif\n\n return getSample(rgba, stpq);\n}\n",
"mesh.fragment.shaded": "varying vec3 vNormal;\nvarying vec3 vLight;\nvarying vec3 vPosition;\n\nvec3 offSpecular(vec3 color) {\n vec3 c = 1.0 - color;\n return 1.0 - c * c;\n}\n\nvec4 getShadedColor(vec4 rgba) {\n \n vec3 color = rgba.xyz;\n vec3 color2 = offSpecular(rgba.xyz);\n\n vec3 normal = normalize(vNormal);\n vec3 light = normalize(vLight);\n vec3 position = normalize(vPosition);\n \n float side = gl_FrontFacing ? -1.0 : 1.0;\n float cosine = side * dot(normal, light);\n float diffuse = mix(max(0.0, cosine), .5 + .5 * cosine, .1);\n \n vec3 halfLight = normalize(light + position);\n\tfloat cosineHalf = max(0.0, side * dot(normal, halfLight));\n\tfloat specular = pow(cosineHalf, 16.0);\n\t\n\treturn vec4(color * (diffuse * .9 + .05) + .25 * color2 * specular, rgba.a);\n}\n",
"mesh.fragment.texture": "",
"mesh.gamma.in": "vec4 getGammaInColor(vec4 rgba) {\n return vec4(rgba.rgb * rgba.rgb, rgba.a);\n}\n",
Expand Down Expand Up @@ -17141,7 +17141,7 @@ Traits = {
},
fragment: {
pass: Types.fragmentPass(),
gamma: Types.bool(true)
gamma: Types.bool(false)
},
transform3: {
position: Types.vec3(),
Expand Down Expand Up @@ -17561,6 +17561,9 @@ Fragment = (function(superClass) {
Fragment.prototype.fragment = function(shader, pass) {
if (this.bind.shader != null) {
if (pass === this.props.pass) {
if (this.props.gamma) {
shader.pipe('mesh.gamma.out');
}
shader.pipe(this.bind.shader.shaderBind());
shader.split();
if (this.props.gamma) {
Expand Down
26 changes: 13 additions & 13 deletions build/mathbox-core.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/shaders.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/primitives.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@
*Apply custom fragment shader pass*

* *classes* = `[]` (string array) - Custom classes, e.g. `["big"]`
* *gamma* = `false` (boolean) - Pass RGBA values in sRGB instead of linear RGB
* *id* = `null` (nullable string) - Unique ID, e.g. `"sampler"`
* *pass* = `"light"` (fragmentPass) - Fragment pass (color, light, rgba)
* *shader* = `"<"` (select) - Shader to use
Expand Down
7 changes: 5 additions & 2 deletions examples/test/fragmentcolor.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#define POSITION_STPQ
vec4 getColor(vec4 rgba, inout vec4 stpq) {
// Retrieve interpolated XYZ from vertices and use as RGB color.
// Note: gamma correction will be auto-applied unless .fragment({ gamma: false }) is set.
// Note: gamma correction is applied with .fragment({ gamma: true }).
vec3 rgb = stpq.xyz;

// Add spatial grid
Expand Down Expand Up @@ -146,7 +146,10 @@
.shader({
code: "#fragment-xyz"
})
.fragment();
.fragment({
// Convert from (web) sRGB to (GL) linear RGB on output
gamma: true
});

// Make surface from data
xf.surface({
Expand Down
1 change: 1 addition & 0 deletions src/docs/traits.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Traits =
pass: ["Vertex pass (data, view, world, eye)", "vertexPass", '"view"']
fragment:
pass: ["Fragment pass (color, light, rgba)", "fragmentPass", '"light"']
gamma: ["Pass RGBA values in sRGB instead of linear RGB", "boolean", "false"]

transform3:
position: ["3D Position", "vec3", "[0, 0, 0]"]
Expand Down
2 changes: 1 addition & 1 deletion src/primitives/types/traits.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Traits =
pass: Types.vertexPass()
fragment:
pass: Types.fragmentPass()
gamma: Types.bool(true)
gamma: Types.bool(false)

transform3:
position: Types.vec3()
Expand Down
3 changes: 2 additions & 1 deletion src/primitives/types/transform/fragment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class Fragment extends Transform
fragment: (shader, pass) ->
if @bind.shader?
if pass == @props.pass
shader.pipe 'mesh.gamma.out' if @props.gamma
shader.pipe @bind.shader.shaderBind()
shader.split()
shader.pipe 'mesh.gamma.in' if @props.gamma
shader.pipe 'mesh.gamma.in' if @props.gamma
shader.pass()
super shader, pass

Expand Down
19 changes: 16 additions & 3 deletions src/shaders/glsl/mesh.fragment.material.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,23 @@ varying vec4 vUVWO;
vec4 getSample(vec4 rgba, vec4 stpq);

vec4 getMaterialColor(vec4 rgba) {
#ifdef POSITION_STPQ
vec4 stpq = vSTPQ;
#else
vec4 stpq = vec4(0.0);

#ifdef POSITION_U
stpq.x = vU;
#endif
#ifdef POSITION_UV
stpq.xy = vUV;
#endif
#ifdef POSITION_UVW
stpq.xyz = vUVW;
#endif
#ifdef POSITION_UVWO
stpq = vUVWO;
#endif

#ifdef POSITION_STPQ
stpq = vSTPQ;
#endif

return getSample(rgba, stpq);
Expand Down

0 comments on commit 854ce8e

Please sign in to comment.