Example #1
0
    public WebJSON.Collider GetColliderJSON(CollideComponent cc)
    {
        if (cc == null)
        {
            return(null);
        }
        var           flags     = cc.CollisionFlagsR2;
        List <string> flagsList = new List <string>();

        foreach (CollideMaterial.CollisionFlags_R2 r in Enum.GetValues(typeof(CollideMaterial.CollisionFlags_R2)))
        {
            if (r == CollideMaterial.CollisionFlags_R2.None)
            {
                continue;
            }
            if ((flags & r) == r)
            {
                flagsList.Add(r.ToString());
            }
        }

        return(new WebJSON.Collider()
        {
            CollideTypes = flagsList.ToArray()
        });
    }
Example #2
0
    void Update()
    {
        if (UnitySettings.IsRaymapGame)
        {
            return;
        }
        highlightedPerso = null;
        UpdateHighlight();
        highlightedCollision = null;
        highlightedWayPoint  = null;
        Rect screenRect = new Rect(0, 0, Screen.width, Screen.height);

        if (!cam.MouseLookEnabled && !cam.MouseLookRMBEnabled &&
            controller.LoadState == Controller.State.Finished &&
            screenRect.Contains(Input.mousePosition))
        {
            HandleCollision();
        }
        if (controller.LoadState == Controller.State.Finished)
        {
            UpdateTooFarLimit();
        }
        if (IsSelecting && (!Input.GetMouseButton(0) || highlightedPerso == null))
        {
            IsSelecting = false;
        }
    }
Example #3
0
        public GameObject GetGameObject(GameObject parent, int index, CollideType collideType = CollideType.None)
        {
            GameObject gao = GameObject.CreatePrimitive(PrimitiveType.Sphere);

            gao.name = "Sphere " + index;
            gao.transform.SetParent(parent.transform);
            MeshRenderer mr = gao.GetComponent <MeshRenderer>();

            gao.transform.localPosition = new Vector3(x / 256f, z / 256f, y / 256f);
            gao.transform.localScale    = Vector3.one * (radius / 256f) * 2;          // default Unity sphere radius is 0.5
            gao.layer = LayerMask.NameToLayer("Collide");

            BillboardBehaviour bill = gao.AddComponent <BillboardBehaviour>();

            bill.mode = BillboardBehaviour.LookAtMode.CameraPosXYZ;

            GameMaterial    gm = (Load as R2PS1Loader).levelHeader.gameMaterials?[gameMaterial];
            CollideMaterial cm = gm?.collideMaterial;

            if (cm != null)
            {
                mr.material = cm.CreateMaterial();
            }
            else
            {
                mr.material = new Material(MapLoader.Loader.collideMaterial);
            }
            if (collideType != CollideType.None)
            {
                Color col = mr.material.color;
                mr.material       = new Material(MapLoader.Loader.collideTransparentMaterial);
                mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f);
                switch (collideType)
                {
                case CollideType.ZDD:
                    mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break;

                case CollideType.ZDM:
                    mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break;

                case CollideType.ZDE:
                    mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break;

                case CollideType.ZDR:
                    mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break;
                }
            }
            CollideComponent cc = gao.AddComponent <CollideComponent>();

            cc.collidePS1 = this;
            cc.type       = collideType;

            return(gao);
        }
Example #4
0
        public GameObject GetGameObject(GameObject parent, int index, CollideType collideType = CollideType.None)
        {
            GameObject gao = GameObject.CreatePrimitive(PrimitiveType.Cube);

            gao.name = "Box " + index + " - " + Offset;
            gao.transform.SetParent(parent.transform);
            MeshRenderer mr        = gao.GetComponent <MeshRenderer>();
            Vector3      minVertex = new Vector3(x0, z0, y0) / R2PS1Loader.CoordinateFactor;
            Vector3      maxVertex = new Vector3(x1, z1, y1) / R2PS1Loader.CoordinateFactor;
            Vector3      center    = Vector3.Lerp(minVertex, maxVertex, 0.5f);

            gao.transform.localPosition = center;
            gao.transform.localScale    = maxVertex - minVertex;
            gao.layer = LayerMask.NameToLayer("Collide");

            GameMaterial    gm = (Load as R2PS1Loader).levelHeader.gameMaterials?[gameMaterial];
            CollideMaterial cm = gm?.collideMaterial;

            if (cm != null)
            {
                mr.material = cm.CreateMaterial();
            }
            else
            {
                mr.material = new Material(MapLoader.Loader.collideMaterial);
            }
            if (collideType != CollideType.None)
            {
                Color col = mr.material.color;
                mr.material       = new Material(MapLoader.Loader.collideTransparentMaterial);
                mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f);
                switch (collideType)
                {
                case CollideType.ZDD:
                    mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break;

                case CollideType.ZDM:
                    mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break;

                case CollideType.ZDE:
                    mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break;

                case CollideType.ZDR:
                    mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break;
                }
            }

            CollideComponent cc = gao.AddComponent <CollideComponent>();

            cc.collidePS1 = this;
            cc.type       = collideType;

            return(gao);
        }
Example #5
0
        private void CreateUnityMesh()
        {
            for (uint i = 0; i < num_spheres; i++)
            {
                GameObject sphere_gao = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                sphere_gao.name = "Collide Spheres @ " + offset + " - " + i + " radius @ " + spheres[i].debug_radiusAddress;
                sphere_gao.transform.SetParent(gao.transform);
                MeshFilter       mf = sphere_gao.GetComponent <MeshFilter>();
                MeshRenderer     mr = sphere_gao.GetComponent <MeshRenderer>();
                CollideComponent cc = sphere_gao.AddComponent <CollideComponent>();
                cc.collide = this;
                cc.type    = geo.type;
                cc.index   = (int)i;
                //MonoBehaviour.Destroy(sphere_gao.GetComponent<SphereCollider>());
                sphere_gao.transform.localPosition = geo.vertices[spheres[i].centerPoint];
                sphere_gao.transform.localScale    = Vector3.one * spheres[i].radius * 2; // default Unity sphere radius is 0.5
                sphere_gao.layer = LayerMask.NameToLayer("Collide");

                BillboardBehaviour b = sphere_gao.AddComponent <BillboardBehaviour>();
                b.mode = BillboardBehaviour.LookAtMode.CameraPosXYZ;

                mr.material = MapLoader.Loader.collideMaterial;
                if (spheres[i].gameMaterial != null && spheres[i].gameMaterial.collideMaterial != null)
                {
                    spheres[i].gameMaterial.collideMaterial.SetMaterial(mr);
                }
                if (geo.type != CollideType.None)
                {
                    Color col = mr.material.color;
                    mr.material       = new Material(MapLoader.Loader.collideTransparentMaterial);
                    mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f);
                    switch (geo.type)
                    {
                    case CollideType.ZDD:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break;

                    case CollideType.ZDE:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break;

                    case CollideType.ZDM:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break;

                    case CollideType.ZDR:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break;
                    }
                }
            }
        }
Example #6
0
        private void CreateUnityMesh()
        {
            for (uint i = 0; i < num_boxes; i++)
            {
                GameObject box_gao = GameObject.CreatePrimitive(PrimitiveType.Cube);
                box_gao.layer = LayerMask.NameToLayer("Collide");
                box_gao.name  = "Collide Aligned Boxes @ " + offset + " - " + i;
                box_gao.transform.SetParent(gao.transform);
                MeshFilter       mf = box_gao.GetComponent <MeshFilter>();
                MeshRenderer     mr = box_gao.GetComponent <MeshRenderer>();
                CollideComponent cc = box_gao.AddComponent <CollideComponent>();
                cc.collide = this;
                cc.type    = geo.type;
                cc.index   = (int)i;
                //MonoBehaviour.Destroy(box_gao.GetComponent<BoxCollider>());
                Vector3 center = Vector3.Lerp(geo.vertices[boxes[i].minPoint], geo.vertices[boxes[i].maxPoint], 0.5f);
                box_gao.transform.localPosition = center;
                box_gao.transform.localScale    = geo.vertices[boxes[i].maxPoint] - geo.vertices[boxes[i].minPoint];

                mr.material = MapLoader.Loader.collideMaterial;
                if (boxes[i].gameMaterial != null && boxes[i].gameMaterial.collideMaterial != null)
                {
                    boxes[i].gameMaterial.collideMaterial.SetMaterial(mr);
                }
                if (geo.type != CollideType.None)
                {
                    Color col = mr.material.color;
                    mr.material       = new Material(MapLoader.Loader.collideTransparentMaterial);
                    mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f);
                    switch (geo.type)
                    {
                    case CollideType.ZDD:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break;

                    case CollideType.ZDE:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break;

                    case CollideType.ZDM:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break;

                    case CollideType.ZDR:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break;
                    }
                }
            }
        }
Example #7
0
 public void Update()
 {
     if (controller.LoadState == Controller.State.Finished && !sentHierarchy)
     {
         SendHierarchy();
         sentHierarchy = true;
     }
     if ((Application.platform == RuntimePlatform.WebGLPlayer || testMessages) && controller.LoadState == Controller.State.Finished)
     {
         if (highlightedPerso_ != selector.highlightedPerso ||
             highlightedCollision_ != selector.highlightedCollision ||
             highlightedWayPoint_ != selector.highlightedWayPoint)
         {
             highlightedPerso_     = selector.highlightedPerso;
             highlightedCollision_ = selector.highlightedCollision;
             highlightedWayPoint_  = selector.highlightedWayPoint;
             Send(GetHighlightMessageJSON());
         }
         if (selectedPerso_ != selector.selectedPerso)
         {
             selectedPerso_ = selector.selectedPerso;
             if (selectedPerso_ != null)
             {
                 selectedPersoStateIndex_ = selectedPerso_.currentState;
                 Send(GetSelectionMessageJSON(true, true));
             }
         }
         if (selectedPerso_ != null && selectedPersoStateIndex_ != selectedPerso_.currentState)
         {
             selectedPersoStateIndex_ = selectedPerso_.currentState;
             Send(GetSelectionMessageJSON(false, false));
         }
         if (controller.CinematicSwitcher != null && cinematicIndex_ != controller.CinematicSwitcher.currentCinematic)
         {
             cinematicIndex_ = controller.CinematicSwitcher.currentCinematic;
             Send(GetCineDataMessageJSON());
         }
     }
 }
Example #8
0
        public GameObject GetGameObject(GeometricObject.Type type, GeometricObject go, CollideType collideType = CollideType.None)
        {
            GameObject gao = null;

            if (type == GeometricObject.Type.Collide)
            {
                gao       = new GameObject("Element Boxes @ " + Offset);
                gao.layer = LayerMask.NameToLayer("Collide");
                gao.transform.localPosition = Vector3.zero;
                if (boxes.Value != null)
                {
                    Vector3[] verts = go.verticesCollide.Value?.GetVectors(go.ScaleFactor);
                    GeometricObjectElementCollideAlignedBoxArray.GeometricElementCollideAlignedBox[] b = boxes.Value.boxes;
                    for (int i = 0; i < num_boxes; i++)
                    {
                        GameObject box_gao = GameObject.CreatePrimitive(PrimitiveType.Cube);
                        box_gao.layer = LayerMask.NameToLayer("Collide");
                        box_gao.name  = "Box " + i;
                        box_gao.transform.SetParent(gao.transform);
                        MeshFilter   mf     = box_gao.GetComponent <MeshFilter>();
                        MeshRenderer mr     = box_gao.GetComponent <MeshRenderer>();
                        Vector3      center = Vector3.Lerp(verts[b[i].minVertex], verts[b[i].maxVertex], 0.5f);
                        box_gao.transform.localPosition = center;
                        box_gao.transform.localScale    = verts[b[i].maxVertex] - verts[b[i].minVertex];

                        CollideComponent cc = box_gao.AddComponent <CollideComponent>();
                        cc.collideROM = this;
                        cc.type       = collideType;
                        cc.index      = i;

                        mr.material = MapLoader.Loader.collideMaterial;
                        GameMaterial gmt = boxes.Value.boxes[i].material.Value;
                        if (gmt != null && gmt.collideMaterial.Value != null)
                        {
                            gmt.collideMaterial.Value.SetMaterial(mr);
                        }
                        if (collideType != CollideType.None)
                        {
                            Color col = mr.material.color;
                            mr.material       = new Material(MapLoader.Loader.collideTransparentMaterial);
                            mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f);
                            switch (collideType)
                            {
                            case CollideType.ZDD:
                                mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break;

                            case CollideType.ZDE:
                                mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break;

                            case CollideType.ZDM:
                                mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break;

                            case CollideType.ZDR:
                                mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break;
                            }
                        }
                    }
                }
            }
            return(gao);
        }
Example #9
0
        public GameObject GetGameObject(GeometricObject.Type type, GeometricObject go, CollideType collideType = CollideType.None)
        {
            GameObject gao = null;

            if (type == GeometricObject.Type.Collide)
            {
                gao       = new GameObject("Element Spheres @ " + Offset);
                gao.layer = LayerMask.NameToLayer("Collide");
                gao.transform.localPosition = Vector3.zero;
                if (spheres.Value != null)
                {
                    Vector3[] verts = go.verticesCollide.Value?.GetVectors(go.ScaleFactor);
                    GeometricObjectElementCollideSphereArray.GeometricElementCollideSphere[] sphere = spheres.Value.spheres;
                    for (int i = 0; i < num_spheres; i++)
                    {
                        GameObject sphere_gao = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                        sphere_gao.name = "Sphere " + i;
                        sphere_gao.transform.SetParent(gao.transform);
                        MeshRenderer mr = sphere_gao.GetComponent <MeshRenderer>();
                        sphere_gao.transform.localPosition = verts[sphere[i].centerPoint];
                        sphere_gao.transform.localScale    = Vector3.one * sphere[i].radius * 2;                      // default Unity sphere radius is 0.5
                        sphere_gao.layer = LayerMask.NameToLayer("Collide");

                        BillboardBehaviour bill = sphere_gao.AddComponent <BillboardBehaviour>();
                        bill.mode = BillboardBehaviour.LookAtMode.CameraPosXYZ;

                        CollideComponent cc = sphere_gao.AddComponent <CollideComponent>();
                        cc.collideROM = this;
                        cc.type       = collideType;
                        cc.index      = i;

                        mr.material = MapLoader.Loader.collideMaterial;
                        GameMaterial gmt = spheres.Value.spheres[i].material.Value;
                        if (gmt != null && gmt.collideMaterial.Value != null)
                        {
                            gmt.collideMaterial.Value.SetMaterial(mr);
                        }
                        if (collideType != CollideType.None)
                        {
                            Color col = mr.material.color;
                            mr.material       = new Material(MapLoader.Loader.collideTransparentMaterial);
                            mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f);
                            switch (collideType)
                            {
                            case CollideType.ZDD:
                                mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break;

                            case CollideType.ZDE:
                                mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break;

                            case CollideType.ZDM:
                                mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break;

                            case CollideType.ZDR:
                                mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break;
                            }
                        }
                    }
                }
            }
            return(gao);
        }
Example #10
0
        private void CreateUnityMesh()
        {
            if (num_triangles > 0)
            {
                Vector3[] new_vertices = new Vector3[num_triangles * 3];
                Vector3[] new_normals  = null;
                if (normals != null)
                {
                    new_normals = new Vector3[num_triangles * 3];
                }
                Vector2[] new_uvs = new Vector2[num_triangles * 3];

                for (int j = 0; j < num_triangles * 3; j++)
                {
                    new_vertices[j] = geo.vertices[triangles[j]];
                    if (normals != null)
                    {
                        new_normals[j] = normals[j / 3];
                    }
                    if (uvs != null)
                    {
                        new_uvs[j] = uvs[mapping[j] % uvs.Length];
                    }
                }
                int[] new_triangles = new int[num_triangles * 3];
                for (int j = 0; j < num_triangles; j++)
                {
                    new_triangles[(j * 3) + 0] = (j * 3) + 0;
                    new_triangles[(j * 3) + 1] = (j * 3) + 2;
                    new_triangles[(j * 3) + 2] = (j * 3) + 1;
                }
                Mesh meshUnity = new Mesh();
                meshUnity.vertices = new_vertices;
                if (normals != null)
                {
                    meshUnity.normals = new_normals;
                }
                meshUnity.triangles = new_triangles;
                if (normals == null)
                {
                    meshUnity.RecalculateNormals();
                }

                // If no UVs exist for collide mesh, generate simple UVs (basically a box projection)

                /*System.Diagnostics.Stopwatch w = new System.Diagnostics.Stopwatch();
                 * w.Start();*/
                if (uvs == null)
                {
                    if (normals == null)
                    {
                        List <Vector3> norm = new List <Vector3>();
                        meshUnity.GetNormals(norm);
                        new_normals = norm.ToArray();
                    }
                    for (int j = 0; j < new_uvs.Length; j++)
                    {
                        Vector3 normal = new_normals[j];
                        normal = new Vector3(Mathf.Abs(normal.x), Mathf.Abs(normal.y), Mathf.Abs(normal.z));
                        float biggestNorm = Mathf.Max(normal.x, normal.y, normal.z);

                        float uvX = (new_vertices[j].x / 20.0f);
                        float uvY = (new_vertices[j].y / 20.0f);
                        float uvZ = (new_vertices[j].z / 20.0f);

                        if (biggestNorm == Mathf.Abs(normal.x))
                        {
                            new_uvs[j] = new Vector2(uvY, uvZ);
                        }
                        else if (biggestNorm == Mathf.Abs(normal.y))
                        {
                            new_uvs[j] = new Vector2(uvX, uvZ);
                        }
                        else
                        {
                            new_uvs[j] = new Vector2(uvX, uvY);
                        }
                    }
                }

                /*MapLoader.Loader.print("UV Code: " + offset + " - " + num_triangles + " - " + w.ElapsedMilliseconds);
                 * w.Stop();*/

                meshUnity.uv = new_uvs;

                MeshFilter   mf = gao.AddComponent <MeshFilter>();
                MeshRenderer mr = gao.AddComponent <MeshRenderer>();
                mf.mesh = meshUnity;
                try {
                    MeshCollider mc = gao.AddComponent <MeshCollider>();
                    //mc.cookingOptions = MeshColliderCookingOptions.None;
                    //mc.sharedMesh = mf.sharedMesh;
                } catch (Exception) { }

                CollideComponent cc = gao.AddComponent <CollideComponent>();
                cc.collide = this;
                cc.type    = geo.type;

                mr.material = MapLoader.Loader.collideMaterial;
                if (gameMaterial != null && gameMaterial.collideMaterial != null)
                {
                    gameMaterial.collideMaterial.SetMaterial(mr);
                }
                if (geo.type != CollideType.None)
                {
                    Color col = mr.material.color;
                    mr.material       = new Material(MapLoader.Loader.collideTransparentMaterial);
                    mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f);
                    switch (geo.type)
                    {
                    case CollideType.ZDD:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break;

                    case CollideType.ZDE:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break;

                    case CollideType.ZDM:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break;

                    case CollideType.ZDR:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break;
                    }
                }
            }
        }
Example #11
0
        public GameObject GetGameObject()
        {
            GameObject parentGao = new GameObject(Offset.ToString());

            // First pass

            Dictionary <byte, List <IPS1PolygonCollide> > elementsDict = new Dictionary <byte, List <IPS1PolygonCollide> >();

            foreach (GeometricObjectCollideTriangle t in triangles)
            {
                byte gmi = t.MaterialIndex;
                if (!elementsDict.ContainsKey(gmi))
                {
                    elementsDict[gmi] = new List <IPS1PolygonCollide>();
                }
                elementsDict[gmi].Add(t);
            }
            foreach (GeometricObjectCollideQuad q in quads)
            {
                byte gmi = q.MaterialIndex;
                if (!elementsDict.ContainsKey(gmi))
                {
                    elementsDict[gmi] = new List <IPS1PolygonCollide>();
                }
                elementsDict[gmi].Add(q);
            }

            // Second pass
            byte[] elements = elementsDict.Keys.ToArray();
            for (int i = 0; i < elements.Length; i++)
            {
                byte            gmi = elements[i];
                GameMaterial    gm  = (Load as R2PS1Loader).levelHeader.gameMaterials?[gmi];
                CollideMaterial cm  = gm?.collideMaterial;

                IPS1PolygonCollide pf  = elementsDict[gmi].FirstOrDefault();
                GameObject         gao = new GameObject(Offset.ToString());
                if (cm != null)
                {
                    /*+ " - " + i
                     + " - " + pf?.Offset*/
                    gao.name += " - " + string.Format("{0:X2}", cm.type)
                                + "|" + string.Format("{0:X2}", cm.identifier);
                }
                gao.transform.SetParent(parentGao.transform);
                gao.transform.localPosition = Vector3.zero;
                gao.layer = LayerMask.NameToLayer("Collide");
                MeshFilter   mf = gao.AddComponent <MeshFilter>();
                MeshRenderer mr = gao.AddComponent <MeshRenderer>();

                List <int>     vertIndices   = new List <int>();
                List <int>     triIndices    = new List <int>();
                List <int>     normalIndices = new List <int>();
                List <Vector3> normals2      = new List <Vector3>();
                foreach (IPS1PolygonCollide p in elementsDict[gmi])
                {
                    int currentCount = vertIndices.Count;
                    switch (p)
                    {
                    case GeometricObjectCollideTriangle t:
                        vertIndices.Add(t.v0);
                        vertIndices.Add(t.v1);
                        vertIndices.Add(t.v2);

                        normalIndices.Add(t.normal);
                        normalIndices.Add(t.normal);
                        normalIndices.Add(t.normal);

                        triIndices.Add(currentCount);
                        triIndices.Add(currentCount + 1);
                        triIndices.Add(currentCount + 2);
                        break;

                    case GeometricObjectCollideQuad q:
                        vertIndices.Add(q.v0);
                        vertIndices.Add(q.v1);
                        vertIndices.Add(q.v2);
                        vertIndices.Add(q.v3);

                        normalIndices.Add(q.normal);
                        normalIndices.Add(q.normal);
                        normalIndices.Add(q.normal);
                        normalIndices.Add(q.normal);

                        triIndices.Add(currentCount);
                        triIndices.Add(currentCount + 1);
                        triIndices.Add(currentCount + 2);

                        triIndices.Add(currentCount + 2);
                        triIndices.Add(currentCount + 1);
                        triIndices.Add(currentCount + 3);
                        break;
                    }
                }
                GeometricObjectCollideVector[] v = vertIndices.Select(vi => vertices[vi]).ToArray();
                GeometricObjectCollideVector[] n = normalIndices.Select(ni => normals[ni]).ToArray();
                Mesh m = new Mesh();
                m.vertices = v.Select(s => new Vector3(s.x, s.z, s.y) / R2PS1Loader.CoordinateFactor).ToArray();
                //m.vertices = verts2.ToArray();
                m.normals = n.Select(s => new Vector3(
                                         s.x / (short.MaxValue / 8f),
                                         s.z / (short.MaxValue / 8f),
                                         s.y / (short.MaxValue / 8f))).ToArray();

                /*for(int j = 0; j < m.normals.Length; j++) {
                 *      GameObject g = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                 *      g.transform.parent = gao.transform;
                 *      g.transform.localPosition = m.vertices[j] + m.normals[j];
                 *      g.transform.localScale = Vector3.one * 0.1f;
                 *      Load.print(m.normals[j].magnitude);
                 * }*/
                //m.SetUVs(0, uvs.Select(s => new Vector4(s.x, s.y, alpha, 0f)).ToList());
                m.triangles = triIndices.ToArray();

                Vector2[] uvs = new Vector2[m.vertexCount];

                // Generate simple UVs for collision checkerboard (basically a box projection)
                for (int j = 0; j < m.vertexCount; j++)
                {
                    Vector3 normal = m.normals[j];
                    normal = new Vector3(Mathf.Abs(normal.x), Mathf.Abs(normal.y), Mathf.Abs(normal.z));
                    float biggestNorm = Mathf.Max(normal.x, normal.y, normal.z);

                    float uvX = (m.vertices[j].x / 20.0f);
                    float uvY = (m.vertices[j].y / 20.0f);
                    float uvZ = (m.vertices[j].z / 20.0f);

                    //Debug.Log("Norms: " + normal.x+","+normal.y+","+normal.z);
                    //Debug.Log("Biggest norm: " + biggestNorm);
                    if (biggestNorm == Mathf.Abs(normal.x))
                    {
                        uvs[j] = new Vector2(uvY, uvZ);
                    }
                    else if (biggestNorm == Mathf.Abs(normal.y))
                    {
                        uvs[j] = new Vector2(uvX, uvZ);
                    }
                    else if (biggestNorm == Mathf.Abs(normal.z))
                    {
                        uvs[j] = new Vector2(uvX, uvY);
                    }
                    else
                    {
                        Debug.LogError("HALP");
                    }
                }
                m.uv = uvs;

                mf.mesh = m;

                mr.material = gm.CreateMaterial();

                try {
                    MeshCollider mc = gao.AddComponent <MeshCollider>();
                    //mc.cookingOptions = MeshColliderCookingOptions.None;
                    //mc.sharedMesh = mf.sharedMesh;
                } catch (Exception) { }

                CollideComponent cc = gao.AddComponent <CollideComponent>();
                cc.collidePS1 = this;
                cc.index      = gmi;            // Abuse the index for the game material index
            }
            return(parentGao);
        }
        public GameObject GetGameObject(GeometricObject.Type type, GeometricObject go, CollideType collideType = CollideType.None)
        {
            GameObject gao = null;

            if (type == GeometricObject.Type.Collide)
            {
                gao       = new GameObject("Element @ " + Offset);
                gao.layer = LayerMask.NameToLayer("Collide");
                gao.transform.localPosition = Vector3.zero;
                MeshRenderer mr = gao.AddComponent <MeshRenderer>();
                MeshFilter   mf = gao.AddComponent <MeshFilter>();
                mr.material = MapLoader.Loader.collideMaterial;
                if (material.Value != null && material.Value is GameMaterial)
                {
                    GameMaterial gmt = material.Value as GameMaterial;
                    //MapLoader.Loader.print(gmt.collideMaterial);
                    if (gmt.collideMaterial.Value != null)
                    {
                        gmt.collideMaterial.Value.SetMaterial(mr);
                    }
                }
                else
                {
                    MapLoader.Loader.print("Type: " + type_material + " - Ind: " + ind_material);
                }
                if (collideType != CollideType.None)
                {
                    Color col = mr.material.color;
                    mr.material       = new Material(MapLoader.Loader.collideTransparentMaterial);
                    mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f);
                    switch (collideType)
                    {
                    case CollideType.ZDD:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break;

                    case CollideType.ZDE:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break;

                    case CollideType.ZDM:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break;

                    case CollideType.ZDR:
                        mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break;
                    }
                }
                Vector3[] vertices = go.verticesCollide.Value.GetVectors(go.ScaleFactor);
                int[]     tris     = triangles.Value.triangles.SelectMany(t => new int[] { t.v2, t.v1, t.v3 }).ToArray();


                Mesh mesh = new Mesh();
                mesh.vertices = tris.Select(t => vertices[t]).ToArray();
                //mesh.normals = go.normals.Value.GetVectors(Int16.MaxValue);
                //mesh.SetUVs(0, triangles.Value.uvs.Select(u => new Vector3(u.x, u.y, 1f)).ToList());
                mesh.triangles = Enumerable.Range(0, tris.Length).ToArray();
                mesh.RecalculateNormals();

                Vector2[] uvs = new Vector2[mesh.vertexCount];

                // Generate simple UVs for collision checkerboard (basically a box projection)
                Vector3[] normals = null;
                {
                    List <Vector3> norm = new List <Vector3>();
                    mesh.GetNormals(norm);
                    normals = norm.ToArray();
                    // Norm can be GC'd after these curly braces
                }
                for (int j = 0; j < uvs.Length; j++)
                {
                    Vector3 normal = normals[j];
                    normal = new Vector3(Mathf.Abs(normal.x), Mathf.Abs(normal.y), Mathf.Abs(normal.z));
                    float biggestNorm = Mathf.Max(normal.x, normal.y, normal.z);

                    float uvX = (mesh.vertices[j].x / 20.0f);
                    float uvY = (mesh.vertices[j].y / 20.0f);
                    float uvZ = (mesh.vertices[j].z / 20.0f);

                    //Debug.Log("Norms: " + normal.x+","+normal.y+","+normal.z);
                    //Debug.Log("Biggest norm: " + biggestNorm);
                    if (biggestNorm == Mathf.Abs(normal.x))
                    {
                        uvs[j] = new Vector2(uvY, uvZ);
                    }
                    else if (biggestNorm == Mathf.Abs(normal.y))
                    {
                        uvs[j] = new Vector2(uvX, uvZ);
                    }
                    else if (biggestNorm == Mathf.Abs(normal.z))
                    {
                        uvs[j] = new Vector2(uvX, uvY);
                    }
                    else
                    {
                        Debug.LogError("HALP");
                    }
                }
                mesh.uv = uvs;

                mf.mesh = mesh;

                try {
                    MeshCollider mc = gao.AddComponent <MeshCollider>();
                    //mc.cookingOptions = MeshColliderCookingOptions.None;
                    //mc.sharedMesh = mf.sharedMesh;
                } catch (Exception) { }

                CollideComponent cc = gao.AddComponent <CollideComponent>();
                cc.collideROM = this;
                cc.type       = collideType;
            }
            return(gao);
        }
Example #13
0
    private void HandleCollision()
    {
        int layerMask = 0;

        if (controller.viewCollision)
        {
            layerMask |= 1 << LayerMask.NameToLayer("Collide");
        }
        else
        {
            layerMask |= 1 << LayerMask.NameToLayer("Visual");
        }
        if (controller.viewGraphs)
        {
            layerMask |= 1 << LayerMask.NameToLayer("Graph");
        }
        Ray ray = cam.cam.ScreenPointToRay(Input.mousePosition);

        RaycastHit[] hits = Physics.RaycastAll(ray, Mathf.Infinity, layerMask, QueryTriggerInteraction.Ignore);
        if (hits != null && hits.Length > 0)
        {
            System.Array.Sort(hits, (x, y) => (x.distance.CompareTo(y.distance)));
            if (controller.showPersos)
            {
                for (int i = 0; i < hits.Length; i++)
                {
                    // the object identified by hit.transform was clicked
                    BasePersoBehaviour pb = hits[i].transform.GetComponentInParent <BasePersoBehaviour>();
                    if (pb != null)
                    {
                        highlightedPerso = pb;
                        if (Input.GetMouseButtonDown(0))
                        {
                            IsSelecting = true;
                        }
                        UpdateHighlight();
                        if (IsSelecting)
                        {
                            if (cam.IsPanningWithThreshold())
                            {
                                IsSelecting = false;
                            }
                            else if (Input.GetMouseButtonUp(0))
                            {
                                Select(pb, view: true);
                            }
                        }
                        break;
                    }
                }
            }
            if (controller.viewCollision)
            {
                for (int i = 0; i < hits.Length; i++)
                {
                    CollideComponent cc = hits[i].transform.GetComponent <CollideComponent>();
                    if (cc != null)
                    {
                        highlightedCollision = cc;
                        break;
                    }
                }
            }
            if (controller.viewGraphs)
            {
                for (int i = 0; i < hits.Length; i++)
                {
                    WayPointBehaviour wp = hits[i].transform.GetComponent <WayPointBehaviour>();
                    if (wp != null)
                    {
                        highlightedWayPoint = wp;
                        break;
                    }
                }
            }

            /*SectorComponent sector = hit.transform.GetComponentInParent<SectorComponent>();
             * if (sector != null) {
             *  JumpTo(sector.gameObject);
             *  return;
             * }*/
        }

        /*if (Input.GetMouseButtonDown(0)) { // if left button pressed...
         *  if (Physics.Raycast(ray, out hit)) {
         *      // the object identified by hit.transform was clicked
         *      PersoBehaviour pb = hit.transform.GetComponentInParent<PersoBehaviour>();
         *      if (pb != null) {
         *          JumpTo(pb.gameObject);
         *          return;
         *      }
         *      SectorComponent sector = hit.transform.GetComponentInParent<SectorComponent>();
         *      if (sector != null) {
         *          JumpTo(sector.gameObject);
         *          return;
         *      }
         *  }
         * }*/
    }