这里有个单位圆。
当你拖拽蓝色圆点的时候 X 和 Y 会改变,它们是那一点在圆上的坐标, 在最上方时 Y 是 1 并且 X 是 0 ,在最右边的时候 X 是 1 并且 Y 是 0 。
数字和 1 相乘结果不变。 例如 123 * 1 = 123 。非常基础,对吧?那么,单位圆,半径为 1.0 的圆也是 1 的一种形式,它是旋转的 1 。所以你可以把一些东西和单位圆相乘, 除了发生一些魔法和旋转之外,某种程度上和乘以 1 相似。
一.核心代码
<canvas id="canvas"></canvas><div id="uiContainer"><div id="ui"><div id="x"></div><div id="y"></div><div id="rotation"></div></div></div><!-- vertex shader --><script id="vertex-shader-2d" type="x-shader/x-vertex">attribute vec2 a_position;uniform vec2 u_resolution;uniform vec2 u_translation;uniform vec2 u_rotation;void main() {// Rotate the positionvec2 rotatedPosition = vec2(a_position.x * u_rotation.y + a_position.y * u_rotation.x,a_position.y * u_rotation.y - a_position.x * u_rotation.x);// Add in the translation.vec2 position = rotatedPosition + u_translation;// convert the position from pixels to 0.0 to 1.0vec2 zeroToOne = position / u_resolution;// convert from 0->1 to 0->2vec2 zeroToTwo = zeroToOne * 2.0;// convert from 0->2 to -1->+1 (clipspace)vec2 clipSpace = zeroToTwo - 1.0;gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);}</script><!-- fragment shader --><script id="fragment-shader-2d" type="x-shader/x-fragment">precision mediump float;uniform vec4 u_color;void main() {gl_FragColor = u_color;}</script><script src="https://webglfundamentals.org/webgl/resources/jquery-1.7.1.min.js"></script><script src="https://webglfundamentals.org/webgl/resources/jquery-ui-1.8.16.custom.min.js"></script><script src="https://webglfundamentals.org/webgl/resources/jquery.mousecapture.js"></script><script src="https://webglfundamentals.org/webgl/resources/jquery.gman.ui.js"></script><script src="https://webglfundamentals.org/webgl/resources/jquery-gman-circle.js"></script><script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script><script src="https://webglfundamentals.org/webgl/resources/webgl-lessons-ui.js"></script>
核心代码块
a_position.x u_rotation.y + a_position.y u_rotation.x,
a_position.y u_rotation.y - a_position.x u_rotation.x
假如你想旋转一个矩形,在开始旋转之前矩形右上角坐标是 3.0, 9.0 , 让我们在单位圆上以十二点方向为起点顺时针旋转30度后取一个点。
圆上该点的位置是 0.50 和 0.87
3.0 0.87 + 9.0 0.50 = 7.1
9.0 0.87 - 3.0 0.50 = 6.3
这个结果正好是我们需要的结果
你会发现在我们顺时针旋转到右边的过程中,X 变大 Y 变小。如果我们继续旋转超过90 度后,X 变小 Y 变大,这种形式形成了旋转。
单位圆上的点还有一个名字,叫做正弦和余弦。所以对于任意给定角, 我们只需要求出正弦和余弦,像这样
function printSineAndCosineForAnAngle(angleInDegrees) {var angleInRadians = angleInDegrees * Math.PI / 180;var s = Math.sin(angleInRadians);var c = Math.cos(angleInRadians);console.log("s = " + s + " c = " + c);}
二.全部代码
"use strict";function main() {// Get A WebGL context/** @type {HTMLCanvasElement} */var canvas = document.querySelector("#canvas");var gl = canvas.getContext("webgl");if (!gl) {return;}// setup GLSL programvar program = webglUtils.createProgramFromScripts(gl, ["vertex-shader-2d", "fragment-shader-2d"]);// look up where the vertex data needs to go.var positionLocation = gl.getAttribLocation(program, "a_position");// lookup uniformsvar resolutionLocation = gl.getUniformLocation(program, "u_resolution");var colorLocation = gl.getUniformLocation(program, "u_color");var translationLocation = gl.getUniformLocation(program, "u_translation");var rotationLocation = gl.getUniformLocation(program, "u_rotation");// Create a buffer to put positions invar positionBuffer = gl.createBuffer();// Bind it to ARRAY_BUFFER (think of it as ARRAY_BUFFER = positionBuffer)gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);// Put geometry data into buffersetGeometry(gl);var translation = [100, 150];var rotation = [0, 1];var color = [Math.random(), Math.random(), Math.random(), 1];drawScene();// Setup a ui.webglLessonsUI.setupSlider("#x", {value: translation[0], slide: updatePosition(0), max: gl.canvas.width });webglLessonsUI.setupSlider("#y", {value: translation[1], slide: updatePosition(1), max: gl.canvas.height});webglLessonsUI.setupSlider("#angle", {slide: updateAngle, max: 360});function updatePosition(index) {return function(event, ui) {translation[index] = ui.value;drawScene();};}function updateAngle(event, ui) {var angleInDegrees = 360 - ui.value;var angleInRadians = angleInDegrees * Math.PI / 180;rotation[0] = Math.sin(angleInRadians);rotation[1] = Math.cos(angleInRadians);drawScene();}// Draw the scene.function drawScene() {webglUtils.resizeCanvasToDisplaySize(gl.canvas);// Tell WebGL how to convert from clip space to pixelsgl.viewport(0, 0, gl.canvas.width, gl.canvas.height);// Clear the canvas.gl.clear(gl.COLOR_BUFFER_BIT);// Tell it to use our program (pair of shaders)gl.useProgram(program);// Turn on the attributegl.enableVertexAttribArray(positionLocation);// Bind the position buffer.gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);// Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)var size = 2; // 2 components per iterationvar type = gl.FLOAT; // the data is 32bit floatsvar normalize = false; // don't normalize the datavar stride = 0; // 0 = move forward size * sizeof(type) each iteration to get the next positionvar offset = 0; // start at the beginning of the buffergl.vertexAttribPointer(positionLocation, size, type, normalize, stride, offset);// set the resolutiongl.uniform2f(resolutionLocation, gl.canvas.width, gl.canvas.height);// set the colorgl.uniform4fv(colorLocation, color);// Set the translation.gl.uniform2fv(translationLocation, translation);// Set the rotation.gl.uniform2fv(rotationLocation, rotation);// Draw the geometry.var primitiveType = gl.TRIANGLES;var offset = 0;var count = 18; // 6 triangles in the 'F', 3 points per trianglegl.drawArrays(primitiveType, offset, count);}}// Fill the buffer with the values that define a letter 'F'.function setGeometry(gl) {gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([// left column0, 0,30, 0,0, 150,0, 150,30, 0,30, 150,// top rung30, 0,100, 0,30, 30,30, 30,100, 0,100, 30,// middle rung30, 60,67, 60,30, 90,30, 90,67, 60,67, 90,]),gl.STATIC_DRAW);}main();// html<canvas id="canvas"></canvas><div id="uiContainer"><div id="ui"><div id="x"></div><div id="y"></div><div id="angle"></div></div></div><!-- vertex shader --><script id="vertex-shader-2d" type="x-shader/x-vertex">attribute vec2 a_position;uniform vec2 u_resolution;uniform vec2 u_translation;uniform vec2 u_rotation;void main() {// Rotate the positionvec2 rotatedPosition = vec2(a_position.x * u_rotation.y + a_position.y * u_rotation.x,a_position.y * u_rotation.y - a_position.x * u_rotation.x);// Add in the translation.vec2 position = rotatedPosition + u_translation;// convert the position from pixels to 0.0 to 1.0vec2 zeroToOne = position / u_resolution;// convert from 0->1 to 0->2vec2 zeroToTwo = zeroToOne * 2.0;// convert from 0->2 to -1->+1 (clipspace)vec2 clipSpace = zeroToTwo - 1.0;gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);}</script><!-- fragment shader --><script id="fragment-shader-2d" type="x-shader/x-fragment">precision mediump float;uniform vec4 u_color;void main() {gl_FragColor = u_color;}</script><script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script><script src="https://webglfundamentals.org/webgl/resources/webgl-lessons-ui.js"></script>
