aboutsummaryrefslogtreecommitdiff
path: root/game/src/Game/GameSession/BillboardManager.gd
blob: ac1610b117a2595233fe93d46eafc14d9d0be045 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
extends MultiMeshInstance3D

#given a name: get the index for the texture in the shader
#var billboard_names = {
   
#}
@export var _map_view : MapView

var billboard_names = []
const SCALE_FACTOR = 1.0/64.0
# Called when the node enters the scene tree for the first time.
func _ready():
   const name_key : StringName = &"name"
   const texture_key : StringName = &"texture"
   const scale_key : StringName = &"scale"
   const noOfFrames_key : StringName = &"noFrames"
   
   
   var textures : Array[Texture2D] = []
   var frames : Array[int] = []
   var scales : Array[float] = []
   
   var billboards:Array[Dictionary] = MapItemSingleton.get_billboards()
   for billboard in billboards:
      var b_name = billboard[name_key]
      var texture = billboard[texture_key]
      var b_scale = billboard[scale_key]
      var noFrames = billboard[noOfFrames_key]
      textures.push_back(AssetManager.get_texture(texture))
      frames.push_back(noFrames)
      scales.push_back(b_scale*SCALE_FACTOR)
      billboard_names.push_back(b_name)
   
   var material:ShaderMaterial = load("res://src/Game/GameSession/billboards.tres")
   material.set_shader_parameter("billboards",textures)
   material.set_shader_parameter("numframes",frames)
   material.set_shader_parameter("sizes",scales)
   
   # Create the multimesh.
   multimesh = MultiMesh.new()
   
   # Set the format first.
   multimesh.transform_format = MultiMesh.TRANSFORM_3D
   multimesh.mesh = QuadMesh.new()
   multimesh.use_custom_data = true
   
   multimesh.mesh.surface_set_material(0,material)
   
   
   # Need to have one instance for every province
   # need to get a list of province centers
   #GameSingleton.get_mapmode_identifier(0)
   #GameSingleton.get_mapmode_count()
   
   var positions_len = MapItemSingleton.get_province_count()
   
   
   print(positions_len)
   # Then resize (otherwise, changing the format is not allowed).
   multimesh.instance_count = positions_len
   multimesh.visible_instance_count = positions_len
   print("===============")
   var positions = MapItemSingleton.get_province_positions()
   print("===============")
   
   var map_positions : PackedVector3Array = PackedVector3Array()
   for pos_in in positions:
      var pos = _map_view._map_to_world_coords(pos_in)# + Vector3(0, 0.5 * SCALE_FACTOR, 0)
      map_positions.push_back(pos)
   #print(positions)
   #print(positions.size())
   # Set the transform of the instances.
   #model.set_position(
   #_map_view._map_to_world_coords(unit_dict[position_key])
   # + Vector3(0, 0.1 * MODEL_SCALE, 0))
   for i in multimesh.visible_instance_count:
      multimesh.set_instance_transform(i, Transform3D(Basis(), 
         map_positions[i]
      ))
      # custom data is a single vec4 float sent as a color
      # Info send to the shader is as follows:
      # x: image from imageArray
      # y: frame from selected image
      # z: visibility
      # w: unused, perhaps progress for progress bars?
      #rf()+0.5 
      var im = r(textures.size()-1)
      multimesh.set_instance_custom_data(i,Color(
         im,r(frames[im]),1.0,0
      ))

func r(x:int=1)->int:
   return randi_range(0,x)
   
func rf(lim:float=1.0)->float:
   return randf_range(0,lim)