Class: Shader

Inherits:
LiteRGSS::Shader show all
Defined in:
scripts/00000 Dependencies/00001 LiteRGSS2/00011 Shader.rb

Overview

Note:

`#version 120` will be automatically added to the begining of the file if not present

Shader loaded applicable to a Sprite/Viewport or Graphics

Special features:

Shader.register(name_sym, frag_file, vert_file = nil, tone_process: false, color_process: false, alpha_process: false)
  This function registers a shader as name name_sym
    if frag_file contains `void main()` it'll assume its the file contents of the shader
    otherwise it'll assume it's the filename and load it from disc
    if vert_file is nil, it won't load the vertex shader
    if vert_file contains `void main()` it'll assume it's the file contents of the shader
    otherwise it'll assume it's the filename and load it from disc
    tone_process adds tone process to the shader (fragment color needs to be called frag), it'll add the required constant and uniforms (tone)
    color_process adds the color process to the shader (fragment color needs to be called frag), it'll add the required uniforms (color)
    alpha_process adds the alpha process to the shader (fragment color needs to be called frag), it'll use gl_Color.a
Shader.create(name_sym)
  This function instanciate a shader by it's name_sym so you don't have to load the files several time and you have all the correct data

Constant Summary collapse

SHADER_VERSION =
PSDK_RUNNING_UNDER_MAC ? "#version 120\n" : "#version 130\n"
COLOR_UNIFORM =
"\\0uniform vec4 color;\n"
COLOR_PROCESS =
"\n  frag.rgb = mix(frag.rgb, color.rgb, color.a);\\0"
TONE_UNIFORM =
"\\0uniform vec4 tone;\nconst vec3 lumaF = vec3(.299, .587, .114);\n"
TONE_PROCESS =
"\n  float luma = dot(frag.rgb, lumaF);\n  frag.rgb = mix(frag.rgb, vec3(luma), tone.w);\n  frag.rgb += tone.rgb;\\0"
ALPHA_PROCESS =
"\n  frag.a *= gl_Color.a;\\0"
DEFAULT_SHADER =
<<~EODEFAULTSHADER
  #{SHADER_VERSION}
  uniform sampler2D texture;
  void main() {
    vec4 frag = texture2D(texture, gl_TexCoord[0].xy);
    gl_FragColor = frag;
  }
EODEFAULTSHADER
SHADER_CONTENT_DETECTION =
'void main()'
SHADER_VERSION_DETECTION =
'#version '
SHADER_FRAG_FEATURE_ADD =
/\n( |)+gl_FragColor( |)+=/
SHADER_UNIFORM_ADD =
/#version[^\n]+\n/

Constants inherited from LiteRGSS::Shader

LiteRGSS::Shader::Fragment, LiteRGSS::Shader::Geometry, LiteRGSS::Shader::Vertex

Constants inherited from LiteRGSS::BlendMode

LiteRGSS::BlendMode::Add, LiteRGSS::BlendMode::DstAlpha, LiteRGSS::BlendMode::DstColor, LiteRGSS::BlendMode::One, LiteRGSS::BlendMode::OneMinusDstAlpha, LiteRGSS::BlendMode::OneMinusDstColor, LiteRGSS::BlendMode::OneMinusSrcAlpha, LiteRGSS::BlendMode::OneMinusSrcColor, LiteRGSS::BlendMode::ReverseSubtract, LiteRGSS::BlendMode::SrcAlpha, LiteRGSS::BlendMode::SrcColor, LiteRGSS::BlendMode::Subtract, LiteRGSS::BlendMode::Zero

Instance Attribute Summary

Attributes inherited from LiteRGSS::BlendMode

#alpha_dest_factor, #alpha_equation, #alpha_src_factor, #blend_type, #color_dest_factor, #color_equation, #color_src_factor

Class Method Summary collapse

Methods inherited from LiteRGSS::Shader

#load, new, #set_bool_uniform, #set_float_array_uniform, #set_float_uniform, #set_int_uniform, #set_matrix_uniform, #set_texture_uniform

Class Method Details

.create(name_sym) ⇒ Shader

Function that creates a shader by its name

Parameters:

  • name_sym (Symbol)

    name of the shader

Returns:



62
63
64
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00011 Shader.rb', line 62

def create(name_sym)
  Shader.new(*@registered_shaders[name_sym])
end

.load_to_string(filename) ⇒ String

Load a shader data from a file

Parameters:

  • filename (String)

    name of the file in Graphics/Shaders

Returns:

  • (String)

    the shader string



69
70
71
72
73
74
75
76
77
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00011 Shader.rb', line 69

def load_to_string(filename)
  log_error('Calling Shader.load_to_string is deprecated, please use Shader.create(name) instead to get the right shader.
The game will sleep 10 seconds to make sure you see this message')
  sleep(10)
  return File.read("graphics/shaders/#{filename.downcase}.txt")
rescue StandardError
  log_error("Failed to load shader #{filename}, sprite using this shader will not display correctly")
  return @registered_shaders[:full_shader]&.last || DEFAULT_SHADER
end

.register(name_sym, frag_file, vert_file = nil, tone_process: false, color_process: false, alpha_process: false)

Register a new shader by it's name

Parameters:

  • name_sym (Symbol)

    name of the shader

  • frag_file (String)

    file content or filename of the frag shader, the function will look at void main() to know

  • vert_file (String) (defaults to: nil)

    file content or filename of the vertex shader, the function will look at void main() to know

  • tone_process (Boolean) (defaults to: false)

    if the function should add tone_process to the shader

  • color_process (Boolean) (defaults to: false)

    if the function should add color_process to the shader

  • alpha_process (Boolean) (defaults to: false)

    if the function should add alpha_process to the shader



49
50
51
52
53
54
55
56
57
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00011 Shader.rb', line 49

def register(name_sym, frag_file, vert_file = nil, tone_process: false, color_process: false, alpha_process: false)
  frag = load_shader_file(frag_file)
  vert = vert_file && load_shader_file(vert_file)
  frag = add_frag_color(frag) if color_process
  frag = add_frag_tone(frag) if tone_process
  frag = add_frag_alpha(frag) if alpha_process

  @registered_shaders[name_sym] = [vert, frag].compact
end