Jump to content

Making and accessing CBUFFER in Basemat


photo

Recommended Posts

Trying to pass uniform array of float2 to fragment shader in .basemat and having problems.

I got advised to use CBUFFER in such case, but I have no idea where to put it and how to access it.

I see example here: https://developer.unigine.com/ru/docs/latest/code/uusl/create_post?rlang=cpp

But it's for shader, not material (also it's only in Russian version of documentation)

 

BaseMaterial <parent=Unigine::mesh_unlit var_prefix=var texture_prefix=tex>
{
	// options
	Option blend = [src_alpha one_minus_src_alpha]

	CBUFFER(parameters)
    		UNIFORM float4 s_vertex_positions[50];
    		UNIFORM float2 s_vertex_texcoords[50];
	END

	Group "Base"
	{
		Color color = [1 1 1 1]
		Slider opacity = 1.0f
		Texture2D text = "ui/sdf_text.png" <unit=0 auto_init=true>
	}
	
	Shader vertex_struct =
	#{
		INIT_BASE_DATA

	#}
	
	
	Shader common =
	#{
	#}
	
	Shader vertex =
	#{
	#}
	
	Shader fragment =
	#{
		float a = var_s_vertex_texcoords[0].x;
		float4 col = float4(a, 1.0, 1.0, 1.0);
		OUT_FRAG_COLOR = col.rgb;
		OUT_FRAG_OPACITY = var_opacity;
	#}
}

 

This gives me 'undeclared' error.

Edited by Armikron
Link to comment
  • Armikron changed the title to Making and accessing CBUFFER in Basemat

Hi,

CBUFFER is UUSL's declaration of Constant Buffer as seen in - UUSL Keywords and Types - Documentation - Unigine Developer.
There for you should move it into Shader common.

Shader common =
#{
	CBUFFER(parameters)
		UNIFORM float4 s_vertex_positions[50];
		UNIFORM float2 s_vertex_texcoords[50];
	END
#}


And since they are not autogenerated by the material but rather declared implicitly in the shader it self - neither of uniforms will have var_ prefix.
In other words you should replace this line

float a = var_s_vertex_texcoords[0].x;

With

float a = s_vertex_texcoords[0].x;

In C++/C# code you have to get specific Render Passe's Shader object and assign array values to it.

Instead, what you can do is - specify arrays as material parameters at the material heading:

Group "Base"
{
    Color color = [1 1 1 1]
    Slider opacity = 1.0f
	Texture2D text = "core/textures/common/checker_d.texture" <unit=0 auto_init=true>
}

ArrayFloat4 vertex_positions <size=50>
ArrayFloat2 vertex_texcoords <size=50>

// ...

 

In shader you should use this:

float a = var_vertex_texcoords[0].x;

Notice absence of s_ prefix and presence of var_ one. That is due to the fact that we declared vertex_positions as a material parameter. All material parameters will have var_ prefix.


From C++/C# stand point now you should use setParameterArray for setting values up.

Hope this helps!

  • Thanks 1
Link to comment
×
×
  • Create New...