diff options
Diffstat (limited to 'game/src/Game')
-rw-r--r-- | game/src/Game/GameSession/BillboardManager.gd | 82 | ||||
-rw-r--r-- | game/src/Game/GameSession/GameSession.tscn | 13 | ||||
-rw-r--r-- | game/src/Game/GameSession/billboard.gdshader | 33 | ||||
-rw-r--r-- | game/src/Game/GameSession/billboards.tres | 9 |
4 files changed, 130 insertions, 7 deletions
diff --git a/game/src/Game/GameSession/BillboardManager.gd b/game/src/Game/GameSession/BillboardManager.gd new file mode 100644 index 0000000..f6b96c0 --- /dev/null +++ b/game/src/Game/GameSession/BillboardManager.gd @@ -0,0 +1,82 @@ +extends MultiMeshInstance3D + +#given a name: get the index for the texture in the shader +#var billboard_names = { + +#} +var billboard_names = [] + +# 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) + 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: PackedVector2Array = MapItemSingleton.get_province_positions() + + # Then resize (otherwise, changing the format is not allowed). + #multimesh.instance_count = positions.size() + #multimesh.visible_instance_count = positions.size() + print("===============") + print(positions) + print(positions.size()) + # Set the transform of the instances. + """for i in multimesh.visible_instance_count: + multimesh.set_instance_transform(i, Transform3D(Basis(), + Vector3(positions[i].x, 0.5, positions[i].y) + )) + # 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) diff --git a/game/src/Game/GameSession/GameSession.tscn b/game/src/Game/GameSession/GameSession.tscn index 5925f3d..b5edb3d 100644 --- a/game/src/Game/GameSession/GameSession.tscn +++ b/game/src/Game/GameSession/GameSession.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=21 format=3 uid="uid://bgnupcshe1m7r"] +[gd_scene load_steps=22 format=3 uid="uid://bgnupcshe1m7r"] [ext_resource type="Script" path="res://src/Game/GameSession/GameSession.gd" id="1_eklvp"] [ext_resource type="PackedScene" uid="uid://cvl76duuym1wq" path="res://src/Game/MusicConductor/MusicPlayer.tscn" id="2_kt6aa"] @@ -6,6 +6,7 @@ [ext_resource type="PackedScene" uid="uid://dvdynl6eir40o" path="res://src/Game/GameSession/GameSessionMenu.tscn" id="3_bvmqh"] [ext_resource type="Script" path="res://src/Game/GameSession/ModelManager.gd" id="3_qwk4j"] [ext_resource type="Script" path="res://src/Game/GameSession/Topbar.gd" id="4_2kbih"] +[ext_resource type="Script" path="res://src/Game/GameSession/BillboardManager.gd" id="4_b3l7b"] [ext_resource type="PackedScene" uid="uid://dkehmdnuxih2r" path="res://src/Game/GameSession/MapView.tscn" id="4_xkg5j"] [ext_resource type="Script" path="res://src/Game/GameSession/NationManagementScreen/ProductionMenu.gd" id="5_16755"] [ext_resource type="Script" path="res://src/Game/GameSession/ProvinceOverviewPanel.gd" id="5_lfv8l"] @@ -40,15 +41,13 @@ _game_session_menu = NodePath("UICanvasLayer/UI/GameSessionMenu") script = ExtResource("3_qwk4j") _map_view = NodePath("../MapView") +[node name="BillboardManager" type="MultiMeshInstance3D" parent="."] +script = ExtResource("4_b3l7b") + [node name="UICanvasLayer" type="CanvasLayer" parent="."] -[node name="UI" type="Control" parent="UICanvasLayer"] -layout_mode = 3 +[node name="UI" type="GUINode" parent="UICanvasLayer"] anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 mouse_filter = 2 [node name="ProvinceOverviewPanel" type="GUINode" parent="UICanvasLayer/UI"] diff --git a/game/src/Game/GameSession/billboard.gdshader b/game/src/Game/GameSession/billboard.gdshader new file mode 100644 index 0000000..e207ff9 --- /dev/null +++ b/game/src/Game/GameSession/billboard.gdshader @@ -0,0 +1,33 @@ +shader_type spatial; +//render_mode unshaded; + +uniform sampler2D billboards[255]; +uniform int numframes[255]; +uniform float sizes[255]; + +//COLOR/INSTANCE_CUSTOM is our custom data, used as follows: +// x=image index +// y=frame in image index +// z=visible (0 = invisible, 1 = visible) + +void vertex() { + COLOR = INSTANCE_CUSTOM; //send instance_custom info to fragment + float size = sizes[int(COLOR.x)]; + + vec3 cam_right_worldspace = vec3(VIEW_MATRIX[0][0],VIEW_MATRIX[1][0],VIEW_MATRIX[2][0]); + vec3 cam_up_worldspace = vec3(VIEW_MATRIX[0][1],VIEW_MATRIX[1][1],VIEW_MATRIX[2][1]); + vec3 vert_pos_world = + cam_right_worldspace * VERTEX.x * size + + cam_up_worldspace * VERTEX.y * size; + VERTEX = vert_pos_world; +} + +void fragment() { + int image_index = int(COLOR.x); + float frame_index = COLOR.y; + + float uv_x_space = 1.0/float(numframes[image_index]); + vec2 uv_out = vec2(uv_x_space * (frame_index + UV.x),UV.y); + ALBEDO.rgb = texture( billboards[image_index] ,uv_out).rgb; + ALPHA = texture(billboards[image_index] ,uv_out).a * COLOR.z; +} diff --git a/game/src/Game/GameSession/billboards.tres b/game/src/Game/GameSession/billboards.tres new file mode 100644 index 0000000..7e9d214 --- /dev/null +++ b/game/src/Game/GameSession/billboards.tres @@ -0,0 +1,9 @@ +[gd_resource type="ShaderMaterial" load_steps=2 format=3 uid="uid://dyeip7yhqf1g7"] + +[ext_resource type="Shader" path="res://src/Game/GameSession/billboard.gdshader" id="1_m1mbq"] + +[resource] +render_priority = 0 +shader = ExtResource("1_m1mbq") +shader_parameter/numframes = PackedInt32Array() +shader_parameter/billboards = Array[Texture2D]([]) |