GML How to have objects colored with uniform pattern no matter their angle?

H

hydrillav

Guest
Hi, I am new to Gamemaker Studio 2 - I would like to be able to create an object that can be laid down in any direction around the player, except that the appearance of the object's pattern/texture is uniform no matter the direction of the object. Hopefully the drawing below shows what I am trying to do. Thank you very much!
 

2Dcube

Member
I don't know of an easy way, only of ways that require some advanced programming knowledge, so I'm not sure how helpful this is:

If you draw the objects around the player as polygons (made out of triangle primitives) which are painted with a texture, you can achieve this effect. It's quite complicated compared to just drawing sprites.

For example this is part of a code I use to make a polygon out of triangles. The triangles are saved in a ds_list with x1,y1, x2,y2, x3,y3 for each triangle.
Code:
v_buff = vertex_create_buffer()
    vertex_begin(v_buff, global.vertex_format)
    for(var i=0; i<ds_list_size(triangles)-1; i+=2)
    {
        vertex_position(v_buff, triangles[| i], triangles[| i+1])
        vertex_colour(v_buff, c_white, 1)
        vertex_texcoord(v_buff, triangles[| i]/256, triangles[| i+1]/256)
    }
    vertex_end(v_buff)
They are then drawn with
Code:
vertex_submit(v_buff, pr_trianglelist, tex)
in the draw event. "tex" is a reference to the texture (a sprite of 256x256).

An extra difficulty comes from having to rotate the polygons. In that case rather than a buffer just drawing primitives might be better.

Maybe there is an easier way by using a shader? I'm not very familiar with shaders so I'm not sure.
 
Two basic ways of doing this. One is to use a surface and blending modes.

The other way uses a shader. This is probably the way I'd do it, because it should be faster. If your background texture does not cover the whole area that your "flower pedals?" cover, then you will either need to enable repeating textures, or use fract(v_vTexcoord2) in the fragment shader (although in the latter case, edges wont be interpolated correctly if texture interpolation is on). Your background texture will need to be on its own texture page. "Scale" should contain the values 1/(background texture width), 1/(background texture height). It would be a good idea to have a controller object draw all these objects, that way the shader and its uniforms can be set once, instead of having to be reset for every instance.

Shader code:
Code:
    //vertex:
    attribute vec3 in_Position;                  // (x,y,z)
    attribute vec4 in_Colour;                    // (r,g,b,a)
    attribute vec2 in_TextureCoord;              // (u,v)
    varying vec2 v_vTexcoord;
    varying vec2 v_vTexcoord2;
    varying vec4 v_vColour;
    uniform vec2 Scale;
    void main(){
        gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4( in_Position.xyz, 1.0);
        v_vColour = in_Colour;
        v_vTexcoord = in_TextureCoord;
        v_vTexcoord2 = in_Position.xy * Scale;
    }
    //fragment
    varying vec2 v_vTexcoord;
    varying vec2 v_vTexcoord2;
    varying vec4 v_vColour;
    uniform sampler2D BackTexture;
    void main(){
        float Alpha = v_vColour.a * texture2D( gm_BaseTexture, v_vTexcoord ).a;
        gl_FragColor = vec4(v_vColour.rgb * texture2D( BackTexture, v_vTexcoord2 ).rgb,Alpha);
    }
 
H

hydrillav

Guest
Thank you! I have not used GameMaker 2 much but I have 2 years of experience with other languages and object based programming so hopefully I will be able to successfully try these out
 
Top