我喜欢Dart,所以我一直在写一个小型的WebGL游戏。我有一个非常基本的Shaders
课程:
class Shaders {
static final int FRAGMENT = 0;
static final int VERTEX = 1;
HashMap<String, webGL.UniformLocation> _uniforms;
webGL.Program _program;
webGL.Shader _vertShader;
webGL.Shader _fragShader;
webGL.RenderingContext _gl;
Shaders(this._gl) {
_uniforms = new HashMap();
_program = _gl.createProgram();
_vertShader = _gl.createShader(webGL.VERTEX_SHADER);
_fragShader = _gl.createShader(webGL.FRAGMENT_SHADER);
}
Future<String> loadSource(String filename) {
return HttpRequest.getString(filename);
}
void compileSource(int type, String data) {
if (type == VERTEX) {
_gl.shaderSource(_vertShader, data);
_gl.compileShader(_vertShader);
_gl.attachShader(_program, _vertShader);
if (!_gl.getShaderParameter(_vertShader, webGL.COMPILE_STATUS))
print("Vertex shader: ${_gl.getShaderInfoLog(_vertShader)}");
} else if (type == FRAGMENT) {
_gl.shaderSource(_fragShader, data);
_gl.compileShader(_fragShader);
_gl.attachShader(_program, _fragShader);
if (!_gl.getShaderParameter(_fragShader, webGL.COMPILE_STATUS))
print("Vertex shader: ${_gl.getShaderInfoLog(_fragShader)}");
}
}
void setUniformMatrix(String name, var mat) {
//if we dont have this uniform get it and store for later
if (!_uniforms.containsKey(name)) {
var loc = _gl.getUniformLocation(_program, name);
_uniforms[name] = loc;
}
_gl.uniformMatrix4fv(_uniforms[name], false, mat.data());
}
void bindAttribute(int index, String name) {
_gl.bindAttribLocation(_program, index, name);
}
void useShader() {
_gl.linkProgram(_program);
_gl.useProgram(_program);
}
}
我还有一个RenderComponent
类,当它们应该是可绘制的时候我给它们。
它看起来像这样(这些类没有完成):
abstract class Component {
static final int RENDER = 1;
int _type;
Component(this._type);
int get type => _type;
}
class RenderComponent extends Component {
BufferObject _vbo;
Shaders _shader;
RenderComponent() : super(Component.RENDER);
set shader(Shaders s) => _shader = s;
set buffer(BufferObject v) => _vbo = v;
Shaders get shader => _shader;
BufferObject get buffer => _vbo;
}
当我创建RenderComponent
的对象并将shader
设置为这样时:
var s = new Shaders(pass in gl context);
var r = new RenderComponent();
r.shader = s;
我收到此错误消息:
Breaking on exception: type 'Shaders' is not a subtype of type 'Shaders' of 's'.谷歌没有提出任何对我有意义的事情。这是我遇到的最无益的信息,我无法超越它。有谁知道造成这种情况的原因是什么?