Skip to content

Commit

Permalink
Fix shaders to support --optimize mode
Browse files Browse the repository at this point in the history
  • Loading branch information
w0rm committed Jan 4, 2020
1 parent 9b348ff commit 203b33b
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 56 deletions.
17 changes: 12 additions & 5 deletions lessons/Lesson07.elm
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ renderEntity mesh thetaX thetaY texture position lighting directionalColour dire
[ WebGL.entity vertexShader fragmentShader mesh (uniformsCube thetaX thetaY tex position lighting directionalColour directional ambientColour) ]


uniformsCube : Float -> Float -> Texture -> Vec3 -> Bool -> Vec3 -> Vec3 -> Vec3 -> { texture : Texture, worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Bool, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 }
uniformsCube : Float -> Float -> Texture -> Vec3 -> Bool -> Vec3 -> Vec3 -> Vec3 -> { texture : Texture, worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Int, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 }
uniformsCube tx ty texture displacement lighting directionalColour directional ambientColour =
let
worldSpace =
Expand All @@ -452,13 +452,20 @@ uniformsCube tx ty texture displacement lighting directionalColour directional a

perspective =
makePerspective 45 1 0.1 100

boolToInt bool =
if bool then
1

else
0
in
{ texture = texture
, worldSpace = worldSpace
, perspective = perspective
, camera = camera
, normalMatrix = transpose (inverseOrthonormal worldSpace)
, lighting = lighting
, lighting = boolToInt lighting
, directionalColour = directionalColour
, ambientColour = ambientColour
, directional = directional
Expand All @@ -469,7 +476,7 @@ uniformsCube tx ty texture displacement lighting directionalColour directional a
-- SHADERS


vertexShader : Shader { attr | position : Vec3, coord : Vec3, norm : Vec3 } { unif | worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Bool, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 } { vcoord : Vec2, lightWeighting : Vec3 }
vertexShader : Shader { attr | position : Vec3, coord : Vec3, norm : Vec3 } { unif | worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Int, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 } { vcoord : Vec2, lightWeighting : Vec3 }
vertexShader =
[glsl|
Expand All @@ -483,7 +490,7 @@ vertexShader =
uniform mat4 perspective;
uniform mat4 normalMatrix;
uniform mat4 camera;
uniform bool lighting;
uniform int lighting;
uniform vec3 directionalColour;
uniform vec3 directional;
uniform vec3 ambientColour;
Expand All @@ -495,7 +502,7 @@ vertexShader =
gl_Position = perspective * camera * worldSpace * vec4(position, 1.0);
vcoord = coord.xy;
if (!lighting) {
if (lighting == 0) {
lightWeighting = vec3(1.0, 1.0, 1.0);
} else {
vec4 transformedNormal = normalMatrix * vec4(norm, 0.0);
Expand Down
17 changes: 12 additions & 5 deletions lessons/Lesson08.elm
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ renderEntity mesh thetaX thetaY texture position lighting blending alpha directi
[ WebGL.entityWith settings vertexShader fragmentShader mesh (uniformsCube thetaX thetaY tex position lighting alpha directionalColour directional ambientColour) ]


uniformsCube : Float -> Float -> Texture -> Vec3 -> Bool -> Float -> Vec3 -> Vec3 -> Vec3 -> { texture : Texture, worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Bool, alpha : Float, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 }
uniformsCube : Float -> Float -> Texture -> Vec3 -> Bool -> Float -> Vec3 -> Vec3 -> Vec3 -> { texture : Texture, worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Int, alpha : Float, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 }
uniformsCube tx ty texture displacement lighting alpha directionalColour directional ambientColour =
let
worldSpace =
Expand All @@ -491,13 +491,20 @@ uniformsCube tx ty texture displacement lighting alpha directionalColour directi

perspective =
makePerspective 45 1 0.1 100

boolToInt bool =
if bool then
1

else
0
in
{ texture = texture
, worldSpace = worldSpace
, perspective = perspective
, camera = camera
, normalMatrix = transpose (inverseOrthonormal (mul worldSpace camera))
, lighting = lighting
, lighting = boolToInt lighting
, alpha = alpha
, directionalColour = directionalColour
, ambientColour = ambientColour
Expand All @@ -509,7 +516,7 @@ uniformsCube tx ty texture displacement lighting alpha directionalColour directi
-- SHADERS


vertexShader : Shader { attr | position : Vec3, coord : Vec3, norm : Vec3 } { unif | worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Bool, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 } { vcoord : Vec2, lightWeighting : Vec3 }
vertexShader : Shader { attr | position : Vec3, coord : Vec3, norm : Vec3 } { unif | worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Int, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 } { vcoord : Vec2, lightWeighting : Vec3 }
vertexShader =
[glsl|
Expand All @@ -523,7 +530,7 @@ vertexShader =
uniform mat4 perspective;
uniform mat4 normalMatrix;
uniform mat4 camera;
uniform bool lighting;
uniform int lighting;
uniform vec3 directionalColour;
uniform vec3 directional;
uniform vec3 ambientColour;
Expand All @@ -535,7 +542,7 @@ vertexShader =
gl_Position = perspective * camera * worldSpace * vec4(position, 1.0);
vcoord = coord.xy;
if (!lighting) {
if (lighting == 0) {
lightWeighting = vec3(1.0, 1.0, 1.0);
} else {
vec4 transformedNormal = normalMatrix * vec4(norm, 0.0);
Expand Down
17 changes: 12 additions & 5 deletions lessons/Lesson11.elm
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ renderEntity mesh thetaX thetaY texture position lighting directionalColour dire
[ WebGL.entity vertexShader fragmentShader mesh (uniformsShpere thetaX thetaY tex position lighting directionalColour directional ambientColour) ]


uniformsShpere : Float -> Float -> Texture -> Vec3 -> Bool -> Vec3 -> Vec3 -> Vec3 -> { texture : Texture, worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Bool, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 }
uniformsShpere : Float -> Float -> Texture -> Vec3 -> Bool -> Vec3 -> Vec3 -> Vec3 -> { texture : Texture, worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Int, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 }
uniformsShpere tx ty texture displacement lighting directionalColour directional ambientColour =
let
worldSpace =
Expand All @@ -399,13 +399,20 @@ uniformsShpere tx ty texture displacement lighting directionalColour directional

perspective =
makePerspective 45 1 0.1 100

boolToInt bool =
if bool then
1

else
0
in
{ texture = texture
, worldSpace = worldSpace
, perspective = perspective
, camera = camera
, normalMatrix = transpose (inverseOrthonormal (mul worldSpace camera))
, lighting = lighting
, lighting = boolToInt lighting
, directionalColour = directionalColour
, ambientColour = ambientColour
, directional = directional
Expand All @@ -416,7 +423,7 @@ uniformsShpere tx ty texture displacement lighting directionalColour directional
-- SHADERS


vertexShader : Shader { attr | position : Vec3, coord : Vec3, norm : Vec3 } { unif | worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Bool, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 } { vcoord : Vec2, lightWeighting : Vec3 }
vertexShader : Shader { attr | position : Vec3, coord : Vec3, norm : Vec3 } { unif | worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Int, directionalColour : Vec3, ambientColour : Vec3, directional : Vec3 } { vcoord : Vec2, lightWeighting : Vec3 }
vertexShader =
[glsl|
Expand All @@ -430,7 +437,7 @@ vertexShader =
uniform mat4 perspective;
uniform mat4 normalMatrix;
uniform mat4 camera;
uniform bool lighting;
uniform int lighting;
uniform vec3 directionalColour;
uniform vec3 directional;
uniform vec3 ambientColour;
Expand All @@ -442,7 +449,7 @@ vertexShader =
gl_Position = perspective * camera * worldSpace * vec4(position, 1.0);
vcoord = coord.xy;
if (!lighting) {
if (lighting == 0) {
lightWeighting = vec3(1.0, 1.0, 1.0);
} else {
vec4 transformedNormal = normalMatrix * vec4(norm, 0.0);
Expand Down
17 changes: 12 additions & 5 deletions lessons/Lesson12.elm
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ renderEntity mesh theta texture position lighting pointColour point ambientColou
[ WebGL.entity vertexShader fragmentShader mesh (uniformsShpere theta texture position lighting pointColour point ambientColour) ]


uniformsShpere : Float -> Texture -> Vec3 -> Bool -> Vec3 -> Vec3 -> Vec3 -> { texture : Texture, worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Bool, pointColour : Vec3, ambientColour : Vec3, point : Vec3 }
uniformsShpere : Float -> Texture -> Vec3 -> Bool -> Vec3 -> Vec3 -> Vec3 -> { texture : Texture, worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Int, pointColour : Vec3, ambientColour : Vec3, point : Vec3 }
uniformsShpere tx texture displacement lighting pointColour point ambientColour =
let
worldSpace =
Expand All @@ -395,13 +395,20 @@ uniformsShpere tx texture displacement lighting pointColour point ambientColour

perspective =
makePerspective 45 1 0.1 100

boolToInt bool =
if bool then
1

else
0
in
{ texture = texture
, worldSpace = worldSpace
, perspective = perspective
, camera = camera
, normalMatrix = transpose (inverseOrthonormal worldSpace)
, lighting = lighting
, lighting = boolToInt lighting
, pointColour = pointColour
, ambientColour = ambientColour
, point = point
Expand All @@ -412,7 +419,7 @@ uniformsShpere tx texture displacement lighting pointColour point ambientColour
-- SHADERS


vertexShader : Shader { attr | position : Vec3, coord : Vec3, norm : Vec3 } { unif | worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Bool, pointColour : Vec3, ambientColour : Vec3, point : Vec3 } { vcoord : Vec2, lightWeighting : Vec3 }
vertexShader : Shader { attr | position : Vec3, coord : Vec3, norm : Vec3 } { unif | worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, lighting : Int, pointColour : Vec3, ambientColour : Vec3, point : Vec3 } { vcoord : Vec2, lightWeighting : Vec3 }
vertexShader =
[glsl|
Expand All @@ -426,7 +433,7 @@ vertexShader =
uniform mat4 perspective;
uniform mat4 normalMatrix;
uniform mat4 camera;
uniform bool lighting;
uniform int lighting;
uniform vec3 pointColour;
uniform vec3 point;
uniform vec3 ambientColour;
Expand All @@ -439,7 +446,7 @@ vertexShader =
gl_Position = perspective * camera * mvPosition;
vcoord = coord.xy;
if (!lighting) {
if (lighting == 0) {
lightWeighting = vec3(1.0, 1.0, 1.0);
} else {
//vec4 directional = vec4(normalize(point), 0.0);
Expand Down
35 changes: 21 additions & 14 deletions lessons/Lesson13.elm
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ renderEntity mesh theta texture position useLighting useTextures usePerFragment
WebGL.entity vertexShaderPV fragmentShaderPV mesh (uniformsShpere theta texture position useLighting useTextures pointColour point ambientColour)


uniformsShpere : Float -> Texture -> Vec3 -> Bool -> Bool -> Vec3 -> Vec3 -> Vec3 -> { texture : Texture, worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, useLighting : Bool, useTextures : Bool, pointColour : Vec3, ambientColour : Vec3, point : Vec3 }
uniformsShpere : Float -> Texture -> Vec3 -> Bool -> Bool -> Vec3 -> Vec3 -> Vec3 -> { texture : Texture, worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, useLighting : Int, useTextures : Int, pointColour : Vec3, ambientColour : Vec3, point : Vec3 }
uniformsShpere tx texture displacement useLighting useTextures pointColour point ambientColour =
let
worldSpace =
Expand All @@ -417,14 +417,21 @@ uniformsShpere tx texture displacement useLighting useTextures pointColour point

perspective =
makePerspective 45 1 0.1 100

boolToInt bool =
if bool then
1

else
0
in
{ texture = texture
, worldSpace = worldSpace
, perspective = perspective
, camera = camera
, normalMatrix = transpose (inverseOrthonormal worldSpace)
, useLighting = useLighting
, useTextures = useTextures
, useLighting = boolToInt useLighting
, useTextures = boolToInt useTextures
, pointColour = pointColour
, ambientColour = ambientColour
, point = point
Expand Down Expand Up @@ -464,14 +471,14 @@ vertexShaderPF =
|]


fragmentShaderPF : Shader {} { unif | texture : Texture, useLighting : Bool, useTextures : Bool, ambientColour : Vec3, pointColour : Vec3, point : Vec3 } { vcoord : Vec2, vPosition : Vec3, vTransformedNormal : Vec3 }
fragmentShaderPF : Shader {} { unif | texture : Texture, useLighting : Int, useTextures : Int, ambientColour : Vec3, pointColour : Vec3, point : Vec3 } { vcoord : Vec2, vPosition : Vec3, vTransformedNormal : Vec3 }
fragmentShaderPF =
[glsl|
precision mediump float;
uniform sampler2D texture;
uniform bool useLighting;
uniform bool useTextures;
uniform int useLighting;
uniform int useTextures;
uniform vec3 ambientColour;
uniform vec3 pointColour;
uniform vec3 point;
Expand All @@ -483,7 +490,7 @@ fragmentShaderPF =
void main () {
vec3 lightWeighting;
lightWeighting = vec3(1.0, 1.0, 1.0);
if (!useLighting) {
if (useLighting == 0) {
lightWeighting = vec3(1.0, 1.0, 1.0);
} else {
vec3 lightDirection = normalize(point - vPosition );
Expand All @@ -492,7 +499,7 @@ fragmentShaderPF =
lightWeighting = ambientColour + pointColour * directionalLightWeighting;
}
vec4 fragmentColor;
if (useTextures) {
if (useTextures == 1) {
fragmentColor = texture2D(texture, vec2(vcoord.s, vcoord.t));
} else {
fragmentColor = vec4(1.0, 1.0, 1.0, 1.0);
Expand All @@ -504,7 +511,7 @@ fragmentShaderPF =
|]


vertexShaderPV : Shader { attr | position : Vec3, coord : Vec3, norm : Vec3 } { unif | worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, useLighting : Bool, pointColour : Vec3, ambientColour : Vec3, point : Vec3 } { vcoord : Vec2, lightWeighting : Vec3 }
vertexShaderPV : Shader { attr | position : Vec3, coord : Vec3, norm : Vec3 } { unif | worldSpace : Mat4, perspective : Mat4, camera : Mat4, normalMatrix : Mat4, useLighting : Int, pointColour : Vec3, ambientColour : Vec3, point : Vec3 } { vcoord : Vec2, lightWeighting : Vec3 }
vertexShaderPV =
[glsl|
Expand All @@ -518,7 +525,7 @@ vertexShaderPV =
uniform mat4 perspective;
uniform mat4 normalMatrix;
uniform mat4 camera;
uniform bool useLighting;
uniform int useLighting;
uniform vec3 pointColour;
uniform vec3 point;
uniform vec3 ambientColour;
Expand All @@ -531,7 +538,7 @@ vertexShaderPV =
gl_Position = perspective * camera * mvPosition;
vcoord = coord.xy;
if (!useLighting) {
if (useLighting == 0) {
lightWeighting = vec3(1.0, 1.0, 1.0);
} else {
vec3 transformedNormal = vec3(normalMatrix * vec4(norm, 0.0));
Expand All @@ -543,20 +550,20 @@ vertexShaderPV =
|]


fragmentShaderPV : Shader {} { unif | texture : Texture, useTextures : Bool } { vcoord : Vec2, lightWeighting : Vec3 }
fragmentShaderPV : Shader {} { unif | texture : Texture, useTextures : Int } { vcoord : Vec2, lightWeighting : Vec3 }
fragmentShaderPV =
[glsl|
precision mediump float;
uniform sampler2D texture;
uniform bool useTextures;
uniform int useTextures;
varying vec2 vcoord;
varying vec3 lightWeighting;
void main () {
vec4 fragmentColor;
if (useTextures) {
if (useTextures == 1) {
fragmentColor = texture2D(texture, vec2(vcoord.s, vcoord.t));
} else {
fragmentColor = vec4(1.0, 1.0, 1.0, 1.0);
Expand Down
Loading

0 comments on commit 203b33b

Please sign in to comment.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy