From deae37e6eb4b016ef2f9ead1f1281e37fbee284a Mon Sep 17 00:00:00 2001 From: Jamie Pate Date: Sun, 17 Mar 2024 19:51:58 -0700 Subject: [PATCH] Fix support for GLES2 uints don't work in GLES2, so just strip out the phasing effect for the shader if we encounter GLES2 --- addons/MagicaVoxelImporter/VoxelMesh.gd | 18 ++++++++++++++++++ addons/MagicaVoxelImporter/points.shader | 8 ++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/addons/MagicaVoxelImporter/VoxelMesh.gd b/addons/MagicaVoxelImporter/VoxelMesh.gd index c84704f..601a4d7 100644 --- a/addons/MagicaVoxelImporter/VoxelMesh.gd +++ b/addons/MagicaVoxelImporter/VoxelMesh.gd @@ -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 = [] diff --git a/addons/MagicaVoxelImporter/points.shader b/addons/MagicaVoxelImporter/points.shader index e92ef10..11ac473 100644 --- a/addons/MagicaVoxelImporter/points.shader +++ b/addons/MagicaVoxelImporter/points.shader @@ -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; @@ -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) { @@ -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) {