Exemple #1
0
        public static Texture2D BuildPatch(string name, WadFile wad, bool ignoreCache = false, bool trueColor = false)
        {
            if (!wad.Contains(name.ToUpper()))
            {
                return(null);
            }

            if (patchCache == null)
            {
                patchCache = new Dictionary <string, Texture2D>();
            }

            if (!ignoreCache)
            {
                if (patchCache.ContainsKey(name))
                {
                    return(patchCache[name]);
                }
            }

            Texture2D output = new DoomGraphic(wad.GetLump(name.ToUpper())).ToRenderMap();

            if (trueColor)
            {
                output = new DoomGraphic(wad.GetLump(name.ToUpper())).ToTexture2D(new Palette(wad.GetLump("PLAYPAL")));
            }

            if (!ignoreCache)
            {
                patchCache.Add(name, output);
            }

            return(output);
        }
Exemple #2
0
        public static Sprite BuildSprite(string name, WadFile wad)
        {
            if (spriteCache == null)
            {
                spriteCache = new Dictionary <string, Sprite>();
            }

            if (spriteCache.ContainsKey(name))
            {
                return(spriteCache[name]);
            }

            Sprite output = new DoomGraphic(wad.GetLump(name.ToUpper())).ToSprite();

            spriteCache.Add(name, output);

            return(output);
        }
        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);
        }
Exemple #4
0
        public static Texture2D BuildTexture(string name, WadFile wad, TextureTable textures, bool trueColor = false)
        {
            if (textureCache == null)
            {
                textureCache = new Dictionary <string, Texture2D>();
            }

            if (textureCache.ContainsKey(name))
            {
                return(textureCache[name]);
            }

            DoomTexture texture = textures.Get(name.ToUpper());


            Texture2D output = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false, true);

            for (int i = 0; i < texture.patches.Count; i++)
            {
                DoomPatch p       = texture.patches[i];
                Texture2D patch2d = DoomGraphic.BuildPatch(p.patchName, wad, trueColor);

                if (patch2d == null)
                {
                    return(null);
                }

                int copyX = (p.originX < 0)?-p.originX:0;
                int copyY = (p.originY < 0)?-p.originY:0;

                int pasteX = (p.originX > 0)?p.originX:0;
                int pasteY = (p.originY > 0)?p.originY:0;

                int copyWidth = patch2d.width - copyX;
                if (copyWidth > output.width - pasteX)
                {
                    copyWidth = output.width - pasteX;
                }

                int copyHeight = patch2d.height - copyY;
                if (copyHeight > output.height - pasteY)
                {
                    copyHeight = output.height - pasteY;
                }

                for (int a = 0; a < copyWidth; a++)
                {
                    for (int b = 0; b < copyHeight; b++)
                    {
                        Color col = patch2d.GetPixel(copyX + a, copyY + b);
                        if (col.a != 0f)
                        {
                            output.SetPixel(pasteX + a, pasteY + b, col);
                        }
                    }
                }
            }

            output.Apply();
            output.wrapMode   = TextureWrapMode.Repeat;
            output.filterMode = FilterMode.Point;

            textureCache.Add(name, output);

            return(output);
        }
        // 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);
        }