Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
html { height: 100%; } img { display: none; } body { background: #000; overflow: hidden; padding: 0; margin: 0; width: 100%; height: 100%; text-align: center; } canvas { height: 100%; width: 100%; margin: auto; }
JavaScript
// karlikpj 2021 // Boostrap for WebGL and Attaching Shaders // // Fragment & Vertex Shaders in HTML window // class Render { constructor() { this.start = Date.now(); this.umouse = [0.0, 0.0, 0.0, 0.0]; this.tmouse = [0.0, 0.0, 0.0, 0.0]; // Setup WebGL canvas and surface object // // Make Canvas and get WebGl2 Context // let width = (this.width = ~~(document.documentElement.clientWidth, window.innerWidth || 0)); let height = (this.height = ~~(document.documentElement.clientHeight, window.innerHeight || 0)); const canvas = (this.canvas = document.createElement("canvas")); const container = document.getElementById("container"); canvas.id = "GLShaders"; canvas.width = width; canvas.height = height; document.body.appendChild(canvas); const gl = (this.gl = canvas.getContext("webgl2")); if (!gl) { console.warn("WebGL 2 is not available."); return; } // WebGl and WebGl2 Extension // this.gl.getExtension("OES_standard_derivatives"); this.gl.getExtension("EXT_shader_texture_lod"); this.gl.getExtension("OES_texture_float"); this.gl.getExtension("WEBGL_color_buffer_float"); this.gl.getExtension("OES_texture_float_linear"); this.gl.viewport(0, 0, canvas.width, canvas.height); // always nice to let people resize window.addEventListener( "resize", () => { let width = ~~(document.documentElement.clientWidth, window.innerWidth || 0); let height = ~~(document.documentElement.clientHeight, window.innerHeight || 0); this.canvas.width = width; this.canvas.height = height; this.gl.viewport(0, 0, this.canvas.width, this.canvas.height); this.resolution = new Float32Array([width, height]); this.gl.uniform2fv( this.gl.getUniformLocation(this.program, "resolution"), this.resolution ); this.clearCanvas(); }, false ); this.init(); } // Shader Bootstrap code // createShader = (type, source) => { const shader = this.gl.createShader(type); this.gl.shaderSource(shader, source); this.gl.compileShader(shader); const success = this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS); if (!success) { console.log(this.gl.getShaderInfoLog(shader)); this.gl.deleteShader(shader); return false; } return shader; }; createWebGL = (vertexSource, fragmentSource) => { // Setup Vertext/Fragment Shader functions this.vertexShader = this.createShader(this.gl.VERTEX_SHADER, vertexSource); this.fragmentShader = this.createShader( this.gl.FRAGMENT_SHADER, fragmentSource ); // Setup Program and Attach Shader functions this.program = this.gl.createProgram(); this.gl.attachShader(this.program, this.vertexShader); this.gl.attachShader(this.program, this.fragmentShader); this.gl.linkProgram(this.program); this.gl.useProgram(this.program); if (!this.gl.getProgramParameter(this.program, this.gl.LINK_STATUS)) { console.warn( "Unable to initialize the shader program: " + this.gl.getProgramInfoLog(this.program) ); return null; } // Create and Bind buffer // const buffer = this.gl.createBuffer(); this.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer); this.gl.bufferData( this.gl.ARRAY_BUFFER, new Float32Array([-1, 1, -1, -1, 1, -1, 1, 1]), this.gl.STATIC_DRAW ); const vPosition = this.gl.getAttribLocation(this.program, "vPosition"); this.gl.enableVertexAttribArray(vPosition); this.gl.vertexAttribPointer( vPosition, 2, // size: 2 components per iteration this.gl.FLOAT, // type: the data is 32bit floats false, // normalize: don't normalize the data 0, // stride: 0 = move forward size * sizeof(type) each iteration to get the next position 0 // start at the beginning of the buffer ); this.clearCanvas(); this.importUniforms(); }; clearCanvas = () => { this.gl.clearColor(0, 0, 0, 0); this.gl.clear(this.gl.COLOR_BUFFER_BIT); }; // add other uniforms here importUniforms = () => { const width = ~~(document.documentElement.clientWidth, window.innerWidth || 0); const height = ~~(document.documentElement.clientHeight, window.innerHeight || 0); this.resolution = new Float32Array([width, height]); this.gl.uniform2fv( this.gl.getUniformLocation(this.program, "resolution"), this.resolution ); // get the uniform ins from the shader fragments this.ut = this.gl.getUniformLocation(this.program, "time"); }; // things that need to be updated per frame updateUniforms = () => { let tm = (Date.now() - this.start) / 1000; //prevent time from getting too big if (tm > 2000) this.start = Date.now(); this.gl.uniform1f(this.ut, (Date.now() - this.start) / 1000); this.gl.drawArrays( this.gl.TRIANGLE_FAN, // primitiveType 0, // Offset 4 // Count ); }; // setup shaders and send to render loop init = () => { this.createWebGL( document.getElementById("vertexShader").textContent, document.getElementById("fragmentShader").textContent ); this.renderLoop(); }; renderLoop = () => { this.updateUniforms(); this.animation = window.requestAnimationFrame(this.renderLoop); }; } const demo = new Render(document.body);
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>六边形| GLSL材质球-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号