Ejemplo n.º 1
0
        public static Material BuildTextureMaterial(WadFile wad, string textureName)
        {
            if (paletteLookup == null)
            {
                Init(wad);
            }

            if (wad.textureTable.Contains(textureName.ToUpper()))
            {
                DoomTexture texture  = wad.textureTable.Get(textureName.ToUpper());
                Material    material = new Material(Shader.Find("Doom/Texture"));
                material.SetTexture("_MainTex", DoomGraphic.BuildTexture(textureName.ToUpper(), wad));
                material.SetTexture("_Palette", paletteLookup);
                material.SetTexture("_Colormap", colormapLookup);
                material.enableInstancing = true;
                return(material);
            }
            return(null);
        }
Ejemplo n.º 2
0
        // public GameObject BuildCollisionMesh()
        // {
        //     GameObject output = new GameObject("Collision");

        //     return output;
        // }

        public GameObject BuildMesh()
        {
            // SETUP
            flatMaterials = MapTextureBuilder.BuildFlatMaterials(wad, MapTextureBuilder.FindMapFlats(map));
            wallMaterials = MapTextureBuilder.BuildTextureMaterials(wad, MapTextureBuilder.FindMapTextures(map));

            DoomTexture texture = wad.textureTable.Get("SKY1");

            skyMaterial = new Material(Shader.Find("Doom/DoomSky"));
            skyMaterial.SetTexture("_MainTex", DoomGraphic.BuildTexture("SKY1", wad));
            skyMaterial.SetTexture("_Palette", MapTextureBuilder.paletteLookup);
            skyMaterial.SetTexture("_Colormap", MapTextureBuilder.colormapLookup);
            skyMaterial.enableInstancing = true;

            wallMaterials.Add("_SKY", MapTextureBuilder.BuildTextureMaterial(wad, "SKY1"));
            GameObject output = new GameObject("MAP");

            lines    = new GameObject("Lines").transform;
            sectors  = new GameObject("Sectors").transform;
            triggers = new GameObject("Triggers").transform;

            lines.parent    = output.transform;
            sectors.parent  = output.transform;
            triggers.parent = output.transform;

            sectorObjects = new SectorObject[map.sectors.Length];

            for (int i = 0; i < map.sectors.Length; i++)
            {
                GameObject gameObject = new GameObject($"Sector {i}");
                gameObject.transform.parent = sectors;
                SectorObject sectorObject = gameObject.AddComponent <SectorObject>();
                sectorObject.sector                 = i;
                sectorObject.lines                  = map.GetLinesOfSector(i);
                sectorObjects[i]                    = sectorObject;
                sectorObject.floor                  = new GameObject("Floor").transform;
                sectorObject.ceiling                = new GameObject("Ceiling").transform;
                sectorObject.floor.parent           = sectorObject.transform;
                sectorObject.ceiling.parent         = sectorObject.transform;
                sectorObject.initialFloorPosition   = map.sectors[i].floorHeight;
                sectorObject.initialCeilingPosition = map.sectors[i].ceilingHeight;

                sectorObject.meshGenerator = this;
            }

            // SECTORS
            for (int i = 0; i < nodeTri.subsectorHulls.Count; i++)
            {
                if (nodeTri.subsectorHulls[i].hull.Length > 2)
                {
                    Sector    sector          = map.sectors[nodeTri.subsectorHulls[i].sector];
                    Transform sectorTransform = sectors.GetChild(nodeTri.subsectorHulls[i].sector);

                    SubsectorFloorObject(
                        MeshFromConvexHull(nodeTri.subsectorHulls[i].hull, sector.floorHeight, false),
                        sector,
                        sector.floorTexture == "F_SKY1"?skyMaterial:flatMaterials[sector.floorTexture]
                        ).transform.SetParent(sectorObjects[nodeTri.subsectorHulls[i].sector].floor, false);

                    SubsectorCeilingObject(
                        MeshFromConvexHull(nodeTri.subsectorHulls[i].hull, sector.ceilingHeight, true),
                        sector,
                        sector.ceilingTexture == "F_SKY1"?skyMaterial:flatMaterials[sector.ceilingTexture]
                        ).transform.SetParent(sectorObjects[nodeTri.subsectorHulls[i].sector].ceiling, false);
                }
            }

            // WALLS

            triggerList = new List <LinedefTrigger>();

            for (int i = 0; i < map.linedefs.Length; i++)
            {
                var line  = new GameObject($"Line {i}");
                var quads = BuildLine(i);
                for (int j = 0; j < quads.Length; j++)
                {
                    quads[j].transform.SetParent(line.transform, false);
                }
                line.transform.parent = lines;

                if (map.linedefs[i].special != 0)
                {
                    var triggerObject = new GameObject($"Trigger {i}");
                    var trigger       = triggerObject.AddComponent <LinedefTrigger>();
                    triggerObject.layer            = LayerMask.NameToLayer("Trigger");
                    triggerObject.transform.parent = triggers;

                    trigger.linedefIndex = i;
                    trigger.sectorTag    = map.linedefs[i].tag;
                    trigger.specialType  = map.linedefs[i].special;
                    trigger.doomMesh     = this;

                    trigger.Init();

                    if (trigger.triggerType == TriggerType.Use || trigger.triggerType == TriggerType.Shoot)
                    {
                        Mesh mesh = BuildTriggerMesh(i);
                        triggerObject.AddComponent <MeshFilter>().sharedMesh = mesh;

                        var collider = triggerObject.AddComponent <MeshCollider>();
                        collider.sharedMesh = mesh;
                        collider.convex     = true;
                        collider.isTrigger  = true;
                    }
                    else
                    {
                        triggerList.Add(trigger);
                    }
                }
            }

            return(output);
        }