public Material GetTextureMaterial(string textureName, bool transparent) { if (!_materials.ContainsKey(textureName)) { Texture2D texture; if (VirtualFilesystem.Instance.FileExists(textureName + ".vqm")) { texture = TextureParser.ReadVqmTexture(textureName + ".vqm", Palette); } else if (VirtualFilesystem.Instance.FileExists(textureName + ".map")) { texture = TextureParser.ReadMapTexture(textureName + ".map", Palette); } else { throw new Exception("Texture not found: " + textureName); } var material = Instantiate(transparent ? TransparentMaterialPrefab : TextureMaterialPrefab); material.mainTexture = texture; material.name = textureName; _materials[textureName] = material; } return(_materials[textureName]); }
private void Awake() { _material = GetComponent <MeshRenderer>().material; if (!string.IsNullOrEmpty(TextureFilename)) { _material.mainTexture = TextureParser.ReadMapTexture(TextureFilename, CacheManager.Instance.Palette); } }
// Use this for initialization void Start() { _material = GetComponent <MeshRenderer>().material; var cacheManager = FindObjectOfType <CacheManager>(); _material.mainTexture = TextureParser.ReadMapTexture(TextureFilename, cacheManager.Palette); }
private void DrawTextureSegment() { var bmp = TextureParser.CreateTextureSegmentBMP(); if (bmp != null) { TextureSegmentPictureBox.Image = bmp; } }
private void SelectTextureButton_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.RestoreDirectory = true; if (fileDialog.ShowDialog() == DialogResult.OK) { SelectedTextureLabel.Text = fileDialog.FileName; FilePathToSelectedTexture = fileDialog.FileName; TextureParser.CheckForTIMHeader(fileDialog.FileName); } }
public ObjLoader Create(MaterialStreamProvider materialStreamProvider) { var dataStore = new DataStore(_name); var faceParser = new FaceParser(dataStore); var groupParser = new GroupParser(dataStore); var normalParser = new NormalParser(dataStore); var textureParser = new TextureParser(dataStore); var vertexParser = new VertexParser(dataStore); var materialLibraryLoader = new MaterialLibraryLoader(dataStore); var materialLibraryLoaderFacade = new MaterialLibraryLoaderFacade(materialLibraryLoader, materialStreamProvider); var materialLibraryParser = new MaterialLibraryParser(materialLibraryLoaderFacade); var useMaterialParser = new UseMaterialParser(dataStore); return(new ObjLoader(dataStore, faceParser, groupParser, normalParser, textureParser, vertexParser, materialLibraryParser, useMaterialParser)); }
private void InjectorSelectTimButton_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.RestoreDirectory = true; fileDialog.Filter = "(bin)|*.bin"; if (fileDialog.ShowDialog() == DialogResult.OK) { Debug.WriteLine($"opening {fileDialog.FileName}"); TextureParser.CheckForTIMHeader(fileDialog.FileName); var selectedBmp = TextureParser.CurrentBitmap; InjectTimPreviewPictureBox.Image = selectedBmp; } }
public Texture2D GetTexture(string textureName) { string filename = Path.GetFileNameWithoutExtension(textureName); Texture2D texture = null; if (VirtualFilesystem.Instance.FileExists(filename + ".vqm")) { texture = TextureParser.ReadVqmTexture(filename + ".vqm", Palette); } else if (VirtualFilesystem.Instance.FileExists(filename + ".map")) { texture = TextureParser.ReadMapTexture(filename + ".map", Palette); } else { Debug.LogWarning("Texture not found: " + textureName); } return(texture); }
public void SetUp() { _textureDataStore = new DataStore(); _faceParser = new FaceParser(_textureDataStore); _groupParser = new GroupParser(_textureDataStore); _normalParser = new NormalParser(_textureDataStore); _textureParser = new TextureParser(_textureDataStore); _vertexParser = new VertexParser(_textureDataStore); _materialStreamProviderSpy = new MaterialStreamProviderSpy(); _materialStreamProviderSpy.StreamToReturn = CreateMemoryStreamFromString(MaterialLibraryString); _materialLibraryLoader = new MaterialLibraryLoader(_textureDataStore); _materialLibraryLoaderFacade = new MaterialLibraryLoaderFacade(_materialLibraryLoader, _materialStreamProviderSpy); _materialLibraryParser = new MaterialLibraryParser(_materialLibraryLoaderFacade); _useMaterialParser = new UseMaterialParser(_textureDataStore); _loader = new Loader.Loaders.ObjLoader(_textureDataStore, _faceParser, _groupParser, _normalParser, _textureParser, _vertexParser, _materialLibraryParser, _useMaterialParser); }
public ObjLoader( DataStore dataStore, FaceParser faceParser, GroupParser groupParser, NormalParser normalParser, TextureParser textureParser, VertexParser vertexParser, MaterialLibraryParser materialLibraryParser, UseMaterialParser useMaterialParser) { _dataStore = dataStore; SetupTypeParsers( vertexParser, faceParser, normalParser, textureParser, groupParser, materialLibraryParser, useMaterialParser); }
private bool TryGetMapTexture(string mapName, out Texture2D mapTexture, out ETbl etbl) { List <ETbl> etbls = _vdf.Etbls; if (etbls == null) { mapTexture = null; etbl = null; return(false); } int etblCount = etbls.Count; for (int i = 0; i < etblCount; ++i) { etbl = _vdf.Etbls[i]; if (etbl.MapFile == mapName) { if (VirtualFilesystem.Instance.FileExists(mapName)) { mapTexture = TextureParser.ReadMapTexture(mapName, _cacheManager.Palette); } else { mapTexture = null; } return(true); } } Debug.LogErrorFormat("Map texture '{0}' not found.", mapName); mapTexture = null; etbl = null; return(false); }
public void SetUp() { _textureDataStoreMock = new TextureDataStoreMock(); _textureParser = new TextureParser(_textureDataStoreMock); }
void LoadLevel(string msnFilename) { var cacheManager = FindObjectOfType <CacheManager>(); var terrainPatches = new Terrain[80, 80]; var mdef = MsnMissionParser.ReadMsnMission(msnFilename); cacheManager.Palette = ActPaletteParser.ReadActPalette(mdef.PaletteFilePath); var _surfaceTexture = TextureParser.ReadMapTexture(mdef.SurfaceTextureFilePath, cacheManager.Palette); _surfaceTexture.filterMode = FilterMode.Point; FindObjectOfType <Sky>().TextureFilename = mdef.SkyTextureFilePath; // var skyTexture = TextureParser.ReadMapTexture(mdef.SkyTextureFilePath, cacheManager.Palette); // SkyMaterial.mainTexture = skyTexture; var worldGameObject = GameObject.Find("World"); if (worldGameObject != null) { Destroy(worldGameObject); } worldGameObject = new GameObject("World"); var splatPrototypes = new[] { new SplatPrototype { texture = _surfaceTexture, tileSize = new Vector2(_surfaceTexture.width, _surfaceTexture.height), metallic = 0, smoothness = 0 } }; for (int z = 0; z < 80; z++) { for (int x = 0; x < 80; x++) { if (mdef.TerrainPatches[x, z] == null) { continue; } var patchGameObject = new GameObject("Ter " + x + ", " + z); patchGameObject.transform.position = new Vector3(x * 640, 0, z * 640); patchGameObject.transform.parent = worldGameObject.transform; var terrain = patchGameObject.AddComponent <Terrain>(); terrain.terrainData = mdef.TerrainPatches[x, z].TerrainData; terrain.terrainData.splatPrototypes = splatPrototypes; terrain.materialTemplate = TerrainMaterial; terrain.materialType = Terrain.MaterialType.Custom; var terrainCollider = patchGameObject.AddComponent <TerrainCollider>(); terrainCollider.terrainData = terrain.terrainData; foreach (var odef in mdef.TerrainPatches[x, z].Objects) { if (odef.ClassId == MsnMissionParser.ClassId.Car) { var lblUpper = odef.Label.ToUpper(); GameObject carObj; switch (lblUpper) { case "SPAWN": carObj = Instantiate(SpawnPrefab); carObj.tag = "Spawn"; break; case "REGEN": carObj = Instantiate(RegenPrefab); carObj.tag = "Regen"; break; default: carObj = cacheManager.ImportVcf(odef.Label + ".vcf"); break; } carObj.transform.parent = patchGameObject.transform; carObj.transform.localPosition = odef.LocalPosition; carObj.transform.localRotation = odef.LocalRotation; } else if (odef.ClassId != MsnMissionParser.ClassId.Special) { cacheManager.ImportSdf(odef.Label + ".sdf", patchGameObject.transform, odef.LocalPosition, odef.LocalRotation); } } terrainPatches[x, z] = terrain; } } foreach (var road in mdef.Roads) { var roadGo = new GameObject("Road"); roadGo.transform.parent = worldGameObject.transform; var meshFilter = roadGo.AddComponent <MeshFilter>(); var meshRenderer = roadGo.AddComponent <MeshRenderer>(); meshRenderer.shadowCastingMode = ShadowCastingMode.Off; string roadTextureFilename; switch (road.SegmentType) { case MsnMissionParser.RoadSegmentType.PavedHighway: roadTextureFilename = "r2ayr_51"; break; case MsnMissionParser.RoadSegmentType.DirtTrack: roadTextureFilename = "r2dnr_37"; break; case MsnMissionParser.RoadSegmentType.RiverBed: roadTextureFilename = "r2wnr_39"; break; case MsnMissionParser.RoadSegmentType.FourLaneHighway: roadTextureFilename = "r2ayr_51"; break; default: throw new ArgumentOutOfRangeException(); } var roadMaterial = cacheManager.GetTextureMaterial(roadTextureFilename, false); meshRenderer.material = roadMaterial; var mesh = new Mesh(); var vertices = new List <Vector3>(); var uvs = new List <Vector2>(); var uvIdx = 0; foreach (var roadSegment in road.RoadSegments) { vertices.Add(roadSegment.Left); vertices.Add(roadSegment.Right); uvs.Add(new Vector2(0, uvIdx)); uvs.Add(new Vector2(1, uvIdx)); uvIdx += 1; } var indices = new List <int>(); var idx = 0; for (int i = 0; i < (vertices.Count - 2) / 2; i++) { indices.Add(idx + 2); indices.Add(idx + 1); indices.Add(idx); indices.Add(idx + 2); indices.Add(idx + 3); indices.Add(idx + 1); idx += 2; } mesh.vertices = vertices.ToArray(); mesh.triangles = indices.ToArray(); mesh.uv = uvs.ToArray(); mesh.RecalculateNormals(); meshFilter.sharedMesh = mesh; } foreach (var ldef in mdef.StringObjects) { var sdfObj = cacheManager.ImportSdf(ldef.Label + ".sdf", null, Vector3.zero, Quaternion.identity); for (int i = 0; i < ldef.StringPositions.Count; i++) { var pos = ldef.StringPositions[i]; var localPosition = new Vector3(pos.x % 640, pos.y, pos.z % 640); var patchPosX = (int)(pos.x / 640.0f); var patchPosZ = (int)(pos.z / 640.0f); sdfObj.name = ldef.Label + " " + i; sdfObj.transform.parent = terrainPatches[patchPosX, patchPosZ].transform; sdfObj.transform.localPosition = localPosition; if (i < ldef.StringPositions.Count - 1) { sdfObj.transform.LookAt(ldef.StringPositions[i + 1], Vector3.up); } else { sdfObj.transform.LookAt(ldef.StringPositions[i - 1], Vector3.up); sdfObj.transform.localRotation *= Quaternion.AngleAxis(180, Vector3.up); } if (i < ldef.StringPositions.Count - 1) { sdfObj = Instantiate(sdfObj); } } } worldGameObject.transform.position = new Vector3(-mdef.Middle.x * 640, 0, -mdef.Middle.y * 640); FindObjectOfType <Light>().color = cacheManager.Palette[176]; Camera.main.backgroundColor = cacheManager.Palette[239]; RenderSettings.fogColor = cacheManager.Palette[239]; RenderSettings.ambientLight = cacheManager.Palette[247]; if (MissionFile.ToLower().StartsWith("m")) { var importedVcf = cacheManager.ImportVcf(VcfToLoad); importedVcf.AddComponent <InputCarController>(); var spawnPoint = GameObject.FindGameObjectsWithTag("Spawn")[0]; importedVcf.transform.position = spawnPoint.transform.position; importedVcf.transform.rotation = spawnPoint.transform.rotation; Camera.main.GetComponent <SmoothFollow>().Target = importedVcf.transform; } }
public IEnumerator LoadLevel(string msnFilename) { _game.LevelName = msnFilename; string sceneName = SceneManager.GetActiveScene().name; if (sceneName != "Level") { AsyncOperation sceneLoad = SceneManager.LoadSceneAsync(1, LoadSceneMode.Single); sceneLoad.allowSceneActivation = true; while (!sceneLoad.isDone) { yield return(null); } yield break; } Terrain[,] terrainPatches = new Terrain[80, 80]; MsnMissionParser.MissonDefinition mdef = MsnMissionParser.ReadMsnMission(msnFilename); _cacheManager.Palette = ActPaletteParser.ReadActPalette(mdef.PaletteFilePath); Texture2D surfaceTexture = TextureParser.ReadMapTexture(mdef.SurfaceTextureFilePath, _cacheManager.Palette, TextureFormat.RGB24, true, FilterMode.Point); GameObject.Find("Sky").GetComponent <Sky>().TextureFilename = mdef.SkyTextureFilePath; GameObject worldGameObject = GameObject.Find("World"); if (worldGameObject != null) { Object.Destroy(worldGameObject); } worldGameObject = new GameObject("World"); TerrainLayer[] terrainLayers = new[] { new TerrainLayer { diffuseTexture = surfaceTexture, tileSize = new Vector2(surfaceTexture.width, surfaceTexture.height) / 10.0f, metallic = 0, smoothness = 0 } }; for (int z = 0; z < 80; z++) { for (int x = 0; x < 80; x++) { if (mdef.TerrainPatches[x, z] == null) { continue; } GameObject patchGameObject = new GameObject("Ter " + x + ", " + z); patchGameObject.layer = LayerMask.NameToLayer("Terrain"); patchGameObject.transform.position = new Vector3(x * 640, 0, z * 640); patchGameObject.transform.parent = worldGameObject.transform; Terrain terrain = patchGameObject.AddComponent <Terrain>(); terrain.terrainData = mdef.TerrainPatches[x, z].TerrainData; terrain.terrainData.terrainLayers = terrainLayers; terrain.materialTemplate = _terrainMaterial; terrain.materialType = Terrain.MaterialType.Custom; TerrainCollider terrainCollider = patchGameObject.AddComponent <TerrainCollider>(); terrainCollider.terrainData = terrain.terrainData; foreach (MsnMissionParser.Odef odef in mdef.TerrainPatches[x, z].Objects) { GameObject go = null; if (odef.ClassId == MsnMissionParser.ClassId.Car) { string lblUpper = odef.Label.ToUpper(); // Training mission uses hardcoded VCF for player. string vcfName = odef.Label; if (msnFilename.ToLower() == "a01.msn" && vcfName == "vppirna1") { vcfName = "vppa01"; } switch (lblUpper) { case "SPAWN": go = Object.Instantiate(_spawnPrefab); go.tag = "Spawn"; break; case "REGEN": go = Object.Instantiate(_regenPrefab); go.tag = "Regen"; break; default: go = _cacheManager.ImportVcf(vcfName + ".vcf", odef.IsPlayer, out _); Car car = go.GetComponent <Car>(); car.TeamId = odef.TeamId; car.IsPlayer = odef.IsPlayer; break; } go.transform.parent = patchGameObject.transform; go.transform.localPosition = odef.LocalPosition; go.transform.localRotation = odef.LocalRotation; if (odef.IsPlayer) { CameraManager.Instance.MainCamera.GetComponent <SmoothFollow>().Target = go.transform; go.AddComponent <CarInput>(); } } else if (odef.ClassId != MsnMissionParser.ClassId.Special) { bool canWreck = odef.ClassId == MsnMissionParser.ClassId.Struct1 || odef.ClassId == MsnMissionParser.ClassId.Ramp || odef.ClassId == MsnMissionParser.ClassId.Struct2; go = _cacheManager.ImportSdf(odef.Label + ".sdf", patchGameObject.transform, odef.LocalPosition, odef.LocalRotation, canWreck, out Sdf sdf, out GameObject wreckedPart); if (odef.ClassId == MsnMissionParser.ClassId.Sign) { go.AddComponent <Sign>(); } else if (canWreck) { Building building = go.AddComponent <Building>(); building.Initialise(sdf, wreckedPart); } } if (go != null) { go.name = odef.Label + "_" + odef.Id; if (mdef.FSM != null) { FSMEntity[] entities = mdef.FSM.EntityTable; for (int i = 0; i < entities.Length; ++i) { if (entities[i].Value == odef.Label && entities[i].Id == odef.Id) { WorldEntity worldEntity = go.GetComponent <WorldEntity>(); if (worldEntity != null) { entities[i].WorldEntity = worldEntity; worldEntity.Id = i; } entities[i].Object = go; break; } } } } } terrainPatches[x, z] = terrain; } } RoadManager roadManager = RoadManager.Instance; foreach (MsnMissionParser.Road road in mdef.Roads) { roadManager.CreateRoadObject(road, mdef.Middle * 640); } foreach (MsnMissionParser.Ldef ldef in mdef.StringObjects) { GameObject sdfObj = _cacheManager.ImportSdf(ldef.Label + ".sdf", null, Vector3.zero, Quaternion.identity, false, out _, out _); for (int i = 0; i < ldef.StringPositions.Length; i++) { Vector3 pos = ldef.StringPositions[i]; Vector3 localPosition = new Vector3(pos.x % 640, pos.y, pos.z % 640); int patchPosX = (int)(pos.x / 640.0f); int patchPosZ = (int)(pos.z / 640.0f); sdfObj.name = ldef.Label + " " + i; sdfObj.transform.parent = terrainPatches[patchPosX, patchPosZ].transform; sdfObj.transform.localPosition = localPosition; if (i < ldef.StringPositions.Length - 1) { sdfObj.transform.LookAt(ldef.StringPositions[i + 1], Vector3.up); } else { sdfObj.transform.LookAt(ldef.StringPositions[i - 1], Vector3.up); sdfObj.transform.localRotation *= Quaternion.AngleAxis(180, Vector3.up); } if (i < ldef.StringPositions.Length - 1) { sdfObj = Object.Instantiate(sdfObj); } } } worldGameObject.transform.position = new Vector3(-mdef.Middle.x * 640, 0, -mdef.Middle.y * 640); Object.FindObjectOfType <Light>().color = _cacheManager.Palette[176]; UnityEngine.Camera.main.backgroundColor = _cacheManager.Palette[239]; RenderSettings.fogColor = _cacheManager.Palette[239]; RenderSettings.ambientLight = _cacheManager.Palette[247]; List <Car> cars = EntityManager.Instance.Cars; foreach (Car car in cars) { car.transform.parent = null; } if (mdef.FSM != null) { foreach (StackMachine machine in mdef.FSM.StackMachines) { machine.Reset(); machine.Constants = new IntRef[machine.InitialArguments.Length]; for (int i = 0; i < machine.Constants.Length; i++) { int stackValue = machine.InitialArguments[i]; machine.Constants[i] = mdef.FSM.Constants[stackValue]; } } FSMRunner fsmRunner = FSMRunner.Instance; fsmRunner.FSM = mdef.FSM; } }
private void ReloadTextureButton_Click(object sender, EventArgs e) { TextureParser.CheckForTIMHeader(SelectedTextureLabel.Text); }