0

使用twgl.js来简化内容,但出于某种原因,我得到了以下警告:

[.WebGL-000031BC01680600]GL_INVALID_OPERATION:必须绑定元素数组缓冲区。

屏幕上什么也没有显示。然而,使用twgl.js创建者的实例非常好。目标是在屏幕上画出圆圈,每个圆圈都有自己的位置和大小。我不太使用webgl,以前也没有做过实例化,所以我不知道哪里出错了

我也在用包裹

文件结构相当简单:

索引.html索引.cjs型钢混凝土/|渲染器.cjs

renderer.cjs:

const twgl=要求(“twgl.js”)const VERT_SHADER=`precision mediump float;属性vec2点;属性vec2位置;属性浮点大小;均匀vec2分辨率;空main(){vec2变换=vec2((点*大小+位置)/分辨率);vec2 clipSpace=变换*vec2(2.0,-2.0)-vec2;gl_Position=vec4(clipSpace,1.0,1.0);}`const FRAG_SHADER=`precision mediump float;空main(){gl_FragColor=vec4(0.19,0.43,0.69,1.0);}`module.exports=类{数组={分数:{num组件:2,数据:[],},位置:{num组件:2,数据:[],除数:1,},大小:{numComponents:1,数据:[],除数:1,}}构造函数(画布){this.gl=画布.getContext(“webgl”)twgl.addExtensionsToContext(this.gl)this.programInfo=twgl.createProgramInfo(this.gl,[VERT_SHADER,FRAG_SHADER])}设置圆形边(边){this.sides=侧面常数θ=2*数学。PI/侧面let数据=[]//在圆中添加三角形的顶点for(设i=0;i<边;++i){数据.推送(0, 0,数学cos(i*theta),数学sin(i*theta),数学cos((i+1)*theta),数学sin((i+1*theta),)}this.arrays.points.data=数据}addCircle(x,y,大小){this.arrays.position.data.push(x,y)this.arrays.size.data.push(大小)}updateBuffers(){this.bufferInfo=twgl.createBufferInfoFromArrays(this.gl,this.arrays)this.vertexArrayInfo=twgl.createVertexArray信息(this.gl,this.programInfo,this.bufferInfo)}渲染(){this.gl.canvas.width=内部宽度this.gl.canvas.height=内部高度twgl.resizeCanvasToDisplaySize(this.gl.cavas)this.gl.视口(0,0,innerWidth,innerHeight)this.gl.清除(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER _BIT)const制服={分辨率:[innerWidth,innerHeight]}this.updateBuffers()this.gl.useProgram(this.programInfo.program)twgl.setBuffersAndAttributes(this.gl、this.programInfo和this.vertexArrayInfo)twgl.setUniforms(this.programInfo,制服)this.gl.drawElementsInstanced(this.gl.TRIANGLES,this.vertexArrayInfo.numElements,this.gl.UNSIGNED_SHORT,0,this.arrays.size.data.length)}}

索引.cjs:

const Renderer=require(“./src/Renderer.cjs”)//在html中使用defer,因此它在加载元素后运行constcanvas=document.querySelector(“canvas”)函数main(){const渲染器=新渲染器(画布)renderer.setCircleSides(8)for(设i=0;i<100;++i){renderer.addCircle(数学随机数()*200,数学随机数()*200,数学随机数()*10)}renderer.render()}main()

我尝试过的一个非常好的例子是在这个帖子上:在twgl.js中使用实例扩展

1答案1

重置为默认值
0

你在打电话图纸元素已安装但你没有指数。呼叫drawArraysInstanced(提取阵列实例)

你的答案

单击“发布您的答案”,表示您同意我们的服务条款并确认您已阅读我们的隐私政策.

不是你想要的答案吗?浏览标记的其他问题问你自己的问题.