NamorvTech
NamorvTech copied to clipboard
Material with no texture Error
If i have understood, i can avoid specifying one texture into the material class and use only the color.
But if i do so
new Material( "crate", undefined, new Color( 255, 0, 255, 255 ) )
i get a webGL error: [.WebGL-00000255CD58DD20]RENDER WARNING: there is no texture bound to the unit 0
I think that the problem is caused by the shader at this line:
gl_FragColor = u_tint * texture2D( u_diffuse, v_texCoord );
If u_diffuse is not set i get "there is no texture bound to the unit 0" message.
One possible solution is to use a "fake-texture". Into Material constructor if _diffuseTextureName is undefined i get a "fake-texture"
constructor( name: string, diffuseTextureName: string, tint: Color ) {
this._name = name;
this._diffuseTextureName = diffuseTextureName;
this._tint = tint;
if ( this._diffuseTextureName !== undefined ) {
this._diffuseTexture = TextureManager.getTexture( this._diffuseTextureName );
} else {
this._diffuseTexture = TextureManager.getTexture( "fake-texture" );
}
}
and into Texture constructor if name is "fake-texture" i avoid asset loading using the TEMP_IMAGE_DATA
constructor(
name: string,
width: number = 1,
height: number = 1
) {
this._name = name;
this._width = width;
this._height = height;
this._handle = gl.createTexture();
Message.subscribe(
MESSAGE_ASSET_LOADER_ASSET_LOADED + this.name, this
);
this.bind();
gl.texImage2D(
gl.TEXTURE_2D,
LEVEL, gl.RGBA,
1,
1,
BORDER,
gl.RGBA,
gl.UNSIGNED_BYTE,
TEMP_IMAGE_DATA
);
if ( this.name === "fake-texture" ) {
this._isLoaded = true;
} else {
const asset = AssetManager.getAsset( this.name ) as ImageAsset;
if ( asset !== undefined ) {
this.loadTextureFromAsset( asset );
}
}
}
With these modification the error is gone.