예제 #1
0
        // TODO: make redundant
        /// <summary>
        /// Loads a Torii object (3D model with animations) and returns a gameobject
        /// </summary>
        public static GameObject LoadObject(string filePath, bool collisionMesh, ResourceLifespan lifespan)
        {
            GameObject g = UnityEngine.Object.Instantiate(ResourceManager.Load <GameObject>(filePath, lifespan));
            TOBJ       t = g.GetComponent <ToriiObject>().ToriiObj;

            Renderer[] renderers = g.GetComponentsInChildren <Renderer>();
            foreach (Renderer r in renderers)
            {
                Material m = r.material;

                // load texture
                if (!string.IsNullOrEmpty(t.ObjectTexture))
                {
                    if (Path.GetFileNameWithoutExtension(t.ObjectTexture).Contains("["))
                    {
                        // part of a texture set
                        m.shader = Shader.Find(GameSettings.CurrentSettings.UseClassicShaders ? "LSD/PSX/TransparentSet" : "LSD/TransparentSet");

                        string texNameWithoutExtension = Path.GetFileNameWithoutExtension(t.ObjectTexture);
                        string baseTexName             = texNameWithoutExtension.Substring(1);

                        string pathToTextureDir = Path.GetDirectoryName(t.ObjectTexture);

                        m.SetTexture("_MainTexA", ResourceManager.Load <Texture2D>(PathCombine(pathToTextureDir, "A" + baseTexName) + ".png", lifespan));
                        m.SetTexture("_MainTexB", ResourceManager.Load <Texture2D>(PathCombine(pathToTextureDir, "B" + baseTexName) + ".png", lifespan));
                        m.SetTexture("_MainTexC", ResourceManager.Load <Texture2D>(PathCombine(pathToTextureDir, "C" + baseTexName) + ".png", lifespan));
                        m.SetTexture("_MainTexD", ResourceManager.Load <Texture2D>(PathCombine(pathToTextureDir, "D" + baseTexName) + ".png", lifespan));
                    }
                    else
                    {
                        m.shader = Shader.Find(GameSettings.CurrentSettings.UseClassicShaders ? "LSD/PSX/Transparent" : "Transparent/Diffuse");
                        m.SetTexture("_MainTex", ResourceManager.Load <Texture2D>(t.ObjectTexture, lifespan));
                    }
                }
            }

            // load animations
            if (t.NumberOfAnimations > 0)
            {
                ToriiObjectAnimator animator = g.AddComponent <ToriiObjectAnimator>();
                foreach (Transform child in g.transform)
                {
                    animator.Objects.Add(child.gameObject);
                }
                animator.ToriiObject = t;
            }

            if (collisionMesh)
            {
                foreach (Transform child in g.transform)
                {
                    child.gameObject.AddComponent <MeshCollider>();
                }
            }

            g.transform.localScale = new Vector3(-g.transform.localScale.x, g.transform.localScale.y, g.transform.localScale.z);
            g.SetActive(true);

            return(g);
        }
예제 #2
0
        // TODO: ToriiObjectReader is probably obsolete
        public static bool Read(string location, ref TOBJ tobj)
        {
            using (BinaryReader reader = new BinaryReader(File.Open(location, FileMode.Open)))
            {
                char[] signature = reader.ReadChars(8);
                if (signature != new [] { 'T', 'O', 'R', 'I', 'I', 'O', 'B', 'J' })
                {
                    throw new FileLoadException("Invalid signature in .tobj file.");
                }

                int version = reader.ReadInt32();
                Debug.Log("Version is: " + version);
                // check version later?

                tobj.ObjectFile         = reader.ReadString();
                tobj.ObjectTexture      = reader.ReadString();
                tobj.NumberOfObjects    = reader.ReadInt32();
                tobj.MaxAnimationID     = reader.ReadInt32();
                tobj.NumberOfAnimations = reader.ReadInt32();

                tobj.Animations = new TANIM[tobj.NumberOfAnimations];
                for (int i = 0; i < tobj.NumberOfAnimations; i++)
                {
                    TANIM anim;
                    anim.Name              = reader.ReadString();
                    anim.ID                = reader.ReadInt32();
                    anim.MaxValue          = reader.ReadSingle();
                    anim.MaxKeyframeID     = reader.ReadInt32();
                    anim.NumberOfKeyframes = reader.ReadInt32();

                    anim.Keyframes = new TKEYFRAME[anim.NumberOfKeyframes];
                    for (int j = 0; j < anim.NumberOfKeyframes; j++)
                    {
                        TKEYFRAME kf;
                        kf.ID    = reader.ReadInt32();
                        kf.Value = reader.ReadSingle();

                        kf.ObjStates = new OBJSTATE[tobj.NumberOfObjects];
                        for (int k = 0; k < tobj.NumberOfObjects; k++)
                        {
                            OBJSTATE state;
                            state.Position = ReadVector3(reader);
                            state.Rotation = ReadQuaternion(reader);
                            state.Scale    = ReadVector3(reader);

                            kf.ObjStates[k] = state;
                        }

                        anim.Keyframes[j] = kf;
                    }

                    tobj.Animations[i] = anim;
                }
            }

            return(true);
        }
예제 #3
0
        // TODO: handle missing files

        public static DataType Load <DataType>(string filePath, ResourceLifespan lifespan = ResourceLifespan.GLOBAL, bool absolutePath = false)
        {
            string normalizedPath = IOUtil.NormalizePath(absolutePath ? filePath : IOUtil.PathCombine(Application.streamingAssetsPath, filePath));

            // if the cache contains the resource, return it
            if (_resources.ContainsKey(normalizedPath))
            {
                return(((GenericResource <DataType>)_resources[normalizedPath]).Resource);
            }

            string fileExt = Path.GetExtension(normalizedPath);

            bool fileNotFound = false;

            switch (fileExt)
            {
            case ".png":
            {
                GenericResource <Texture2D> resource = new GenericResource <Texture2D>();
                resource.Resource = IOUtil.LoadPNG(normalizedPath);

                if (resource.Resource == null)
                {
                    fileNotFound = true;
                    break;
                }

                resource.Lifespan = lifespan;
                _resources.Add(normalizedPath, resource);
                break;
            }

            case ".json":
            {
                GenericResource <JSONClass> resource = new GenericResource <JSONClass>();
                resource.Resource = IOUtil.ReadJSONFromDisk(normalizedPath);

                if (resource.Resource == null)
                {
                    fileNotFound = true;
                    break;
                }

                resource.Lifespan = lifespan;
                _resources.Add(normalizedPath, resource);
                break;
            }

            case ".map":
            {
                GenericResource <GameObject> resource = new GenericResource <GameObject>();

                if (!File.Exists(normalizedPath))
                {
                    Debug.LogError("Could not load resource " + normalizedPath);
                    fileNotFound = true;
                    break;
                }

                resource.Resource = MapReader.LoadMap(normalizedPath, IOUtil.PathCombine(Application.streamingAssetsPath, "textures", "wad"),
                                                      Shader.Find(GameSettings.CurrentSettings.UseClassicShaders ? "LSD/PSX/DiffuseSetNoAffine" : "LSD/DiffuseSet"),
                                                      Shader.Find(GameSettings.CurrentSettings.UseClassicShaders ? "LSD/PSX/TransparentSetNoAffine" : "LSD/TransparentSet"));
                resource.Resource.transform.SetParent(_resourceManagerGameObject.transform);
                resource.Resource.SetActive(false);
                resource.Lifespan = lifespan;
                _resources.Add(normalizedPath, resource);
                break;
            }

            case ".tmap":
            {
                GenericResource <TMAP> resource = new GenericResource <TMAP>();

                if (!File.Exists(normalizedPath))
                {
                    Debug.LogError("Could not load resource " + normalizedPath);
                    fileNotFound = true;
                    break;
                }

                resource.Resource = ToriiMapReader.ReadFromFile(normalizedPath);
                resource.Lifespan = lifespan;
                _resources.Add(normalizedPath, resource);
                break;
            }

            case ".tobj":
            {
                GenericResource <GameObject> resource = new GenericResource <GameObject>();

                if (!File.Exists(normalizedPath))
                {
                    Debug.LogError("Could not load resource " + normalizedPath);
                    fileNotFound = true;
                    break;
                }

                TOBJ tobj = new TOBJ();
                ToriiObjectReader.Read(normalizedPath, ref tobj);
                resource.Resource = OBJReader.ReadOBJString(tobj.ObjectFile);
                resource.Resource.AddComponent <ToriiObject>();
                ToriiObject toriiObjScript = resource.Resource.GetComponent <ToriiObject>();
                toriiObjScript.ToriiObj = tobj;
                resource.Resource.transform.SetParent(_resourceManagerGameObject.transform);
                resource.Resource.SetActive(false);
                resource.Lifespan = lifespan;
                _resources.Add(normalizedPath, resource);
                break;
            }

            default:
            {
                throw new ResourceLoadException("Did not recognize file type: " + normalizedPath);
            }
            }

            if (fileNotFound)
            {
                throw new ResourceLoadException("Could not find resource!");
            }

            return(((GenericResource <DataType>)_resources[normalizedPath]).Resource);
        }