Jump to content

Texture not Binding to Shader?


Pryme8
 Share

Recommended Posts

Ok so I have my little animated atlas shader and I am to the point where I want to add 'sub-layers' or overlays to each plane with the shader on it.

So everything works, except the texture bound to the overlayA does not ever seem to display the correct data.  If I just try to output the overlayA as well it just seems to load up the atlas texture which I do not understand at all!

Here is the working example with no overlay added https://pryme8.github.io/TileMaster/dev/demo.html

Then here is the one with the overlay added: https://pryme8.github.io/TileMaster/dev/demo_broke.html

As far as I can tell I am handling the textures the same way.
Maybe Im missing something...
https://github.com/Pryme8/TileMaster/blob/gh-pages/dev/demo.html
^Source.
There may be some bugs and things because I have been tearing down and re arranging the code trying to figure out what the heck is going on.
 

Link to comment
Share on other sites

		this.shader = new BABYLON.ShaderMaterial("basicShader", this.parent.scene, {
			vertex: "basic",
			fragment: "basic",
			},{
			attributes: ["position", "normal", "uv"],
			uniforms: ["world",
			"worldView", 
			"worldViewProjection",
			"view", "viewOffset",
			"viewportSize",
			"inverseTileTextureSize",
			"inverseSpriteTextureSize","layerHeight",
			"tileSize", "time", "projection",
			"globalLight", "fps",
			"sprites", "tiles", "animationMap", "overlayA"]
		});

effectively that did nothing, did I do it correctly?

Link to comment
Share on other sites

That fixed it thanks @Deltakosh, your constantly my favorite person on the internet.

Now what happens when I do not use all the shaders i added to the list, do I need to bind a blank texture to them or something to prevent the webGL error?

the demo should be working now I know its not impressive because of my lack of ability at pixl art but its getting there as far as what I was creating it for.

Link to comment
Share on other sites

Look here:

https://github.com/BabylonJS/Babylon.js/blob/master/src/Shaders/default.fragment.fx#L46

the #if DIFFUSE will allow the shader to expect a diffuse texture only if I call the Shader compilation with a defines string set to "#define DIFFUSE"

 

The defines string is part of the ShaderMaterial options that you can set in the constructor (where you already set up the samplers for instance)

Link to comment
Share on other sites

so like:

this.shader = new BABYLON.ShaderMaterial("basicShader", this.parent.scene, {
			vertex: "basic",
			fragment: "basic",
			},{
			attributes: ["position", "normal", "uv"],
			uniforms: ["world",
			"worldView", 
			"worldViewProjection",
			"view", "viewOffset",
			"viewportSize",
			"inverseTileTextureSize",
			"inverseSpriteTextureSize","layerHeight",
			"tileSize", "time", "projection",
			"globalLight", "fps"],
            samplers:["sprites", "tiles", "animationMap", "overlayA"],
            defines:["ANIMATED", "OVERA"]
		});

?

Link to comment
Share on other sites

ah semi colon separated? gotcha.

and so do like #define ANIMATED;

and then I can do in the shader

#if ANIMATED
do animation stuff
#endif

ect...

can I change defines after creation of the shader? or do I need to rebind the whole thing?

Link to comment
Share on other sites

Ok cool, which wont really come into effect with my final product because everything will be loaded from a serialized file and so by the time it comes to compiling the shader everything will effectively be known.

Thanks for your help!

Last question, how when I bind textures would I bind for a texture array?
this.shader.setTexture('overlays[0]', this._overlays[0]);

Link to comment
Share on other sites

When I did the define string:

var defines = "#define LAYERS "+parseInt(this.layers.length)+";";		
		
		this.shader = new BABYLON.ShaderMaterial("basicShader", this._core.scene, {
			vertex: "editor",
			fragment: "editor",
			},{
			attributes: ["position", "normal", "uv"],
			defines: defines,
			uniforms: ["world",
			"worldView", 
			"worldViewProjection",
			"view", "viewOffset",
			"viewportSize",
			"time", "spriteSize"]
			});

It did something really weird here is a copy from the console output on the error that drops:

BJS - [12:17:56]: Vertex shader: editor
1    #
2    d
3    e
4    f
5    i
6    n
7    e
8     
9    L
10    A
11    Y
12    E
13    R
14    S
15     
16    0
17    ;

looks like it splits the string up into separate lines and of course that messes up the shader.

Link to comment
Share on other sites

Hi, I'm not sure if this will fix your problem, but defines must be array. like 

var defines = ["#define LAYERS "+parseInt(this.layers.length)+";"];

edit , more simplified version :D

var defines = ["#define LAYERS "+parseInt(this.layers.length)];

 

Link to comment
Share on other sites

is there a better way to do this:
 

var defines = []	
defines.push("#define LAYERS"+this.layers.length);

In the setting up of the defines
and then in the shader:
 

#ifdef LAYERS0
const int layerCount = 0;
#endif
#ifdef LAYERS1
const int layerCount = 1;
uniform sampler2d sprites[1];
#endif
#ifdef LAYERS2
const int layerCount = 2;
uniform sampler2d sprites[2];
#endif
#ifdef LAYERS3
const int layerCount = 3;
uniform sampler2d sprites[3];
#endif
#ifdef LAYERS4
const int layerCount = 4;
uniform sampler2d sprites[4];
#endif

 

Link to comment
Share on other sites

I am having trouble assigning a texture array:

I have tried:
uniform sampler2DArray sprites;
and 
uniform sampler2DArray sprites[4];

But I keep getting:
 

1	#version 300 es
2	#define LAYERS 0
3	#define NUM_BONE_INFLUENCERS 0
4	precision highp float;
5	#define pU 0.00392156862
6	
7	uniform float time;
8	uniform float fps;
9	uniform vec2 viewOffset;
10	uniform vec2 viewportSize;
11	uniform float spriteSize;
12	
13	uniform sampler2DArray sprites;
14	uniform sampler2DArray layers;
15	
16	in vec3 vPosition;
17	in vec2 vUV;
18	in vec2 pixelCoord;
19	in vec2 texCoord;
20	
21	
22	out vec4 glFragColor;
23	void main(){
24	vec2 spriteCoord = mod(pixelCoord, spriteSize);
25	vec3 color = vec3(spriteCoord.x,1.0,spriteCoord.y);
26	if((texCoord.x > 1.0  || texCoord.x < 0.0 || texCoord.y > 1.0 || texCoord.y < 0.0)){color = vec3(0.0); }
27	float alpha = 1.0;
28	
29	glFragColor =  vec4(color, alpha);
30	}
31	

'sampler2DArray' : No precision specified
ERROR: 0:14: 'sampler2DArray' : No precision specified

No clue what I am doing wrong...

Is it because "precision highp float;" is not the first line?


**EDIT*
uniform highp sampler2DArray sprites[4];

^^ got it

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...