using Godot; namespace Gmtk24 { public partial class BuildingBlockCreator : Node3D { [Export] public Node3D TrenchbroomMap; // [Export] // public PackedScene BuildingBlockPrefab; [Export] public ShaderMaterial BuildingBlockMaterial; [Export] public float BuildingBlockScale = 0.05f; public override void _Ready() { var children = TrenchbroomMap.FindChildren("*_buildingblock"); foreach (var buildingBlockStaticBody in children) { var buildingBlock = new RigidBody3D(); var smallMesh = (MeshInstance3D)buildingBlockStaticBody.GetChild(0).Duplicate(); smallMesh.Scale = Vector3.One * BuildingBlockScale; 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 = (ShaderMaterial)BuildingBlockMaterial.Duplicate(); 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) { CollisionShape3D newShape = (CollisionShape3D)shape.Duplicate(); var bigPoints = ((ConvexPolygonShape3D)newShape.Shape).Points; var points = new Vector3[bigPoints.Length]; for (int i = 0; i < bigPoints.Length; i++) { points[i] = bigPoints[i] * BuildingBlockScale; } ((ConvexPolygonShape3D)newShape.Shape).Points = points; buildingBlock.AddChild(newShape); } AddChild(buildingBlock); } } public override void _Process(double delta) { } } }