Skip to content

Commit

Permalink
Find takes probe volume into account
Browse files Browse the repository at this point in the history
  • Loading branch information
pkraif committed May 2, 2023
1 parent 17fe39c commit b8c2bfb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion BBMOD_GML/objects/OMain/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,5 @@ var _probeY = _terrainHeight / 2;
var _probeZ = global.terrain.get_height(_probeX, _probeY) + 20;

var _reflectionProbe = new BBMOD_ReflectionProbe(new BBMOD_Vec3(_probeX, _probeY, _probeZ));
_reflectionProbe.Size = new BBMOD_Vec3(_terrainWidth / 2 + 100, _terrainHeight / 2 + 100, 1000);
_reflectionProbe.set_size(new BBMOD_Vec3(_terrainWidth / 2 + 100, _terrainHeight / 2 + 100, 1000));
bbmod_reflection_probe_add(_reflectionProbe);
21 changes: 14 additions & 7 deletions BBMOD_GML/scripts/BBMOD_BaseRenderer/BBMOD_BaseRenderer.gml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ function BBMOD_BaseRenderer()
/// @private
__surFinal = -1;

/// @var {Struct.BBMOD_Cubemap} For reflection probe capture.
/// @private
static __cubemap = new BBMOD_Cubemap(128);

/// @var {Id.Surface} For reflection probe capture.
/// @private
__surProbe1 = -1;

/// @var {Id.Surface} For reflection probe capture.
/// @private
__surProbe2 = -1;

/// @func get_width()
///
/// @desc Retrieves the width of the renderer on the screen.
Expand Down Expand Up @@ -372,11 +384,6 @@ function BBMOD_BaseRenderer()
return self;
};

static __cubemap = new BBMOD_Cubemap(128);

__surProbe1 = -1;
__surProbe2 = -1;

/// @func __render_reflection_probes()
///
/// @desc
Expand Down Expand Up @@ -442,8 +449,8 @@ function BBMOD_BaseRenderer()

var _to = (global.__bbmodImageBasedLight != undefined)
? global.__bbmodImageBasedLight.Texture
: sprite_get_texture(BBMOD_SprBlack, 0);

: sprite_get_texture(BBMOD_SprBlack, 0);

var _reflectionProbe = bbmod_reflection_probe_find(bbmod_camera_get_position());
if (_reflectionProbe != undefined)
{
Expand Down
39 changes: 29 additions & 10 deletions BBMOD_GML/scripts/BBMOD_ReflectionProbe/BBMOD_ReflectionProbe.gml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ function BBMOD_ReflectionProbe(_position=undefined, _sprite=undefined)
/// @see BBMOD_ReflectionProbe.set_size
Size = new BBMOD_Vec3(0.5);

/// @var {Real} The volume of the reflection probe's AABB.
/// @private
__volume = 0.5 * 0.5 * 0.5;

/// @var {Bool} If `true` then the position or size has changed.
/// @private
__positionSizeChanged = true;
Expand Down Expand Up @@ -134,6 +138,7 @@ function BBMOD_ReflectionProbe(_position=undefined, _sprite=undefined)
static set_size = function (_size)
{
Size = _size;
__volume = _size.X * _size.Y * _size.Z;
__positionSizeChanged = true;
return self;
};
Expand Down Expand Up @@ -225,11 +230,13 @@ function bbmod_reflection_probe_get(_index)

/// @func bbmod_reflection_probe_find(_position)
///
/// @desc Finds an enabled reflection probe at given position.
/// @desc Finds a reflection probe that influences given position.
///
/// @param {Struct.BBMOD_Vec3} _position The position to find a reflection probe at.
/// @param {Struct.BBMOD_Vec3} _position The position to find a reflection probe
/// at.
///
/// @return {Struct.BBMOD_ReflectionProbe} The found reflection probe or `undefined`.
/// @return {Struct.BBMOD_ReflectionProbe, undefined} The found reflection probe
/// or `undefined` if none was found.
///
/// @see BBMOD_ReflectionProbe.Enabled
/// @see bbmod_reflection_probe_add
Expand All @@ -240,30 +247,42 @@ function bbmod_reflection_probe_get(_index)
/// @see bbmod_reflection_probe_clear
function bbmod_reflection_probe_find(_position)
{
// TODO: Use spatial index for reflection probes
gml_pragma("forceinline");
var _reflectionProbes = global.__bbmodReflectionProbes;
var _probe = undefined;
var _probeVolume = infinity;
var i = 0;
repeat (array_length(_reflectionProbes))
{
with (_reflectionProbes[i])
with (_reflectionProbes[i++])
{
if (!Enabled)
{
continue;
}
var _min = Position.Sub(Size);
if (_position.X < _min.X
|| _position.Y < _min.Y
|| _position.Z < _min.Z)
{
continue;
}
var _max = Position.Add(Size);
if (_position.X < _min.X || _position.X > _max.X
|| _position.Y < _min.Y || _position.Y > _max.Y
|| _position.Z < _min.Z || _position.Z > _max.Z)
if (_position.X > _max.X
|| _position.Y > _max.Y
|| _position.Z > _max.Z)
{
continue;
}
return self;
if (__volume < _probeVolume)
{
_probe = self;
_probeVolume = __volume;
}
}
++i;
}
return undefined;
return _probe;
}

/// @func bbmod_reflection_probe_remove(_reflectionProbe)
Expand Down

0 comments on commit b8c2bfb

Please sign in to comment.