Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

木を作る

これは、木や他の種類の植生をゼロから作成する方法に関する短いチュートリアルです。

The aim is to not focus on the modeling techniques (there are plenty of tutorials about that), but how to make them look good in Godot.

../../_images/tree_sway.gif

木から始める

SketchFabからこのツリーを取得しました:

../../_images/tree_base.png

https://sketchfab.com/models/ea5e6ed7f9d6445ba69589d503e8cebf

Blenderで開きました。

頂点カラーでペイントする

The first thing you may want to do is to use the vertex colors to paint how much the tree will sway when there is wind. Just use the vertex color painting tool of your favorite 3D modeling program and paint something like this:

../../_images/tree_vertex_paint.png

これは少し誇張されていますが、考え方は、色がツリーのすべての部分に影響する揺れの度合いを示すというものです。ここのこのスケールはそれをよりよく表しています:

../../_images/tree_gradient.png

葉のカスタムシェーダーを作成する

This is an example of a shader for leaves:

shader_type spatial;
render_mode depth_prepass_alpha, cull_disabled, world_vertex_coords;

これはSpatialシェーダーです。フロント/バックのカリングはありません(したがって、葉は両側から見ることができます)。また、アルファプリパスが使用されるため、透明度を使用することで生じる深度アーティファクトが少なくなります(影を落とします)。最後に、揺れ効果のために、ワールド座標が推奨されます。そのため、ツリーは複製、移動などができ、他のツリーと一緒に機能します。

uniform sampler2D texture_albedo : source_color;
uniform vec4 transmission : source_color;

ここでは、テクスチャと透過色が読み取られます。透過色は、葉にバックライトを追加して、サブサーフェススキャタリングをシミュレートするために使用されます。

uniform float sway_speed = 1.0;
uniform float sway_strength = 0.05;
uniform float sway_phase_len = 8.0;

void vertex() {
    float strength = COLOR.r * sway_strength;
    VERTEX.x += sin(VERTEX.x * sway_phase_len * 1.123 + TIME * sway_speed) * strength;
    VERTEX.y += sin(VERTEX.y * sway_phase_len + TIME * sway_speed * 1.12412) * strength;
    VERTEX.z += sin(VERTEX.z * sway_phase_len * 0.9123 + TIME * sway_speed * 1.3123) * strength;
}

これは、葉の揺れを作成するコードです。これは基本なものです(時間と軸の位置を乗算する正弦波を使用しますが、うまく機能します)。強度に色が乗算されていることに注意してください。すべての軸は、1.0に近い小さな乗算係数を使用するため、軸は同期して表示されません。

Finally, all that's left is the fragment shader:

void fragment() {
    vec4 albedo_tex = texture(texture_albedo, UV);
    ALBEDO = albedo_tex.rgb;
    ALPHA = albedo_tex.a;
    METALLIC = 0.0;
    ROUGHNESS = 1.0;
    TRANSMISSION = transmission.rgb;
}

そして、これでほぼ全てです。

幹のシェーダ-は、アルファチャネルに書き込まない(したがってアルファ プリパスは必要ありません)、転送を必要としない点を除いて、同様です。両方のシェーダーは、通常のマッピング、AO、その他のマップを追加することで改善できます。

シェーダーの改善

これを行うための方法に関するリソースは、色々と数多くあります。基本はわかったので、GPU Gems3の、Crysis によるこの方法に関する章でを読むことをお勧めします(他の多くの手法は廃止されているため、主に揺らぎのコードに焦点を当てています)。

https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch16.html