我在 webgl 中有一个包含多个对象的场景。对于每个对象,我想使用不同的顶点和片段着色器。我的第一个问题是,是否可以为每个对象都有一个着色器?我知道在 opengl 中这是可能的。
这是与我的想法类似的伪代码。任何例子将不胜感激。
glenableshader
draw triangle
gldisableshader
glenableshader
draw square
gldisableshader
谢谢
您几乎可以查找任何 WebGL 示例并将其转换为多着色器示例。
伪代码
// At init time
for each shader program
create and compile vertex shader
create and compile fragment shader
create program and attach shaders
link program
record locations of attributes and uniforms
for each model/set of geometry/points/data
create buffer(s) for model
copy data into buffer(s) for model
for each texture
create texture
usually asynchronously load textures
// at draw time
clear
for each model
useProgram(program for model)
setup attributes for model
setup textures for model
set uniforms for model
draw
这与使用 1 个着色器程序绘制 1 个模型没有什么不同。只需进行相同的设置即可。
更多代码...
设置属性看起来像
for each attribute used by model
gl.enableVertexAttribArray(attribLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, bufferWithDataForAttribute);
gl.vertexAttribPointer(attribLocation, ...);
设置纹理(可能)看起来像这样
for each texture used by model
gl.activeTexture(gl.TEXTURE0 + ndx);
gl.bindTexture(gl.TEXTURE_2D, texture);
最后你会使用该程序
gl.useProgram(programForModel);
for each uniform
gl.uniform???(uniformLocation, uniformValue);
gl.drawArrays(...)
or
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferOfIndicesForModel);
gl.drawElements(...);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)