Exemple #1
0
    public Level(Parser.Tr2Level leveldata, Material sharedmaterial, Transform roottransform)
    {
        m_LevelRoot = roottransform;
        m_leveldata = leveldata;
        //if (m_leveldata != null && m_leveldata.NumRooms > 0)
        {
            //TextureUV.GenerateTextureTile ismoved to Loader.cs for better responsibility managment
            //Trying to set assigned render material property, marks shared material as instance.
            //So change property of shared material before assign it to renderer.
            m_SharedMaterialWater       = Resources.Load("water", typeof(Material)) as Material;
            m_SharedMaterialWaterHolder = Resources.Load("water_holder", typeof(Material)) as Material;
            Shader waterEffectShader = Resources.Load("WaterEffect", typeof(Shader)) as Shader;
            //init materials
            m_SharedMaterial       = sharedmaterial;
            m_SharedMaterial.color = new Color(1f, 1f, 1f, 1.0f);
            m_SharedMaterial.SetFloat("_InSideWater", 0);
            m_SharedMaterial.SetFloat("_WaterPlaneY", 0);

            m_SharedMaterialWater.mainTexture = m_SharedMaterial.mainTexture;
            //m_SharedMaterialWater.color = new Color(0.045f, 0.075f,0.090f, 1) ; //should be set by user
            m_SharedMaterialWaterHolder.mainTexture = m_SharedMaterial.mainTexture;
            //m_SharedMaterialWaterHolder.color = new Color(0.45f * 0.5f, 0.75f * 0.5f, 0.90f * 0.5f, 1);
            m_SharedMaterialWaterHolder.SetFloat("_InSideWater", 0);

            m_RoomExs = new RoomEx[m_leveldata.NumRooms];

            Transform PrefavContainer = new GameObject("PrefavContainer").transform;
            PrefavContainer.parent = m_LevelRoot;

            m_DynamicPrefabs = BuildDynamicPrefabObjects(PrefavContainer);
            m_StaticPrefabs  = BuildStaticPrefabObjects(PrefavContainer);

            //determine animation clip size for each movable object
            for (int i = 0; i < m_DynamicPrefabs.Count - 1; i++)
            {
                int startoffset0 = m_DynamicPrefabs[i].Animation;
                int startoffset1 = m_DynamicPrefabs[i + 1].Animation;
                m_DynamicPrefabs[i].NumClips = startoffset1 - startoffset0;
            }
            if (m_DynamicPrefabs.Count > 0)
            {
                int startoffset0 = m_DynamicPrefabs[m_DynamicPrefabs.Count - 1].Animation;
                m_DynamicPrefabs[m_DynamicPrefabs.Count - 1].NumClips = (int)m_leveldata.NumAnimations - 1 - startoffset0;
            }

            //attach animation and their state change
            for (int i = 0; i < m_DynamicPrefabs.Count; i++)
            {
                List <TRAnimationClip> clips = Animator.AttachAnimation(m_DynamicPrefabs[i], m_leveldata);
                AnimationStateMapper.BuildMap(clips, m_DynamicPrefabs[i].UnityAnimation, m_leveldata);
            }

            //attach 3DText Box to movable objects  to mark their ID
            if (Settings.ShowObjectID)
            {
                for (int i = 0; i < m_DynamicPrefabs.Count; i++)
                {
                    if (m_leveldata.Text3DPrefav != null)
                    {
                        TextMesh text3d = (TextMesh)GameObject.Instantiate(m_leveldata.Text3DPrefav);
                        text3d.transform.position = m_DynamicPrefabs[i].UnityObject.transform.position + Vector3.up * 1000 * Settings.SceneScaling;
                        text3d.transform.parent   = m_DynamicPrefabs[i].UnityObject.transform;
                        text3d.characterSize      = 100 * Settings.SceneScaling;
                        text3d.text = "" + m_DynamicPrefabs[i].ObjectID;
                    }
                }
            }

            //build rooms
            //container for water go
            Transform WaterContainer = new GameObject("WaterContainer").transform;
            WaterContainer.parent = m_LevelRoot;

            Transform RoomContainer = new GameObject("RoomContainer").transform;
            RoomContainer.parent = m_LevelRoot;

            for (int i = 0; i < m_leveldata.NumRooms; i++)
            {
                Parser.Tr2Room tr2room   = leveldata.Rooms[i];
                bool           has_water = false;
                Mesh           roommesh  = MeshBuilder.CreateRoomMesh(tr2room, m_leveldata, ref has_water);
                Vector3        position  = new Vector3(m_leveldata.Rooms[i].info.x, 0, m_leveldata.Rooms[i].info.z);
                GameObject     go        = CreateRoom(roommesh, position * Settings.SceneScaling, i, m_SharedMaterial, FloorAttribute.Solid);
                go.transform.parent = RoomContainer;
                m_RoomExs[i]        = go.AddComponent <RoomEx>();
                //build room object
                List <GameObject> objects = InstantiateStaticObjects(tr2room, i, go.transform);
                m_RoomExs[i].InitRoom(tr2room, objects);



                if ((tr2room.Flags & 1) == 1) //Is room water holder
                {
                    //override water holder material
                    //MeshFilter mf = go.GetComponent<MeshFilter>();
                    //mf.mesh = MeshModifier.VertexWeild(mf.mesh);

                    MeshRenderer mr = go.GetComponent <MeshRenderer>();
                    mr.sharedMaterial = new Material(waterEffectShader); // Generate material instances for water holder using m_SharedMaterialWaterHolder
                    mr.receiveShadows = false;
                    Vector3 center = m_RoomExs[i].GetCenterPoint();
                    mr.sharedMaterial.SetFloat("_CenterX", center.x);
                    mr.sharedMaterial.SetFloat("_CenterY", center.y);
                    mr.sharedMaterial.SetFloat("_CenterZ", center.z);
                    mr.sharedMaterial.SetFloat("_WaterPlaneY", Mathf.Infinity);
                    mr.sharedMaterial.SetTexture("_MainTex", m_SharedMaterialWaterHolder.mainTexture);
                    mr.sharedMaterial.SetColor("_Color", m_SharedMaterialWaterHolder.color);
                    mr.sharedMaterial.SetFloat("_InSideWater", 0);
                    m_InstancedMaterialWaterHolders.Add(mr.sharedMaterial);
                }
                else //regular room
                {
                }

                //create room water surface
                if (has_water) //surface?
                {
                    //create water surface
                    roommesh            = MeshBuilder.CreateRoomWaterMesh(tr2room, m_leveldata);
                    go                  = CreateRoom(roommesh, position * Settings.SceneScaling, i, m_SharedMaterialWater, FloorAttribute.Water);
                    go.name             = "water_" + i;
                    go.transform.parent = WaterContainer;
                }
            }

            Transform ObjectContainer = new GameObject("ObjectContainer").transform;
            ObjectContainer.parent = m_LevelRoot;

            m_MovableInstances = InstantiateDynamicObjects(ObjectContainer);
            try //
            {
                SetupTrigers();
            }
            catch (System.Exception e)
            {
                Debug.LogError(e.Message);
            }

            //attach components to m_MovableInstances
            for (int i = 0; i < m_MovableInstances.Count; i++)
            {
                InitialiseInstance(m_MovableInstances[i]);
            }
        }
    }