Pass x,y,z coords from vertex shader to material shader?
  • MindBuffer
    junior Member
    Posts: 9
    Joined: Thu Feb 04, 2016 5:26 am

    Pass x,y,z coords from vertex shader to material shader?

    by MindBuffer » Tue Jan 03, 2017 4:28 am

    Hi guys, I started a post on the facebook madmapper user group page and was suggested by Vacintosh to post it on the forums, here is a link to the facebook post -> https://www.facebook.com/groups/madmapp ... 2142214935

    Just wondering if anyone on here has had any luck hacking the default material shaders to map across the *entire* 3D object? Would be nice to send gradients rippling through the object and not just onto each individual face, as in the image below.

    Image

    I understand that the material shaders are rendering for each pixel in the uv space, what would be ideal and super useful would be to have a vertex shader pass on each x,y,z location onto a fragment shader. I can achieve this functionality in Mapamok using openFrameworks to get the following effect......

    Image

    Image

    Is anyone from Mad Mapper able to provide a super simple vertex shader that we can insert into the material shaders that allows for this. Thanks. Would be super useful to be able to have the option to mad to both spaces.
  • MindBuffer
    junior Member
    Posts: 9
    Joined: Thu Feb 04, 2016 5:26 am

    Re: Pass x,y,z coords from vertex shader to material shader?

    by MindBuffer » Wed Jan 04, 2017 11:27 am

    Ok I think I can articulate myself better now. Is there a way to custom code up 3d material shaders? As you can see mad mapper makes a mess of unpacking 3d geometry onto a 2d material. What I need is a way of creating my own 3d materials that has a vertex shader pass on the x,y AND z of each vertex. *Note* it seems like the lighting calculation does this so i'm hoping its not too hard to get the materials taking the Z axis into account....... ?

    Image
  • mad-matt
    garageCube team
    Posts: 343
    Joined: Mon Sep 09, 2013 5:50 pm

    Re: Pass x,y,z coords from vertex shader to material shader?

    by mad-matt » Wed Jan 04, 2017 1:05 pm

    Hi,
    You can make a Material with a .vs which contains a function that will be called by the vertex shader (so for each geometry point). For instance "Line Patterns" uses the vertex shader (to avoid doing some computations for each pixel)
    This function can access those inputs / uniforms:

    #if defined(RENDER_WIREFRAME)
    in vec4 in_Vertex;
    #else
    in vec4 in_Vertex;
    in vec3 in_Normal;
    in vec2 in_TexCoord0;

    uniform bool lightingEnabled;
    uniform vec3 lightPositions;
    uniform vec3 lightAttenuations;
    uniform vec3 lightSpotDirections;

    uniform mat4 modelViewMatrix;
    uniform mat4 modelViewProjectionMatrix;
    uniform mat3 normalMatrix;
    uniform mat4 textureMatrix;
    #endif

    So when rendering wireframes, MadMapper will only let you access the vertex (for performance reasons). Normally you only need this input, but you can use normals etc too for non wireframe.

    Using the in_Vextex.xyz you can pass it to an output vec3 you declare before your function, that you must declare as an input in the pixel shader and then you can use it in materialColorForPixel

    For instance with this code, it will do RGB from geometry XYZ

    vs file:

    Code: Select all

    out vec3posXYZ;

    void materialVsFunc(vec2 posInUvs, vec2 posInSurface) {
        posXYZ=in_Vertex.xyz;
    }

    $MM_SHADER_CODE


    fs file:

    Code: Select all

    /*{
      "CREDIT": "Mad Team",
      "CATEGORIES": ["Graphic"],
      "INPUTS": [
      ],
      "GENERATORS": [
      ]
    }*/

    in vec3 posXYZ;

    vec4 materialColorForPixel(vec2 texCoord)
    {
        return vec4(posXYZ,1);
    }

    $MM_SHADER_CODE



    Keep me up to date
  • MindBuffer
    junior Member
    Posts: 9
    Joined: Thu Feb 04, 2016 5:26 am

    Re: Pass x,y,z coords from vertex shader to material shader?

    by MindBuffer » Thu Jan 05, 2017 3:17 am

    Thanks Matt,

    I'm getting an error though regarding the in_Vertex variable. If I include it in the vertex shader I get...

    Error (quad) = "ERROR: 0:27: Type(vec2) of redeclaration of 'in_Vertex' is incompatible with type (vec4) of previous declaration
    Material = ERROR: 0:37: Regular non-array variable 'in_Vertex' may not be redeclared

    But if i comment out "in vec4 in_Vertex;" then I get an error saying "Use of undeclared identifier 'in_Vertex'

    This seems weird, as if I include it in my code its saying im trying to redefine in_Vertex but if I don't include it then it complains that in_Vertex isn't defined.... :?

    Any tips?
  • mad-matt
    garageCube team
    Posts: 343
    Joined: Mon Sep 09, 2013 5:50 pm

    Re: Pass x,y,z coords from vertex shader to material shader?

    by mad-matt » Thu Jan 05, 2017 9:37 am

    Ok I understand, the declaration of in_Vertex is added at the end of your .vs file, so you can't use variables there. Anyway, doing this kind of thing in a Material is not a good idea because this material will not be standard and would only work with 3D surfaces. That's why the materialVsFunc only takes position as argument.
    You should not write a Material, but make a Surface Shader instead. In folder Resources/SystemModules/Surface Shaders/shaders/surface3d
    You'll have to restart when you add a surface shader.
    The compilation errors are not displayed there, you have to:
    - activate logging: defaults write com.garagecube.MadMapper logenabled 1
    - look for errors in the log, using Console application, ~Library/Logs/GarageCube/MadMapper/
    Let me know !
  • MindBuffer
    junior Member
    Posts: 9
    Joined: Thu Feb 04, 2016 5:26 am

    Re: Pass x,y,z coords from vertex shader to material shader?

    by MindBuffer » Thu Jan 05, 2017 10:47 am

    Cheers i'll give that ago. Excuse my ignorance but if I create a new surface shader how do I load and use it in mad mapper?
  • mad-matt
    garageCube team
    Posts: 343
    Joined: Mon Sep 09, 2013 5:50 pm

    Re: Pass x,y,z coords from vertex shader to material shader?

    by mad-matt » Thu Jan 05, 2017 11:04 am

    Excuse my ignorance but if I create a new surface shader how do I load and use it in mad mapper?


    Here:
    Screen Shot 2017-01-05 at 10.06.06.png
    Screen Shot 2017-01-05 at 10.06.06.png (70.58 KiB) Viewed 1097 times
  • MindBuffer
    junior Member
    Posts: 9
    Joined: Thu Feb 04, 2016 5:26 am

    Re: Pass x,y,z coords from vertex shader to material shader?

    by MindBuffer » Thu Jan 05, 2017 11:55 am

    YESSSSS!!!!!! Thanks Matt for helping me get up and running :D Time to start building a mother ship 3D shader :twisted:

    Image
  • mad-matt
    garageCube team
    Posts: 343
    Joined: Mon Sep 09, 2013 5:50 pm

    Re: Pass x,y,z coords from vertex shader to material shader?

    by mad-matt » Thu Jan 05, 2017 5:01 pm

    Cool ! Don't hesitate to post a video of your project :-)

Who is online

Users browsing this forum: No registered users and 1 guest