Skip to content

Commit 1ef5091

Browse files
committed
demo: new texture example
1 parent 429ffb2 commit 1ef5091

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<!--
2+
~ Copyright 2021 Kazimierz Pogoda
3+
~
4+
~ This file is part of shader-web-background.
5+
~
6+
~ shader-web-background is free software: you can redistribute it and/or modify
7+
~ it under the terms of the GNU General Public License as published by
8+
~ the Free Software Foundation, either version 3 of the License, or
9+
~ (at your option) any later version.
10+
~
11+
~ shader-web-background is distributed in the hope that it will be useful,
12+
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
~ GNU General Public License for more details.
15+
~
16+
~ You should have received a copy of the GNU General Public License
17+
~ along with shader-web-background. If not, see <https://www.gnu.org/licenses/>.
18+
-->
19+
20+
<!DOCTYPE html>
21+
<html lang="en">
22+
<head>
23+
<meta charset="utf-8">
24+
<title>texture: Blue Marble to Flat Earth mapping</title>
25+
<meta name="viewport" content="width=device-width, initial-scale=1">
26+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
27+
<link rel="stylesheet" href="demo.css"/>
28+
<script src="../src/test/js/show-demo-source.js"></script>
29+
30+
<script src="../dist/shader-web-background.min.js"></script>
31+
32+
<script type="x-shader/x-fragment" id="image">
33+
precision highp float;
34+
35+
uniform vec2 iResolution;
36+
uniform float iMinDimension;
37+
uniform float iTime;
38+
uniform float iRotationSpeed;
39+
uniform sampler2D iBlueMarble;
40+
41+
const float EDGE_SMOOTHING = .02;
42+
const float PI = 3.14159265359;
43+
44+
void main() {
45+
vec2 st = (2. * gl_FragCoord.xy - iResolution) / iMinDimension;
46+
float angle = atan(st.x, st.y);
47+
float distance = length(st);
48+
vec2 earthUv = vec2(
49+
mod(
50+
((angle + PI) / PI / 2.)
51+
+ (iTime * iRotationSpeed),
52+
1.
53+
),
54+
distance
55+
);
56+
vec4 earthColor = texture2D(iBlueMarble, earthUv);
57+
gl_FragColor = earthColor * smoothstep(1., 1. - EDGE_SMOOTHING, distance);
58+
}
59+
</script>
60+
<script>
61+
shaderWebBackground.shade({
62+
onInit: (ctx) => {
63+
const image = new Image();
64+
image.crossOrigin = "anonymous";
65+
image.src = "../media/nasa-blue-marble.jpg";
66+
image.onload = () => {
67+
const gl = ctx.gl;
68+
const texture = gl.createTexture();
69+
gl.bindTexture(gl.TEXTURE_2D, texture);
70+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
71+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
72+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
73+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
74+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
75+
gl.bindTexture(gl.TEXTURE_2D, null);
76+
ctx.iBlueMarble = texture;
77+
}
78+
},
79+
onResize: (width, height, ctx) => {
80+
ctx.minDimension = Math.min(width, height);
81+
},
82+
shaders: {
83+
image: {
84+
uniforms: {
85+
iResolution: (gl, loc, ctx) => gl.uniform2f(loc, ctx.width, ctx.height),
86+
iTime: (gl, loc, ctx) => gl.uniform1f(loc, performance.now() / 1000),
87+
iMinDimension: (gl, loc, ctx) => gl.uniform1f(loc, ctx.minDimension),
88+
iRotationSpeed: (gl, loc, ctx) => gl.uniform1f(loc, .1)
89+
iBlueMarble: (gl, loc, ctx) => ctx.texture(loc, ctx.iBlueMarble)
90+
}
91+
}
92+
}
93+
});
94+
</script>
95+
</head>
96+
<body>
97+
<header>
98+
<h1><a href="#source">texture: Blue Marble to Flat Earth mapping</a></h1>
99+
</header>
100+
</body>
101+
</html>

index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,11 @@ <h2>Demo</h2>
961961
<a href="demo/mouse-normalized.html">mouse normalized</a> &mdash; slight modification
962962
of the mouse example to avoid normalization of the same mouse coordinates for each pixel.
963963
</li>
964+
<li>
965+
<a href="demo/texture-blue-marble-to-flat-earth.html">
966+
texture: Blue Marble to Flat Earth mapping
967+
</a> &mdash; how to load a texture.
968+
</li>
964969
<li>
965970
<a href="demo/multiple-shaders.html">multiple shaders</a> &mdash; how to use multiple
966971
shaders with multiple canvases?

media/nasa-blue-marble.jpg

241 KB
Loading

0 commit comments

Comments
 (0)