Load and apply textures
This commit is contained in:
		
							parent
							
								
									79f7ebd7eb
								
							
						
					
					
						commit
						da3f9b9203
					
				|  | @ -24,11 +24,11 @@ public partial class Mission : Node3D | ||||||
| 	public bool Dump = false; | 	public bool Dump = false; | ||||||
| 
 | 
 | ||||||
| 	DbFile _file; | 	DbFile _file; | ||||||
| 	List<Image> _textures; // TODO: Make these textures :) | 	List<ImageTexture> _textures; | ||||||
| 
 | 
 | ||||||
| 	public override void _Ready() | 	public override void _Ready() | ||||||
| 	{ | 	{ | ||||||
| 		_textures = new List<Image>(); | 		_textures = new List<ImageTexture>(); | ||||||
| 
 | 
 | ||||||
| 		var missionSelector = GetNode<Control>("%MissionSelector") as MissionSelector; | 		var missionSelector = GetNode<Control>("%MissionSelector") as MissionSelector; | ||||||
| 		missionSelector.LoadMission += (string path) => | 		missionSelector.LoadMission += (string path) => | ||||||
|  | @ -77,7 +77,7 @@ public partial class Mission : Node3D | ||||||
| 
 | 
 | ||||||
| 		_file = new(FileName); | 		_file = new(FileName); | ||||||
| 		var textureList = (TxList)_file.Chunks["TXLIST"]; | 		var textureList = (TxList)_file.Chunks["TXLIST"]; | ||||||
| 		// LoadTextures(textureList); | 		LoadTextures(textureList); | ||||||
| 		if (Dump) DumpTextureList(textureList); | 		if (Dump) DumpTextureList(textureList); | ||||||
| 
 | 
 | ||||||
| 		var wr = (WorldRep)_file.Chunks["WREXT"]; | 		var wr = (WorldRep)_file.Chunks["WREXT"]; | ||||||
|  | @ -106,13 +106,27 @@ public partial class Mission : Node3D | ||||||
| 		var surfaceArrays = BuildSurfaceArrays(cell, maxPolyIdx, out var packingRects); | 		var surfaceArrays = BuildSurfaceArrays(cell, maxPolyIdx, out var packingRects); | ||||||
| 
 | 
 | ||||||
| 		Image lightmap = BuildLightmap(cell, packingRects, surfaceArrays); | 		Image lightmap = BuildLightmap(cell, packingRects, surfaceArrays); | ||||||
|  | 
 | ||||||
|  | 		// !Hack: This should be somewhere else? | ||||||
|  | 		var materials = new List<StandardMaterial3D>(); | ||||||
|  | 		for (var i = 0; i < maxPolyIdx; i++) | ||||||
|  | 		{ | ||||||
|  | 			var textureId = cell.RenderPolys[i].TextureId; | ||||||
|  | 			// !HACK: Sky textures :) | ||||||
|  | 			if (textureId >= _textures.Count) | ||||||
|  | 			{ | ||||||
|  | 				textureId = 0; | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
| 			var material = new StandardMaterial3D | 			var material = new StandardMaterial3D | ||||||
| 			{ | 			{ | ||||||
| 			AlbedoTexture = ImageTexture.CreateFromImage(lightmap), | 				AlbedoTexture = _textures[textureId], | ||||||
| 				TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest, | 				TextureFilter = BaseMaterial3D.TextureFilterEnum.Nearest, | ||||||
| 			}; | 			}; | ||||||
|  | 			materials.Add(material); | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 		MeshInstance3D mesh = GenerateMesh(surfaceArrays, material); | 		MeshInstance3D mesh = GenerateMesh(surfaceArrays, materials); | ||||||
| 		OccluderInstance3D occluderInstance = GenerateOccluder(surfaceArrays); | 		OccluderInstance3D occluderInstance = GenerateOccluder(surfaceArrays); | ||||||
| 
 | 
 | ||||||
| 		var cellNode = new Node3D(); | 		var cellNode = new Node3D(); | ||||||
|  | @ -253,13 +267,13 @@ public partial class Mission : Node3D | ||||||
| 		return occluderInstance; | 		return occluderInstance; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private static MeshInstance3D GenerateMesh(List<Godot.Collections.Array> surfaceArrays, StandardMaterial3D material) | 	private static MeshInstance3D GenerateMesh(List<Godot.Collections.Array> surfaceArrays, List<StandardMaterial3D> materials) | ||||||
| 	{ | 	{ | ||||||
| 		var arrMesh = new ArrayMesh(); | 		var arrMesh = new ArrayMesh(); | ||||||
| 		for (var i = 0; i < surfaceArrays.Count; i++) | 		for (var i = 0; i < surfaceArrays.Count; i++) | ||||||
| 		{ | 		{ | ||||||
| 			arrMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, surfaceArrays[i]); | 			arrMesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, surfaceArrays[i]); | ||||||
| 			arrMesh.SurfaceSetMaterial(i, material); | 			arrMesh.SurfaceSetMaterial(i, materials[i]); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		var meshInstance = new MeshInstance3D | 		var meshInstance = new MeshInstance3D | ||||||
|  | @ -360,8 +374,7 @@ public partial class Mission : Node3D | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			if (Dump) GD.Print($"Loading texture: {path}"); | 			if (Dump) GD.Print($"Loading texture: {path}"); | ||||||
| 			// _textures.Add(ImageTexture.CreateFromImage(Image.LoadFromFile(path))); | 			_textures.Add(ImageTexture.CreateFromImage(Image.LoadFromFile(path))); | ||||||
| 			_textures.Add(Image.LoadFromFile(path)); |  | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue