a * x + b * y
ax+by
和ex+fy
x' = a * x + b * y
y' = e * x + f * y
bx = cosβ * ax - sinβ * ay
by = sinβ * ax + cosβ * ay
a = cosβ
b = -sinβ
e = sinβ
f = cosβ
a * x + b * y = cosβ * ax - sinβ * ay
e * x + f * y = sinβ *ax + cosβ * ay
1 ) 核心代码
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec4 a_Position;
float angle = radians(40.0);
float sinB = sin(angle);
float cosB = cos(angle);
mat2 m2 = mat2(
cosB, sinB,
-sinB, cosB
);
void main() {
gl_Position = vec4(
m2 * vec2(a_Position),
a_Position.z, a_Position.w
);
}
script>
2 )完整代码
<canvas id="canvas">canvas>
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec4 a_Position;
float angle = radians(40.0);
float cosB = cos(angle);
float sinB = sin(angle);
// 列主序
mat2 m2 = mat2(
cosB, sinB,
-sinB, cosB
);
void main() {
gl_Position = vec4(
m2 * vec2(a_Position),
a_Position.z, a_Position.w
);
}
script>
<script id="fragmentShader" type="x-shader/x-fragment">
void main() {
gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
}
script>
<script type="module">
import { initShaders } from './utils.js';
const canvas = document.getElementById('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const gl = canvas.getContext('webgl');
const vsSource = document.getElementById('vertexShader').innerText;
const fsSource = document.getElementById('fragmentShader').innerText;
initShaders(gl, vsSource, fsSource);
const vertices = new Float32Array([
0.0, 0.1,
-0.1, -0.1,
0.1, -0.1
])
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
const a_Position = gl.getAttribLocation(gl.program, 'a_Position');
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(a_Position);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
script>
1 )核心代码
在顶点着色器中建立uniform变量
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec4 a_Position;
uniform mat2 u_Matrix;
void main() {
gl_Position = vec4(
u_Matrix * vec2(a_Position),
a_Position.z, a_Position.w
);
}
script>
获取并修改uniform 变量
const u_Matrix = gl.getUniformLocation(gl.program, 'u_Matrix');
let angle = 0.2;
const [sinB, cosB] = [Math.sin(angle), Math.cos(angle)];
const matrix = [
cosB, sinB,
-sinB, cosB
];
gl.uniformMatrix2fv(u_Matrix, false, matrix);
后面我们也可以在其中添加动画
const u_Matrix = gl.getUniformLocation(gl.program, 'u_Matrix');
let angle = 0.2;
!(function animate() {
angle += 0.02;
const [sinB, cosB] = [Math.sin(angle), Math.cos(angle)];
const matrix = [
cosB, sinB,
-sinB, cosB
];
gl.uniformMatrix2fv(u_Matrix, false, matrix);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
requestAnimationFrame(animate)
})()
2 )完整代码
<canvas id="canvas">canvas>
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec4 a_Position;
// 列主序
uniform mat2 u_Matrix;
void main() {
gl_Position = vec4(
u_Matrix * vec2(a_Position),
a_Position.z,a_Position.w
);
}
script>
<script id="fragmentShader" type="x-shader/x-fragment">
void main() {
gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
}
script>
<script type="module">
import { initShaders } from './utils.js';
const canvas = document.getElementById('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const gl = canvas.getContext('webgl');
const vsSource = document.getElementById('vertexShader').innerText;
const fsSource = document.getElementById('fragmentShader').innerText;
initShaders(gl, vsSource, fsSource);
const vertices = new Float32Array([
0.0, 0.1,
-0.1, -0.1,
0.1, -0.1
]);
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
const a_Position = gl.getAttribLocation(gl.program, 'a_Position');
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(a_Position);
const u_Matrix = gl.getUniformLocation(gl.program, 'u_Matrix');
let angle = 0.5;
const sinB = Math.sin(angle);
const cosB = Math.cos(angle);
const matrix = [
cosB, sinB,
-sinB, cosB
];
gl.uniformMatrix2fv(u_Matrix, false, matrix);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
!(function animate() {
angle += 0.05;
const sinB = Math.sin(angle);
const cosB = Math.cos(angle);
const matrix = [
cosB, sinB,
-sinB, cosB
];
gl.uniformMatrix2fv(u_Matrix, false, matrix);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
requestAnimationFrame(animate);
})()
script>
1 )核心代码
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec4 a_Position;
float angle = radians(10.0);
float cosB = cos(angle);
float sinB = sin(angle);
mat4 m4 = mat4(
cosB, sinB, 0.0, 0.0,
-sinB, cosB, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
);
void main() {
gl_Position = m4 * a_Position;
}
script>
2 )完整代码
<canvas id="canvas">canvas>
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec4 a_Position;
float angle = radians(10.0);
float cosB = cos(angle);
float sinB = sin(angle);
// 列主序
mat4 m4 = mat4(
cosB, sinB, 0.0, 0.0,
-sinB, cosB, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
);
void main() {
gl_Position = m4 * a_Position;
}
script>
<script id="fragmentShader" type="x-shader/x-fragment">
void main() {
gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
}
script>
<script type="module">
import { initShaders } from './utils.js';
const canvas = document.getElementById('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const gl = canvas.getContext('webgl');
const vsSource = document.getElementById('vertexShader').innerText;
const fsSource = document.getElementById('fragmentShader').innerText;
initShaders(gl, vsSource, fsSource);
const vertices = new Float32Array([
0.0, 0.1,
-0.1, -0.1,
0.1, -0.1
])
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
const a_Position = gl.getAttribLocation(gl.program, 'a_Position');
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(a_Position);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
script>
1 )核心代码
const u_Matrix = gl.getUniformLocation(gl.program, 'u_Matrix');
let angle = 0.1;
const [sinB, cosB] = [Math.sin(angle), Math.cos(angle)];
const matrix = [
cosB, sinB, 0.0, 0.0,
-sinB, cosB, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
];
gl.uniformMatrix4fv(u_Matrix, false, matrix);
2 )完整代码
<canvas id="canvas">canvas>
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec4 a_Position;
// 列主序
uniform mat4 u_Matrix;
void main() {
gl_Position = u_Matrix * a_Position;
}
script>
<script id="fragmentShader" type="x-shader/x-fragment">
void main() {
gl_FragColor = vec4(1.0,1.0,0.0,1.0);
}
script>
<script type="module">
import { initShaders } from './utils.js';
const canvas = document.getElementById('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const gl = canvas.getContext('webgl');
const vsSource = document.getElementById('vertexShader').innerText;
const fsSource = document.getElementById('fragmentShader').innerText;
initShaders(gl, vsSource, fsSource);
const vertices = new Float32Array([
0.0, 0.1,
-0.1, -0.1,
0.1, -0.1
]);
const vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
const a_Position = gl.getAttribLocation(gl.program, 'a_Position');
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(a_Position);
const u_Matrix = gl.getUniformLocation(gl.program, 'u_Matrix');
let angle = 0.5;
const sinB = Math.sin(angle);
const cosB = Math.cos(angle);
const matrix = [
cosB, sinB, 0, 0,
-sinB, cosB, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
];
gl.uniformMatrix4fv(u_Matrix, false, matrix);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
!(function animate() {
angle += 0.05;
const sinB = Math.sin(angle);
const cosB = Math.cos(angle);
const matrix = [
cosB, sinB, 0, 0,
-sinB, cosB, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
];
gl.uniformMatrix4fv(u_Matrix, false, matrix);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, 3);
requestAnimationFrame(animate);
})()
script>