Skip to content

Commit

Permalink
Fix support for GLES2
Browse files Browse the repository at this point in the history
uints don't work in GLES2, so just strip out the phasing effect for the
shader if we encounter GLES2
  • Loading branch information
jamie-pate committed Mar 18, 2024
1 parent 77c0ec8 commit deae37e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 18 additions & 0 deletions addons/MagicaVoxelImporter/VoxelMesh.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ export(float, 0.0, 1.0) var phase_shift setget _set_phase_shift
func _ready():
_set_neck_height(neck_height)
_set_render_head(true)
if OS.get_current_video_driver() == OS.VIDEO_DRIVER_GLES2:
_strip_gles3_from_shader()


func _strip_gles3_from_shader():
if Engine.editor_hint:
return
var mat = mesh.surface_get_material(0) as ShaderMaterial
if !mat || mat.shader.has_meta('gles3_stripped'):
return
var strip_expr = RegEx.new()
strip_expr.compile("(?s)\\/\\/ #ifndef GLES3.+?\\/\\/ #endif")
var code = mat.shader.code
var stripped_code = strip_expr.sub(code, '', true)
if code != stripped_code:
mat.shader.code = stripped_code
mat.shader.set_meta('gles3_stripped', true)


func _get_mats() -> Array:
var result = []
Expand Down
8 changes: 6 additions & 2 deletions addons/MagicaVoxelImporter/points.shader
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ uniform float lod_bias = 1.0;
// limiting this is somehow faster than allowing the whole mesh to be discarded.
uniform float lod_worst = 5.0;
// multiply a uint by this number to get a range from 0 .. 2.0
// #ifndef GLES3
const float MAX_UINT_INV_2 = 2.0 / float(~0u);
// #endif
const float PHASE_TRANSLATE = 0.01;
const float PHASE_SHRINK = 0.25;

// #ifndef GLES3
uint pcg_hash(uint input) {
// https://www.reedbeta.com/blog/hash-functions-for-gpu-rendering/
uint state = input * 747796405u + 2891336453u;
Expand All @@ -53,7 +56,7 @@ vec3 phase_shift_vertex(vec3 value, float amount) {
phase_shift_component(value.z + value.y + amount, value.z, amount)
);
}

// #endif

void vertex() {
if (!render_head && VERTEX.y > neck_height) {
Expand Down Expand Up @@ -143,13 +146,14 @@ void vertex() {
mod(VERTEX.x, reduction) >= 1.0 && mod(VERTEX.y, reduction) >= 1.0 && mod(VERTEX.z + 1.0, reduction) >= 1.0
) {
voxel_size = vec2(0.0);
const uint MAX_UINT = ~0u;
}
}
// #ifndef GLES3
if (phase_shift > 0.0) {
VERTEX = phase_shift_vertex(VERTEX, phase_shift * voxel_size.x * PHASE_TRANSLATE);
voxel_size *= 1.0 - phase_shift * (1.0 - PHASE_SHRINK);
}
// #endif
POINT_SIZE = max(voxel_size.x, voxel_size.y);

if (show_normals > 0.0) {
Expand Down

0 comments on commit deae37e

Please sign in to comment.