using System.Collections.Generic; using Godot; using Godot.Collections; namespace Gmtk24 { public partial class BuildingBlockCreator : Node3D { [Export] public Node3D TrenchbroomMap; [Export] public PackedScene BuildingBlockPrefab; [Export] public ShaderMaterial BuildingBlockMaterial; public override void _Ready() { var scale = 0.05f; var children = TrenchbroomMap.FindChildren("*_buildingblock"); foreach (var buildingBlockStaticBody in children) { var buildingBlock = BuildingBlockPrefab.Instantiate(); var smallMesh = new MeshInstance3D { Mesh = buildingBlockStaticBody.GetChild(0).Mesh, Scale = Vector3.One * scale, }; for (int i = 0; i < smallMesh.GetSurfaceOverrideMaterialCount(); i++) { var replacedMaterial = smallMesh.Mesh.SurfaceGetMaterial(i); var transparency = replacedMaterial.Get("transparency"); if (((int)transparency) != 0) { // Make a new material, using the partly-transparent albedo as an alpha mask var blockMaterialWithAlphaMask = new ShaderMaterial { Shader = BuildingBlockMaterial.Shader, }; foreach (var uniform in BuildingBlockMaterial.Shader.GetShaderUniformList()) { var uniformName = (StringName)((Dictionary)uniform).GetValueOrDefault("name"); blockMaterialWithAlphaMask.SetShaderParameter(uniformName, BuildingBlockMaterial.GetShaderParameter(uniformName)); } blockMaterialWithAlphaMask.SetShaderParameter("texture_albedo_for_alpha", replacedMaterial.Get("albedo_texture")); smallMesh.SetSurfaceOverrideMaterial(i, blockMaterialWithAlphaMask); } else { smallMesh.SetSurfaceOverrideMaterial(i, BuildingBlockMaterial); } } buildingBlock.AddChild(smallMesh); var collisionShapes = buildingBlockStaticBody.FindChildren("*_collision_shape"); foreach (var shape in collisionShapes) { var bigPoints = ((ConvexPolygonShape3D)((CollisionShape3D)shape).Shape).Points; var points = new Vector3[bigPoints.Length]; for (int i = 0; i < bigPoints.Length; i++) { points[i] = bigPoints[i] * scale; } buildingBlock.AddChild(new CollisionShape3D { Shape = new ConvexPolygonShape3D { Points = points, }, }); } AddChild(buildingBlock); } } public override void _Process(double delta) { } } }