Jump to content

[SOLVED] Can't access texture in shader


photo

Recommended Posts

Posted

Hi, I have a simple custom material that should be rendered in the forward pass, and I can't seem to access the texture I pass in. Currently, the object appears black, when it should be displaying the texture. Simply writing the texture coordinates to the frag color gives a correct result, though.

Any idea why this is? Where am I going wrong?

This is my material definition:

<?xml version="1.0" encoding="utf-8"?>
<base_material version="2.0" name="video_texture" editable="0" parameters_prefix="m">

  <options transparent="2"/>
  
	<!-- States -->
	<state name="ambient">1</state>
	
	<!-- Deferred rendering shaders -->
	<shader pass="ambient" node="object_mesh_static"
		ambient="1"
		defines="BASE_AMBIENT"
		vertex="shaders/video_texture.vert"
		fragment="shaders/video_texture.frag"/>
	
	<!-- Bindings -->
	<bind node="object_mesh_dynamic" to="object_mesh_static"/>
	<bind node="object_mesh_skinned" to="object_mesh_static"/>

	<!-- Textures -->
	<texture unit="0" name="albedo" anisotropy="1" shader="fragment" pass="deferred, ambient">core/textures/common/white.dds</texture>
  
</base_material>

The Vertex shader:

// Include Unified Unigine Shader Language (UUSL) header
#include <core/shaders/common/common.h>

// Input data struct
STRUCT(VERTEX_IN)
	INIT_ATTRIBUTE(float4,0,POSITION)	// Vertex position
	INIT_ATTRIBUTE(float4,1,TEXCOORD0)	// Vertex texcoord (uv)
END

// Our output vertex data struct
STRUCT(VERTEX_OUT)
	INIT_POSITION					// Out projected position
	INIT_OUT(float4,0)				// Texcoord (uv)
END

MAIN_BEGIN(VERTEX_OUT,VERTEX_IN)
	
	// Get transform with scale and rotation (without translation)
	float4 row_0 = s_transform[0];
	float4 row_1 = s_transform[1];
	float4 row_2 = s_transform[2];
	
	// Get Modelview-space transform
	float4 in_vertex = float4(IN_ATTRIBUTE(0).xyz,1.0f);
	float4 position = mul4(row_0,row_1,row_2,in_vertex);
	
	// Set output UV
	float4 texcoord = IN_ATTRIBUTE(1);
	OUT_DATA(0) = texcoord;
	
	// Set output position
	OUT_POSITION = getPosition(position);
MAIN_END

The fragment shader:

// Include the UUSL language header
#include <core/shaders/common/fragment.h>

// Adds a texture sampler
INIT_TEXTURE(0, TEX_COLOR)

STRUCT(FRAGMENT_IN)
	INIT_POSITION				// Projected position
	INIT_IN(float4,0)			// Texcoord (uv)
END


MAIN_BEGIN(FRAGMENT_OUT, FRAGMENT_IN)
	
	// Get the UV coords
	float2 texcoord = IN_DATA(0).xy;

	// Get the texture data
	float4 macro, macro_u, macro_r, macro_ur;

	float4 fragColor = TEXTURE(TEX_COLOR, texcoord);

	OUT_COLOR = fragColor;
	OUT_COLOR.a = 1.0f;

MAIN_END

 

Posted

Hi uzadov,

try replacing the following lines in your fragment shader:

	// Get the UV coords
	float2 texcoord = IN_DATA(0).xy;

	// Get the texture data
	float4 macro, macro_u, macro_r, macro_ur;

	float4 fragColor = TEXTURE(TEX_COLOR, texcoord);

with these ones:

	// Get the UV coords
	float4 texcoord = IN_DATA(0);

	float4 macro, macro_u, macro_r, macro_ur;
	
	// Get the texture data
	float4 fragColor = TEXTURE(TEX_COLOR,texcoord.xy);

This should help!

Thanks!

Posted

Hi Fox,

thanks for your support!

Unfortunately, I just tried your suggestion, and there is no change. The quad is rendered in black.

To check if the tex coordinates are correct,  I output them:

OUT_COLOR = float4(texcoord.x, texcoord.y, 0, 1);

This gives me the expected color gradient on my quad. Right now I'm assuming that for some reason, it can't access the texture. Does that make sense?

Posted

Sure, no problem. Since it fails to upload here, I've placed the reproducer at:

https://www.libavg.de/owncloud/index.php/s/dzm1SfDcrq4KBCq

If you open the world file in the editor and navigate to materials, there is a test.basemat material. It should render a white texture, but it renders black. It does render white if I hardcode that in the shader.

Posted

Hi uzadow,

Seems like the problem was in your material file, try removing a space in this line in the pass list (after the comma between "deferred" and "ambient"):

<texture unit="0" name="albedo" anisotropy="1" shader="fragment" pass="deferred,ambient">core/textures/common/white.dds</texture>

Thanks!

Posted

Yep, that was it. Thanks for the quick and precise support!

A follow-up question: Is there a way to debug situations like this ourselves? Did I miss an error message somewhere?

Posted

Glad that everything is ok! Normally, all messages, warnings and errors are displayed in the console. However, looks like this one was an exception,  sorry for any inconvenience caused!

Thanks!

  • silent changed the title to [SOLVED] Can't access texture in shader
×
×
  • Create New...