Almost there object casting
This commit is contained in:
		
							parent
							
								
									122bd2bfa7
								
							
						
					
					
						commit
						1532a2750c
					
				|  | @ -119,6 +119,8 @@ public class LightMapper | ||||||
| 
 | 
 | ||||||
|         var vertices = new List<Vector3>(); |         var vertices = new List<Vector3>(); | ||||||
|         var indices = new List<int>(); |         var indices = new List<int>(); | ||||||
|  |          | ||||||
|  |         // Worldrep mesh | ||||||
|         foreach (var cell in worldRep.Cells) |         foreach (var cell in worldRep.Cells) | ||||||
|         { |         { | ||||||
|             var numPolys = cell.PolyCount; |             var numPolys = cell.PolyCount; | ||||||
|  | @ -169,6 +171,102 @@ public class LightMapper | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|          |          | ||||||
|  |         // Object meshes?? | ||||||
|  |         // TODO: Should this throw? | ||||||
|  |         if (!_mission.TryGetChunk<BrList>("BRLIST", out var brList)) | ||||||
|  |         { | ||||||
|  |             return null; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         foreach (var brush in brList.Brushes) | ||||||
|  |         { | ||||||
|  |             if (brush.media != BrList.Brush.Media.Object) | ||||||
|  |             { | ||||||
|  |                 continue; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             var id = (int)brush.brushInfo; | ||||||
|  |             var modelNameProp = _hierarchy.GetProperty<PropLabel>(id, "P$ModelName"); | ||||||
|  |             var scaleProp = _hierarchy.GetProperty<PropVector>(id, "P$Scale"); | ||||||
|  |             var renderTypeProp = _hierarchy.GetProperty<PropRenderType>(id, "P$RenderTyp"); | ||||||
|  |             var jointPosProp = _hierarchy.GetProperty<PropJointPos>(id, "P$JointPos"); | ||||||
|  |             var immobileProp = _hierarchy.GetProperty<PropBool>(id, "P$Immobile"); | ||||||
|  |             var staticShadowProp = _hierarchy.GetProperty<PropBool>(id, "P$StatShad"); | ||||||
|  | 
 | ||||||
|  |             var joints = jointPosProp?.Positions ?? [0, 0, 0, 0, 0, 0]; | ||||||
|  |             var castsShadows = (immobileProp?.value ?? false) || (staticShadowProp?.value ?? false); | ||||||
|  |             var renderMode = renderTypeProp?.mode ?? PropRenderType.Mode.Normal; | ||||||
|  |              | ||||||
|  |             // TODO: Check which rendermodes cast shadows :) | ||||||
|  |             if (modelNameProp == null || !castsShadows || renderMode != PropRenderType.Mode.Normal) | ||||||
|  |             { | ||||||
|  |                 continue; | ||||||
|  |             } | ||||||
|  |              | ||||||
|  |             // Let's try and place an object :) | ||||||
|  |             var modelName = modelNameProp.value.ToLower() + ".bin"; | ||||||
|  |             var modelPath = _campaign.GetResourcePath(ResourceType.Object, modelName); | ||||||
|  |             if (modelPath == null) | ||||||
|  |             { | ||||||
|  |                 continue; | ||||||
|  |             } | ||||||
|  |              | ||||||
|  |             // TODO: Handle failing to find model more gracefully | ||||||
|  |             var pos = brush.position; | ||||||
|  |             var rot = brush.angle; | ||||||
|  |             var scale = scaleProp?.value ?? Vector3.One; | ||||||
|  |             var model = new ModelFile(modelPath); | ||||||
|  |             pos -= model.Header.Center; | ||||||
|  |              | ||||||
|  |             // for each object modify the vertices | ||||||
|  |             // TODO: Something about the transform is fucky | ||||||
|  |             foreach (var subObj in model.Objects) | ||||||
|  |             { | ||||||
|  |                 Console.WriteLine($"ID: {subObj.Joint}, l: {joints.Length}"); | ||||||
|  |                 var jointTrans = Matrix4x4.Identity; | ||||||
|  |                 if (subObj.Joint != -1) | ||||||
|  |                 { | ||||||
|  |                     var ang = float.DegreesToRadians(joints[subObj.Joint]); | ||||||
|  |                     var jointRot = Matrix4x4.CreateFromYawPitchRoll(0, ang, 0); | ||||||
|  |                     var objTrans = subObj.Transform; | ||||||
|  |                     jointTrans = jointRot * objTrans; | ||||||
|  |                 } | ||||||
|  |                 var scalePart = Matrix4x4.CreateScale(scale); | ||||||
|  |                 var rotPart = Matrix4x4.CreateFromYawPitchRoll(float.DegreesToRadians(rot.X), | ||||||
|  |                     float.DegreesToRadians(rot.Y), float.DegreesToRadians(rot.Z)); | ||||||
|  |                 var transPart = Matrix4x4.CreateTranslation(pos); | ||||||
|  |                 var transform = jointTrans * scalePart * rotPart * transPart; | ||||||
|  |                  | ||||||
|  |                 var start = subObj.PointIdx; | ||||||
|  |                 var end = start + subObj.PointCount; | ||||||
|  |                 for (var i = start; i < end; i++) | ||||||
|  |                 { | ||||||
|  |                     var v = model.Vertices[i]; | ||||||
|  |                     model.Vertices[i] = Vector3.Transform(v, transform); | ||||||
|  |                      | ||||||
|  |                     Console.WriteLine($"Raw: {v}, Pos: {model.Vertices[i]}"); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |              | ||||||
|  |             // for each polygon slam it's vertices and indices :) | ||||||
|  |             foreach (var poly in model.Polygons) | ||||||
|  |             { | ||||||
|  |                 var indexOffset = vertices.Count; | ||||||
|  |                 foreach (var idx in poly.VertexIndices) | ||||||
|  |                 { | ||||||
|  |                     vertices.Add(model.Vertices[idx]); | ||||||
|  |                 } | ||||||
|  |                  | ||||||
|  |                 for (int i = 1; i < poly.VertexCount - 1; i++) | ||||||
|  |                 { | ||||||
|  |                     indices.Add(indexOffset); | ||||||
|  |                     indices.Add(indexOffset + i); | ||||||
|  |                     indices.Add(indexOffset + i + 1); | ||||||
|  |                     _triangleTypeMap.Add(SurfaceType.Solid); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |          | ||||||
|         var rt = new Raytracer(); |         var rt = new Raytracer(); | ||||||
|         rt.AddMesh(new TriangleMesh([.. vertices], [.. indices])); |         rt.AddMesh(new TriangleMesh([.. vertices], [.. indices])); | ||||||
|         rt.CommitScene(); |         rt.CommitScene(); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue