Exemple #1
0
        // Append a texture definition from wad
        public void Add(byte[] lumpData, PatchTable patchTable)
        {
            uint amt = BitConverter.ToUInt32(lumpData, 0);

            uint[] offsets = new uint[amt];
            int    i;

            for (i = 0; i < amt; i++)
            {
                offsets[i] = BitConverter.ToUInt32(lumpData, 4 + (i * 4));
            }


            for (i = 0; i < amt; i++)
            {
                int offset = (int)offsets[i];

                uint             patchCount = BitConverter.ToUInt16(lumpData, offset + 20);
                List <DoomPatch> patches    = new List <DoomPatch>();
                for (int j = offset + 22; j < (offset + 22) + (patchCount * 10); j += 10)
                {
                    DoomPatch np = new DoomPatch(
                        (int)BitConverter.ToInt16(lumpData, j),
                        (int)BitConverter.ToInt16(lumpData, j + 2),
                        patchTable.patches[(int)BitConverter.ToUInt16(lumpData, j + 4)]
                        );
                    patches.Add(np);
                }

                DoomTexture newTex = new DoomTexture(
                    WadFile.GetString(lumpData, offset),
                    (int)BitConverter.ToUInt16(lumpData, offset + 12),
                    (int)BitConverter.ToUInt16(lumpData, offset + 14),
                    patches
                    );

                if (textures.ContainsKey(newTex.name))
                {
                    textures[newTex.name] = newTex;
                }
                else
                {
                    //Debug.Log(newTex.name);
                    textures.Add(newTex.name, newTex);
                }
            }
        }
        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 #3
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);
        }