Skip to content

Commit

Permalink
Control emissive spheres with UI
Browse files Browse the repository at this point in the history
  • Loading branch information
kraifpatrik committed Aug 14, 2022
1 parent c355821 commit d2a1e33
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 64 deletions.
8 changes: 5 additions & 3 deletions objects/omain/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ surSSGI = noone;
ssgi = new SSGI();
ssgi.ClipFar = clipFar;
ssgi.GIDistance = 2.5;
ssgi.GISteps = 7;
ssgi.GISteps = 12;
ssgi.DepthThickness = 0.8;
ssgi.BlurDepthRange = 1.0;
ssgiEnabled = true;
Expand All @@ -47,11 +47,11 @@ ssao.Power = 2.0;
ssao.ClipFar = clipFar;
ssaoEnabled = true;

ambientColor = [255, 255, 255, 0.2];

sunPosition = [0.0, 0.0, 0.0];
sunDirection = [0.5, 0.0, -1.0];
sunColor = [255, 255, 255, 1.0];

ambientColor = [255, 255, 255, 0.2];

shadowmapResolution = 2048;
shadowmapArea = 64;
Expand Down Expand Up @@ -96,3 +96,5 @@ displayModeNames = [
];

displayMode = EDisplayMode.Final;

screenshotMode = false;
7 changes: 4 additions & 3 deletions objects/omain/Draw_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ model.Submit()
with (OSphere)
{
shader_set_uniform_f(_uEmissive,
color_get_red(color) / 255,
color_get_green(color) / 255,
color_get_blue(color) / 255);
color[0] / 255.0,
color[1] / 255.0,
color[2] / 255.0,
color[3]);
matrix_set(matrix_world, matrix_build(x, y, z, 0, 0, 0, scale, scale, scale));
_modelSphere.Submit();
}
Expand Down
111 changes: 111 additions & 0 deletions objects/omain/Draw_64.gml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ case EDisplayMode.Final:
//
// GUI
//
if (screenshotMode)
{
exit;
}

var _text = "FPS: " + string(fps) + " (" + string(fps_real) + ")";
draw_text_color(_windowWidth - string_width(_text) - 8, 8, _text,
c_silver, c_silver, c_silver, c_silver, 1.0);
Expand Down Expand Up @@ -299,4 +304,110 @@ if (guiShow)
})
.Newline()
;

////////////////////////////////////////////////////////////////////////////
// Emissive spheres
var _sphereExists = instance_exists(sphere);

gui.Text("Emissive spheres:")
.Newline()
.Slider("sphere-color-r", _sphereExists ? sphere.color[0] : 0.0, {
Width: 62,
Min: 0,
Max: 255,
Round: true,
OnChange: method(self, function (_value) {
if (instance_exists(sphere))
{
sphere.color[@ 0] = _value;
}
}),
})
.Move(7)
.Slider("sphere-color-g", _sphereExists ? sphere.color[1] : 0.0, {
Width: 62,
Min: 0,
Max: 255,
Round: true,
OnChange: method(self, function (_value) {
if (instance_exists(sphere))
{
sphere.color[@ 1] = _value;
}
}),
})
.Move(7)
.Slider("sphere-color-b", _sphereExists ? sphere.color[2] : 0.0, {
Label: "Color",
Width: 62,
Min: 0,
Max: 255,
Round: true,
OnChange: method(self, function (_value) {
if (instance_exists(sphere))
{
sphere.color[@ 2] = _value;
}
}),
})
.Newline()
.Slider("sphere-color-a", _sphereExists ? sphere.color[3] : 0.0, {
Label: "Intensity",
OnChange: method(self, function (_value) {
if (instance_exists(sphere))
{
sphere.color[@ 3] = _value;
}
}),
})
.Newline()
.Slider("sphere-distance", sphereDistance, {
Label: "Distance",
Min: 1.0,
Max: 20.0,
OnChange: method(self, function (_value) { sphereDistance = _value; }),
})
.Newline()
.Button("Spawn", {
Width: 100,
OnClick: method(self, function () {
sphere = instance_create_layer(0, 0, layer, OSphere);
}),
})
.Newline()
.Button("Place", {
Width: 100,
OnClick: method(self, function () {
sphere = noone;
}),
})
.Newline()
.Button("Destroy", {
Width: 100,
OnClick: method(self, function () {
if (instance_exists(sphere))
{
instance_destroy(sphere);
sphere = noone;
}
else
{
with (OSphere)
{
instance_destroy();
break;
}
}
}),
})
.Newline()
.Button("Destroy all", {
Width: 100,
OnClick: method(self, function () {
instance_destroy(OSphere);
sphere = noone;
}),
})
.Newline()
;
}
52 changes: 16 additions & 36 deletions objects/omain/Step_0.gml
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
if (keyboard_check_pressed(vk_f1))
{
guiShow = !guiShow;
}

if (keyboard_check_pressed(vk_space))
{
ssgiEnabled = !ssgiEnabled;
}

////////////////////////////////////////////////////////////////////////////////
// Camera controls
// Controls
var _mouseX = window_mouse_get_x();
var _mouseY = window_mouse_get_y();

Expand Down Expand Up @@ -68,6 +58,14 @@ ssgi.Fov = fov;
camera_set_proj_mat(camera, matrix_build_projection_perspective_fov(
-fov, -_aspectRatio, 0.1, clipFar));

if (instance_exists(sphere))
{
var _dcosDirectionUp = dcos(directionUp);
sphere.x = x + lengthdir_x(sphereDistance * _dcosDirectionUp, direction);
sphere.y = y + lengthdir_y(sphereDistance * _dcosDirectionUp, direction);
sphere.z = z - lengthdir_y(sphereDistance, directionUp);
}

////////////////////////////////////////////////////////////////////////////////
// Shadowmap
shadowmapView = matrix_build_lookat(
Expand All @@ -81,39 +79,21 @@ shadowmapView = matrix_build_lookat(
shadowmapProjection = matrix_build_projection_ortho(
shadowmapArea, shadowmapArea, -shadowmapArea * 0.5, shadowmapArea * 0.5);
shadowmapViewProjection = matrix_multiply(shadowmapView, shadowmapProjection);


////////////////////////////////////////////////////////////////////////////////
// Spawn spheres
if (mouse_check_button_pressed(mb_left)
&& !gui.MouseOverUI)
if (keyboard_check_pressed(vk_f1))
{
sphere = instance_create_layer(0, 0, layer, OSphere);
guiShow = !guiShow;
}

if (instance_exists(sphere))
if (keyboard_check_pressed(vk_f2))
{
if (!keyboard_check(vk_control))
{
sphereDistance += (mouse_wheel_up() - mouse_wheel_down()) * 0.25;
}

var _dcosDirectionUp = dcos(directionUp);
sphere.x = x + lengthdir_x(sphereDistance * _dcosDirectionUp, direction);
sphere.y = y + lengthdir_y(sphereDistance * _dcosDirectionUp, direction);
sphere.z = z - lengthdir_y(sphereDistance, directionUp);
screenshotMode = !screenshotMode;
}

if (keyboard_check_pressed(vk_backspace))
if (keyboard_check_pressed(vk_space))
{
if (instance_exists(sphere))
{
instance_destroy(sphere);
sphere = noone;
}
else
{
instance_destroy(OSphere);
}
ssgiEnabled = !ssgiEnabled;
}

gui.Update();
2 changes: 1 addition & 1 deletion objects/osphere/Create_0.gml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
z = 0;
color = make_color_hsv(irandom(255), 255, 255);
color = [255, 255, 255, 1.0];
scale = 0.25;
43 changes: 26 additions & 17 deletions scripts/cgui/cgui.gml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ function CGUI() constructor
return self;
};

/// @func DrawTextShadow(_text[, _color[, _alpha[, _shadowColor[, _shadowAlpha]]]])
///
/// @desc
///
/// @param {String} _text
/// @param {Constant.Color} [_color]
/// @param {Real} [_alpha]
/// @param {Constant.Color} [_shadowColor]
/// @param {Real} [_shadowAlpha]
///
/// @return {Struct.CGUI} Returns `self`.
static DrawTextShadow = function (_text, _color=c_white, _alpha=1.0, _shadowColor=c_black, _shadowAlpha=1.0) {
gml_pragma("forceinline");
var _textY = DrawY + floor((LineHeight - string_height(_text)) / 2);
if (_shadowAlpha > 0.0)
{
draw_text_color(DrawX + 1, _textY + 1, _text,
_shadowColor, _shadowColor, _shadowColor, _shadowColor, _shadowAlpha);
}
draw_text_color(DrawX, _textY, _text, _color, _color, _color, _color, _alpha);
return self;
};

/// @func Text(_text[, _props])
///
/// @desc
Expand All @@ -98,11 +121,7 @@ function CGUI() constructor
/// @return {Struct.CGUI} Returns `self.`
static Text = function (_text, _props={}) {
var _color = _props[$ "Color"] ?? c_white;
draw_text_color(
DrawX,
DrawY + floor((LineHeight - string_height(_text)) / 2),
_text,
_color, _color, _color, _color, 1.0);
DrawTextShadow(_text, _color);
return self;
};

Expand Down Expand Up @@ -193,12 +212,7 @@ function CGUI() constructor
if (_label != undefined)
{
DrawX += 8;

draw_text(
DrawX,
DrawY + floor((_height - string_height(_label)) / 2),
_label);

DrawTextShadow(_label);
DrawX += string_width(_label);
}

Expand Down Expand Up @@ -282,12 +296,7 @@ function CGUI() constructor
if (_label != undefined)
{
DrawX += 8;

draw_text(
DrawX,
DrawY + floor((_height - string_height(_label)) / 2),
_label);

DrawTextShadow(_label);
DrawX += string_width(_label);
}

Expand Down
4 changes: 2 additions & 2 deletions scripts/ssao/ssao.gml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ function SSAO() constructor
DepthRange = 1.0;

/// @var {Real} Maximum depth difference of samples when blurring SSAO.
/// Default value is 0.1.
BlurDepthRange = 0.1;
/// Default value is 0.2.
BlurDepthRange = 0.2;

/// @var {Id.Sprite}
/// @private
Expand Down
4 changes: 2 additions & 2 deletions shaders/shgbuffer/shgbuffer.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ varying vec2 v_vTexCoord;

uniform float u_fClipFar;
uniform sampler2D u_texBestFitNormals;
uniform vec3 u_vEmissive;
uniform vec4 u_vEmissive;

/// @source http://advances.realtimerendering.com/s2010/Kaplanyan-CryEngine3(SIGGRAPH%202010%20Advanced%20RealTime%20Rendering%20Course).pdf
vec3 xBestFitNormal(vec3 normal, sampler2D tex)
Expand Down Expand Up @@ -49,5 +49,5 @@ void main()
gl_FragData[0] = base;
gl_FragData[1] = vec4(xEncodeDepth(v_vPosition.z / u_fClipFar), 1.0);
gl_FragData[2] = vec4(xBestFitNormal(v_vNormal, u_texBestFitNormals) * 0.5 + 0.5, 1.0);
gl_FragData[3] = vec4(u_vEmissive, 1.0);
gl_FragData[3] = vec4(u_vEmissive.rgb * u_vEmissive.a, 1.0);
}

0 comments on commit d2a1e33

Please sign in to comment.