|
| 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> |
0 commit comments