GLSL Shader in Maya Part 2: Textures


Welcome to a new chapter in our experiment to make Maya work as a shader editor.
I'm going to show you a simple shader to load textures using the second method I've shown you in the previous article.
The code is pretty much self explanatory, look at the comments to find out more.

basic_texture.ogsfx 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Tell included shaders to use OGSFX semantics and streams:
#define OGSFX 1

//########################### UNIFORMS ###########################
// Uniform parameter handling
// Loads the uniforms from all shader stage files
#define HIDE_OGSFX_UNIFORMS 0
#define HIDE_OGSFX_STREAMS 1
#define HIDE_OGSFX_CODE 1

#include "basic_texture.glslv"
#include "basic_texture.glslf"

//########################### ATTRIBUTES ###########################
// Input stream handling:
// Loads the attribute streams from all shader stage files
#define HIDE_OGSFX_UNIFORMS 1
#define HIDE_OGSFX_STREAMS 0
#define HIDE_OGSFX_CODE 1

#include "basic_texture.glslv"
#include "basic_texture.glslf"

//########################### FUNCTION ###########################
// Code handling:
// We need to load the vertex stage and fragment stage in two
// different GLSLShader blocks in order to specify them in the
// technique definition below:
#define HIDE_OGSFX_UNIFORMS 1
#define HIDE_OGSFX_STREAMS 1
#define HIDE_OGSFX_CODE 0

// Vertex shader.
GLSLShader VS
{
#include "basic_texture.glslv"
}

// Fragment shader.
GLSLShader FS
{
#include "basic_texture.glslf"
}

// Techniques.
technique Main
{
    pass p0
    {
        VertexShader (in vs_input, out vs_out) = VS;
        PixelShader (in fs_input, out fs_out) = FS;
    }
}

basic_texture.glslv (vertex shader)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//########################### GLSL VERSION ###########################
//If it's not a Maya OGSFX Shader set the GLSL version for common GLSL shaders
#if !OGSFX
#version 330
#endif

//########################### UNIFORMS ###########################
//Put here OGSFX uniform specific code
#if !HIDE_OGSFX_UNIFORMS
#if OGSFX

uniform mat4 MVP : WorldViewProjection;

#else

uniform mat4 MVP;

#endif //OGSFX
#endif // HIDE_OGSFX_UNIFORMS

//########################### ATTRIBUTES ###########################
//Put here OGSFX attribute specific code
#if !HIDE_OGSFX_STREAMS
#if OGSFX

attribute vs_input
{
    vec3 Position : POSITION;
    vec2 UV : TEXCOORD0;
    vec3 Normal : NORMAL;
};

// The vertex shader ouput and also the pixel shader input
attribute vs_out
{
    vec2 outUV;
};

#else

in vec3 Position;

#endif //OGSFX
#endif //HIDE_OGSFX_STREAMS

//########################### FUNCTION ###########################
//Put here main function
#if !HIDE_OGSFX_CODE

void main()
{
    outUV = vec2(UV.x, 1.0 - UV.y);
    gl_Position = MVP * vec4(Position, 1);
}

#endif //HIDE_OGSFX_STREAMS

basic_texture.glslf (fragment shader)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//########################### GLSL VERSION ###########################
//If it's not a Maya OGSFX Shader set the GLSL version for common GLSL shaders
#if !OGSFX
#version 330
#endif

//########################### UNIFORMS ###########################
//Put here OGSFX uniform specific code
#if !HIDE_OGSFX_UNIFORMS

#if OGSFX
uniform texture2D gTexture <
    string UIName = "Texture";
    string UIGroup = "Editable Parameters";
    int UIOrder = 1;
    string ResourceName = "centaur.jpg";
    string ResourceType = "2D";
    // string UIWidget = "None";
    string UIDesc = "Special Mipped Stripe";
>;
#endif

//Anytime you add a texture,, make sure you also add a sampler2D otherwise Maya will crash
uniform sampler2D gTextureSampler
#if OGSFX
    = sampler_state {
        Texture = <gTexture>;
    }
#endif
    ;


#endif //HIDE_OGSFX_UNIFORMS

//########################### ATTRIBUTES ###########################
//Put here OGSFX attribute specific code
#if !HIDE_OGSFX_STREAMS
#if OGSFX
attribute fs_input
{
    vec2 outUV : TEXCOORD0;
};

attribute fs_out
{
    vec4 out_color : COLOR0;
};

#else 

in vec2 outUV;
out vec4 out_color;

#endif //OGSFX
#endif //HIDE_OGSFX_STREAMS

//########################### FUNCTION ###########################
//Put here main function
#if !HIDE_OGSFX_CODE

void main()
{

    float ambientStrength = 0.1;

    out_color = ambientStrength * texture2D(gTextureSampler, outUV);
}

#endif //HIDE_OGSFX_STREAMS

Comments

Popular posts from this blog

GLSL Shader in Maya Part 1

Unity Shader: Toon Water Shader

Unity Shader: Spherical Mask Dissolve