-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade shaders to es 300 (WebGL2 only)
- Loading branch information
Showing
20 changed files
with
179 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Threeify - Getting Started - 7 Metallic Roughness</title> | ||
<script type="module" src="/dist/examples/gettingstarted/7_metallicRoughness/index.js"></script> | ||
</head> | ||
<body></body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
precision highp float; | ||
|
||
varying vec3 v_color; | ||
in vec3 v_color; | ||
|
||
out vec4 fragColor; | ||
|
||
void main() { | ||
|
||
gl_FragColor = vec4(v_color, 1.0); | ||
fragColor = vec4(v_color, 1.0); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
attribute vec2 position; | ||
in vec2 position; | ||
|
||
uniform float scale; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
precision highp float; | ||
|
||
in vec2 v_texCoord; | ||
|
||
uniform sampler2D map; | ||
|
||
varying vec2 v_texCoord; | ||
out vec4 fragColor; | ||
|
||
void main() { | ||
|
||
gl_FragColor = texture2D(map, v_texCoord); | ||
fragColor = texture(map, v_texCoord); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
precision highp float; | ||
|
||
in vec3 v_viewPosition; | ||
in vec3 v_viewNormal; | ||
in vec2 v_uv; | ||
|
||
uniform sampler2D map; | ||
uniform vec3 viewLightPosition; | ||
|
||
varying vec3 v_viewPosition; | ||
varying vec3 v_viewNormal; | ||
varying vec2 v_uv; | ||
out vec4 fragColor; | ||
|
||
void main() { | ||
|
||
vec3 albedo = texture2D(map, v_uv).xyz; | ||
vec3 albedo = texture(map, v_uv).xyz; | ||
vec3 directionToLight = normalize( viewLightPosition - v_viewPosition ); | ||
float lambertianIntensity = dot( directionToLight, v_viewNormal ); | ||
|
||
gl_FragColor = vec4( albedo * lambertianIntensity, 1.0 ); | ||
fragColor = vec4( albedo * lambertianIntensity, 1.0 ); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
src/examples/gettingstarted/5_reflectivePolyhedral/fragment.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
precision highp float; | ||
|
||
in vec3 v_viewPosition; | ||
in vec3 v_viewNormal; | ||
|
||
uniform samplerCube cubeMap; | ||
|
||
varying vec3 v_viewPosition; | ||
varying vec3 v_viewNormal; | ||
out vec4 fragColor; | ||
|
||
void main() { | ||
|
||
vec3 reflectDir = reflect( normalize( v_viewPosition ),normalize(v_viewNormal) ); | ||
gl_FragColor = textureCube(cubeMap, reflectDir); | ||
fragColor = texture(cubeMap, reflectDir); | ||
|
||
} |
10 changes: 5 additions & 5 deletions
10
src/examples/gettingstarted/5_reflectivePolyhedral/vertex.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
src/examples/gettingstarted/6_interleavedBuffers/fragment.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
precision highp float; | ||
|
||
varying vec3 v_color; | ||
in vec3 v_color; | ||
|
||
out vec4 fragColor; | ||
|
||
void main() { | ||
|
||
gl_FragColor = vec4(v_color, 1.0); | ||
fragColor = vec4(v_color, 1.0); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/examples/gettingstarted/7_metallicRoughness/fragment.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
precision highp float; | ||
|
||
in vec3 v_viewPosition; | ||
in vec3 v_viewNormal; | ||
|
||
uniform samplerCube cubeMap; | ||
|
||
out vec4 fragColor; | ||
|
||
void main() { | ||
|
||
vec3 reflectDir = reflect( normalize( v_viewPosition ),normalize(v_viewNormal) ); | ||
fragColor = texture(cubeMap, reflectDir); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { convertToInterleavedGeometry } from "../../../lib/geometry/Geometry.Functions"; | ||
import { icosahedron } from "../../../lib/geometry/primitives/Polyhedrons"; | ||
import { fetchImage } from "../../../lib/io/loaders/Image"; | ||
import { ShaderMaterial } from "../../../lib/materials/ShaderMaterial"; | ||
import { Euler } from "../../../lib/math/Euler"; | ||
import { Matrix4 } from "../../../lib/math/Matrix4"; | ||
import { | ||
makeMatrix4Perspective, | ||
makeMatrix4RotationFromEuler, | ||
makeMatrix4Translation, | ||
} from "../../../lib/math/Matrix4.Functions"; | ||
import { Vector3 } from "../../../lib/math/Vector3"; | ||
import { BufferGeometry } from "../../../lib/renderers/webgl2/buffers/BufferGeometry"; | ||
import { DepthTestFunc, DepthTestState } from "../../../lib/renderers/webgl2/DepthTestState"; | ||
import { Program } from "../../../lib/renderers/webgl2/programs/Program"; | ||
import { RenderingContext } from "../../../lib/renderers/webgl2/RenderingContext"; | ||
import { TexImage2D } from "../../../lib/renderers/webgl2/textures/TexImage2D"; | ||
import { CubeTexture } from "../../../lib/textures/CubeTexture"; | ||
import fragmentSourceCode from "./fragment.glsl"; | ||
import vertexSourceCode from "./vertex.glsl"; | ||
|
||
async function init(): Promise<null> { | ||
const geometry = convertToInterleavedGeometry(icosahedron(0.75, 3)); | ||
const material = new ShaderMaterial(vertexSourceCode, fragmentSourceCode); | ||
const cubeTexture = new CubeTexture([ | ||
await fetchImage("/assets/textures/cube/pisa/px.png"), | ||
await fetchImage("/assets/textures/cube/pisa/nx.png"), | ||
await fetchImage("/assets/textures/cube/pisa/py.png"), | ||
await fetchImage("/assets/textures/cube/pisa/ny.png"), | ||
await fetchImage("/assets/textures/cube/pisa/pz.png"), | ||
await fetchImage("/assets/textures/cube/pisa/nz.png"), | ||
]); | ||
|
||
const context = new RenderingContext(); | ||
const canvasFramebuffer = context.canvasFramebuffer; | ||
document.body.appendChild(canvasFramebuffer.canvas); | ||
|
||
const program = new Program(context, material); | ||
const uniforms = { | ||
localToWorld: new Matrix4(), | ||
worldToView: makeMatrix4Translation(new Matrix4(), new Vector3(0, 0, -1)), | ||
viewToScreen: makeMatrix4Perspective(new Matrix4(), -0.25, 0.25, 0.25, -0.25, 0.1, 4.0), | ||
roughnessFactor: 0, | ||
cubeMap: new TexImage2D(context, cubeTexture), | ||
}; | ||
const bufferGeometry = new BufferGeometry(context, geometry); | ||
const depthTestState = new DepthTestState(true, DepthTestFunc.Less); | ||
|
||
function animate(): void { | ||
requestAnimationFrame(animate); | ||
|
||
const now = Date.now(); | ||
uniforms.localToWorld = makeMatrix4RotationFromEuler( | ||
uniforms.localToWorld, | ||
new Euler(now * 0.0001, now * 0.00033, now * 0.000077), | ||
); | ||
uniforms.roughnessFactor = Math.sin(now * 0.0001) * 0.5 + 0.5; | ||
canvasFramebuffer.renderBufferGeometry(program, uniforms, bufferGeometry, depthTestState); | ||
} | ||
|
||
animate(); | ||
|
||
return null; | ||
} | ||
|
||
init(); |
17 changes: 17 additions & 0 deletions
17
src/examples/gettingstarted/7_metallicRoughness/vertex.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
in vec3 position; | ||
in vec3 normal; | ||
|
||
uniform mat4 localToWorld; | ||
uniform mat4 worldToView; | ||
uniform mat4 viewToScreen; | ||
|
||
out vec3 v_viewPosition; | ||
out vec3 v_viewNormal; | ||
|
||
void main() { | ||
|
||
v_viewNormal = normalize( ( worldToView * localToWorld * vec4( normal, 0.0 ) ).xyz ); | ||
v_viewPosition = ( worldToView * localToWorld * vec4( position, 1.0 ) ).xyz; | ||
gl_Position = viewToScreen * vec4( v_viewPosition, 1.0 ); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters