LOD System (Tutorial Part 4)
This commit is contained in:
parent
00526b9e1f
commit
37bcb52195
2
grass-stalk-simple.mtl
Normal file
2
grass-stalk-simple.mtl
Normal file
@ -0,0 +1,2 @@
|
||||
# Blender 4.4.3 MTL File: 'None'
|
||||
# www.blender.org
|
15
grass-stalk-simple.obj
Normal file
15
grass-stalk-simple.obj
Normal file
@ -0,0 +1,15 @@
|
||||
# Blender 4.4.3
|
||||
# www.blender.org
|
||||
mtllib grass-stalk-simple.mtl
|
||||
o Plane
|
||||
v -0.123385 -0.000477 0.000000
|
||||
v 0.128999 -0.000477 0.000000
|
||||
v -0.001473 1.039178 -0.000000
|
||||
vn 0.7726 0.0308 0.6342
|
||||
vn -0.0977 0.0030 0.9952
|
||||
vn -0.7726 0.0307 0.6342
|
||||
vt 0.121405 0.000035
|
||||
vt 0.056603 0.999965
|
||||
vt 0.000035 0.000035
|
||||
s 1
|
||||
f 2/1/1 3/2/2 1/3/3
|
25
grass-stalk-simple.obj.import
Normal file
25
grass-stalk-simple.obj.import
Normal file
@ -0,0 +1,25 @@
|
||||
[remap]
|
||||
|
||||
importer="wavefront_obj"
|
||||
importer_version=1
|
||||
type="Mesh"
|
||||
uid="uid://c4c5i2vg0gdt"
|
||||
path="res://.godot/imported/grass-stalk-simple.obj-a57ffbc21d988749ddd9aebd794678af.mesh"
|
||||
|
||||
[deps]
|
||||
|
||||
files=["res://.godot/imported/grass-stalk-simple.obj-a57ffbc21d988749ddd9aebd794678af.mesh"]
|
||||
|
||||
source_file="res://grass-stalk-simple.obj"
|
||||
dest_files=["res://.godot/imported/grass-stalk-simple.obj-a57ffbc21d988749ddd9aebd794678af.mesh", "res://.godot/imported/grass-stalk-simple.obj-a57ffbc21d988749ddd9aebd794678af.mesh"]
|
||||
|
||||
[params]
|
||||
|
||||
generate_tangents=true
|
||||
generate_lods=true
|
||||
generate_shadow_mesh=true
|
||||
generate_lightmap_uv2=false
|
||||
generate_lightmap_uv2_texel_size=0.2
|
||||
scale_mesh=Vector3(1, 1, 1)
|
||||
offset_mesh=Vector3(0, 0, 0)
|
||||
force_disable_mesh_compression=false
|
@ -14,14 +14,16 @@ uniform float patch_scale = 5.0;
|
||||
varying float patch_factor;
|
||||
|
||||
uniform sampler2D wind_noise;
|
||||
uniform float wind_strength = 0.1;
|
||||
uniform float wind_strength = 0.15;
|
||||
uniform vec2 wind_direction = vec2(1.0, 0.0);
|
||||
uniform float wind_bend_strength = 2.0;
|
||||
uniform float wind_ao_affect = 1.5;
|
||||
uniform float wind_ao_affect = 1.0;
|
||||
|
||||
uniform float object_radius = 1.0;
|
||||
uniform vec3 object_position;
|
||||
|
||||
instance uniform float alpha = 1.0;
|
||||
|
||||
varying float bottom_to_top;
|
||||
varying float current_wind_bend;
|
||||
varying float cut_height;
|
||||
@ -29,7 +31,7 @@ varying float cut_height;
|
||||
void vertex() {
|
||||
cut_height = 1.0;
|
||||
|
||||
// Cutting all grass on the right
|
||||
//// Cutting all grass on the right
|
||||
//if (NODE_POSITION_WORLD.x > 0.0) {
|
||||
//cut_height = 0.5;
|
||||
//
|
||||
@ -53,14 +55,14 @@ void vertex() {
|
||||
|
||||
VERTEX.xz += current_wind_bend * local_direction * wind_bend_strength;
|
||||
|
||||
// Bend away from the object
|
||||
float object_distance = distance(object_position, NODE_POSITION_WORLD);
|
||||
float bend_away_strength = max(object_radius - object_distance, 0.0) / object_radius;
|
||||
vec2 bend_direction = normalize(object_position.xz - NODE_POSITION_WORLD.xz);
|
||||
|
||||
VERTEX.xz -= (inv_model * vec4(bend_direction.x, 0.0, bend_direction.y, 0.0)).xz
|
||||
* bend_away_strength * bottom_to_top;
|
||||
VERTEX.y -= bend_away_strength * bottom_to_top * 0.5;
|
||||
//// Bend away from the object
|
||||
//float object_distance = distance(object_position, NODE_POSITION_WORLD);
|
||||
//float bend_away_strength = max(object_radius - object_distance, 0.0) / object_radius;
|
||||
//vec2 bend_direction = normalize(object_position.xz - NODE_POSITION_WORLD.xz);
|
||||
//
|
||||
//VERTEX.xz -= (inv_model * vec4(bend_direction.x, 0.0, bend_direction.y, 0.0)).xz
|
||||
//* bend_away_strength * bottom_to_top;
|
||||
//VERTEX.y -= bend_away_strength * bottom_to_top * 0.5;
|
||||
|
||||
// General appearance
|
||||
VERTEX.z += blade_bend * pow(bottom_to_top, 2.0);
|
||||
@ -68,19 +70,21 @@ void vertex() {
|
||||
patch_factor = texture(patch_noise, NODE_POSITION_WORLD.xz / patch_scale).r;
|
||||
VERTEX *= mix(size_small, size_large, patch_factor);
|
||||
|
||||
NORMAL = mix(NORMAL, vec3(0.0, 1.0, 0.0), bottom_to_top);
|
||||
NORMAL = mix(NORMAL, vec3(0.0, 1.0, 0.0), mix(1.0, bottom_to_top, alpha + 0.2));
|
||||
}
|
||||
|
||||
void fragment() {
|
||||
// Correct normals on back-faces
|
||||
if (!FRONT_FACING) NORMAL = -NORMAL;
|
||||
|
||||
|
||||
AO = bottom_to_top - current_wind_bend * wind_ao_affect;
|
||||
AO_LIGHT_AFFECT = cut_height;
|
||||
AO_LIGHT_AFFECT = mix(0.2, 1.0, alpha);
|
||||
|
||||
ALBEDO = mix(color_small, color_large, patch_factor);
|
||||
BACKLIGHT = vec3(0.2);
|
||||
|
||||
ROUGHNESS = 0.4;
|
||||
SPECULAR = 0.2;
|
||||
SPECULAR = 0.12;
|
||||
|
||||
ALPHA = alpha;
|
||||
ALPHA_HASH_SCALE = 1.0;
|
||||
}
|
||||
|
34
grass_chunk.gd
Normal file
34
grass_chunk.gd
Normal file
@ -0,0 +1,34 @@
|
||||
@tool
|
||||
extends Node3D
|
||||
|
||||
@export var lod_switch := 10.0
|
||||
@export var impostor_fade_in_start := 5.0
|
||||
@export var impostor_fade_in_end := 10.0
|
||||
@export var grass_fade_out_start := 10.0
|
||||
@export var grass_fade_out_end := 20.0
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
var camera_pos
|
||||
|
||||
if Engine.is_editor_hint():
|
||||
camera_pos = EditorInterface.get_editor_viewport_3d().get_camera_3d().global_position
|
||||
else:
|
||||
camera_pos = get_viewport().get_camera_3d().global_position
|
||||
|
||||
var camera_distance = global_position.distance_to(camera_pos)
|
||||
|
||||
if camera_distance < lod_switch:
|
||||
$Grass.multimesh = preload("res://grass_multimesh_detailed.tres")
|
||||
else:
|
||||
$Grass.multimesh = preload("res://grass_multimesh_simple.tres")
|
||||
|
||||
var start_to_mid = smoothstep(impostor_fade_in_start, impostor_fade_in_end, camera_distance)
|
||||
var mid_to_end = smoothstep(grass_fade_out_start, grass_fade_out_end, camera_distance)
|
||||
|
||||
$Grass.visible = mid_to_end < 1.0
|
||||
$Impostor.visible = start_to_mid >= 0.0
|
||||
|
||||
# Interpolate
|
||||
$Impostor.set_instance_shader_parameter("alpha", start_to_mid)
|
||||
$Grass.set_instance_shader_parameter("alpha", 1.0 - mid_to_end)
|
1
grass_chunk.gd.uid
Normal file
1
grass_chunk.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://bbnhmrtk1fxnl
|
106
grass_chunk.tscn
Normal file
106
grass_chunk.tscn
Normal file
@ -0,0 +1,106 @@
|
||||
[gd_scene load_steps=20 format=3 uid="uid://cnhjdcsr1e0ov"]
|
||||
|
||||
[ext_resource type="Script" path="res://grass_chunk.gd" id="1_ofmm3"]
|
||||
[ext_resource type="Shader" uid="uid://db6rwrkgyosy0" path="res://grass.gdshader" id="2_0pvim"]
|
||||
[ext_resource type="MultiMesh" uid="uid://3l6gx28y48io" path="res://grass_multimesh_detailed.tres" id="3_ofmm3"]
|
||||
[ext_resource type="Script" uid="uid://bmx385ngdvuwt" path="res://grass.gd" id="4_sx5rw"]
|
||||
[ext_resource type="Shader" path="res://impostor_grass.gdshader" id="5_ry5bi"]
|
||||
[ext_resource type="Texture2D" uid="uid://bkm3w1ujws8d7" path="res://grass_normals.png" id="6_pxbre"]
|
||||
|
||||
[sub_resource type="Gradient" id="Gradient_odt3n"]
|
||||
offsets = PackedFloat32Array(0)
|
||||
colors = PackedColorArray(0, 0, 0, 1)
|
||||
|
||||
[sub_resource type="GradientTexture1D" id="GradientTexture1D_jugg6"]
|
||||
gradient = SubResource("Gradient_odt3n")
|
||||
|
||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_301yx"]
|
||||
albedo_color = Color(0, 0, 0, 1)
|
||||
ao_enabled = true
|
||||
ao_light_affect = 1.0
|
||||
ao_texture = SubResource("GradientTexture1D_jugg6")
|
||||
|
||||
[sub_resource type="PlaneMesh" id="PlaneMesh_n0l7m"]
|
||||
size = Vector2(5, 5)
|
||||
|
||||
[sub_resource type="FastNoiseLite" id="FastNoiseLite_tid6t"]
|
||||
|
||||
[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_h2qfw"]
|
||||
seamless = true
|
||||
noise = SubResource("FastNoiseLite_tid6t")
|
||||
|
||||
[sub_resource type="FastNoiseLite" id="FastNoiseLite_tlwt5"]
|
||||
frequency = 0.0043
|
||||
fractal_type = 2
|
||||
fractal_gain = 0.45
|
||||
|
||||
[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_aqk2v"]
|
||||
seamless = true
|
||||
noise = SubResource("FastNoiseLite_tlwt5")
|
||||
|
||||
[sub_resource type="ShaderMaterial" id="ShaderMaterial_fj7yv"]
|
||||
render_priority = 0
|
||||
shader = ExtResource("2_0pvim")
|
||||
shader_parameter/size_small = 0.2
|
||||
shader_parameter/size_large = 0.6
|
||||
shader_parameter/blade_bend = 0.5
|
||||
shader_parameter/color_small = Color(0.3, 0.6, 0.1, 1)
|
||||
shader_parameter/color_large = Color(0.9, 0.9, 0.2, 1)
|
||||
shader_parameter/patch_noise = SubResource("NoiseTexture2D_h2qfw")
|
||||
shader_parameter/patch_scale = 5.0
|
||||
shader_parameter/wind_noise = SubResource("NoiseTexture2D_aqk2v")
|
||||
shader_parameter/wind_strength = 0.15
|
||||
shader_parameter/wind_direction = Vector2(1, 0)
|
||||
shader_parameter/wind_bend_strength = 2.0
|
||||
shader_parameter/wind_ao_affect = 1.0
|
||||
shader_parameter/object_radius = 1.0
|
||||
shader_parameter/object_position = Vector3(0, 0.3, 0)
|
||||
|
||||
[sub_resource type="FastNoiseLite" id="FastNoiseLite_aqk2v"]
|
||||
frequency = 0.1
|
||||
|
||||
[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_036b0"]
|
||||
seamless_blend_skirt = 0.0
|
||||
noise = SubResource("FastNoiseLite_aqk2v")
|
||||
|
||||
[sub_resource type="ShaderMaterial" id="ShaderMaterial_036b0"]
|
||||
render_priority = 0
|
||||
shader = ExtResource("5_ry5bi")
|
||||
shader_parameter/color_small = Color(0.3, 0.6, 0.1, 1)
|
||||
shader_parameter/color_large = Color(0.9, 0.9, 0.2, 1)
|
||||
shader_parameter/ground_color = Color(0, 0, 0, 1)
|
||||
shader_parameter/patch_noise = SubResource("NoiseTexture2D_h2qfw")
|
||||
shader_parameter/patch_scale = 5.0
|
||||
shader_parameter/high_frequency_noise = SubResource("NoiseTexture2D_036b0")
|
||||
shader_parameter/baked_normals = ExtResource("6_pxbre")
|
||||
shader_parameter/wind_noise = SubResource("NoiseTexture2D_aqk2v")
|
||||
shader_parameter/wind_strength = 0.1
|
||||
shader_parameter/wind_direction = Vector2(1, 0)
|
||||
shader_parameter/wind_bend_strength = 2.0
|
||||
shader_parameter/wind_ao_affect = 1.5
|
||||
|
||||
[sub_resource type="PlaneMesh" id="PlaneMesh_dwbse"]
|
||||
size = Vector2(5, 5)
|
||||
|
||||
[node name="GrassChunk" type="Node3D"]
|
||||
script = ExtResource("1_ofmm3")
|
||||
lod_switch = 7.0
|
||||
|
||||
[node name="Ground" type="MeshInstance3D" parent="."]
|
||||
material_override = SubResource("StandardMaterial3D_301yx")
|
||||
mesh = SubResource("PlaneMesh_n0l7m")
|
||||
skeleton = NodePath("../..")
|
||||
|
||||
[node name="Grass" type="MultiMeshInstance3D" parent="."]
|
||||
material_override = SubResource("ShaderMaterial_fj7yv")
|
||||
cast_shadow = 0
|
||||
instance_shader_parameters/alpha = 1.0
|
||||
multimesh = ExtResource("3_ofmm3")
|
||||
script = ExtResource("4_sx5rw")
|
||||
|
||||
[node name="Impostor" type="MeshInstance3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.01, 0)
|
||||
material_override = SubResource("ShaderMaterial_036b0")
|
||||
instance_shader_parameters/alpha = 0.471145
|
||||
mesh = SubResource("PlaneMesh_dwbse")
|
||||
skeleton = NodePath("../..")
|
30
grass_grid.gd
Normal file
30
grass_grid.gd
Normal file
@ -0,0 +1,30 @@
|
||||
@tool
|
||||
extends Node3D
|
||||
|
||||
|
||||
@export var extent := 10:
|
||||
set(new_extent):
|
||||
extent = new_extent
|
||||
reload()
|
||||
@export var chunk_size := 5.0
|
||||
|
||||
|
||||
func _ready():
|
||||
reload()
|
||||
|
||||
|
||||
func reload():
|
||||
for child in get_children():
|
||||
child.queue_free()
|
||||
|
||||
for x in range(-extent / 2, extent):
|
||||
for y in range(-extent / 2, extent):
|
||||
var chunk = preload("res://grass_chunk.tscn").instantiate()
|
||||
|
||||
chunk.position = Vector3(
|
||||
chunk_size * x,
|
||||
0.0,
|
||||
chunk_size * y
|
||||
)
|
||||
|
||||
add_child(chunk)
|
1
grass_grid.gd.uid
Normal file
1
grass_grid.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://bi74il47aar6l
|
9
grass_multimesh_detailed.tres
Normal file
9
grass_multimesh_detailed.tres
Normal file
File diff suppressed because one or more lines are too long
9
grass_multimesh_simple.tres
Normal file
9
grass_multimesh_simple.tres
Normal file
File diff suppressed because one or more lines are too long
BIN
grass_normals.png
Normal file
BIN
grass_normals.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 MiB |
35
grass_normals.png.import
Normal file
35
grass_normals.png.import
Normal file
@ -0,0 +1,35 @@
|
||||
[remap]
|
||||
|
||||
importer="texture"
|
||||
type="CompressedTexture2D"
|
||||
uid="uid://bkm3w1ujws8d7"
|
||||
path.s3tc="res://.godot/imported/grass_normals.png-9b451e629acb5e2749c3e593a0ea92f5.s3tc.ctex"
|
||||
metadata={
|
||||
"imported_formats": ["s3tc_bptc"],
|
||||
"vram_texture": true
|
||||
}
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://grass_normals.png"
|
||||
dest_files=["res://.godot/imported/grass_normals.png-9b451e629acb5e2749c3e593a0ea92f5.s3tc.ctex"]
|
||||
|
||||
[params]
|
||||
|
||||
compress/mode=2
|
||||
compress/high_quality=false
|
||||
compress/lossy_quality=0.7
|
||||
compress/hdr_compression=1
|
||||
compress/normal_map=1
|
||||
compress/channel_pack=0
|
||||
mipmaps/generate=true
|
||||
mipmaps/limit=-1
|
||||
roughness/mode=0
|
||||
roughness/src_normal=""
|
||||
process/fix_alpha_border=true
|
||||
process/premult_alpha=false
|
||||
process/normal_map_invert_y=false
|
||||
process/hdr_as_srgb=false
|
||||
process/hdr_clamp_exposure=false
|
||||
process/size_limit=0
|
||||
detect_3d/compress_to=0
|
61
impostor_grass.gdshader
Normal file
61
impostor_grass.gdshader
Normal file
@ -0,0 +1,61 @@
|
||||
shader_type spatial;
|
||||
|
||||
uniform vec3 color_small: source_color = vec3(0.3, 0.6, 0.1);
|
||||
uniform vec3 color_large: source_color = vec3(0.9, 0.9, 0.2);
|
||||
|
||||
uniform vec3 ground_color: source_color = vec3(0.0, 0.0, 0.0);
|
||||
|
||||
uniform sampler2D patch_noise;
|
||||
uniform float patch_scale = 5.0;
|
||||
|
||||
uniform sampler2D high_frequency_noise: filter_linear_mipmap_anisotropic;
|
||||
uniform sampler2D baked_normals: hint_normal, filter_linear_mipmap_anisotropic;
|
||||
|
||||
uniform sampler2D wind_noise;
|
||||
uniform float wind_strength = 0.1;
|
||||
uniform vec2 wind_direction = vec2(1.0, 0.0);
|
||||
uniform float wind_bend_strength = 2.0;
|
||||
uniform float wind_ao_affect = 1.5;
|
||||
|
||||
instance uniform float alpha = 1.0;
|
||||
|
||||
varying vec3 world_vertex;
|
||||
|
||||
void vertex() {
|
||||
world_vertex = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
|
||||
}
|
||||
|
||||
void fragment() {
|
||||
float normal_to_view = 1.0 - dot(VIEW, NORMAL);
|
||||
|
||||
float patch_factor = texture(patch_noise, world_vertex.xz / patch_scale).r;
|
||||
patch_factor = mix(patch_factor, 1.0, normal_to_view * 0.4);
|
||||
|
||||
ALBEDO = mix(color_small, color_large, patch_factor);
|
||||
|
||||
float high_frequency_sample = texture(high_frequency_noise, world_vertex.xz / patch_scale, -1.0).r;
|
||||
|
||||
float spottiness = (1.0 - normal_to_view) * 0.5 - patch_factor * 0.2;
|
||||
float ground_factor = smoothstep(spottiness + 0.1, spottiness - 0.1, high_frequency_sample);
|
||||
|
||||
ALBEDO = mix(ALBEDO, ground_color, clamp(ground_factor + 1.0 - alpha, 0.0, 1.0));
|
||||
|
||||
// Wind
|
||||
vec2 wind_position = world_vertex.xz / 10.0;
|
||||
wind_position -= (TIME + 8.0) * wind_direction * wind_strength;
|
||||
|
||||
float current_wind_bend = texture(wind_noise, wind_position).x;
|
||||
current_wind_bend *= wind_strength * 2.0;
|
||||
|
||||
float bottom_to_top_simulation = high_frequency_sample + smoothstep(0.6, 1.0, normal_to_view) * 0.4;
|
||||
AO = mix(0.5, 1.0, bottom_to_top_simulation) - current_wind_bend * wind_ao_affect;
|
||||
AO_LIGHT_AFFECT = 1.0;
|
||||
|
||||
// Normal Map
|
||||
NORMAL_MAP = texture(baked_normals, world_vertex.xz / 5.0, -1.0).xyz;
|
||||
|
||||
// Lighting
|
||||
BACKLIGHT = vec3(0.2);
|
||||
ROUGHNESS = 0.4;
|
||||
SPECULAR = 0.12;
|
||||
}
|
1
impostor_grass.gdshader.uid
Normal file
1
impostor_grass.gdshader.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://dtufyv24svwrw
|
79
world.tscn
79
world.tscn
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user