Skip to content

Commit

Permalink
Fix sprite rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
kraifpatrik committed Jul 3, 2024
1 parent 7fba852 commit f96a62e
Show file tree
Hide file tree
Showing 56 changed files with 192 additions and 224 deletions.
2 changes: 1 addition & 1 deletion BBMOD_GML/BBMOD.resource_order
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
{"name":"BBMOD_DefaultMaterial","order":17,"path":"scripts/BBMOD_DefaultMaterial/BBMOD_DefaultMaterial.yy",},
{"name":"BBMOD_DefaultRenderer","order":18,"path":"scripts/BBMOD_DefaultRenderer/BBMOD_DefaultRenderer.yy",},
{"name":"BBMOD_DefaultShader","order":19,"path":"scripts/BBMOD_DefaultShader/BBMOD_DefaultShader.yy",},
{"name":"BBMOD_DefaultSpriteShader","order":20,"path":"scripts/BBMOD_DefaultSpriteShader/BBMOD_DefaultSpriteShader.yy",},
{"name":"BBMOD_DefaultSpriteShader","order":3,"path":"scripts/BBMOD_DefaultSpriteShader/BBMOD_DefaultSpriteShader.yy",},
{"name":"BBMOD_DeferredRenderer","order":5,"path":"scripts/BBMOD_DeferredRenderer/BBMOD_DeferredRenderer.yy",},
{"name":"BBMOD_DepthOfFieldEffect","order":2,"path":"scripts/BBMOD_DepthOfFieldEffect/BBMOD_DepthOfFieldEffect.yy",},
{"name":"BBMOD_DirectionalLight","order":1,"path":"scripts/BBMOD_DirectionalLight/BBMOD_DirectionalLight.yy",},
Expand Down
2 changes: 1 addition & 1 deletion BBMOD_GML/BBMOD.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions BBMOD_GML/Xshaders/MetallicMaterial.xsh
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,19 @@ Material UnpackMaterial(
{
Material m = CreateMaterial();

// Base color and opacity
vec4 baseOpacity = texture2D(texBaseOpacity,
#if defined(X_2D)
mix(bbmod_BaseOpacityUV.xy, bbmod_BaseOpacityUV.zw, uv)
#else
uv
vec2 spriteUv = (uv - bbmod_BaseOpacityUV.xy) / (bbmod_BaseOpacityUV.zw - bbmod_BaseOpacityUV.xy);
#endif
);

// Base color and opacity
vec4 baseOpacity = texture2D(texBaseOpacity, uv);
m.Base = xGammaToLinear(baseOpacity.rgb);
m.Opacity = baseOpacity.a;

// Normal vector and smoothness/roughness
vec4 normalW = texture2D(texNormalW,
#if defined(X_2D)
mix(bbmod_NormalWUV.xy, bbmod_NormalWUV.zw, uv)
mix(bbmod_NormalWUV.xy, bbmod_NormalWUV.zw, spriteUv)
#else
uv
#endif
Expand Down Expand Up @@ -83,7 +81,7 @@ Material UnpackMaterial(
#else
vec4 materialProps = texture2D(texMaterial,
#if defined(X_2D)
mix(bbmod_MaterialUV.xy, bbmod_MaterialUV.zw, uv)
mix(bbmod_MaterialUV.xy, bbmod_MaterialUV.zw, spriteUv)
#else
uv
#endif
Expand All @@ -106,9 +104,9 @@ Material UnpackMaterial(
// Subsurface (color and intensity)
vec4 subsurface = texture2D(texSubsurface,
#if defined(X_2D)
mix(bbmod_SubsurfaceUV.xy, bbmod_SubsurfaceUV.zw, uv)
mix(bbmod_SubsurfaceUV.xy, bbmod_SubsurfaceUV.zw, spriteUv)
#else
uv
uv
#endif
);
m.Subsurface = vec4(xGammaToLinear(subsurface.rgb).rgb, subsurface.a);
Expand All @@ -117,9 +115,9 @@ Material UnpackMaterial(
// Emissive color
m.Emissive = xGammaToLinear(xDecodeRGBM(texture2D(texEmissive,
#if defined(X_2D)
mix(bbmod_EmissiveUV.xy, bbmod_EmissiveUV.zw, uv)
mix(bbmod_EmissiveUV.xy, bbmod_EmissiveUV.zw, spriteUv)
#else
uv
uv
#endif
)));
#endif
Expand Down
11 changes: 9 additions & 2 deletions BBMOD_GML/objects/OMain/Draw_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,27 @@ modSphere.render([matSphere]);

terrain.render();

// Create sprite material
var _matSprite = BBMOD_MATERIAL_DEFERRED.clone();
_matSprite.set_shader(BBMOD_ERenderPass.Shadows, BBMOD_SHADER_DEFAULT_DEPTH);
_matSprite.NormalSmoothness = sprite_get_texture(Sprite18, current_time * 0.001);
_matSprite.Culling = cull_noculling;

// Draw sprite
var _queue = bbmod_render_queue_get_default();
_queue.ApplyMaterial(_matSprite, BBMOD_VFORMAT_DEFAULT_SPRITE);
_queue.BeginConditionalBlock();
_queue.SetWorldMatrix(matrix_build(10, 0, 0, 0, 90, 0, 0.1, 0.1, 0.1));
_queue.DrawSprite(Sprite17, 0, 0, 0);
_queue.SetWorldMatrix(matrix_build(20, 0, 7, 90, 0, current_time * 0.05, 0.1, 0.1, 0.1));
_queue.DrawSprite(Sprite17, current_time * 0.001, -32, 0);
_queue.ResetMaterial();
_queue.EndConditionalBlock();

// Reset world matrix to identity
BBMOD_MATRIX_IDENTITY.ApplyWorld();

// Render world from camera's perspective
camera.apply();
renderer.render();

// Destroy temporary sprite material
_matSprite.destroy();
39 changes: 29 additions & 10 deletions BBMOD_GML/scripts/BBMOD_DefaultShader/BBMOD_DefaultShader.gml
Original file line number Diff line number Diff line change
Expand Up @@ -126,33 +126,52 @@ function BBMOD_DefaultShader(_shader, _vertexFormat)

var _shaderCurrent = shader_current();

// Base opacity UVs
var _baseOpacity = _material.BaseOpacity;
if (_baseOpacity != pointer_null)
{
bbmod_shader_set_base_opacity_uv(_shaderCurrent, texture_get_uvs(_baseOpacity));
}

// Normal smoothness/roughness
if (_material.NormalSmoothness != undefined)
var _normalSmoothness = _material.NormalSmoothness;
if (_normalSmoothness != undefined)
{
bbmod_shader_set_normal_smoothness(_shaderCurrent, _material.NormalSmoothness);
bbmod_shader_set_normal_smoothness(_shaderCurrent, _normalSmoothness);
bbmod_shader_set_normal_w_uv(_shaderCurrent, texture_get_uvs(_normalSmoothness));
}

if (_material.NormalRoughness != undefined)
var _normalRoughness = _material.NormalRoughness;
if (_normalRoughness != undefined)
{
bbmod_shader_set_normal_roughness(_shaderCurrent, _material.NormalRoughness);
bbmod_shader_set_normal_roughness(_shaderCurrent, _normalRoughness);
bbmod_shader_set_normal_w_uv(_shaderCurrent, texture_get_uvs(_normalSmoothness));
}

// Specular color/Metallic and AO
if (_material.SpecularColor != undefined)
var _specularColor = _material.SpecularColor;
if (_specularColor != undefined)
{
bbmod_shader_set_specular_color(_shaderCurrent, _material.SpecularColor);
bbmod_shader_set_specular_color(_shaderCurrent, _specularColor);
bbmod_shader_set_material_uv(_shaderCurrent, texture_get_uvs(_specularColor));
}

if (_material.MetallicAO != undefined)
var _metallicAO = _material.MetallicAO;
if (_metallicAO != undefined)
{
bbmod_shader_set_metallic_ao(_shaderCurrent, _material.MetallicAO);
bbmod_shader_set_metallic_ao(_shaderCurrent, _metallicAO);
bbmod_shader_set_material_uv(_shaderCurrent, texture_get_uvs(_metallicAO));
}

// Subsurface
bbmod_shader_set_subsurface(_shaderCurrent, _material.Subsurface);
var _subsurface = _material.Subsurface;
bbmod_shader_set_subsurface(_shaderCurrent, _subsurface);
bbmod_shader_set_subsurface_uv(_shaderCurrent, texture_get_uvs(_subsurface));

// Emissive
bbmod_shader_set_emissive(_shaderCurrent, _material.Emissive);
var _emissive = _material.Emissive;
bbmod_shader_set_emissive(_shaderCurrent, _emissive);
bbmod_shader_set_emissive_uv(_shaderCurrent, texture_get_uvs(_emissive));

return self;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,9 @@
/// by the shader.
///
/// @see BBMOD_DefaultMaterial
///
/// @deprecated Please use {@link BBMOD_DefaultShader} instead.
function BBMOD_DefaultSpriteShader(_shader, _vertexFormat)
: BBMOD_DefaultShader(_shader, _vertexFormat) constructor
{
static DefaultShader_set_material = set_material;

static set_material = function (_material)
{
gml_pragma("forceinline");

DefaultShader_set_material(_material);

var _shaderCurrent = shader_current();

var _texture = _material.BaseOpacity;
if (_texture != pointer_null)
{
bbmod_shader_set_base_opacity_uv(_shaderCurrent, texture_get_uvs(_texture));
}

_texture = _material.NormalSmoothness ?? _material.NormalRoughness;
if (_texture != undefined)
{
bbmod_shader_set_normal_w_uv(_shaderCurrent, texture_get_uvs(_texture));
}

_texture = _material.SpecularColor ?? _material.MetallicAO;
if (_texture != undefined)
{
bbmod_shader_set_material_uv(_shaderCurrent, texture_get_uvs(_texture));
}

_texture = _material.Subsurface;
if (_texture != pointer_null)
{
bbmod_shader_set_subsurface_uv(_shaderCurrent, texture_get_uvs(_texture));
}

_texture = _material.Emissive;
if (_texture != pointer_null)
{
bbmod_shader_set_emissive_uv(_shaderCurrent, texture_get_uvs(_texture));
}

return self;
};
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
/// @note This shader does not support subsurface scattering!
#macro BBMOD_SHADER_DEFAULT_LIGHTMAP __bbmod_shader_default_lightmap()

/// @macro {Struct.BBMOD_DefaultSpriteShader} Shader for 2D sprites.
#macro BBMOD_SHADER_DEFAULT_SPRITE __bbmod_shader_default_sprite()

/// @macro {Struct.BBMOD_DefaultMaterial} The default material.
#macro BBMOD_MATERIAL_DEFAULT __bbmod_material_default()

Expand All @@ -45,9 +42,6 @@
/// @macro This material does not support subsurface scattering!
#macro BBMOD_MATERIAL_DEFAULT_LIGHTMAP __bbmod_material_default_lightmap()

/// @macro {Struct.BBMOD_DefaultMaterial} Material for 2D sprites.
#macro BBMOD_MATERIAL_DEFAULT_SPRITE __bbmod_material_default_sprite()

////////////////////////////////////////////////////////////////////////////////
// Vertex formats

Expand Down Expand Up @@ -82,6 +76,7 @@ function __bbmod_shader_default()
.add_variant(BBMOD_ShDefaultColor, BBMOD_VFORMAT_DEFAULT_COLOR)
.add_variant(BBMOD_ShDefaultColorAnimated, BBMOD_VFORMAT_DEFAULT_COLOR_ANIMATED)
.add_variant(BBMOD_ShDefaultColorBatched, BBMOD_VFORMAT_DEFAULT_COLOR_BATCHED)
.add_variant(BBMOD_ShDefaultSprite, BBMOD_VFORMAT_DEFAULT_SPRITE)
;
return _shader;
}
Expand All @@ -106,13 +101,6 @@ function __bbmod_shader_default_lightmap()
return _shader;
}

function __bbmod_shader_default_sprite()
{
static _shader = new BBMOD_DefaultSpriteShader(
BBMOD_ShDefaultSprite, BBMOD_VFORMAT_DEFAULT_SPRITE)
return _shader;
}

function __bbmod_shader_default_depth()
{
static _shader = new BBMOD_BaseShader(
Expand Down Expand Up @@ -182,6 +170,14 @@ bbmod_material_register("BBMOD_MATERIAL_DEFAULT_SPRITE", BBMOD_MATERIAL_DEFAUL
////////////////////////////////////////////////////////////////////////////////
// DEPRECATED!!!

/// @macro {Struct.BBMOD_DefaultSpriteShader} Shader for 2D sprites.
/// @deprecated Please use {@link BBMOD_SHADER_DEFAULT} instead.
#macro BBMOD_SHADER_DEFAULT_SPRITE BBMOD_SHADER_DEFAULT

/// @macro {Struct.BBMOD_DefaultMaterial} Material for 2D sprites.
/// @deprecated Please use {@link BBMOD_MATERIAL_DEFAULT} instead.
#macro BBMOD_MATERIAL_DEFAULT_SPRITE BBMOD_MATERIAL_DEFAULT

/// @macro {Struct.BBMOD_VertexFormat} Vertex format of 2D sprites.
/// @deprecated Please use {@link BBMOD_VFORMAT_DEFAULT_SPRITE} instead.
#macro BBMOD_VFORMAT_SPRITE BBMOD_VFORMAT_DEFAULT_SPRITE
Expand Down
28 changes: 20 additions & 8 deletions BBMOD_GML/scripts/__bbmod_shader_set/__bbmod_shader_set.gml
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,12 @@ function bbmod_shader_set_base_opacity_uv(_shader, _uv)
function bbmod_shader_set_normal_w_uv(_shader, _uv)
{
gml_pragma("forceinline");
shader_set_uniform_f_array(
shader_set_uniform_f(
shader_get_uniform(_shader, BBMOD_U_NORMAL_W_UV),
_uv);
_uv[0],
_uv[1],
_uv[2],
_uv[3]);
}

/// @macro {String} Name of a fragment shader uniform of type `vec4` that holds
Expand All @@ -413,9 +416,12 @@ function bbmod_shader_set_normal_w_uv(_shader, _uv)
function bbmod_shader_set_material_uv(_shader, _uv)
{
gml_pragma("forceinline");
shader_set_uniform_f_array(
shader_set_uniform_f(
shader_get_uniform(_shader, BBMOD_U_MATERIAL_UV),
_uv);
_uv[0],
_uv[1],
_uv[2],
_uv[3]);
}

/// @macro {String} Name of a fragment shader uniform of type `vec4` that holds
Expand All @@ -433,9 +439,12 @@ function bbmod_shader_set_material_uv(_shader, _uv)
function bbmod_shader_set_subsurface_uv(_shader, _uv)
{
gml_pragma("forceinline");
shader_set_uniform_f_array(
shader_set_uniform_f(
shader_get_uniform(_shader, BBMOD_U_SUBSURFACE_UV),
_uv);
_uv[0],
_uv[1],
_uv[2],
_uv[3]);
}

/// @macro {String} Name of a fragment shader uniform of type `vec4` that holds
Expand All @@ -453,9 +462,12 @@ function bbmod_shader_set_subsurface_uv(_shader, _uv)
function bbmod_shader_set_emissive_uv(_shader, _uv)
{
gml_pragma("forceinline");
shader_set_uniform_f_array(
shader_set_uniform_f(
shader_get_uniform(_shader, BBMOD_U_EMISSIVE_UV),
_uv);
_uv[0],
_uv[1],
_uv[2],
_uv[3]);
}

/// @macro {String} Name of a fragment shader uniform of type `float` that
Expand Down
8 changes: 3 additions & 5 deletions BBMOD_GML/shaders/BBMOD_ShDefault/BBMOD_ShDefault.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,7 @@ Material UnpackMaterial(
Material m = CreateMaterial();

// Base color and opacity
vec4 baseOpacity = texture2D(texBaseOpacity,
uv
);
vec4 baseOpacity = texture2D(texBaseOpacity, uv);
m.Base = xGammaToLinear(baseOpacity.rgb);
m.Opacity = baseOpacity.a;

Expand Down Expand Up @@ -604,13 +602,13 @@ Material UnpackMaterial(

// Subsurface (color and intensity)
vec4 subsurface = texture2D(texSubsurface,
uv
uv
);
m.Subsurface = vec4(xGammaToLinear(subsurface.rgb).rgb, subsurface.a);

// Emissive color
m.Emissive = xGammaToLinear(xDecodeRGBM(texture2D(texEmissive,
uv
uv
)));

return m;
Expand Down
Loading

0 comments on commit f96a62e

Please sign in to comment.