Initialize shared Metin2 SDK with rendering optimizations and tests
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
#pragma once
|
||||
|
||||
namespace speedtree {
|
||||
|
||||
// Reference GLSL / Vulkan / Metal / OpenGL 4.5+ shader sources for high-fidelity foliage rendering.
|
||||
// Can be embedded or compiled into SPIR-V.
|
||||
|
||||
inline const char* GetFoliageVertexShaderGLSL() {
|
||||
return R"glsl(
|
||||
#version 450 core
|
||||
|
||||
layout(location = 0) in vec3 in_Position;
|
||||
layout(location = 1) in vec3 in_Normal;
|
||||
layout(location = 2) in vec2 in_TexCoord;
|
||||
layout(location = 3) in vec4 in_WindWeights; // [0]:Trunk, [1]:Branch, [2]:Leaf, [3]:Phase
|
||||
layout(location = 4) in vec3 in_LeafPivotOffset; // Leaf center to vertex
|
||||
layout(location = 5) in uint in_Diffuse;
|
||||
|
||||
layout(std140, binding = 0) uniform FrameBlock {
|
||||
mat4 u_ViewProj;
|
||||
vec3 u_CameraPos;
|
||||
float u_Time;
|
||||
vec3 u_WindDir;
|
||||
float u_WindStrength;
|
||||
};
|
||||
|
||||
layout(std140, binding = 1) uniform InstanceBlock {
|
||||
mat4 u_ModelMatrix;
|
||||
vec3 u_TreeOrigin;
|
||||
float u_TreeHeight;
|
||||
};
|
||||
|
||||
layout(location = 0) out vec3 v_WorldPos;
|
||||
layout(location = 1) out vec3 v_WorldNormal;
|
||||
layout(location = 2) out vec2 v_TexCoord;
|
||||
layout(location = 3) out float v_CanopyAO;
|
||||
|
||||
vec3 RotateAngleAxis(vec3 v, vec3 axis, float angle) {
|
||||
float cosTheta = cos(angle);
|
||||
float sinTheta = sin(angle);
|
||||
return v * cosTheta + cross(axis, v) * sinTheta + axis * dot(axis, v) * (1.0 - cosTheta);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 localPos = in_Position;
|
||||
vec3 localNormal = in_Normal;
|
||||
|
||||
float trunkW = in_WindWeights.r;
|
||||
float branchW = in_WindWeights.g;
|
||||
float leafW = in_WindWeights.b;
|
||||
float phase = in_WindWeights.a;
|
||||
|
||||
// Level 0: Global Wind Gust Wave
|
||||
float gustSpatial = dot(u_TreeOrigin.xz, u_WindDir.xz * 0.04);
|
||||
float gust = sin(u_Time * 0.8 + gustSpatial) * 0.5 + 0.5;
|
||||
float effectiveWind = u_WindStrength * (0.8 + 0.4 * gust);
|
||||
|
||||
vec3 windCrossAxis = normalize(cross(vec3(0.0, 1.0, 0.0), u_WindDir));
|
||||
|
||||
// Level 1: Trunk bending (quadratic height response, length-preserving)
|
||||
float trunkAngle = (trunkW * trunkW) * effectiveWind * 0.12 * sin(u_Time * 1.2 + gustSpatial);
|
||||
localPos = RotateAngleAxis(localPos, windCrossAxis, trunkAngle);
|
||||
localNormal = RotateAngleAxis(localNormal, windCrossAxis, trunkAngle);
|
||||
|
||||
// Level 2: Branch sway
|
||||
if (branchW > 0.01) {
|
||||
float branchOsc = sin(u_Time * 2.5 + phase * 6.28 + gustSpatial * 2.0);
|
||||
float branchAngle = branchW * effectiveWind * 0.15 * branchOsc;
|
||||
localPos = RotateAngleAxis(localPos, windCrossAxis, branchAngle);
|
||||
localNormal = RotateAngleAxis(localNormal, windCrossAxis, branchAngle);
|
||||
}
|
||||
|
||||
// Level 3: Leaf flutter and tumble around leaf pivot (Leaf Shimmering!)
|
||||
if (leafW > 0.01) {
|
||||
float leafOsc = sin(u_Time * 8.0 + phase * 15.0 + dot(localPos, vec3(1.0)));
|
||||
float leafAngle = leafW * (0.2 + 0.2 * gust) * leafOsc;
|
||||
|
||||
vec3 leafRel = in_LeafPivotOffset;
|
||||
vec3 rotatedOffset = RotateAngleAxis(leafRel, u_WindDir, leafAngle);
|
||||
localPos += (rotatedOffset - leafRel);
|
||||
localNormal = RotateAngleAxis(localNormal, u_WindDir, leafAngle);
|
||||
}
|
||||
|
||||
vec3 worldPos = (u_ModelMatrix * vec4(localPos, 1.0)).xyz;
|
||||
vec3 worldNormal = normalize((u_ModelMatrix * vec4(localNormal, 0.0)).xyz);
|
||||
|
||||
// Spherical / Canopy Normal Blending (removes paper-thin leaf look)
|
||||
vec3 crownCenter = u_TreeOrigin + vec3(0.0, u_TreeHeight * 0.65, 0.0);
|
||||
vec3 sphericalNormal = normalize(worldPos - crownCenter);
|
||||
worldNormal = normalize(mix(worldNormal, sphericalNormal, leafW * 0.65));
|
||||
|
||||
// Canopy interior AO
|
||||
float distToCenter = length(worldPos - crownCenter);
|
||||
v_CanopyAO = clamp(distToCenter / (u_TreeHeight * 0.45), 0.35, 1.0);
|
||||
|
||||
v_WorldPos = worldPos;
|
||||
v_WorldNormal = worldNormal;
|
||||
v_TexCoord = in_TexCoord;
|
||||
|
||||
gl_Position = u_ViewProj * vec4(worldPos, 1.0);
|
||||
}
|
||||
)glsl";
|
||||
}
|
||||
|
||||
inline const char* GetFoliageFragmentShaderGLSL() {
|
||||
return R"glsl(
|
||||
#version 450 core
|
||||
|
||||
layout(location = 0) in vec3 v_WorldPos;
|
||||
layout(location = 1) in vec3 v_WorldNormal;
|
||||
layout(location = 2) in vec2 v_TexCoord;
|
||||
layout(location = 3) in float v_CanopyAO;
|
||||
|
||||
layout(binding = 0) uniform sampler2D u_DiffuseAtlas;
|
||||
|
||||
layout(std140, binding = 2) uniform LightBlock {
|
||||
vec3 u_SunDir;
|
||||
vec3 u_SunColor;
|
||||
vec3 u_AmbientSky;
|
||||
vec3 u_CameraPos;
|
||||
};
|
||||
|
||||
layout(location = 0) out vec4 out_Color;
|
||||
|
||||
void main() {
|
||||
vec4 texColor = texture(u_DiffuseAtlas, v_TexCoord);
|
||||
|
||||
// Alpha Cutoff
|
||||
if (texColor.a < 0.45) {
|
||||
discard;
|
||||
}
|
||||
|
||||
vec3 N = normalize(v_WorldNormal);
|
||||
vec3 L = normalize(u_SunDir);
|
||||
vec3 V = normalize(u_CameraPos - v_WorldPos);
|
||||
vec3 H = normalize(L + V);
|
||||
|
||||
// Wrap Lighting (Half-Lambert)
|
||||
float NdotL = dot(N, L);
|
||||
float wrapNdotL = max(0.0, (NdotL + 0.35) / 1.35);
|
||||
vec3 diffuse = texColor.rgb * u_SunColor * wrapNdotL;
|
||||
|
||||
// Subsurface Transmission (Sunlight penetrating leaf)
|
||||
float viewThrough = max(0.0, dot(-L, V));
|
||||
float transIntensity = pow(viewThrough, 3.5) * 0.85;
|
||||
vec3 transColor = texColor.rgb * vec3(1.1, 1.3, 0.7) * u_SunColor * transIntensity;
|
||||
|
||||
// Specular Highlight
|
||||
float NdotH = max(0.0, dot(N, H));
|
||||
float specular = pow(NdotH, 32.0) * 0.25;
|
||||
|
||||
// Canopy AO & Ambient
|
||||
vec3 ambient = u_AmbientSky * texColor.rgb * v_CanopyAO;
|
||||
|
||||
vec3 finalRGB = (diffuse + transColor + specular) * v_CanopyAO + ambient;
|
||||
out_Color = vec4(finalRGB, 1.0);
|
||||
}
|
||||
)glsl";
|
||||
}
|
||||
|
||||
} // namespace speedtree
|
||||
@@ -0,0 +1,181 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <map>
|
||||
|
||||
namespace speedtree {
|
||||
|
||||
constexpr float kPI = 3.14159265358979323846f;
|
||||
constexpr float kTAU = 2.0f * kPI;
|
||||
|
||||
struct UVRect {
|
||||
float u0 = 0.0f;
|
||||
float v0 = 0.0f;
|
||||
float u1 = 1.0f;
|
||||
float v1 = 1.0f;
|
||||
};
|
||||
|
||||
// Unified vertex layout for modern GPU foliage rendering
|
||||
// Compatible with Vulkan, Metal, DX11/12, OpenGL
|
||||
struct SpeedTreeVertex {
|
||||
float position[3]; // Local space (Z-forward / Y-up)
|
||||
float normal[3]; // Blended spherical / canopy normal
|
||||
float uv[2]; // Texture atlas coordinates
|
||||
float wind_weights[4]; // [0]: Trunk bend, [1]: Branch sway, [2]: Leaf flutter, [3]: Phase
|
||||
float leaf_pivot_offset[3]; // Vector from leaf cluster pivot to vertex (for rotation/tumble)
|
||||
uint32_t diffuse; // RGBA8 (Canopy AO in alpha/rgb, default 0xFFFFFFFF)
|
||||
};
|
||||
|
||||
// Represents a submesh (Trunk or Leaf/Canopy)
|
||||
struct SpeedTreeMesh {
|
||||
std::string name;
|
||||
std::vector<SpeedTreeVertex> vertices;
|
||||
std::vector<uint32_t> indices;
|
||||
std::string texture;
|
||||
bool is_leaf = false;
|
||||
bool is_double_sided = false;
|
||||
bool alpha_test = false;
|
||||
float alpha_ref = 0.45f;
|
||||
};
|
||||
|
||||
struct SpeedTreeSptInfo {
|
||||
bool ok = false;
|
||||
float size = 1000.0f;
|
||||
float variance = 0.0f;
|
||||
std::string bark_texture;
|
||||
std::string leaf_texture;
|
||||
std::string shadow_texture;
|
||||
std::vector<UVRect> leaf_uvs;
|
||||
std::vector<UVRect> frond_uvs;
|
||||
std::vector<UVRect> billboard_uvs;
|
||||
|
||||
static bool SniffFile(const std::string& filepath, SpeedTreeSptInfo& out);
|
||||
static bool SniffMemory(const uint8_t* data, size_t size, SpeedTreeSptInfo& out);
|
||||
static bool SniffSpt(const std::string& filepath, SpeedTreeSptInfo& out) { return SniffFile(filepath, out); }
|
||||
};
|
||||
|
||||
struct SpeedTreeProperty {
|
||||
uint32_t crc = 0;
|
||||
std::string property_name;
|
||||
std::string tree_file;
|
||||
float tree_size = 1000.0f;
|
||||
float tree_variance = 0.0f;
|
||||
|
||||
static bool LoadFromFile(const std::string& filename, SpeedTreeProperty& out);
|
||||
};
|
||||
|
||||
// Wind configuration and evaluation parameters
|
||||
struct WindParameters {
|
||||
float time = 0.0f;
|
||||
float strength = 1.0f; // 0.0: calm, 1.0: gentle breeze, 2.5: storm
|
||||
float direction[3] = {1.0f, 0.0f, 0.0f}; // Normalized wind direction
|
||||
float gust_frequency = 0.8f;
|
||||
float gust_strength = 0.5f;
|
||||
};
|
||||
|
||||
// Core Model class representing a unique species/variant of tree
|
||||
class SpeedTreeModel {
|
||||
public:
|
||||
SpeedTreeModel();
|
||||
~SpeedTreeModel() = default;
|
||||
|
||||
static std::shared_ptr<SpeedTreeModel> Create(const std::string& sptPath, float treeSize = 1000.0f) { return CreateFromSpt(sptPath, treeSize); }
|
||||
static std::shared_ptr<SpeedTreeModel> CreateFromSpt(const std::string& sptPath, float treeSize = 1000.0f);
|
||||
static std::shared_ptr<SpeedTreeModel> CreateFromMemory(const uint8_t* data, size_t size,
|
||||
const std::string& name = "", float treeSize = 1000.0f);
|
||||
static std::shared_ptr<SpeedTreeModel> CreateProcedural(const std::string& species_name, float height = 1000.0f,
|
||||
const std::string& bark_tex = "",
|
||||
const std::string& leaf_tex = "");
|
||||
|
||||
const SpeedTreeMesh& GetTrunkMesh() const { return m_trunk; }
|
||||
const SpeedTreeMesh& GetLeafMesh() const { return m_leaf; }
|
||||
|
||||
float GetHeight() const { return m_height; }
|
||||
float GetRadius() const { return m_radius; }
|
||||
bool IsGrassOrShrub() const { return m_is_grass_or_shrub; }
|
||||
const std::string& GetName() const { return m_name; }
|
||||
const std::string& GetBarkTexture() const { return m_bark_texture; }
|
||||
const std::string& GetLeafTexture() const { return m_leaf_texture; }
|
||||
const SpeedTreeSptInfo& GetSptInfo() const { return m_info; }
|
||||
|
||||
// CPU fallback vertex deformer for legacy clients/renderers without custom foliage vertex shaders
|
||||
void EvaluateCpuTransformedVertices(const SpeedTreeMesh& src_mesh,
|
||||
std::vector<float>& out_positions,
|
||||
std::vector<float>& out_normals,
|
||||
float worldX, float worldY, float worldZ,
|
||||
float yawDeg, float scale,
|
||||
const WindParameters& wind) const;
|
||||
|
||||
private:
|
||||
void BuildGeometry(const SpeedTreeSptInfo& info);
|
||||
void BuildTreeGeometry(float H, const SpeedTreeSptInfo& info);
|
||||
void BuildGrassShrubGeometry(float H, const SpeedTreeSptInfo& info);
|
||||
|
||||
std::string m_name;
|
||||
std::string m_spt_path;
|
||||
float m_height = 1000.0f;
|
||||
float m_radius = 350.0f;
|
||||
bool m_is_grass_or_shrub = false;
|
||||
|
||||
std::string m_bark_texture;
|
||||
std::string m_leaf_texture;
|
||||
SpeedTreeSptInfo m_info;
|
||||
|
||||
SpeedTreeMesh m_trunk;
|
||||
SpeedTreeMesh m_leaf;
|
||||
};
|
||||
|
||||
// Forest management and batched instance tracking
|
||||
class SpeedTreeForest {
|
||||
public:
|
||||
struct Instance {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
uint32_t crc = 0;
|
||||
float yaw = 0.0f;
|
||||
float scale = 1.0f;
|
||||
std::string name;
|
||||
std::shared_ptr<SpeedTreeModel> model;
|
||||
};
|
||||
|
||||
SpeedTreeForest();
|
||||
~SpeedTreeForest();
|
||||
|
||||
void Clear();
|
||||
void ClearMainTree();
|
||||
|
||||
bool RegisterProperty(uint32_t crc, const std::string& prtPath);
|
||||
bool RegisterPropertyData(const SpeedTreeProperty& prop);
|
||||
void ScanPropertyDirectory(const std::string& dirPath);
|
||||
|
||||
Instance* CreateInstance(float x, float y, float z, uint32_t crc,
|
||||
float yaw = 0.0f, float scale = 1.0f, const char* name = nullptr);
|
||||
void DeleteInstance(Instance* pTree);
|
||||
|
||||
void Update(float dt);
|
||||
|
||||
size_t GetInstanceCount() const { return m_instances.size(); }
|
||||
size_t GetMainTreeCount() const { return m_tree_templates.size(); }
|
||||
const std::vector<std::unique_ptr<Instance>>& GetInstances() const { return m_instances; }
|
||||
|
||||
std::shared_ptr<SpeedTreeModel> GetMainTree(uint32_t crc);
|
||||
bool HasMainTree(uint32_t crc) const;
|
||||
|
||||
void SetWindStrength(float strength) { m_wind.strength = strength; }
|
||||
float GetWindStrength() const { return m_wind.strength; }
|
||||
|
||||
void SetWindDirection(float x, float y, float z);
|
||||
const WindParameters& GetWindParams() const { return m_wind; }
|
||||
|
||||
private:
|
||||
WindParameters m_wind;
|
||||
std::map<uint32_t, SpeedTreeProperty> m_properties;
|
||||
std::map<uint32_t, std::shared_ptr<SpeedTreeModel>> m_tree_templates;
|
||||
std::vector<std::unique_ptr<Instance>> m_instances;
|
||||
};
|
||||
|
||||
} // namespace speedtree
|
||||
Reference in New Issue
Block a user