{"id":24501,"date":"2024-11-21T17:39:50","date_gmt":"2024-11-21T09:39:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24501\/"},"modified":"2024-11-21T17:39:50","modified_gmt":"2024-11-21T09:39:50","slug":"webman%e6%8a%80%e6%9c%af%e5%9c%a8%e6%95%b0%e5%ad%97%e5%8c%96%e8%89%ba%e6%9c%af%e5%88%9b%e4%bd%9c%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8%e4%b8%8e%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24501\/","title":{"rendered":"WebMan\u6280\u672f\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u5e94\u7528\u4e0e\u4f18\u5316"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169300955894391.jpg\" class=\"aligncenter\" title=\"WebMan\u6280\u672f\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u5e94\u7528\u4e0e\u4f18\u5316\u63d2\u56fe\" alt=\"WebMan\u6280\u672f\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u5e94\u7528\u4e0e\u4f18\u5316\u63d2\u56fe\" \/><\/p>\n<p>WebMan\u6280\u672f\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u5e94\u7528\u4e0e\u4f18\u5316<\/p>\n<p>\u6458\u8981\uff1a<br \/>\u968f\u7740\u79d1\u6280\u7684\u53d1\u5c55\u548c\u4e92\u8054\u7f51\u7684\u666e\u53ca\uff0c\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u6210\u4e3a\u4e86\u827a\u672f\u5bb6\u4eec\u5c55\u793a\u521b\u610f\u7684\u91cd\u8981\u624b\u6bb5\u3002WebMan\u6280\u672f\u4ee5\u5176\u9ad8\u6548\u7684\u56fe\u50cf\u5904\u7406\u548c\u4f18\u5316\u80fd\u529b\uff0c\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u53d1\u6325\u4e86\u91cd\u8981\u4f5c\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecdWebMan\u6280\u672f\u7684\u539f\u7406\u548c\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u5e94\u7528\uff0c\u5e76\u7ed9\u51fa\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001WebMan\u6280\u672f\u7684\u539f\u7406<br \/>WebMan\u6280\u672f\u662f\u4e00\u79cd\u57fa\u4e8eWebGL\u7684\u56fe\u50cf\u5904\u7406\u5f15\u64ce\uff0c\u5b83\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e0a\u8fd0\u884c\uff0c\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u56fe\u50cf\u6e32\u67d3\u548c\u5904\u7406\u3002WebMan\u6280\u672f\u901a\u8fc7\u5229\u7528GPU\u7684\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\uff0c\u5c06\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u5206\u89e3\u4e3a\u591a\u4e2a\u5c0f\u4efb\u52a1\u5e76\u884c\u6267\u884c\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u56fe\u50cf\u5904\u7406\u7684\u6548\u7387\u3002<\/p>\n<p>\u4e8c\u3001WebMan\u6280\u672f\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u5e94\u7528<\/p>\n<ol>\n<li>\u827a\u672f\u6ee4\u955c<br \/>WebMan\u6280\u672f\u80fd\u591f\u5feb\u901f\u5b9e\u73b0\u5404\u79cd\u827a\u672f\u6ee4\u955c\u6548\u679c\uff0c\u5982\u6cb9\u753b\u3001\u7d20\u63cf\u3001\u6c34\u5f69\u7b49\u3002\u901a\u8fc7\u8c03\u6574\u6ee4\u955c\u53c2\u6570\u548c\u6df7\u5408\u6a21\u5f0f\uff0c\u827a\u672f\u5bb6\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u9020\u51fa\u72ec\u7279\u800c\u4e30\u5bcc\u7684\u827a\u672f\u6548\u679c\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\u9ed1\u767d\u6ee4\u955c\u6548\u679c\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>const canvas = document.getElementById('canvas');\nconst context = canvas.getContext('webgl');\n\nconst fragmentShaderSource = `\n  precision highp float;\n\n  uniform sampler2D texture;\n  varying vec2 uv;\n\n  void main() {\n    vec4 color = texture2D(texture, uv);\n    float gray = (color.r + color.g + color.b) \/ 3.0;\n    gl_FragColor = vec4(gray, gray, gray, color.a);\n  }\n`;\n\nconst vertexShaderSource = `\n  attribute vec2 position;\n  attribute vec2 uv;\n  varying vec2 v_uv;\n\n  void main() {\n    gl_Position = vec4(position, 0.0, 1.0);\n    v_uv = uv;\n  }\n`;\n\nconst vertexBuffer = context.createBuffer();\ncontext.bindBuffer(context.ARRAY_BUFFER, vertexBuffer);\ncontext.bufferData(context.ARRAY_BUFFER, new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]), context.STATIC_DRAW);\n\nconst program = context.createProgram();\nconst vertexShader = context.createShader(context.VERTEX_SHADER);\nconst fragmentShader = context.createShader(context.FRAGMENT_SHADER);\ncontext.shaderSource(vertexShader, vertexShaderSource);\ncontext.shaderSource(fragmentShader, fragmentShaderSource);\ncontext.compileShader(vertexShader);\ncontext.compileShader(fragmentShader);\ncontext.attachShader(program, vertexShader);\ncontext.attachShader(program, fragmentShader);\ncontext.linkProgram(program);\ncontext.useProgram(program);\n\nconst positionLocation = context.getAttribLocation(program, 'position');\nconst uvLocation = context.getAttribLocation(program, 'uv');\ncontext.enableVertexAttribArray(positionLocation);\ncontext.enableVertexAttribArray(uvLocation);\ncontext.vertexAttribPointer(positionLocation, 2, context.FLOAT, false, 0, 0);\ncontext.vertexAttribPointer(uvLocation, 2, context.FLOAT, false, 0, 0);\n\nconst texture = context.createTexture();\nconst image = new Image();\nimage.onload = () =&amp;gt; {\n  context.bindTexture(context.TEXTURE_2D, texture);\n  context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE);\n  context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE);\n  context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.LINEAR);\n  context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, context.LINEAR);\n  context.texImage2D(context.TEXTURE_2D, 0, context.RGBA, context.RGBA, context.UNSIGNED_BYTE, image);\n  context.drawArrays(context.TRIANGLE_STRIP, 0, 4);\n};\n\nimage.src = 'image.jpg';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316<br \/>WebMan\u6280\u672f\u53ef\u4ee5\u5e2e\u52a9\u827a\u672f\u5bb6\u5b9e\u73b0\u4ea4\u4e92\u5f0f\u7684\u53ef\u89c6\u5316\u6548\u679c\uff0c\u5982\u7c92\u5b50\u7cfb\u7edf\u3001\u6d41\u4f53\u6a21\u62df\u7b49\u3002\u901a\u8fc7\u4f7f\u7528WebGL\u4e2d\u7684\u8ba1\u7b97\u548c\u6e32\u67d3\u529f\u80fd\uff0c\u827a\u672f\u5bb6\u53ef\u4ee5\u521b\u9020\u51fa\u4e30\u5bcc\u591a\u6837\u7684\u4ea4\u4e92\u5f0f\u827a\u672f\u4f5c\u54c1\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\u4ea4\u4e92\u5f0f\u7c92\u5b50\u7cfb\u7edf\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u7c92\u5b50\u5c5e\u6027\nconst particleCount = 1000;\nconst particleSize = 4.0;\n\n\/\/ \u7c92\u5b50\u4f4d\u7f6e\u548c\u901f\u5ea6\nconst positions = new Float32Array(particleCount * 2);\nconst velocities = new Float32Array(particleCount * 2);\n\nfor (let i = 0; i  1) velocities[i * 2] *= -1;\n    if (positions[i * 2 + 1]  1) velocities[i * 2 + 1] *= -1;\n  }\n}\n\n\/\/ \u4e3b\u5faa\u73af\nfunction mainLoop() {\n  updateParticles();\n  renderParticles();\n  requestAnimationFrame(mainLoop);\n}\n\nmainLoop();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e09\u3001WebMan\u6280\u672f\u7684\u4f18\u5316<br \/>WebMan\u6280\u672f\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u4f18\u5316\u4e3b\u8981\u5305\u62ec\u4e24\u4e2a\u65b9\u9762\uff1a\u4e00\u662f\u901a\u8fc7GPU\u52a0\u901f\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6027\u80fd\uff1b\u4e8c\u662f\u4f18\u5316\u4ee3\u7801\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u51cf\u5c11\u8ba1\u7b97\u65f6\u95f4\u548c\u8d44\u6e90\u6d88\u8017\u3002<\/p>\n<ol>\n<li>GPU\u52a0\u901f<br \/>\u901a\u8fc7\u5229\u7528GPU\u7684\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\uff0c\u5c06\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u5206\u89e3\u4e3a\u591a\u4e2a\u5c0f\u4efb\u52a1\u5e76\u884c\u6267\u884c\uff0c\u53ef\u4ee5\u63d0\u9ad8\u56fe\u50cf\u5904\u7406\u7684\u901f\u5ea6\u3002\u540c\u65f6\uff0c\u5408\u7406\u5229\u7528GPU\u5185\u5b58\u548c\u7f13\u5b58\uff0c\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u4f20\u8f93\u548c\u8bfb\u53d6\u7684\u65f6\u95f4\uff0c\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li>\u4f18\u5316\u4ee3\u7801\u7ed3\u6784\u548c\u7b97\u6cd5<br \/>\u5728\u7f16\u5199WebMan\u6280\u672f\u7684\u4ee3\u7801\u65f6\uff0c\u827a\u672f\u5bb6\u53ef\u4ee5\u4f18\u5316\u4ee3\u7801\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u548c\u5185\u5b58\u5360\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u77e9\u9635\u8fd0\u7b97\u4ee3\u66ff\u5faa\u73af\u8fd0\u7b97\u3001\u907f\u514d\u9891\u7e41\u7684\u6570\u636e\u62f7\u8d1d\u7b49\uff0c\u90fd\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\u3002<\/li>\n<\/ol>\n<p>\u56db\u3001\u7ed3\u8bba<br \/>WebMan\u6280\u672f\u4ee5\u5176\u9ad8\u6548\u7684\u56fe\u50cf\u5904\u7406\u548c\u4f18\u5316\u80fd\u529b\uff0c\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u53d1\u6325\u4e86\u91cd\u8981\u4f5c\u7528\u3002\u901a\u8fc7WebMan\u6280\u672f\uff0c\u827a\u672f\u5bb6\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0\u5404\u79cd\u827a\u672f\u6ee4\u955c\u548c\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u6548\u679c\uff0c\u5c55\u793a\u51fa\u4e30\u5bcc\u591a\u6837\u7684\u521b\u610f\u4f5c\u54c1\u3002\u672a\u6765\uff0c\u968f\u7740WebGL\u548cWebMan\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u5c06\u53d8\u5f97\u66f4\u52a0\u591a\u6837\u5316\u548c\u521b\u9020\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fWebMan\u6280\u672f\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u5e94\u7528\u4e0e\u4f18\u5316\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WebMan\u6280\u672f\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u5e94\u7528\u4e0e\u4f18\u5316 \u6458\u8981\uff1a\u968f\u7740\u79d1\u6280\u7684\u53d1\u5c55\u548c\u4e92\u8054\u7f51\u7684\u666e\u53ca\uff0c\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u6210\u4e3a\u4e86\u827a\u672f\u5bb6\u4eec\u5c55\u793a\u521b\u610f\u7684\u91cd\u8981\u624b\u6bb5\u3002WebMan\u6280\u672f\u4ee5\u5176\u9ad8\u6548\u7684\u56fe\u50cf\u5904\u7406\u548c\u4f18\u5316\u80fd\u529b\uff0c\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u53d1\u6325\u4e86\u91cd\u8981\u4f5c\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecdWebMan\u6280\u672f\u7684\u539f\u7406\u548c\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u5e94\u7528\uff0c\u5e76\u7ed9\u51fa\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001WebMan\u6280\u672f\u7684\u539f\u7406WebMan\u6280\u672f\u662f\u4e00\u79cd\u57fa\u4e8eWebGL\u7684\u56fe\u50cf\u5904\u7406\u5f15\u64ce\uff0c\u5b83\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e0a\u8fd0\u884c\uff0c\u5b9e\u73b0\u9ad8\u6027\u80fd\u7684\u56fe\u50cf\u6e32\u67d3\u548c\u5904\u7406\u3002WebMan\u6280\u672f\u901a\u8fc7\u5229\u7528GPU\u7684\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\uff0c\u5c06\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u5206\u89e3\u4e3a\u591a\u4e2a\u5c0f\u4efb\u52a1\u5e76\u884c\u6267\u884c\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u56fe\u50cf\u5904\u7406\u7684\u6548\u7387\u3002 \u4e8c\u3001WebMan\u6280\u672f\u5728\u6570\u5b57\u5316\u827a\u672f\u521b\u4f5c\u4e2d\u7684\u5e94\u7528 \u827a\u672f\u6ee4\u955cWebMan\u6280\u672f\u80fd\u591f\u5feb\u901f\u5b9e\u73b0\u5404\u79cd\u827a\u672f\u6ee4\u955c\u6548\u679c\uff0c\u5982\u6cb9\u753b\u3001\u7d20\u63cf\u3001\u6c34\u5f69\u7b49\u3002\u901a\u8fc7\u8c03\u6574\u6ee4\u955c\u53c2\u6570\u548c\u6df7\u5408\u6a21\u5f0f\uff0c\u827a\u672f\u5bb6\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u9020\u51fa\u72ec\u7279\u800c\u4e30\u5bcc\u7684\u827a\u672f\u6548\u679c\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\u9ed1\u767d\u6ee4\u955c\u6548\u679c\u7684\u4ee3\u7801\u793a\u4f8b\uff1a const canvas = document.getElementById(&#8216;canvas&#8217;); const context = canvas.getContext(&#8216;webgl&#8217;); const fragmentShaderSource = ` precision highp float; uniform sampler2D texture; varying vec2 uv; void main() { vec4 color = texture2D(texture, uv); float gray = (color.r + color.g + color.b) \/ 3.0; gl_FragColor = vec4(gray, gray, gray, color.a); } `; const vertexShaderSource [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24501","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24501"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24501\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}