Skip to content

Commit

Permalink
Merge pull request #224 from TokisanGames/separate-material
Browse files Browse the repository at this point in the history
Separate material from storage to a new savable resource
  • Loading branch information
TokisanGames authored Oct 13, 2023
2 parents d9b5bb2 + d08d1fb commit 1413e11
Show file tree
Hide file tree
Showing 36 changed files with 1,415 additions and 1,146 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The creation of this plugin is thanks to the following contributors.

* Cory Petkovsek @TokisanGames
* Roope Palmroos @outobugi
* Cory Petkovsek @TokisanGames
* Loïc Chen (陳明佑) @painfulexistence
* Lorenz Wildberg @lw64
7 changes: 2 additions & 5 deletions LICENSE → LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
MIT License

Copyright (c) 2023 Roope Palmroos, Cory Petkovsek, and Contributors. All Rights Reserved.
See CONTRIBUTORS.md
Copyright (c) 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -19,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ Geometry clipmap mesh code created by [Mike J. Savage](https://mikejsavage.co.uk

## License

This plugin has been released under the [MIT License](https://github.com/TokisanGames/Terrain3D/blob/main/LICENSE).
This plugin has been released under the [MIT License](https://github.com/TokisanGames/Terrain3D/blob/main/LICENSE.txt).

9 changes: 8 additions & 1 deletion Terrain3D.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,32 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="src\constants.h" />
<ClInclude Include="src\generated_tex.h" />
<ClInclude Include="src\geoclipmap.h" />
<ClInclude Include="src\register_types.h" />
<ClInclude Include="src\terrain_3d.h" />
<ClInclude Include="src\terrain_3d_editor.h" />
<ClInclude Include="src\terrain_3d_logger.h" />
<ClInclude Include="src\logger.h" />
<ClInclude Include="src\util.h" />
<ClInclude Include="src\terrain_3d_material.h" />
<ClInclude Include="src\terrain_3d_storage.h" />
<ClInclude Include="src\terrain_3d_surface.h" />
<ClInclude Include="src\terrain_3d_texture.h" />
<ClInclude Include="src\terrain_3d_texture_list.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\generated_tex.cpp" />
<ClCompile Include="src\geoclipmap.cpp" />
<ClCompile Include="src\register_types.cpp" />
<ClCompile Include="src\terrain_3d.cpp" />
<ClCompile Include="src\terrain_3d_editor.cpp" />
<ClCompile Include="src\terrain_3d_material.cpp" />
<ClCompile Include="src\terrain_3d_storage.cpp" />
<ClCompile Include="src\terrain_3d_surface.cpp" />
<ClCompile Include="src\terrain_3d_texture.cpp" />
<ClCompile Include="src\terrain_3d_texture_list.cpp" />
<ClCompile Include="src\util.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="project\addons\terrain_3d\plugin.cfg" />
Expand Down
12 changes: 12 additions & 0 deletions Terrain3D.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<ClInclude Include="src\terrain_3d_texture.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\terrain_3d_material.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\generated.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\geoclipmap.cpp">
Expand All @@ -68,6 +74,12 @@
<ClCompile Include="src\terrain_3d_texture_list.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\terrain_3d_material.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\generated.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="src\shaders\world_noise.glsl" />
Expand Down
2 changes: 1 addition & 1 deletion Terrain3D.vcxproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ShowAllFiles>false</ShowAllFiles>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommand>c:\gd\bin\Godot_v4.1-stable_win64_console.exe</LocalDebuggerCommand>
<LocalDebuggerCommand>c:\gd\bin\Godot_v4.1.2-stable_win64.exe</LocalDebuggerCommand>
<LocalDebuggerCommandArguments>-e project.godot</LocalDebuggerCommandArguments>
<LocalDebuggerWorkingDirectory>project</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class ListEntry extends VBoxContainer:
resource = res
if resource:
resource.setting_changed.connect(_on_texture_changed)
resource.texture_changed.connect(_on_texture_changed)
resource.file_changed.connect(_on_texture_changed)

if button_clear:
button_clear.set_visible(resource != null)
Expand Down
5 changes: 2 additions & 3 deletions project/addons/terrain_3d/editor/editor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ func is_terrain_valid() -> bool:
return valid


func update_texture_dock() -> void:

func update_texture_dock(args: Array) -> void:
texture_dock.clear()

if is_terrain_valid() and terrain.texture_list:
Expand Down Expand Up @@ -230,7 +229,7 @@ func _load_textures() -> void:
if terrain and terrain.texture_list:
if not terrain.texture_list.textures_changed.is_connected(update_texture_dock):
terrain.texture_list.textures_changed.connect(update_texture_dock)
update_texture_dock()
update_texture_dock(Array())


func _load_storage() -> void:
Expand Down
2 changes: 1 addition & 1 deletion project/addons/terrain_3d/tools/importer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func start_import(value: bool) -> void:
var img: Image
if height_file_name:
img = Terrain3DStorage.load_image(height_file_name, ResourceLoader.CACHE_MODE_IGNORE, r16_range, r16_size)
min_max = Terrain3DStorage.get_min_max(img)
min_max = Terrain3D.get_min_max(img)
imported_images[Terrain3DStorage.TYPE_HEIGHT] = img
if control_file_name:
img = Terrain3DStorage.load_image(control_file_name, ResourceLoader.CACHE_MODE_IGNORE)
Expand Down
19 changes: 19 additions & 0 deletions src/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

#ifndef CONSTANTS_CLASS_H
#define CONSTANTS_CLASS_H

//////////////////////////////////////
// Macro Constants & Syntactic Sugar
//////////////////////////////////////

#define RS RenderingServer::get_singleton()

#define COLOR_ZERO Color(0.0f, 0.0f, 0.0f, 0.0f)
#define COLOR_BLACK Color(0.0f, 0.0f, 0.0f, 1.0f)
#define COLOR_WHITE Color(1.0f, 1.0f, 1.0f, 1.0f)
#define COLOR_ROUGHNESS Color(1.0f, 1.0f, 1.0f, 0.5f)
#define COLOR_CHECKED Color(1.f, 1.f, 1.0f, -1.0f)
#define COLOR_NORMAL Color(0.5f, 0.5f, 1.0f, 1.0f)

#endif CONSTANTS_CLASS_H
46 changes: 46 additions & 0 deletions src/generated_tex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

#include <godot_cpp/classes/rendering_server.hpp>

#include "generated_tex.h"
#include "logger.h"

///////////////////////////
// Public Functions
///////////////////////////

void GeneratedTex::create(const TypedArray<Image> &p_layers) {
if (!p_layers.is_empty()) {
if (Terrain3D::_debug_level >= DEBUG) {
LOG(DEBUG, "RenderingServer creating Texture2DArray, layers size: ", p_layers.size());
for (int i = 0; i < p_layers.size(); i++) {
Ref<Image> img = p_layers[i];
LOG(DEBUG, i, ": ", img, ", empty: ", img->is_empty(), ", size: ", img->get_size(), ", format: ", img->get_format());
}
}
_rid = RS->texture_2d_layered_create(p_layers, RenderingServer::TEXTURE_LAYERED_2D_ARRAY);
_dirty = false;
} else {
clear();
}
}

void GeneratedTex::create(const Ref<Image> &p_image) {
LOG(DEBUG, "RenderingServer creating Texture2D");
_image = p_image;
_rid = RS->texture_2d_create(_image);
_dirty = false;
}

void GeneratedTex::clear() {
if (_rid.is_valid()) {
LOG(DEBUG, "GeneratedTex freeing ", _rid);
RS->free_rid(_rid);
}
if (_image.is_valid()) {
LOG(DEBUG, "GeneratedTex unref image", _image);
_image.unref();
}
_rid = RID();
_dirty = true;
}
26 changes: 26 additions & 0 deletions src/generated_tex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

#ifndef GENERATEDTEX_CLASS_H
#define GENERATEDTEX_CLASS_H

#include <godot_cpp/classes/image.hpp>

using namespace godot;

class GeneratedTex {
private:
static inline const char *__class__ = "Terrain3DGeneratedTex";
RID _rid = RID();
Ref<Image> _image;
bool _dirty = false;

public:
void clear();
bool is_dirty() { return _dirty; }
void create(const TypedArray<Image> &p_layers);
void create(const Ref<Image> &p_image);
Ref<Image> get_image() const { return _image; }
RID get_rid() { return _rid; }
};

#endif // GENERATEDTEX_CLASS_H
13 changes: 7 additions & 6 deletions src/geoclipmap.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright © 2023 Roope Palmroos, Cory Petkovsek, and Contributors. All rights reserved. See LICENSE.
#include <godot_cpp/core/class_db.hpp>
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

#include <godot_cpp/classes/rendering_server.hpp>

#include "geoclipmap.h"
#include "terrain_3d_logger.h"
#include "logger.h"

///////////////////////////
// Private Functions
Expand All @@ -19,11 +20,11 @@ RID GeoClipMap::_create_mesh(PackedVector3Array p_vertices, PackedInt32Array p_i
arrays[RenderingServer::ARRAY_INDEX] = p_indices;

LOG(DEBUG, "Creating mesh via the Rendering server");
RID mesh = RenderingServer::get_singleton()->mesh_create();
RenderingServer::get_singleton()->mesh_add_surface_from_arrays(mesh, RenderingServer::PRIMITIVE_TRIANGLES, arrays);
RID mesh = RS->mesh_create();
RS->mesh_add_surface_from_arrays(mesh, RenderingServer::PRIMITIVE_TRIANGLES, arrays);

LOG(DEBUG, "Setting custom aabb: ", p_aabb.position, ", ", p_aabb.size);
RenderingServer::get_singleton()->mesh_set_custom_aabb(mesh, p_aabb);
RS->mesh_set_custom_aabb(mesh, p_aabb);

return mesh;
}
Expand Down
6 changes: 3 additions & 3 deletions src/geoclipmap.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright © 2023 Roope Palmroos, Cory Petkovsek, and Contributors. All rights reserved. See LICENSE.
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

#ifndef GEOCLIPMAP_CLASS_H
#define GEOCLIPMAP_CLASS_H

#include <godot_cpp/classes/rendering_server.hpp>
#include <godot_cpp/templates/vector.hpp>

using namespace godot;

class GeoClipMap {
private:
static inline const char *__class__ = "GeoClipMap";
static inline const char *__class__ = "Terrain3DGeoClipMap";

static inline int _patch_2d(int x, int y, int res);
static RID _create_mesh(PackedVector3Array p_vertices, PackedInt32Array p_indices, AABB p_aabb);
Expand Down
16 changes: 12 additions & 4 deletions src/terrain_3d_logger.h → src/logger.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright © 2023 Roope Palmroos, Cory Petkovsek, and Contributors. All rights reserved. See LICENSE.
#ifndef TERRAIN3D_LOGGER_CLASS_H
#define TERRAIN3D_LOGGER_CLASS_H
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

#ifndef LOGGER_CLASS_H
#define LOGGER_CLASS_H

#include <godot_cpp/variant/utility_functions.hpp>

#include "terrain_3d.h"

Expand All @@ -9,6 +12,11 @@
* Regular messages are filtered based on the user specified debug level.
* Warnings and errors always print except in release builds.
* DEBUG_CONT is for continuously called prints like inside snapping
*
* Note that in DEBUG mode Godot will crash on quit due to an
* access violation in editor_log.cpp EditorLog::_process_message().
* This is most likely caused by us printing messages as Godot is
* attempting to quit.
*/
#define MESG -1 // Always print
#define ERROR 0
Expand All @@ -25,4 +33,4 @@
else if (Terrain3D::_debug_level >= level) \
UtilityFunctions::print(__class__, "::", __func__, ": ", __VA_ARGS__)

#endif // TERRAIN3D_LOGGER_CLASS_H
#endif // LOGGER_CLASS_H
7 changes: 5 additions & 2 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright © 2023 Roope Palmroos, Cory Petkovsek, and Contributors. All rights reserved. See LICENSE.
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

#include <gdextension_interface.h>
#include <godot_cpp/core/class_db.hpp>
#include <godot_cpp/core/defs.hpp>
Expand All @@ -7,6 +8,7 @@
#include "register_types.h"
#include "terrain_3d.h"
#include "terrain_3d_editor.h"
#include "terrain_3d_material.h"
#include "terrain_3d_storage.h"

using namespace godot;
Expand All @@ -16,8 +18,9 @@ void initialize_terrain_3d(ModuleInitializationLevel p_level) {
return;
}
ClassDB::register_class<Terrain3D>();
ClassDB::register_class<Terrain3DMaterial>();
ClassDB::register_class<Terrain3DStorage>();
ClassDB::register_class<Terrain3DSurface>(); // DEPRECATED 0.8.3, remove 0.9-1.0
ClassDB::register_class<Terrain3DSurface>(); // DEPRECATED 0.8.3, remove 0.9
ClassDB::register_class<Terrain3DTexture>();
ClassDB::register_class<Terrain3DTextureList>();
ClassDB::register_class<Terrain3DEditor>();
Expand Down
3 changes: 2 additions & 1 deletion src/register_types.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright © 2023 Roope Palmroos, Cory Petkovsek, and Contributors. All rights reserved. See LICENSE.
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

#ifndef TERRAIN3D_REGISTER_TYPES_H
#define TERRAIN3D_REGISTER_TYPES_H

Expand Down
2 changes: 2 additions & 0 deletions src/shaders/debug_views.glsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

R"(
//INSERT: DEBUG_CHECKERED
// Show a checkered grid
Expand Down
2 changes: 2 additions & 0 deletions src/shaders/main.glsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

R"(shader_type spatial;
render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx;

Expand Down
2 changes: 2 additions & 0 deletions src/shaders/world_noise.glsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright © 2023 Cory Petkovsek, Roope Palmroos, and Contributors.

R"(

//INSERT: WORLD_NOISE1
Expand Down
Loading

0 comments on commit 1413e11

Please sign in to comment.