gmtk24/scripts/BuildingBlockCreator.cs

61 lines
2.7 KiB
C#
Raw Normal View History

2024-08-17 20:52:00 +02:00
using Godot;
namespace Gmtk24 {
public partial class BuildingBlockCreator : Node3D {
[Export]
2024-08-17 21:05:30 +02:00
public Node3D TrenchbroomMap;
2024-08-18 18:17:01 +02:00
// [Export]
// public PackedScene BuildingBlockPrefab;
2024-08-17 21:05:30 +02:00
[Export]
public ShaderMaterial BuildingBlockMaterial;
[Export]
public float BuildingBlockScale = 0.05f;
2024-08-17 20:52:00 +02:00
public override void _Ready() {
2024-08-17 21:05:30 +02:00
var children = TrenchbroomMap.FindChildren("*_buildingblock");
2024-08-17 20:52:00 +02:00
foreach (var buildingBlockStaticBody in children) {
var buildingBlock = new RigidBody3D() {
CollisionMask = 0b10,
CollisionLayer = 0b10,
};
2024-08-17 20:52:00 +02:00
var smallMesh = (MeshInstance3D)buildingBlockStaticBody.GetChild<MeshInstance3D>(0).Duplicate();
smallMesh.Scale = Vector3.One * BuildingBlockScale;
2024-08-17 20:52:00 +02:00
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();
2024-08-17 20:52:00 +02:00
blockMaterialWithAlphaMask.SetShaderParameter("texture_albedo_for_alpha", replacedMaterial.Get("albedo_texture"));
smallMesh.SetSurfaceOverrideMaterial(i, blockMaterialWithAlphaMask);
} else {
2024-08-17 21:05:30 +02:00
smallMesh.SetSurfaceOverrideMaterial(i, BuildingBlockMaterial);
2024-08-17 20:52:00 +02:00
}
}
2024-08-17 21:05:30 +02:00
buildingBlock.AddChild(smallMesh);
2024-08-17 20:52:00 +02:00
var collisionShapes = buildingBlockStaticBody.FindChildren("*_collision_shape");
foreach (var shape in collisionShapes) {
CollisionShape3D newShape = (CollisionShape3D)shape.Duplicate();
var bigPoints = ((ConvexPolygonShape3D)newShape.Shape).Points;
2024-08-17 20:52:00 +02:00
var points = new Vector3[bigPoints.Length];
for (int i = 0; i < bigPoints.Length; i++) {
points[i] = bigPoints[i] * BuildingBlockScale;
2024-08-17 20:52:00 +02:00
}
((ConvexPolygonShape3D)newShape.Shape).Points = points;
buildingBlock.AddChild(newShape);
2024-08-17 20:52:00 +02:00
}
2024-08-17 21:05:30 +02:00
AddChild(buildingBlock);
var rng = new RandomNumberGenerator();
buildingBlock.Translate(Vector3.One * rng.Randf() * 0.5f);
2024-08-17 20:52:00 +02:00
}
}
public override void _Process(double delta) {
}
}
}