UPBGE Gpu shader error. (warning c7555). How to fix this?

Hi,
i have this blend file originally posted by cuervo1003 which is a realtime texture painter.


It works fine on blender 2.77 but on upbge 0.1.4 it shows this error:


Blend file:

https://blenderartists.org/forum/attachment.php?attachmentid=435612&d=1462487605

I am using a very low end laptop win7 64bit with a graphics card nvidia 105M.

Please help!

Original thread:

Here is the fixed code and file.


import bge
from bge import texture
from bge import logic
import Rasterizer
import math

cont = logic.getCurrentController()
own = cont.owner

if "once" not in own:
    own["once"] = True    
    Rasterizer.showMouse(1)

    # size of brush
    brush = bytearray(32*32*4)
    alpha = 0.3
    sigma = 2.5
    ro = 2.0*sigma*sigma
    rgb = [255, 255, 255]
    for x in range(32):
        for y in range(32):
            i=y*128+x*4
            xf=(x-16)+0.5
            yf=(y-16)+0.5
            a=int(255*alpha*math.exp(-(xf*xf+yf*yf)/ro))
            brush[i] = rgb[0]
            brush[i+1] = rgb[1]
            brush[i+2] = rgb[2]
            brush[i+3] = a
            
    logic.brush = brush

    if not hasattr(logic, 'drawing'):
        logic.tex = texture.Texture(own,0,0)
        logic.tex.source = texture.ImageBuff()
        logic.tex.source.load(b'\x00\x00\x00' * (256*256),256,256)
        logic.tex.refresh(False)

    logic.tex.refresh(True)

    VertexShader = """
    uniform sampler2D renderedTexture;
    out vec2 Texcoord1, Texcoord2;
    out vec3 eyeVec, normal;

    void main()
    {
        Texcoord1     = gl_MultiTexCoord0.xy;
        Texcoord2     = gl_MultiTexCoord1.xy;
        
        vec2 uv = vec2(Texcoord1.x  , Texcoord1.y );
        vec4 v1 = gl_Vertex ;

        v1.z += texture2D(renderedTexture, uv).b * -.035;
            
        normal = gl_NormalMatrix * gl_Normal;
        vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
        eyeVec = -vVertex;
        
        gl_Position = gl_ModelViewProjectionMatrix * v1;
    }
    """
     
    FragmentShader = """
    uniform sampler2D renderedTexture, texture_color1, texture_color2, texture_color3;
    in vec2 Texcoord1, Texcoord2;
    in vec3  eyeVec, normal;
       
    void main()
    {  
        vec3 N = normalize(normal);
        vec3 E = normalize(eyeVec);
        float fresnelTerm = pow( max(1.5 * dot(.5 * N,  E), .30), .850);

        vec3 color;
        vec4 blend = vec4(texture2D(renderedTexture,Texcoord1 .xy)); 
        vec3 sum1 = vec3(texture2D(texture_color1, Texcoord1.xy* 2.5)); 
        vec3 sum2 = vec3(texture2D(texture_color2, Texcoord1.xy * 5.5 ));    
        vec3 sum3 = vec3(texture2D(texture_color3, vec2(Texcoord2.x * 5.5 , Texcoord2.y * 10.5 - blend.r * .5  + .49) ));    
        
        if (blend[0] <= .001)
        {
            color = sum1; 
        }
        else
        {
            color = sum3; 
        }
        
        if (blend[0] >= .9)
        {
            color = sum2; 
        }

        if (blend[0] <= .001 && blend[0] >= 0.0)
        {
            color = sum1;
        }
        
        gl_FragColor = (vec4(color - .1, 1.0)  + (.25 - blend * .5) * .5 ) * fresnelTerm * 1.5;
    }
    """
     
    def shader():
        cont = logic.getCurrentController()
        obj = cont.owner
        mesh = cont.owner.meshes[0]
        for mat in mesh.materials:
            shader = mat.getShader()
            if shader != None:
                if not shader.isValid():
                    shader.setSource(VertexShader, FragmentShader, 1)
            shader.setSampler("renderedTexture", 0)
            shader.setSampler("texture_color1", 1)
            shader.setSampler("texture_color2", 2)
            shader.setSampler("texture_color3", 3)
            
    shader() 


if "once" in own:
    ray = cont.sensors['ray']
    click = cont.sensors['click']
    own = cont.owner

    if ray.positive and click.positive:
        UV=ray.hitUV
        x=int(UV[0]*256.0-16.0)
        y=int(UV[1]*256.0-16.0)

        logic.tex.source.plot(logic.brush,32,32,x,y,1)
        logic.tex.refresh(False)

Attachments

testTexPaint_28.blend (570 KB)

Thanx, it works fine!!!

Do you also know how to fix this?
It is benj’s open world terrain, physics mesh and grass shader.
https://blenderartists.org/forum/showthread.php?256768-Terrrain-LOD-UPDATE-dynamic-shadows-!!-huge-open-world-for-all-(glsl-shader)

There are a lot different version of this shader.
Upload the version that you are using and I will try to fix it.

I changed the “varying vec” to “in vec” at grass shader script and it shows this:

0(3): error C204: version directive must be first statement and may not be repeated.

Blend file:http://benjamin.vedrenne.free.fr/STOCKAGE/terrain_LOD_physics_grass.rar

Here is the grass shader script:

Grass Shader, made by benj

Feel free to use it, share it, improve it :slight_smile:

Have fun

VertexShader = “”"

//WIND SETTINGS-------------------------------*
vec2 windDirection = vec2(-1.0,0.5);
float windStrength = 0.05;
float waveLength = 1.5;
//--------------------------------------------*

// position of the vertex (and fragment) in view space
varying vec4 position;

// getting texture and properties values
uniform sampler2D heightMap;
uniform int uvX;
uniform int uvY;
uniform float height;
uniform int cellNumber;
uniform float windTime;

void main(){

//get the first UV layout and move it accordingly to object position
gl_TexCoord[0].x = gl_MultiTexCoord0.x+float(uvX)/float(cellNumber);
gl_TexCoord[0].y = gl_MultiTexCoord0.y+float(uvY)/float(cellNumber);

// get initial vertex position in world space
vec4 v = gl_Vertex;

// add the wind’s contribution
float windDisplacement = sin(6.2832*(windTime+dot(float(cellNumber)gl_TexCoord[0].xy,normalize(windDirection))));
v.xy += windStrength
gl_MultiTexCoord1.t*normalize(windDirection)*windDisplacement;

// assign height
float heightFactor = texture2D(heightMap, clamp(gl_TexCoord[0].st,0.0,1.0)).a;
v.z += heightFactor*(height);

//deform the mesh
gl_Position = gl_ModelViewProjectionMatrix * v;

//get the vertex position
position = gl_Position;

//get second UV layout for grass color and alpha
gl_TexCoord[1] = gl_MultiTexCoord1;
}

“”"
FragmentShader = “”"

#version 120

//--------------------------------------------------------------------------------------------------**
// VARIABLES TO TWEAK

//Set how much to darken the grass at it’s base(to create an AO-like effect).
//If you’ve already darkened the bottom of your grass texture for just that purpose,
//or just don’t want it darkened, set this to 0.
const float AO = .5;

//Material settings:
vec3 ambientColor = vec3(0.06,0.15,0.2); // Set the ambient color (R,G,B)
const float rimLight = 0.3; // Set the rim lighting effect

//Lighting settings:
vec3 sunColor = vec3(1.0,0.8,0.6); // Set the Sun color (R,G,B)
float sunIntensity = 1.5; // Set the Sun intensity (at least 0.0)
vec3 sunDirection = vec3(-1.0,0.0,0.4); // Set the sun direction (X,Y,Z)

//Grass fading settings :
float GrassDistance = 14.0; // Distance where grass start to fade away, in blender units
float GrassFade = 16.0; // Distance of the Fade, in blender units

//---------------------------------------------------------------------------------------------------**

// get textures
uniform sampler2D heightMap;
uniform sampler2D grass;
uniform sampler2D stencil;
uniform sampler2D lightmap;

//get vertex position
in vec4 position;

void main()
{

// get color and alpha of the grass
vec3 grasscolor = texture2D(grass, gl_TexCoord[1].st).rgb;
float grassAlpha = texture2D(grass, gl_TexCoord[1].st).a;

// get normals of the terrain
vec3 normalGlobal = normalize(vec3(2.0, 2.0, 1.0) * (texture2D(heightMap, gl_TexCoord[0].st).rgb) - vec3(1.0, 1.0, 0.0));

//get lightmap
float lightmap = texture2D(lightmap, gl_TexCoord[0].st).r;

// compute lighting
float diffuseTemp = dot(normalGlobal, normalize(sunDirection)); // compute diffuse lighting
vec3 diffuseRaw = vec3(max(0.0,diffuseTemp)); // returns 0 when negative
if (rimLight != 0.0)
diffuseRaw += rimLight*pow(1.0-abs(diffuseTemp),5.0); // add rim light effect
diffuseRaw *= vec3(sunIntensity * sunColor); // multiply by sun values
diffuseRaw *= vec3(lightmap); // Affect with lightmap
diffuseRaw += ambientColor; // Add ambient color
diffuseRaw *= grasscolor; // Multiply by color

// compute AO
if (AO != 0.0) //This comparison should have no performance overhead since AO is a compile-time const
diffuseRaw = gl_TexCoord[1].tAO+1.0-AO; //If AO is set to 0.0, the compiler should be able to remove this whole “if” block at compile time

//decrease alpha with distance
float z = gl_FragCoord.z / gl_FragCoord.w;
z /= GrassFade;
z -= GrassDistance/GrassFade;
z = clamp(1.0-z,0.0,1.0);

// make grass visible only on grassy areas
vec3 allStencil = texture2D(stencil, gl_TexCoord[0].st).rgb;
float grassStencil = (1.0-(allStencil.r+allStencil.g+allStencil.b));
if (grassStencil < 0.5) {
grassStencil = 0.0;
} else {
grassStencil = 1.0;
}

// Get the final alpha value
float alpha = grassAlphagrassStencilz;

// set final values
gl_FragColor.rgb = diffuseRaw;
gl_FragColor.a = alpha;
}
“”"

Get Mesh and properties values’

import bge
from bge import logic
cont = logic.getCurrentController()
own = cont.owner
mesh = own.meshes[0]
cell = own[‘cell_size’]
player=logic.getCurrentScene().objects[str(own[‘LODcenter’])]

Get object position

uvX=(int(round(player.localPosition[0]/cell)))
uvY=(int(round(player.localPosition[1]/cell)))

Move the object to follow the Player step by step

own.localPosition[0] = uvXcell
own.localPosition[1] = uvY
cell

increase time value and keep only floating numbers

own[‘windTime’] += own[‘windSpeed’]
own[‘windTime’] = own[‘windTime’] % 1

#Run the Shader
for mat in mesh.materials:
shader = mat.getShader()
if shader != None:
shader.setSource(VertexShader, FragmentShader, 1)
shader.setSampler(‘heightMap’,0)
shader.setSampler(‘grass’,1)
shader.setSampler(‘stencil’,2)
shader.setSampler(‘lightmap’,3)
shader.setUniform1i(‘uvX’,uvX)
shader.setUniform1i(‘uvY’,uvY)
shader.setUniform1f(‘height’,own[“height”])
shader.setUniform1f(‘windTime’,own[‘windTime’])
shader.setUniform1i(‘cellNumber’,own[“cell_number”])

Use [code tag] when pasting python code script.

Your download is not working.

0(3): error C204: version directive must be first statement and may not be repeated.

0(3): means that the error is in the third line in the shader (this time in the fragment shader).


1 FragmentShader = """
2
3 #version 120

In that case UPBGE already defines the shader version, so you have to delete the version directive “#version 120” in line 3 of the shader.

In order that you changed the varying of the fragment shader you also need to change the vertex output too.
Vertex shader varying -> out
Fragment shader varying -> in


// position of the vertex (and fragment) in view space
out vec4 position;

Here is the full shader code.


# Grass Shader, made by benj
# Feel free to use it, share it, improve it :)
# Have fun

VertexShader = """

//WIND SETTINGS-------------------------------*
vec2 windDirection = vec2(-1.0,0.5);
float windStrength = 0.05;
float waveLength = 1.5;
//--------------------------------------------*

// position of the vertex (and fragment) in view space
out vec4 position; 

// getting texture and properties values
uniform sampler2D heightMap;
uniform int uvX;
uniform int uvY;
uniform float height;
uniform int cellNumber;
uniform float windTime;

void main(){

//get the first UV layout and move it accordingly to object position
gl_TexCoord[0].x = gl_MultiTexCoord0.x+float(uvX)/float(cellNumber);
gl_TexCoord[0].y = gl_MultiTexCoord0.y+float(uvY)/float(cellNumber);

// get initial vertex position in world space
vec4 v = gl_Vertex;

// add the wind's contribution
float windDisplacement = sin(6.2832*(windTime+dot(float(cellNumber)*gl_TexCoord[0].xy,normalize(windDirection))));
v.xy += windStrength*gl_MultiTexCoord1.t*normalize(windDirection)*windDisplacement;

// assign height
float heightFactor = texture2D(heightMap, clamp(gl_TexCoord[0].st,0.0,1.0)).a;
v.z += heightFactor*(height);

//deform the mesh
gl_Position = gl_ModelViewProjectionMatrix * v;
 
//get the vertex position
position = gl_Position;

//get second UV layout for grass color and alpha
gl_TexCoord[1] = gl_MultiTexCoord1;
}

"""
FragmentShader = """


//--------------------------------------------------------------------------------------------------**
// VARIABLES TO TWEAK

//Set how much to darken the grass at it's base(to create an AO-like effect).
//If you've already darkened the bottom of your grass texture for just that purpose,
//or just don't want it darkened, set this to 0.
const float AO = .5;

//Material settings:
vec3 ambientColor = vec3(0.06,0.15,0.2);     // Set the ambient color (R,G,B)
const float rimLight = 0.3;                 // Set the rim lighting effect

//Lighting settings:
vec3 sunColor = vec3(1.0,0.8,0.6);            // Set the Sun color (R,G,B)
float sunIntensity = 1.5;                    // Set the Sun intensity (at least 0.0)
vec3 sunDirection = vec3(-1.0,0.0,0.4);        // Set the sun direction (X,Y,Z)

//Grass fading settings :
float GrassDistance = 14.0;                  // Distance where grass start to fade away, in blender units
float GrassFade = 16.0;                      // Distance of the Fade, in blender units

//---------------------------------------------------------------------------------------------------**

// get textures
uniform sampler2D heightMap;
uniform sampler2D grass;
uniform sampler2D stencil;
uniform sampler2D lightmap;

//get vertex position
in vec4 position; 

void main()
{

// get color and alpha of the grass
vec3 grasscolor = texture2D(grass, gl_TexCoord[1].st).rgb;
float grassAlpha = texture2D(grass, gl_TexCoord[1].st).a;

// get normals of the terrain
vec3 normalGlobal = normalize(vec3(2.0, 2.0, 1.0) * (texture2D(heightMap, gl_TexCoord[0].st).rgb) - vec3(1.0, 1.0, 0.0)); 

//get lightmap
float lightmap = texture2D(lightmap, gl_TexCoord[0].st).r;

// compute lighting
float diffuseTemp = dot(normalGlobal, normalize(sunDirection));     // compute diffuse lighting
vec3 diffuseRaw = vec3(max(0.0,diffuseTemp));                       // returns 0 when negative
if (rimLight != 0.0)
    diffuseRaw += rimLight*pow(1.0-abs(diffuseTemp),5.0);           // add rim light effect         
diffuseRaw *= vec3(sunIntensity * sunColor);                        // multiply by sun values
diffuseRaw *= vec3(lightmap);                                       // Affect with lightmap
diffuseRaw += ambientColor;                                         // Add ambient color
diffuseRaw *= grasscolor;                                           // Multiply by color

// compute AO
if (AO != 0.0)                                 //This comparison should have no performance overhead since AO is a compile-time const
    diffuseRaw *= gl_TexCoord[1].t*AO+1.0-AO;  //If AO is set to 0.0, the compiler should be able to remove this whole "if" block at compile time

//decrease alpha with distance
float z = gl_FragCoord.z / gl_FragCoord.w;
z /= GrassFade;
z -= GrassDistance/GrassFade;
z = clamp(1.0-z,0.0,1.0);

// make grass visible only on grassy areas
vec3 allStencil = texture2D(stencil, gl_TexCoord[0].st).rgb;
float grassStencil = (1.0-(allStencil.r+allStencil.g+allStencil.b));
if (grassStencil &lt; 0.5) {
    grassStencil = 0.0;
  } else {
    grassStencil = 1.0;
  }

// Get the final alpha value
float alpha = grassAlpha*grassStencil*z;

// set final values
gl_FragColor.rgb = diffuseRaw;
gl_FragColor.a = alpha;
}
"""



# Get Mesh and properties values'
import bge
from bge import logic
cont = logic.getCurrentController()
own = cont.owner
mesh = own.meshes[0]
cell = own['cell_size']
player=logic.getCurrentScene().objects[str(own['LODcenter'])]

# Get object position
uvX=(int(round(player.localPosition[0]/cell)))
uvY=(int(round(player.localPosition[1]/cell)))

# Move the object to follow the Player step by step
own.localPosition[0] = uvX*cell
own.localPosition[1] = uvY*cell

# increase time value and keep only floating numbers  
own['windTime'] += own['windSpeed']
own['windTime'] = own['windTime'] % 1

#Run the Shader
for mat in mesh.materials:
    shader = mat.getShader()
    if shader != None:
        shader.setSource(VertexShader, FragmentShader, 1)
        shader.setSampler('heightMap',0)
        shader.setSampler('grass',1)
        shader.setSampler('stencil',2)        
        shader.setSampler('lightmap',3)
        shader.setUniform1i('uvX',uvX)
        shader.setUniform1i('uvY',uvY)
        shader.setUniform1f('height',own["height"])
        shader.setUniform1f('windTime',own['windTime'])
        shader.setUniform1i('cellNumber',own["cell_number"])

Edit:
It seems that there is a physic bug in the UPBGE. If the material is set to wire frame and the physics mesh has been reinstated the object will lose collision.
So if you have the same bug you need to change the material render setting from “Wire” to “Surface”.
Here a file which is showing this bug. PhysicBugUPBGE.blend (94.1 KB)

@HG1: both bugs are fixed in 3801c525e2d1be66524e4fe7d086171d82550c48 and fedadad77dcac1be2e6687a11121d38a9cd6eac5. Thanks for the report.

It works fine thanx!!!
Got what i have to and will help in the future.
… Unfortunately i cannot use it because i cannot generate my own (fully working) terrain+grass+physics mesh+visual mesh in viewport with this method…