2024年全网最新最详细《WebGL教程》

news/2024/12/14 15:38:22/文章来源:https://www.cnblogs.com/KooTeam/p/18606822

2024年全网最新最详细《WebGL教程》

https://www.bilibili.com/video/BV1AS411A74T/

1 2

02-canvas的基础知识

https://bx2eq6ulg18.feishu.cn/docx/I4hTdO95qozPBOxauPlcbDfInVd

2.1 获取渲染上下文
HTMLCanvasElement.getContext()方法用于返回绘图上下文对象,绘图上下文对象是2D上下文还是3D上下文取决于传入的参数。
getContext('2d'):创建一个CanvasRenderingContext2D 二维的渲染上下文对象getContext('webgl'):创建一个 WebGLRenderingContext 三维的渲染上下文对象

css图片会拉伸 标签属性width不会

03-了解类型化数组

作用:常用在处理图像数据、音频数据、视频数据这些方向。对应的API有:WebGL、Canvas API、WebRTC、File

04-着色器基础语法

GLSL(OpenGL Shading Language)是一种用于编写图形着色器的编程语言。着色器是用于在图形处理单元
(GPU)上执行特定图形处理任务的程序。通俗讲,着色器是画点的工具,一个图形是由无数个点组成的,每个点都有其自己的颜色。

变量修饰符:
用于指定变量的作用域、生命周期和用途。不同的修饰符在顶点着色器和片段着色器中起到不同的作用。
attribute:用于顶点着色器,定义从顶点缓冲区传入的变量(仅在顶点着色器中使用)。
uniform:定义在整个渲染过程中保持不变的变量,常用于传递变换矩阵、光照参数等。
varying:用于在顶点着色器和片段着色器之间传递插值数据。
内罟变量:

05-webGL接口api

06-第一个WebGL程序

另类script 需要加;才行

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">void main(){gl_PointSize=10.0;gl_Position=vec4(0.0,0.0,0.0,1.0);}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">void main(){gl_FragColor=vec4(1.0,0.0,0.0,1.0);}</script><script>// const vertexSource=`//     void main(){//         gl_PointSize=10.0;//         gl_Position=vec4(0.0,0.0,0.0,1.0)//     }// `const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);gl.drawArrays(gl.POINTS, 0, 1);</script>
</body>
</html>

07-attribute动态传递点数据

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec2 aPos;attribute vec2 aPos1;void main(){gl_PointSize=10.0;vec2 newPos=aPos+aPos1;gl_Position=vec4(newPos,0.0,1.0);}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">void main(){gl_FragColor=vec4(1.0,0.0,0.0,1.0);}</script><script>// const vertexSource=`//     void main(){//         gl_PointSize=10.0;//         gl_Position=vec4(0.0,0.0,0.0,1.0)//     }// `const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);//动态传数据 坐标const posLocation=gl.getAttribLocation(program, 'aPos')const posLocation1=gl.getAttribLocation(program, 'aPos1')const offset=[0.5,0.5]const offset1=[-0.1,-0.2]gl.vertexAttrib2f(posLocation,...offset)gl.vertexAttrib2f(posLocation1,...offset1)gl.drawArrays(gl.POINTS, 0, 1);</script>
</body>
</html>

08-使用缓冲区数据绘制线段

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec2 aPosition;void main(){gl_Position=vec4(aPosition,0.0,1.0);}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">void main(){gl_FragColor=vec4(1.0,0.0,0.0,1.0);}</script><script>const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([-0.5,-0.5,  //第一个点的x轴和y轴坐标0.5,0.5,    //第二个点的xy轴坐标])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 2, gl.FLOAT, false, 2*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)gl.drawArrays(gl.LINES, 0, 2);</script>
</body>
</html>

09-varying实现彩色线段

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec2 aPosition;attribute vec4 aColor;varying vec4 vColor;void main(){gl_Position=vec4(aPosition,0.0,1.0);vColor=aColor;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">precision highp float;varying vec4 vColor;void main(){gl_FragColor=vColor;}</script><script>const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([-0.5,-0.5,  //第一个点的x轴和y轴坐标0.5,0.5,    //第二个点的xy轴坐标])const colors=new Float32Array([0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 2, gl.FLOAT, false, 2*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)const colorBuffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer)gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW)const colorLocation=gl.getAttribLocation(program, 'aColor')gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 2*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(colorLocation)gl.drawArrays(gl.LINES, 0, 2);</script>
</body>
</html>

10-如何使用单个buffer解析数据

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec2 aPosition;attribute vec4 aColor;varying vec4 vColor;void main(){gl_Position=vec4(aPosition,0.0,1.0);vColor=aColor;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">precision highp float;varying vec4 vColor;void main(){gl_FragColor=vColor;}</script><script>const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([-0.5,-0.5, 0.0,0.0,0.0,1.0, //第一个点的x轴和y轴坐标0.5,0.5, 1.0,0.0,0.0,0.0,    //第二个点的xy轴坐标])// const colors=new Float32Array([//     0.0,0.0,0.0,1.0,//     1.0,0.0,0.0,0.0,// ])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 2, gl.FLOAT, false, 6*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)// const colorBuffer=gl.createBuffer()// gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer)// gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW)const colorLocation=gl.getAttribLocation(program, 'aColor')gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 6*Float32Array.BYTES_PER_ELEMENT, 2*Float32Array.BYTES_PER_ELEMENT)gl.enableVertexAttribArray(colorLocation)gl.drawArrays(gl.LINES, 0, 2);</script>
</body>
</html>

11-绘制三角形案例

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec2 aPosition;attribute vec4 aColor;varying vec4 vColor;void main(){gl_Position=vec4(aPosition,0.0,1.0);vColor=aColor;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">precision highp float;varying vec4 vColor;void main(){gl_FragColor=vColor;}</script><script>const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([-0.5,-0.5, 0.0,0.0,0.0,1.0, //第一个点的x轴和y轴坐标 前2坐标 后4颜色0.5,-0.5, 1.0,0.0,0.0,0.0,    //第二个点的xy轴坐标0.0,0.5, 0.0,0.0,1.0,0.0,  //3 point])// const colors=new Float32Array([//     0.0,0.0,0.0,1.0,//     1.0,0.0,0.0,0.0,// ])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 2, gl.FLOAT, false, 6*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)// const colorBuffer=gl.createBuffer()// gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer)// gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW)const colorLocation=gl.getAttribLocation(program, 'aColor')gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 6*Float32Array.BYTES_PER_ELEMENT, 2*Float32Array.BYTES_PER_ELEMENT)gl.enableVertexAttribArray(colorLocation)gl.drawArrays(gl.TRIANGLES, 0, 3);</script>
</body>
</html>

12-图元绘制类型详解

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec2 aPosition;attribute vec4 aColor;varying vec4 vColor;void main(){gl_Position=vec4(aPosition,0.0,1.0);gl_PointSize=10.0;vColor=aColor;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">precision highp float;varying vec4 vColor;void main(){gl_FragColor=vColor;}</script><script>const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([-1.0,1.0, 0.0,0.0,0.0,1.0, //第一个点的x轴和y轴坐标 前2坐标 后4颜色-0.5,-1.0, 1.0,0.0,0.0,0.0,    //第二个点的xy轴坐标-0.5,1.0, 0.0,0.0,1.0,0.0,  //3 point0.0,-1.0, 0.0,0.0,1.0,0.0,  //3 point0.0,1.0, 0.0,0.0,1.0,0.0,  //3 point0.5,-1.0, 0.0,0.0,1.0,0.0,  //3 point])// const colors=new Float32Array([//     0.0,0.0,0.0,1.0,//     1.0,0.0,0.0,0.0,// ])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 2, gl.FLOAT, false, 6*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)// const colorBuffer=gl.createBuffer()// gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer)// gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW)const colorLocation=gl.getAttribLocation(program, 'aColor')gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 6*Float32Array.BYTES_PER_ELEMENT, 2*Float32Array.BYTES_PER_ELEMENT)gl.enableVertexAttribArray(colorLocation)gl.drawArrays(gl.POINTS, 0, 6);gl.drawArrays(gl.LINE_LOOP, 0, 6);gl.drawArrays(gl.TRIANGLES, 0, 6);</script>
</body>
</html>

13-uniform传递矩阵变换数据

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec2 aPosition;uniform vec2 uTranslate;void main(){vec2 newPos=uTranslate+aPosition;gl_Position=vec4(newPos,0.0,1.0);gl_PointSize=10.0;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">void main(){gl_FragColor=vec4(1.0,0.0,0.0,1.0);}</script><script>const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([-0.5,0.5,-0.5,-0.5,0.5,-0.5,0.5,0.5,])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 2, gl.FLOAT, false, 2*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)const translationLocation=gl.getUniformLocation(program,'uTranslate')const translation=[0.2,0.2]gl.uniform2fv(translationLocation,translation)gl.drawArrays(gl.POINTS, 0, 4);gl.drawArrays(gl.LINE_LOOP, 0, 4);</script>
</body>
</html>

14-推导旋转矩阵

scale *

一行 矩阵 行向量

三角形 cos sin tan 等知识换算

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec2 aPosition;void main(){float radian=radians(45.0);float sin=sin(radian);float cos=cos(radian);mat4 roatate=mat4(cos,-sin,0,0,sin,cos,0,0,0,0,1,0,0,0,0,1);gl_Position=roatate * vec4(aPosition,0.0,1.0);gl_PointSize=10.0;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">void main(){gl_FragColor=vec4(1.0,0.0,0.0,1.0);}</script><script>const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([-0.5,0.5,-0.5,-0.5,0.5,-0.5,0.5,0.5,])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 2, gl.FLOAT, false, 2*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)gl.drawArrays(gl.POINTS, 0, 4);gl.drawArrays(gl.LINE_LOOP, 0, 4);</script>
</body>
</html>

15-绘制线框立方体

也有旋转公式

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec4 aPosition;void main(){float radian=radians(10.0);float sin=sin(radian);float cos=cos(radian);mat4 mx=mat4(1,0,0,0,0,cos,-sin,0,0,sin,cos,0,0,0,0,1);mat4 my=mat4(cos,0,-sin,0,0,1,0,0,sin,0,cos,0,0,0,0,1);gl_Position= mx * my * aPosition;gl_PointSize=10.0;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">void main(){gl_FragColor=vec4(1.0,0.0,0.0,1.0);}</script><script>const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([0.5,0.5,0.5,-0.5,0.5,0.5,-0.5,-0.5,0.5,0.5,-0.5,0.5,0.5,0.5,-0.5,-0.5,0.5,-0.5,-0.5,-0.5,-0.5,0.5,-0.5,-0.5,0.5,0.5,0.5,0.5,0.5,-0.5,-0.5,0.5,0.5,-0.5,0.5,-0.5,-0.5,-0.5,0.5,   -0.5,-0.5,-0.5,      0.5,-0.5,0.5,0.5,-0.5,-0.5,])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 3, gl.FLOAT, false, 3*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)gl.drawArrays(gl.POINTS, 0, 4);gl.drawArrays(gl.LINE_LOOP, 0, 4);gl.drawArrays(gl.POINTS, 4, 4);gl.drawArrays(gl.LINE_LOOP, 4, 4);gl.drawArrays(gl.LINES, 8, 8);</script>
</body>
</html>

16-顶点索引绘制立方体

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec4 aPosition;void main(){float radian=radians(10.0);float sin=sin(radian);float cos=cos(radian);mat4 mx=mat4(1,0,0,0,0,cos,-sin,0,0,sin,cos,0,0,0,0,1);mat4 my=mat4(cos,0,-sin,0,0,1,0,0,sin,0,cos,0,0,0,0,1);gl_Position= mx * my * aPosition;gl_PointSize=10.0;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">precision mediump float;uniform vec4 uColor;void main(){gl_FragColor=uColor;}</script><script>const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([//前面-0.5,-0.5,0.5,0.5,-0.5,0.5,0.5,0.5,0.5,-0.5,0.5,0.5,//后面-0.5,-0.5,-0.5,-0.5,0.5,-0.5,0.5,0.5,-0.5,0.5,-0.5,-0.5,//上面-0.5,0.5,-0.5,-0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,-0.5,//下面-0.5,-0.5,-0.5,0.5,-0.5,-0.5,0.5,-0.5,0.5,-0.5,-0.5,0.5,//右边0.5,-0.5,-0.5,   0.5,0.5,-0.5,      0.5,0.5,0.5,0.5,-0.5,0.5,//左边-0.5,-0.5,-0.5,   -0.5,-0.5,0.5,      -0.5,0.5,0.5,-0.5,0.5,-0.5,])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const indices=new Uint16Array([0,1,2, 0,2,3,4,5,6, 4,6,7,8,9,10, 8,10,11,12,13,14, 12,14,15,16,17,18, 16,18,19,20,21,22, 20,22,23])const indexBuffer=gl.createBuffer()gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer)gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 3, gl.FLOAT, false, 3*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)// gl.drawElements(gl.TRIANGLES,indices.length,gl.UNSIGNED_SHORT,0)const faceColors=[[1.0,0.0,0.0,1.0],//前面[0.0,1.0,0.0,1.0],[1.0,0.0,1.0,1.0],[0.0,0.0,0.0,1.0],[1.0,0.0,1.0,1.0],[1.0,0.0,0.0,1.0],];const colorLocation=gl.getUniformLocation(program,'uColor')for(let i=0;i<6;i++){gl.uniform4fv(colorLocation,faceColors[i])gl.drawElements(gl.TRIANGLES,6,gl.UNSIGNED_SHORT,i*6*Uint16Array.BYTES_PER_ELEMENT)}</script>
</body>
</html>

17-实现webgl动画

requestAnimationFrmae(callback)
每秒钟60次

20 = 360
Π = 180
弧度 = 角度*(n/180)

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec4 aPosition;uniform mat4 u_x_matrix;uniform mat4 u_y_matrix;void main(){gl_Position= u_x_matrix * u_y_matrix * aPosition;gl_PointSize=10.0;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">precision mediump float;uniform vec4 uColor;void main(){gl_FragColor=uColor;}</script><script>const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([//前面-0.5,-0.5,0.5,0.5,-0.5,0.5,0.5,0.5,0.5,-0.5,0.5,0.5,//后面-0.5,-0.5,-0.5,-0.5,0.5,-0.5,0.5,0.5,-0.5,0.5,-0.5,-0.5,//上面-0.5,0.5,-0.5,-0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,-0.5,//下面-0.5,-0.5,-0.5,0.5,-0.5,-0.5,0.5,-0.5,0.5,-0.5,-0.5,0.5,//右边0.5,-0.5,-0.5,   0.5,0.5,-0.5,      0.5,0.5,0.5,0.5,-0.5,0.5,//左边-0.5,-0.5,-0.5,   -0.5,-0.5,0.5,      -0.5,0.5,0.5,-0.5,0.5,-0.5,])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const indices=new Uint16Array([0,1,2, 0,2,3,4,5,6, 4,6,7,8,9,10, 8,10,11,12,13,14, 12,14,15,16,17,18, 16,18,19,20,21,22, 20,22,23])const indexBuffer=gl.createBuffer()gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer)gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 3, gl.FLOAT, false, 3*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)// gl.drawElements(gl.TRIANGLES,indices.length,gl.UNSIGNED_SHORT,0)const faceColors=[[1.0,0.0,1.0,1.0],//前面[0.0,1.0,0.0,1.0],[1.0,0.0,1.0,1.0],[0.0,1.0,1.0,1.0],[1.0,0.0,1.0,1.0],[1.0,1.0,0.0,1.0],];const colorLocation=gl.getUniformLocation(program,'uColor')gl.enable(gl.DEPTH_TEST)const u_x_matrix=gl.getUniformLocation(program,'u_x_matrix')const u_y_matrix=gl.getUniformLocation(program,'u_y_matrix')let deg=30function render(){deg+=0.1const cosB=Math.cos(deg*Math.PI/180)const sinB=Math.sin(deg*Math.PI/180)gl.uniformMatrix4fv(u_x_matrix,false,new Float32Array([1,0,0,0,0,cosB,-sinB,0,0,sinB,cosB,0,0,0,0,1]))gl.uniformMatrix4fv(u_y_matrix,false,new Float32Array([cosB,0,-sinB,0,0,1,0,0,sinB,0,cosB,0,0,0,0,1]))for(let i=0;i<6;i++){gl.uniform4fv(colorLocation,faceColors[i])gl.drawElements(gl.TRIANGLES,6,gl.UNSIGNED_SHORT,i*6*Uint16Array.BYTES_PER_ELEMENT)}window.requestAnimationFrame(render)}window.requestAnimationFrame(render)</script>
</body>
</html>

18-glmatrix矩阵库的使用

gl-matrix矩阵库

glmatrix.net/docs/

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.canvas{border: 1px solid #ccc;}</style>
</head>
<body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec4 aPosition;unifrom mat4 mTranslate;void main(){gl_Position=mTranslate*aPosition;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">void main(){gl_FragColor=vec4(1.0,0.0,0.0,1.0);}</script><script type="module">import {mat4} from 'gl-matrix';const canvas=document.querySelector('.canvas')const gl=canvas.getContext("webgl")//创建顶点着色器和片元着色器const vertexShader=gl.createShader(gl.VERTEX_SHADER)const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)//获取着色器源码const vertexSource=document.getElementById('vertex-shader').innerTextconst fragmentSource=document.getElementById('fragment-shader').innerText//绑定着色器源码gl.shaderSource(vertexShader, vertexSource)gl.shaderSource(fragmentShader, fragmentSource)//编译着色器源码gl.compileShader(vertexShader)gl.compileShader(fragmentShader)//创建程序对象const program=gl.createProgram()gl.attachShader(program, vertexShader)gl.attachShader(program, fragmentShader)gl.linkProgram(program)gl.useProgram(program);const vertices=new Float32Array([-0.5,-0.5,0.5,  //第一个点的x轴和y轴坐标-0.5,0,0.5    //第二个点的xy轴坐标])const buffer=gl.createBuffer()gl.bindBuffer(gl.ARRAY_BUFFER, buffer)gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)const posLocation=gl.getAttribLocation(program, 'aPosition')gl.vertexAttribPointer(posLocation, 2, gl.FLOAT, false, 2*Float32Array.BYTES_PER_ELEMENT, 0)gl.enableVertexAttribArray(posLocation)const uniformTranslate=gl.getUniformLocation(program,'mTranslate')const matrix=mat4.create()mat4.translate(matrix,matrix,[0.5,0.5,0])gl.uniformMatrix4fv(uniformTranslate,false,matrix)gl.drawArrays(gl.TRIANGLES, 0, 3);</script>
</body>
</html>

19-矩阵方法的使用

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title><style>.canvas {border: 1px solid #ccc;}</style></head><body><canvas class="canvas" width="500" height="500"></canvas><!-- 顶点着色器 --><script id="vertex-shader" type="x-shader/x-vertex">attribute vec4 aPosition;uniform mat4 mTranslate;void main(){gl_Position=mTranslate * aPosition;}</script><!-- 片段着色器 --><script id="fragment-shader" type="x-shader/x-fragment">void main(){gl_FragColor=vec4(1.0,0.0,0.0,1.0);}</script><script type="module">import { mat4 } from "gl-matrix";const canvas = document.querySelector(".canvas");const gl = canvas.getContext("webgl");//创建顶点着色器和片元着色器const vertexShader = gl.createShader(gl.VERTEX_SHADER);const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);//获取着色器源码const vertexSource = document.getElementById("vertex-shader").innerText;const fragmentSource =document.getElementById("fragment-shader").innerText;//绑定着色器源码gl.shaderSource(vertexShader, vertexSource);gl.shaderSource(fragmentShader, fragmentSource);//编译着色器源码gl.compileShader(vertexShader);gl.compileShader(fragmentShader);//创建程序对象const program = gl.createProgram();gl.attachShader(program, vertexShader);gl.attachShader(program, fragmentShader);gl.linkProgram(program);gl.useProgram(program);const vertices = new Float32Array([-0.5,-0.5,0.5, //第一个点的x轴和y轴坐标-0.5,0.0,0.5, //第二个点的xy轴坐标]);const buffer = gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER, buffer);gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);const posLocation = gl.getAttribLocation(program, "aPosition");gl.vertexAttribPointer(posLocation,2,gl.FLOAT,false,2 * Float32Array.BYTES_PER_ELEMENT,0);gl.enableVertexAttribArray(posLocation);const uniformTranslate = gl.getUniformLocation(program, "mTranslate");const matrix = mat4.create();//   mat4.translate(matrix, matrix, [0.5, 0.5, 0]);mat4.scale(matrix, matrix, [0.5, 0.5, 0]);gl.uniformMatrix4fv(uniformTranslate, false, matrix);gl.drawArrays(gl.TRIANGLES, 0, 3);</script></body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/852534.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

命令行下php加载模块

命令行下php加载模块 在命令行下使用 PHP 加载模块,你可以使用 -d 选项来设置 php.ini 中的设置,或者使用 dl() 函数来动态加载 PHP 扩展模块。 例如,如果你想要加载一个名为 my_module.so 的模块,你可以这样做: php -d "extension=my_module.so" script.php &l…

虚拟机配置rsync同步

虚拟机配置rsync同步安装 apt install rsync修改配置文件 vim /etc/rsyncd.confuid = nobody gid = nogroup use chroot = no max connections = 4 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log[html]path = /home/xuxb/ht…

PHP实现开源Consul服务发现与治理

PHP实现开源Consul服务发现与治理 在分布式系统中,服务发现和治理是必不可少的组成部分。其中,consul作为一款服务发现和治理工具,被广泛应用于微服务架构中。本文将介绍如何使用php实现开源consul服务发现与治理。 一、什么是Consul? Consul是一款由HashiCorp公司开发的服…

mysql-搭建主从复制

mysql-搭建主从复制 Master(主):docker run -p 3339:3306 --name master -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 Slave(从):docker run -p 3340:3306 --name slave -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 Master对外映射的端口是3339,Slave对外映射的端口是334…

haproxy配置负载均衡

####安装#####sudo apt updatesudo apt install haproxysudo haproxy -vsudo systemctl status haproxysudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg-org ####配置站点#####vim /etc/haproxy/haproxy.cfg==========================================frontend w…

MUR3060PT-ASEMI快恢复二极管MUR3060PT

MUR3060PT-ASEMI快恢复二极管MUR3060PT编辑:ll MUR3060PT-ASEMI快恢复二极管MUR3060PT 型号:MUR3060PT 品牌:ASEMI 封装:TO-247 特性:快恢复二极管 正向电流:30A 反向耐压:600V 恢复时间:35ns 引脚数量:3 芯片个数:2 芯片尺寸:MIL 浪涌电流:300A 漏电流:10ua 工作…

Jenkins安装ubuntu

ubuntu安装Jenkins apt install fontconfig openjdk-17-jre sudo wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.keyecho "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" https://pkg.jenkin…

docker更新源

更新docker源修改docker源 vim /etc/docker/daemon.json{"registry-mirrors": ["https://ustc-edu-cn.mirror.aliyuncs.com/","https://ccr.ccs.tencentyun.com/","https://docker.m.daocloud.io/"] }重新加载docker的配置文件 system…

gitlab-runner执行失败

gitlab runner : ERROR: Job failed: prepare environment https://segmentfault.com/a/1190000044719792 查询相关的站点显示错误的原因是由于gitlab runner执行了用户文件夹下的.bash_logout脚本。 一般的解决方法都是将github-runner用户下的.bash_logout中的内容注释掉即可…

gitlab-cid示例1

default: before_script: - echo "start deploy" - echo $CI_COMMIT_REF_NAME # 阶段stages: - install - buildDev - buildTest - buildProd - deployDev - deployTest - deployProd cache: paths: - node_modules/ # 安装依赖install: stage: i…

typora 无限试用去除弹窗修改未激活按钮|Typora实现cnblog文章的推送

typora 无限试用|去除弹窗|修改未激活按钮|Typora实现cnblog文章的推送 改注册表无限试用 start.bat文件 @echo off setlocal enabledelayedexpansion chcp 65001 > nulREM 获取当前日期,格式为MM/dd/yyyy for /f "delims=" %%a in (wmic OS Get localdatetime ^…

微信模板消息

1、注册登陆微信公众平台公众号 2、申请测试账号 3、关注微信公众号、添加测试模板 发消息实现代码package org.example.weixin;import cn.hutool.core.util.ObjectUtil; import cn.hutool.http.HttpUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil;i…