贵州网站建设hsyunso,wordpress 分享到微信二维码,织梦网站文章相互调用,淄博微信小程序代理1. WebGL是什么#xff1f; WebGL#xff08;Web Graphics Library#xff09;是一种JavaScript API#xff0c;它允许你在不需要安装任何额外插件的情况下#xff0c;直接在浏览器中渲染高性能的2D和3D图形。WebGL利用了用户的图形处理单元#xff08;GPU#xff09;来…1. WebGL是什么 WebGLWeb Graphics Library是一种JavaScript API它允许你在不需要安装任何额外插件的情况下直接在浏览器中渲染高性能的2D和3D图形。WebGL利用了用户的图形处理单元GPU来加快渲染速度这意味着你可以在网页上创建类似桌面应用程序的视觉效果。 1.1 WebGL入门代码实现如下 
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0title给画布换个颜色/title
/head
bodycanvas idcanvas width400 height400此浏览器不支持canvas/canvasscriptconst ctx  document.getElementById(canvas)const gl  ctx.getContext(webgl)gl.clearColor(1.0, 0.0, 0.0, 1.0)gl.clear(gl.COLOR_BUFFER_BIT)/script
/body
/html 
1.2 方法详解 
1gl.clearColor(r,g,b,a) 指定清空canvas的颜色接收四个参数取值区间为 0.0~1.0) 
2gl.clear(buffer) 清空canvas参数分为三项 
gl.COLOR_BUFFER_BIT 清空颜色缓存gl.DEPTH_BUFFER_BIT 清空深度缓冲区gl.STENCIL_BUFFER_BIT 清空模板缓冲区 
3gl.clear 需要和 gl.clearColor 提到的函数搭配使用 
gl.clear(gl.COLOR_BUFFER_BIT) 和 gl.clearColor(0.0,0.0,0.0,1.0)gl.clear(gl.DEPTH_BUFFER_BIT) 和 gl.clearDepth(1.0)gl.clear(gl.STENCIL_BUFFER_BIT) 和 gl.clearStencil(0) 2. WebGL的基本组件 
2.1 什么是着色器 
1着色器就是让开发者自己去编写一段程序用来代替固定渲染管线来处理图像的渲染。 
顶点着色器处理每个顶点的数据如位置、颜色和纹理坐标。片元着色器处理每个像素片元的颜色和其他属性最终决定屏幕上显示的颜色。 
2.2 webgl 绘制一个点的流程 2.2.1 创建着色器源码 
// 顶点着色器源码
const vertexShaderSource  
// 必须要存在 main 函数
void main() {// 要绘制的点的坐标gl_Position  vec4(0.0,0.0,0.0,1.0); // x, y, z, w齐次坐标(x/wy/w z/w)// 点的大小gl_PointSize  10.0;
}// 片源着色器源码
const fragmentShaderSource  
// 必须要存在 main 函数
void main() {gl_FragColor  vec4(0.0,0.0,0.0,1.0); // r, g, b, a
} 
2.2.2 创建着色器 
// 创建顶点着色器对象
const vertexShader  gl.createShader(gl.VERTEX_SHADER);
// 创建片段着色器对象
const fragmentShader  gl.createShader(gl.FRAGMENT_SHADER); 
2.2.3 指定关联着色器源码 
// 设置顶点着色器源代码
gl.shaderSource(vertexShader, vertexShaderSource);
// 设置片段着色器源代码
gl.shaderSource(fragmentShader, fragmentShaderSource); 
2.2.4 编译着色器 
// 编译顶点着色器
gl.compileShader(vertexShader);
// 编译片段着色器
gl.compileShader(fragmentShader); 
2.2.5 创建程序对象 
const program  gl.createProgram();
gl.attachShader(program,vertexShader)
gl.attachShader(program,fragmentShader)
gl.linkProgram(program)
gl.useProgram(program) 
2.2.6 执行绘制 
gl.drawArrays(gl.POINTS,0,1);    // 绘制一个点需要1个点  文章转载自: http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.trzzm.cn.gov.cn.trzzm.cn http://www.morning.bflws.cn.gov.cn.bflws.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.rtmqy.cn.gov.cn.rtmqy.cn http://www.morning.kycwt.cn.gov.cn.kycwt.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.xwlhc.cn.gov.cn.xwlhc.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.xysxj.com.gov.cn.xysxj.com http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.mdxwz.cn.gov.cn.mdxwz.cn http://www.morning.jgrjj.cn.gov.cn.jgrjj.cn http://www.morning.jfnbh.cn.gov.cn.jfnbh.cn http://www.morning.gftnx.cn.gov.cn.gftnx.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.tqpnf.cn.gov.cn.tqpnf.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.hwbmn.cn.gov.cn.hwbmn.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.skkln.cn.gov.cn.skkln.cn http://www.morning.yrjfb.cn.gov.cn.yrjfb.cn http://www.morning.mnclk.cn.gov.cn.mnclk.cn http://www.morning.qlckc.cn.gov.cn.qlckc.cn http://www.morning.jpjxb.cn.gov.cn.jpjxb.cn http://www.morning.bxbkq.cn.gov.cn.bxbkq.cn http://www.morning.pfjbn.cn.gov.cn.pfjbn.cn http://www.morning.srcth.cn.gov.cn.srcth.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.sbyhj.cn.gov.cn.sbyhj.cn http://www.morning.cbynh.cn.gov.cn.cbynh.cn http://www.morning.jxltk.cn.gov.cn.jxltk.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.syssdz.cn.gov.cn.syssdz.cn http://www.morning.wkjzt.cn.gov.cn.wkjzt.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.fnmtc.cn.gov.cn.fnmtc.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.tbrnl.cn.gov.cn.tbrnl.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.lbxhy.cn.gov.cn.lbxhy.cn http://www.morning.yptwn.cn.gov.cn.yptwn.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.phtqr.cn.gov.cn.phtqr.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.nqxdg.cn.gov.cn.nqxdg.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.mzwqt.cn.gov.cn.mzwqt.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.lxthr.cn.gov.cn.lxthr.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.rfqkx.cn.gov.cn.rfqkx.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.ydflc.cn.gov.cn.ydflc.cn http://www.morning.ffbp.cn.gov.cn.ffbp.cn http://www.morning.sbpt.cn.gov.cn.sbpt.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.qhjkz.cn.gov.cn.qhjkz.cn http://www.morning.ktxd.cn.gov.cn.ktxd.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.dhmll.cn.gov.cn.dhmll.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.nfdty.cn.gov.cn.nfdty.cn http://www.morning.pudejun.com.gov.cn.pudejun.com