Shaders problem - 3D texture goes wrong

Hi everyone, in this project im trying to make game like a Subway Surf, Angry granny, etc. i’ve done with the logic and here what the games look like.

and now, i try to make at the end of road looks slighty curve just look like subway surf

to make it happen, im using a custom shader. and finally at the end of my road has become slighty curve just like in subway surve. But, my 3D texture gone wrong just like this.

Below how do i make a shader and apply it to 3D object.

vertex file.vert

uniform float u_bound;
uniform float u_farplane;
uniform vec4 u_cam;
uniform vec4 u_color;
uniform mat4 u_MVPMatrix;

attribute vec4 a_position;
attribute vec2 a_texCoord;

varying vec2 v_texture_coord;

void main(void)
{
    vec4 real_pos = u_MVPMatrix * a_position;
    gl_Position = real_pos;
}

Frag file.frag

#ifdef GL_ES
varying mediump vec2 v_texture_coord;
#else
varying vec2 v_texture_coord;
#endif
varying float v_shade;
uniform sampler2D u_sampler0;
uniform vec4 u_color;
 
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;

vec4 blur(vec2);
uniform vec4 u_Time;

void main (void)
{    
    gl_FragColor = texture2D(u_sampler0, v_texture_coord);
}

SceneGame.cpp


#define SET_UNIFORM(ps, name, value)  do {   \
decltype(value) __v = value;                           \
auto __loc = (ps)->getUniformLocation(name);  \
(ps)->setUniform(__loc, &__v, sizeof(__v));  \
} while(false) 

bool SceneGame::init()
{

  float shaderBound = 5100;
  float shaderFarPlane = 4 * 5100;
  
  auto vertexSource = FileUtils::getInstance()->getStringFromFile("shaders/3D_curvature.vert");
  auto fragSource = FileUtils::getInstance()->getStringFromFile("shaders/3D_texture.frag");
  auto program = backend::Device::getInstance()->newProgram(vertexSource, fragSource);
  _programState = new backend::ProgramState(program);
  
  SET_UNIFORM(_programState, "u_bound", shaderBound);
  SET_UNIFORM(_programState, "u_farplane", shaderFarPlane);

    // EXAMPLE
    for(auto data : vecData3D)
    {
      auto spr3DRoad = Sprite3D::create(data.path3D);
      this->addChild(spr3DRoad);

      addShaderCurve(spr3DRoad)   
    }
}

void SceneGame::addShaderCurve(Sprite3D* spr)
{
	for(auto Mesh : spr->getMeshes())
	{
		Mesh->setProgramState(_programState);
	}
}

Anyone can help me ? i’ve try to search but still no clue. Please if you have any thought or solution let me know. Thanks !

Note: Im using cocos2dx-v4

Perhaps we can crowdsource some thoughts…

@Rusty @stevetranby @R101 @bilalmirza @vkreal2 @stephenhurry

Is it similar to this issue here endless terrain distortion

hi @vkreal2 , i just read the link that you gave. But unfortunately i cant understand the solution in that discussion. and i cant open the image and the link into raywenderlich tutorial.

But my problem start from the beggining when open the game. not like the issue in that discussion which is the problem start when the the terrain going further. Thanks !

[UPDATE]
i just look up into Shaders and Materials tutorial, and i was currious about this line “u_sampler0

sampler u_sampler0
{
    path = Sprite3DTest/boss.png
}

its look same name in my file.frag

void main (void)
{    
    gl_FragColor = texture2D(u_sampler0, v_texture_coord);
}

if in Cocos cpp Sprite3D test, the ‘u_sampler0’ was contain path into the texture. But, in my file.frag. im not declaring ‘u_sampler0’. if i using the material that use in cpp test → Sprite 3D test → basic toon shader. my game texture become like in the cpp test. the texture was fine.

if im not include the path into ‘u_sampler0’, the texture will goes wrong just like the above screenshoot.

texture image:
image

result:

So here my question, is ‘u_sampler0’ must contains path into texture ? how do i show the original texture of my 3D object without declaring ‘path’ int ‘u_sampler’ ?

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.