|
| 1 | +/* |
| 2 | + Copyright 2017 Google Inc. All Rights Reserved. |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + Unless required by applicable law or agreed to in writing, software |
| 8 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + See the License for the specific language governing permissions and |
| 11 | + limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +const image = new Image(); |
| 15 | +image.src = 'image.jpg'; |
| 16 | +image.onload = function () { |
| 17 | + const canvas = document.createElement('canvas'); |
| 18 | + document.body.appendChild(canvas); |
| 19 | + |
| 20 | + canvas.width = image.naturalWidth; |
| 21 | + canvas.height = image.naturalHeight; |
| 22 | + |
| 23 | + const gl = canvas.getContext('webgl'); |
| 24 | + |
| 25 | + gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); |
| 26 | + gl.clearColor(1.0, 0.8, 0.1, 1.0); |
| 27 | + gl.clear(gl.COLOR_BUFFER_BIT); |
| 28 | + |
| 29 | + const vertShaderSource = ` |
| 30 | + attribute vec2 position; |
| 31 | +
|
| 32 | + varying vec2 texCoords; |
| 33 | +
|
| 34 | + void main() { |
| 35 | + texCoords = (position + 1.0) / 2.0; |
| 36 | + texCoords.y = 1.0 - texCoords.y; |
| 37 | + gl_Position = vec4(position, 0, 1.0); |
| 38 | + } |
| 39 | + `; |
| 40 | + |
| 41 | + const fragShaderSource = ` |
| 42 | + precision highp float; |
| 43 | +
|
| 44 | + varying vec2 texCoords; |
| 45 | +
|
| 46 | + uniform sampler2D textureSampler; |
| 47 | +
|
| 48 | + void main() { |
| 49 | + float warmth = -0.2; |
| 50 | + float brightness = 0.2; |
| 51 | +
|
| 52 | + vec4 color = texture2D(textureSampler, texCoords); |
| 53 | +
|
| 54 | + color.r += warmth; |
| 55 | + color.b -= warmth; |
| 56 | +
|
| 57 | + color.rgb += brightness; |
| 58 | +
|
| 59 | + gl_FragColor = color; |
| 60 | + } |
| 61 | + `; |
| 62 | + |
| 63 | + const vertShader = gl.createShader(gl.VERTEX_SHADER); |
| 64 | + const fragShader = gl.createShader(gl.FRAGMENT_SHADER); |
| 65 | + |
| 66 | + gl.shaderSource(vertShader, vertShaderSource); |
| 67 | + gl.shaderSource(fragShader, fragShaderSource); |
| 68 | + |
| 69 | + gl.compileShader(vertShader); |
| 70 | + gl.compileShader(fragShader); |
| 71 | + |
| 72 | + const program = gl.createProgram(); |
| 73 | + gl.attachShader(program, vertShader); |
| 74 | + gl.attachShader(program, fragShader); |
| 75 | + |
| 76 | + gl.linkProgram(program); |
| 77 | + |
| 78 | + gl.useProgram(program); |
| 79 | + |
| 80 | + const vertices = new Float32Array([ |
| 81 | + -1, -1, |
| 82 | + -1, 1, |
| 83 | + 1, 1, |
| 84 | + |
| 85 | + -1, -1, |
| 86 | + 1, 1, |
| 87 | + 1, -1, |
| 88 | + ]); |
| 89 | + |
| 90 | + const vertexBuffer = gl.createBuffer(); |
| 91 | + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); |
| 92 | + gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); |
| 93 | + |
| 94 | + const positionLocation = gl.getAttribLocation(program, 'position'); |
| 95 | + |
| 96 | + gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0); |
| 97 | + gl.enableVertexAttribArray(positionLocation); |
| 98 | + |
| 99 | + const texture = gl.createTexture(); |
| 100 | + gl.activeTexture(gl.TEXTURE0); |
| 101 | + gl.bindTexture(gl.TEXTURE_2D, texture); |
| 102 | + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image); |
| 103 | + |
| 104 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
| 105 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
| 106 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); |
| 107 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); |
| 108 | + |
| 109 | + gl.drawArrays(gl.TRIANGLES, 0, 6); |
| 110 | +}; |
0 commit comments