Skip to content

Instantly share code, notes, and snippets.

View HungryProton's full-sized avatar

HungryProton HungryProton

View GitHub Profile
@HungryProton
HungryProton / format_values.gd
Last active October 8, 2023 10:35
Print large and small numbers with their prefix multipliers
func format_value(value: float) -> String:
var result := ""
var negative := value < 0.0
value = abs(value)
if _last_value > 1000.0:
var scale = ["K", "M", "G", "T", "P", "E"]
var v = _last_value
var index = -1
while v > 1000.0 and index < (scale.size() - 1):
@HungryProton
HungryProton / autosmooth.gd
Last active July 8, 2023 14:46
Autosmooth meshes in GDScript
class_name MeshUtils
static func auto_smooth(mesh: Mesh, threshold_degrees := 30.0) -> Mesh:
var result := ArrayMesh.new()
var threshold := deg_to_rad(threshold_degrees)
var sanitized_mesh := merge_duplicate_vertices(mesh)
# Auto smooth each surfaces.
@HungryProton
HungryProton / underwater.gdshader
Created June 7, 2023 17:42
Under water shader with caustics
shader_type spatial;
uniform sampler2D depth_texture : hint_depth_texture, repeat_disable, filter_nearest;
uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
uniform float v_depth_offset = 0.0;
uniform float v_depth_fade = 1.0;
uniform sampler2D underwater_color : repeat_disable;
uniform sampler2D caustics_texture : hint_default_black;
@HungryProton
HungryProton / depth_override_shader.gdshader
Last active March 29, 2024 12:45
A Godot 4 shader to make things appear on top of other things within a range.
// A Godot 4 shader to make things appear on top of other things within a range.
// Initially, this was made so my characters' facial features would be rendered on top of their hair.
shader_type spatial;
render_mode unshaded;
uniform sampler2D depth_texture : hint_depth_texture, repeat_disable, filter_nearest;
// Maximum depth we can overdraw relative to the object original depth, in ENGINE UNITS.
@HungryProton
HungryProton / box_gizmo_plugin.gd
Last active February 1, 2023 10:14
Box gizmo example
extends EditorSpatialGizmoPlugin
var editor_plugin: EditorPlugin
var _previous_size
func _init():
create_material("lines", Color(1, 1, 1))
create_material("box", Color(1.0, 1.0, 1.0, 0.1))
@HungryProton
HungryProton / shader_cache.gd
Last active February 2, 2023 03:46
Shader Cache v2
class_name ShaderCache
extends Spatial
signal stage_completed
signal all_shaders_compiled
export var shaders_folder := "res://material/shaders/"
export var particles_folder := "res://vfx/"
@HungryProton
HungryProton / grid_shape.gd
Created July 21, 2021 12:04
Editor Inspector plugin example
tool
extends Resource
class_name GridShape
export var size := Vector2.ONE
export var grid := []
func _init() -> void:
@HungryProton
HungryProton / box.gd
Last active April 20, 2022 05:29
Spatial gizmo example - A box you can resize
tool
class_name ConceptBoxInput
extends Spatial
signal input_changed
signal property_changed
export var size := Vector3.ONE setget set_size
@HungryProton
HungryProton / smoke_shader.shader
Last active August 4, 2022 17:19
Godot smoke shader test
shader_type spatial;
uniform sampler2D mask;
uniform float min_scale = 0.0;
uniform float max_scale = 1.0;
varying float scale;
float range_lerp(float value, float istart, float istop, float ostart, float oend) {
@HungryProton
HungryProton / color_space_conversion.shader
Created September 16, 2020 10:55
convert from rgb to srgb and the other way around in godot shaders
float lts(float L) {
if (abs(L) < 0.0031308) {
return 12.92 * L;
}
return 1.055 * pow(L, (1.0 / 2.4)) - 0.055;
}
vec3 rgb_to_srgb(vec3 color) {
return vec3(lts(color.r), lts(color.g), lts(color.b));
}