-
Notifications
You must be signed in to change notification settings - Fork 1
/
texture1.html
103 lines (91 loc) · 2.72 KB
/
texture1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>画贴图</title>
<script src="../libs/dat.gui.min.js"></script>
<script src="../libs/utils.js"></script>
<style>
#webgl,
body {
padding: 0;
margin: 0;
position: absolute;
height: 100%;
width: 100%;
}
</style>
</head>
<body onload="init()">
<canvas id="webgl"></canvas>
<script id="vertexShader" type="x-shader/x-vertex">
precision mediump float;
attribute vec2 aPosition;
attribute vec2 aTexCoord;
attribute vec2 resolution;//webgl宽高
varying vec2 vTexCoord;
void main() {
vec2 pos=aPosition/resolution;
gl_Position = vec4(pos.x*2.0-1.0,1.0-pos.y*2.0, 0.0, 1);
vTexCoord=aTexCoord;
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTexCoord;
uniform sampler2D uTex;
void main() {
gl_FragColor = texture2D(uTex, vTexCoord);
}
</script>
<script>
async function init() {
var gl = initGl('webgl');
const vs = document.getElementById('vertexShader').innerText;
const fs = document.getElementById('fragmentShader').innerText;
var program = initShaderProgram(gl, vs, fs);
const textureCoord = [
[0.0, 0.0],
[1.0, 0.0],
[0.0, 1.0],
[0.0, 1.0],
[1.0, 0.0],
[1.0, 1.0]
].reverse();
initArrBuffer(gl, 'aTexCoord', new Float32Array(flatArr(textureCoord)), 2);
var { texture, image } = await initTexture(gl, 'images/a.jpg');
function getImageRect(x, y, width, height) {
var x1 = x;
var x2 = x + width;
var y1 = y;
var y2 = y + height;
return [
[x1, y1],
[x2, y1],
[x1, y2],
[x1, y2],
[x2, y1],
[x2, y2]
].reverse();
}
const positions = getImageRect(0, 0, image.width, image.height);
initArrBuffer(gl, 'aPosition', new Float32Array(flatArr(positions)), 2);
gl.vertexAttrib2f(
gl.getAttribLocation(program, 'resolution'),
gl.canvas.width,
gl.canvas.height
);
//开启正反面
gl.enable(gl.CULL_FACE);
function drawScene() {
cleanGl(gl);
//画三角形
gl.drawArrays(gl.TRIANGLES, 0, positions.length);
}
drawScene();
}
</script>
</body>
</html>