Beispiel #1
0
    // Use this for initialization
    void Start()
    {
        CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-us");

        _gobManager = new GOBManager(GamePath, new[] { Path.Combine(GamePath, "Resource\\Res1hi.gob"), Path.Combine(GamePath, "Resource\\Res2.gob"), Path.Combine(GamePath, "Episode\\JK1.GOB") });

        _cmp  = new CMP();
        Atlas = new Texture2D(256, 256, TextureFormat.ARGB32, true);
        StandardMaterial.mainTexture = Atlas;

        if (!string.IsNullOrEmpty(JKLToLoad))
        {
            var jkl       = new JKL();
            var jklParser = new JKLParser(jkl, _gobManager.GetStream(@"jkl\" + JKLToLoad)); //new FileStream(@"Extracted\jkl\" + JKLToLoad, FileMode.Open));
            jklParser.Parse();

            _cmp.ParseCMP(_gobManager.GetStream(@"misc\cmp\" + jkl.WorldColorMaps[0]));
            LoadTextures(jkl.Materials.Take(jkl.ActualNumberOfMaterials).Select(m => m.Name).ToArray());
            BuildMapSectors(jkl);

            foreach (var thing in jkl.Things)
            {
                if (thing.Template.Parameters.ContainsKey("model3d"))
                {
                    var modelFilename = thing.Template.Parameters["model3d"];

                    _3DO       threedo;
                    GameObject thingGameObject;
                    if (_3DOCache.ContainsKey(modelFilename))
                    {
                        var threedoLoadResult = _3DOCache[modelFilename];
                        threedo         = threedoLoadResult.Threedo;
                        thingGameObject = Instantiate(threedoLoadResult.GameObject);
                    }
                    else
                    {
                        var threedoLoadResult = Load3DO(modelFilename);
                        threedo         = threedoLoadResult.Threedo;
                        thingGameObject = threedoLoadResult.GameObject;
                    }

                    thingGameObject.transform.position = new Vector3(thing.X * 10, thing.Z * 10, thing.Y * 10);
                    thingGameObject.transform.rotation = Quaternion.Euler(thing.Pitch, thing.Yaw, thing.Roll);

                    if (thing.Template.Parameters.ContainsKey("puppet"))
                    {
                        var puppetFilename = thing.Template.Parameters["puppet"];
                        var puppet         = PUPPETParser.Parse(_gobManager.GetStream(@"misc\pup\" + puppetFilename));
                        var kp             = new KEYParser();

                        if (puppet.Modes.ContainsKey(0) && puppet.Modes[0].ContainsKey("stand"))
                        {
                            var keyFilename = puppet.Modes[0]["stand"].KeyFile;
                            var animClip    = kp.Parse(threedo, thingGameObject.transform, keyFilename,
                                                       _gobManager.GetStream(@"3do\key\" + keyFilename));

                            var anim = thingGameObject.GetComponent <Animation>();
                            if (anim == null)
                            {
                                anim = thingGameObject.AddComponent <Animation>();
                                anim.AddClip(animClip, animClip.name);
                            }
                            anim.Play(animClip.name);
                        }
                    }
                }
            }
        }
    }
Beispiel #2
0
    private void BuildMapSectors(JKL jkl)
    {
        for (var sectorIdx = 0; sectorIdx < jkl.Sectors.Length; sectorIdx++)
        {
            var sector = jkl.Sectors[sectorIdx];
            var go     = new GameObject("Sector " + sectorIdx);
            go.transform.SetParent(transform, false);

            var meshFilter   = go.AddComponent <MeshFilter>();
            var meshRenderer = go.AddComponent <MeshRenderer>();
            meshRenderer.sharedMaterial = StandardMaterial;

            var mesh = new Mesh();

            var vertices     = new List <Vector3>();
            var triangles    = new List <int>();
            var normals      = new List <Vector3>();
            var uvs          = new List <Vector2>();
            var atlasPosSize = new List <Vector4>();
            var intensities  = new List <Color>();

            for (int i = 0; i < sector.SurfaceCount; i++)
            {
                var surfaceIdx = sector.SurfaceStartIdx + i;

                var surface = jkl.WorldSurfaces[surfaceIdx];
                if (surface.Adjoin != null)
                {
                    continue;
                }

                var viStart = vertices.Count;
                for (var s = 0; s < surface.SurfaceVertexGroups.Length; s++)
                {
                    SurfaceVertexGroup t = surface.SurfaceVertexGroups[s];
                    var vertIndex        = t.VertexIdx;
                    var vert             = new Vector3(jkl.WorldVertices[vertIndex].x, jkl.WorldVertices[vertIndex].z,
                                                       jkl.WorldVertices[vertIndex].y);
                    vertices.Add(vert);
                    normals.Add(new Vector3(surface.SurfaceNormal.x, surface.SurfaceNormal.z, surface.SurfaceNormal.y));

                    var intensity = Mathf.Clamp01(surface.Intensities[s]);
                    intensities.Add(new Color(intensity, intensity, intensity));

                    var uv = t.TextureVertex;
                    if (uv.HasValue)
                    {
                        var material = _materialLookup[surface.Material.Name.ToLower()];

                        var uv2 = uv.Value;
                        atlasPosSize.Add(new Vector4(material.Rects[0].x, material.Rects[0].y, material.Rects[0].width,
                                                     material.Rects[0].height));

                        uv2.x = uv2.x / material.Sizes[0].x;
                        uv2.y = uv2.y / material.Sizes[0].y;

                        uvs.Add(uv2);
                    }
                    else
                    {
                        uvs.Add(Vector2.zero);
                        atlasPosSize.Add(new Vector4(0, 0, 0, 0));
                    }
                }

                var numTriangles = surface.SurfaceVertexGroups.Length - 3 + 1;
                for (var t = 1; t <= numTriangles; t++)
                {
                    triangles.Add(viStart + t);
                    triangles.Add(viStart);
                    triangles.Add(viStart + t + 1);
                }
            }

            mesh.SetVertices(vertices);
            mesh.SetUVs(0, uvs);
            mesh.SetUVs(1, atlasPosSize);
            mesh.SetColors(intensities);
            mesh.SetNormals(normals);
            mesh.SetTriangles(triangles, 0);

            meshFilter.sharedMesh = mesh;

            var collider = go.AddComponent <MeshCollider>();
            collider.cookingOptions = MeshColliderCookingOptions.WeldColocatedVertices |
                                      MeshColliderCookingOptions.EnableMeshCleaning |
                                      MeshColliderCookingOptions.CookForFasterSimulation;

            collider.sharedMesh = mesh;
        }
    }