graphics - Animating lots of lines in WebGL -
i'm trying animate scene @ 1280x720, 60 fps, antialiased. there ~500k triangles, 2 each tree branch. can't use gl.lines
because angle.
to rough idea performance can expect, made test 1m of random triangles:
<!doctype html> <head> <title>triangles</title> <style> html, body { background: #000; height: 100%; margin: 0; } canvas { width: 1280px; height: 720px; position: absolute; margin: auto; top: 0; right: 0; left: 0; bottom: 0; } </style> </head> <body> <script> 'use strict'; const trianglecount = 1e6; const antialias = true; const generatetriangles = (count, width, height) => { const coords = new float32array(9 * count); (var = 0; < coords.length;) { const x = math.random() * 2 - 1; const y = math.random() * 2 - 1; const z = math.random() * 2 - 1; const theta = math.random() * math.pi; const ax = 10 * math.cos(theta) / width; const ay = 10 * math.sin(theta) / height; const bx = 10 * math.cos(theta + 0.3) / width; const = 10 * math.sin(theta + 0.3) / height; coords[i++] = x + ax; coords[i++] = y + ay; coords[i++] = z; coords[i++] = x + bx; coords[i++] = y + by; coords[i++] = z; coords[i++] = x - ax; coords[i++] = y - ay; coords[i++] = z; coords[i++] = x - ax; coords[i++] = y - ay; coords[i++] = z; coords[i++] = x - bx; coords[i++] = y - by; coords[i++] = z; coords[i++] = x + ax; coords[i++] = y + ay; coords[i++] = z; } return coords; }; const vertexshadersource = ` precision lowp float; attribute vec3 aposition; uniform float uwobble; void main() { float p = 1.0 / (0.3 * aposition.z - 1.4 + 0.1 * uwobble); gl_position = vec4(p * aposition.x, p * aposition.y, aposition.z, 1.0); } `; const fragmentshadersource = ` precision lowp float; void main() { float z = gl_fragcoord.z; gl_fragcolor = vec4(1.2 * z, z * z, z, 1.0); } `; const canvas = document.createelement('canvas'); document.body.appendchild(canvas); canvas.width = canvas.clientwidth; canvas.height = canvas.clientheight; const gl = canvas.getcontext('webgl', {alpha: false, antialias}); const vertexshader = gl.createshader(gl.vertex_shader); gl.shadersource(vertexshader, vertexshadersource); gl.compileshader(vertexshader); const fragmentshader = gl.createshader(gl.fragment_shader); gl.shadersource(fragmentshader, fragmentshadersource); gl.compileshader(fragmentshader); const program = gl.createprogram(); gl.attachshader(program, vertexshader); gl.attachshader(program, fragmentshader); gl.linkprogram(program); gl.useprogram(program); const avertexposition = gl.getattriblocation(program, 'aposition'); gl.enablevertexattribarray(avertexposition); const uwobble = gl.getuniformlocation(program, 'uwobble'); gl.uniform1f(uwobble, 1); const vertices = generatetriangles(trianglecount, canvas.width, canvas.height); const vertexbuffer = gl.createbuffer(); gl.bindbuffer(gl.array_buffer, vertexbuffer); gl.bufferdata(gl.array_buffer, vertices, gl.static_draw); gl.vertexattribpointer(avertexposition, 3, gl.float, false, 0, 0); const render = (timestamp) => { requestanimationframe(render); gl.uniform1f(uwobble, math.sin(0.002 * timestamp)); gl.drawarrays(gl.triangles, 0, vertices.length / 3); }; window.requestanimationframe(render); </script> </body>
on macbook iris 1536 mb i'm getting 12 frames per second. there optimizations i'm missing, such alternative ways draw lines, faster antialiasing, magic flags? or limit of 1 can expect average gpu?
Comments
Post a Comment