Skip to content

Commit

Permalink
Update grass generator (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
fresh-ter authored Aug 12, 2021
1 parent fc27534 commit e24a50c
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 21 deletions.
Binary file modified project/assets/battle/grass/Grass.material
Binary file not shown.
104 changes: 92 additions & 12 deletions project/battle/tactical_map/grass/Grass.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,25 @@ export var blade_sway_pitch = Vector2(0, 0.175)
export var thread_count: int = 4
var threads = []

var multimesh_rid: RID
var mesh_rid: RID

var material = preload("res://assets/battle/grass/Grass.material").get_rid()

var grass_cells: Array
var grass_cell_size: Vector2
var amount_blade_in_cell: int


func _ready():
pass


func generate():
if multimesh_rid != null and mesh_rid != null:
clear()
rebuild()


func make_blade_mesh() -> ArrayMesh:
var mesh = ArrayMesh.new()
Expand Down Expand Up @@ -66,6 +80,7 @@ func rid_blade_mesh() -> RID:

func rebuild():
var multimesh = VisualServer.multimesh_create()
multimesh_rid = multimesh

VisualServer.multimesh_allocate(multimesh, get_count(), VisualServer.MULTIMESH_TRANSFORM_3D, VisualServer.MULTIMESH_COLOR_NONE, VisualServer.MULTIMESH_CUSTOM_DATA_FLOAT)

Expand All @@ -74,15 +89,34 @@ func rebuild():

var bpt = get_count() / thread_count # blades per thread
threads = []

amount_blade_in_cell = get_parent()._amount_blade_in_cell
grass_cell_size = get_parent().cell_size
grass_cells = get_parent().get_global_coord_used_grass_cells()

amount_blade_in_cell = 140

var bpt_cell = grass_cells.size() / thread_count

var arg: Array

for t in thread_count:
arg = [
multimesh,
bpt * t,
bpt * t + bpt,
bpt_cell * t,
bpt_cell * t + bpt_cell,
grass_cells, amount_blade_in_cell, grass_cell_size
]
threads.append(Thread.new())
threads[t].start(self, "thread_worker", [multimesh, bpt * t, bpt * t + bpt])
threads[t].start(self, "thread_worker", arg)
# breakpoint

for t in thread_count:
threads[t].wait_to_finish()


# for i in get_count():
# setup_blade(multimesh, i)

var instance = VisualServer.instance_create()
var scenario = get_world().scenario
Expand All @@ -92,31 +126,77 @@ func rebuild():

VisualServer.instance_geometry_set_material_override(instance, material)


func clear():
VisualServer.free_rid(mesh_rid)
VisualServer.free_rid(multimesh_rid)


func thread_worker(data: Array):
var rid = data[0]
var start = data[1]
var stop = data[2]
var start_cell = data[3]
var stop_cell = data[4]
var cells = data[5]
var amount_blade_in_cell = data[6]
var grass_cell_size: Vector2 = data[7]

var rng = RandomNumberGenerator.new()

var checking: bool = false

var cell_index: int = start_cell
var cell_current_number_blades: int = 0

var amount_available_cells = stop_cell - start_cell
var amount_wishful_blades = amount_available_cells * amount_blade_in_cell
var amount_available_blades = stop - start

# if amount_wishful_blades <= amount_available_blades:
# checking = true
# else:
# amount_blade_in_cell = amount_available_blades / amount_available_cells

for i in range(start, stop):
setup_blade(rid, i)
setup_blade(
rid, i,
cells[cell_index], grass_cell_size,
rng
)
cell_current_number_blades += 1

if cell_current_number_blades == amount_blade_in_cell:
cell_current_number_blades = 0
cell_index += 1


if cell_index == stop_cell:
break

func setup_blade(rid: RID, i: int):
func setup_blade(rid: RID, i: int,
cell_coord: Vector3, grass_cell_size: Vector2,
rng
):
var width = rand_range(blade_width.x, blade_width.y)
var height = rand_range(blade_height.x, blade_height.y)

var position: Vector2
while true:
position = Vector2(rand_range(-area.x/2.0, area.x/2.0), rand_range(-area.y/2.0, area.y/2.0))
if get_parent().is_grass_soil(Vector3(position.x, 0, position.y)):
break

var x = grass_cell_size.x
var y = grass_cell_size.y

var rand_shift = Vector2(rng.randf_range(-x/2, x/2), rng.randf_range(-y/2, y/2))

var position: Vector3
position = cell_coord
position += Vector3(rand_shift.x, 0, rand_shift.y)

var rotation = rand_range(blade_rotation.x, blade_rotation.y)

var sway_yaw = rand_range(blade_sway_yaw.x, blade_sway_yaw.y)
var sway_pitch = rand_range(blade_sway_pitch.x, blade_sway_pitch.y)

var transform = Transform.IDENTITY
transform.origin = Vector3(position.x, 0, position.y)
transform.origin = Vector3(position.x, 0, position.z)
transform.basis = Basis.IDENTITY.rotated(Vector3.UP, deg2rad(rotation))

VisualServer.multimesh_instance_set_transform(rid, i, transform)
Expand Down
29 changes: 22 additions & 7 deletions project/battle/tactical_map/scripts/tactical_map_generator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,35 @@ func generate_map(
_map_height=TacticalMap.map_height
):
var n: float
var amount_grass_cells: int = 0

for x in _map_widht:
for y in _map_height:
n = noise.get_noise_2d(x, y)
var cell = offset_to_origin(x,y)
_noise_parsing(cell, n)
var items = _noise_parsing(cell, n)

if items.soil == TacticalMap.ITEM_SOIL.GRASS:
amount_grass_cells += 1

set_meta("_amount_grass_cells", amount_grass_cells)


func get_amount_grass_cells():
return get_meta("_amount_grass_cells")


func _noise_parsing(_cell: Array, _n):
var items: Dictionary

if _n >= 0.0:
soil.set_cell_item(_cell[0], 0, _cell[1],
TacticalMap.ITEM_SOIL.GRASS)
items["soil"] = TacticalMap.ITEM_SOIL.GRASS
soil.set_cell_item(_cell[0], 0, _cell[1], items["soil"])

var r = randf()
if r < OAK_TREE_3_CHANCE:
obstacles.set_cell_item(_cell[0], 0, _cell[1],
TacticalMap.ITEM_OBSTACLES.OAK_TREE_3)
items["obstacles"] = TacticalMap.ITEM_OBSTACLES.OAK_TREE_3
obstacles.set_cell_item(_cell[0], 0, _cell[1], items["obstacles"])
elif r < OAK_TREE_2_CHANCE:
obstacles.set_cell_item(_cell[0], 0, _cell[1],
TacticalMap.ITEM_OBSTACLES.OAK_TREE_1)
Expand All @@ -60,8 +73,8 @@ func _noise_parsing(_cell: Array, _n):
TacticalMap.ITEM_OBSTACLES.OAK_TREE_2)

else:
soil.set_cell_item(_cell[0], 0, _cell[1],
TacticalMap.ITEM_SOIL.DIRT)
items["soil"] = TacticalMap.ITEM_SOIL.DIRT
soil.set_cell_item(_cell[0], 0, _cell[1], items["soil"])

var r3 = randf()
if r3 < STONE_CHANCE:
Expand Down Expand Up @@ -92,6 +105,8 @@ func _noise_parsing(_cell: Array, _n):
elif r < DEAD_SPRUCE_2_CHANCE:
obstacles.set_cell_item(_cell[0], 0, _cell[1],
TacticalMap.ITEM_OBSTACLES.DEAD_SPRUCE_2)

return items


func offset_to_origin(x: int, y: int) -> Array:
Expand Down
27 changes: 25 additions & 2 deletions project/battle/tactical_map/tactical_map.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ enum ITEM_VEGETATION {
var map_widht: int = 70
var map_height: int = 70

var grass_game_count: int = 600
var cell_size: Vector2 = Vector2(2, 2)

var grass_game_count: int = 600000
var grass_area: Vector2 = Vector2(map_widht*2, map_height*2)
var grass_blade_height: Vector2 = Vector2(0.5, 0.5) # rand_range(x, y)
var grass_blade_width: Vector2 = Vector2(0.01, 0.2) # rand_range(x, y)
var _amount_blade_in_cell: int

onready var grass: Spatial = $Grass

Expand Down Expand Up @@ -75,11 +78,15 @@ func generate_map():
soil, obstacles, vegetation)
noiseMapGenerator.generate_map()

var amount: int = noiseMapGenerator.get_amount_grass_cells()
_amount_blade_in_cell = grass_game_count / amount
_amount_blade_in_cell = ceil(_amount_blade_in_cell)

grass.game_count = grass_game_count
grass.area = grass_area
grass.blade_height = grass_blade_height
grass.blade_width = grass_blade_width
grass.rebuild()
grass.generate()


func clear_map():
Expand All @@ -103,6 +110,22 @@ func is_grass_soil(_global_v: Vector3) -> bool:
return result


func get_global_coord_used_grass_cells():
var used_cells: Array = soil.get_used_cells()
var global_coord_used_grass_cells: Array

var item: int
var global_coord_item: Vector3

for cell in used_cells:
item = soil.get_cell_item(cell.x, cell.y, cell.z)
if item == ITEM_SOIL.GRASS:
global_coord_item = soil.map_to_world(cell.x, cell.y, cell.z)
global_coord_used_grass_cells.append(global_coord_item)

return global_coord_used_grass_cells


########

func get_map_path(from: Vector3, to: Vector3) -> PoolVector3Array:
Expand Down
1 change: 1 addition & 0 deletions project/battle/tactical_map/tactical_map.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ __meta__ = {
[node name="Grass" parent="." instance=ExtResource( 6 )]
blade_height = Vector2( 0.5, 0.08 )
blade_width = Vector2( 0.5, 0.02 )
thread_count = 8

[node name="Camera" parent="." instance=ExtResource( 4 )]
transform = Transform( 1, 0, 0, 0, 0.707107, 0.707107, 0, -0.707107, 0.707107, 0, 38.2787, 3 )
Expand Down
1 change: 1 addition & 0 deletions project/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -1203,4 +1203,5 @@ F11={

[rendering]

threads/thread_model=2
environment/default_environment="res://default_env.tres"

0 comments on commit e24a50c

Please sign in to comment.