示例#1
0
        public PlanetSide GetPlanetSideFor(Vector3 localPos)
        {
            PlanetSide targetPlanetSide = this.up;
            float      angle            = Vector3.Angle(this.transform.up, localPos);

            if (Vector3.Angle(-this.transform.up, localPos) < angle)
            {
                targetPlanetSide = this.down;
                angle            = Vector3.Angle(-this.transform.up, localPos);
            }

            if (Vector3.Angle(this.transform.right, localPos) < angle)
            {
                targetPlanetSide = this.right;
                angle            = Vector3.Angle(this.transform.right, localPos);
            }
            if (Vector3.Angle(-this.transform.right, localPos) < angle)
            {
                targetPlanetSide = this.left;
                angle            = Vector3.Angle(-this.transform.right, localPos);
            }

            if (Vector3.Angle(this.transform.forward, localPos) < angle)
            {
                targetPlanetSide = this.forward;
                angle            = Vector3.Angle(this.transform.forward, localPos);
            }
            if (Vector3.Angle(-this.transform.forward, localPos) < angle)
            {
                targetPlanetSide = this.back;
                angle            = Vector3.Angle(-this.transform.forward, localPos);
            }

            return(targetPlanetSide);
        }
示例#2
0
        public void AddBlockAt(Vector3 worldPos, int block)
        {
            Vector3    localPos = this.transform.InverseTransformPoint(worldPos);
            PlanetSide pSide    = this.GetPlanetSideFor(localPos);

            pSide.AddBlockAt(localPos, block);
        }
示例#3
0
        public void WorldPositionToIJKPos(Vector3 worldPos, out PlanetSide planetSide, out int iPos, out int jPos, out int kPos)
        {
            planetSide = WorldPositionToPlanetSide(worldPos);
            Vector3 localPos = planetSide.transform.worldToLocalMatrix * worldPos;
            float   r        = localPos.magnitude;

            if (Mathf.Abs(localPos.x) > 1f)
            {
                localPos = localPos / localPos.x;
            }
            if (Mathf.Abs(localPos.y) > 1f)
            {
                localPos = localPos / localPos.y;
            }
            if (Mathf.Abs(localPos.z) > 1f)
            {
                localPos = localPos / localPos.z;
            }

            float tanX = localPos.x;
            float tanY = localPos.y;
            float tanZ = localPos.z;

            float xDeg = Mathf.Atan(tanX);
            float yDeg = Mathf.Atan(tanY);
            float zDeg = Mathf.Atan(tanZ);

            xDeg = Mathf.Rad2Deg * xDeg;
            yDeg = Mathf.Rad2Deg * yDeg;
            zDeg = Mathf.Rad2Deg * zDeg;

            iPos = Mathf.FloorToInt((zDeg + 45f) / 90f * planetSide.Size);
            jPos = Mathf.FloorToInt((yDeg + 45f) / 90f * planetSide.Size);
            kPos = Mathf.FloorToInt(r - rMin);
        }
示例#4
0
 public ChunckDataToInstantiate(int iPos, int jPos, int kPos, PlanetSide side)
 {
     this.iPos = iPos;
     this.jPos = jPos;
     this.kPos = kPos;
     this.side = side;
 }
示例#5
0
 static public void SetAt(int blockType, int i, int j, int k, PlanetSide planetSide)
 {
     if ((PlanetCursor.posInPlanetSide != new Vector3(i, j, k)) || (planetSide.transform != PlanetCursor.CursorTransform.parent))
     {
         cursorPlanetSide = planetSide;
         PlanetCursor.BuildCursor(blockType, i, j, k, planetSide);
     }
 }
示例#6
0
        public void SetCursorAt(int blockType, Vector3 worldPos)
        {
            Vector3    localPos = this.transform.InverseTransformPoint(worldPos);
            PlanetSide pSide    = this.GetPlanetSideFor(localPos);
            Vector3    ijk      = pSide.GetIJKFor(localPos);

            PlanetCursor.SetAt(blockType, (int)ijk.x, (int)ijk.y, (int)ijk.z, pSide);
        }
示例#7
0
        public void AddGameObjectAt(int i, int j, int k, PlanetSide pSide, GameObject prefab)
        {
            GameObject dropped = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;

            dropped.transform.parent        = pSide.transform;
            dropped.transform.localPosition = pSide.GetLocalPosBlockCenter(i, j, k);
            float a = Vector3.Angle(dropped.transform.up, (dropped.transform.position - dropped.transform.parent.position));

            dropped.transform.RotateAround(dropped.transform.position, Vector3.Cross(dropped.transform.up, (dropped.transform.position - dropped.transform.parent.position)), a);
            dropped.transform.RotateAround(dropped.transform.position, dropped.transform.up, UnityEngine.Random.Range(0, 360f));
            dropped.transform.parent = this.vegetationFolder;
        }
示例#8
0
		public void Initialize (Vector3 orientation, Vector3 posInChunck, PlanetSide planetSide) {
			this.orientation = orientation;
			this.posInChunck = posInChunck;
			this.planetSide = planetSide;

			this.blocks = new int[PlanetUtility.ChunckSize * PlanetUtility.ChunckSize * 32];
			for (int i = 0; i < PlanetUtility.ChunckSize; i++) {
				for (int j = 0; j < PlanetUtility.ChunckSize; j++) {
					for (int k = 0; k < 32; k++) {
						this.blocks[i + j * PlanetUtility.ChunckSize + k * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] = 0;
					}
				}
			}
		}
示例#9
0
        public void SearchPlanetSide()
        {
            if (this.up == null)
            {
                this.up = this.transform.Find("up").GetComponent <PlanetSide> ();
            }
            if (this.down == null)
            {
                this.down = this.transform.Find("down").GetComponent <PlanetSide> ();
            }
            if (this.right == null)
            {
                this.right = this.transform.Find("right").GetComponent <PlanetSide> ();
            }
            if (this.left == null)
            {
                this.left = this.transform.Find("left").GetComponent <PlanetSide> ();
            }
            if (this.forward == null)
            {
                this.forward = this.transform.Find("forward").GetComponent <PlanetSide> ();
            }
            if (this.back == null)
            {
                this.back = this.transform.Find("back").GetComponent <PlanetSide> ();
            }
            if (this.vegetationFolder == null)
            {
                this.vegetationFolder = this.transform.Find("Vegetation");
                if (this.vegetationFolder == null)
                {
                    this.vegetationFolder               = (new GameObject()).transform;
                    this.vegetationFolder.name          = "Vegetation";
                    this.vegetationFolder.parent        = this.transform;
                    this.vegetationFolder.localPosition = Vector3.zero;
                    this.vegetationFolder.localRotation = Quaternion.identity;
                    this.vegetationFolder.localScale    = Vector3.one;
                }
            }

            this.planetSides = new List <PlanetSide> ();
            this.planetSides.Add(this.up);
            this.planetSides.Add(this.down);
            this.planetSides.Add(this.right);
            this.planetSides.Add(this.left);
            this.planetSides.Add(this.forward);
            this.planetSides.Add(this.back);
        }
示例#10
0
		static public PlanetChunck InstantiatePlanetChunck (Vector3 orientation, Vector3 posInChunck, PlanetSide planetSide) {
			GameObject planetChunckInstance = new GameObject ();

			PlanetChunck planetChunck = planetChunckInstance.AddComponent<PlanetChunck> ();
			planetChunck.Initialize (orientation, posInChunck, planetSide);

			planetChunckInstance.AddComponent<MeshFilter> ();
			MeshRenderer planetChunckMeshRenderer = planetChunckInstance.AddComponent<MeshRenderer> ();
			planetChunckMeshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
			planetChunckMeshRenderer.receiveShadows = true;

			// Adding MeshCollider at runtime gives much better performances.
			//planetChunckInstance.AddComponent<MeshCollider> ();

			return planetChunck;
		}
示例#11
0
        public void Initialize(Vector3 orientation, Vector3 posInChunck, PlanetSide planetSide)
        {
            this.orientation = orientation;
            this.posInChunck = posInChunck;
            this.planetSide  = planetSide;

            this.blocks = new int[PlanetUtility.ChunckSize * PlanetUtility.ChunckSize * 32];
            for (int i = 0; i < PlanetUtility.ChunckSize; i++)
            {
                for (int j = 0; j < PlanetUtility.ChunckSize; j++)
                {
                    for (int k = 0; k < 32; k++)
                    {
                        this.blocks[i + j * PlanetUtility.ChunckSize + k * PlanetUtility.ChunckSize * PlanetUtility.ChunckSize] = 0;
                    }
                }
            }
        }
示例#12
0
        public PlanetChunck SetDataAtIJKPos(PlanetSide planetSide, int iPos, int jPos, int kPos, Byte data, bool ifZero = false, bool rebuild = true, bool save = true)
        {
            PlanetChunck planetChunck = planetSide.chuncks[iPos / PlanetUtility.ChunckSize][jPos / PlanetUtility.ChunckSize][kPos / PlanetUtility.ChunckSize];

            if ((ifZero && planetChunck.data[iPos % PlanetUtility.ChunckSize][jPos % PlanetUtility.ChunckSize][kPos % PlanetUtility.ChunckSize] == 0) || !ifZero)
            {
                planetChunck.SetData(data, iPos % PlanetUtility.ChunckSize, jPos % PlanetUtility.ChunckSize, kPos % PlanetUtility.ChunckSize);
            }
            if (rebuild)
            {
                planetChunck.SetMesh();
            }
            if (save)
            {
                PlanetUtility.Save(planetChunck.PlanetName, planetChunck.data, planetChunck.iPos, planetChunck.jPos, planetChunck.kPos, planetChunck.planetSide.side);
            }
            return(planetChunck);
        }
示例#13
0
        private void GenerateSide(Planet.Side side)
        {
            PlanetSide planetSide = Target.planetSides[side];

            planetSide.Initialize(Target);

            for (int i = 0; i < planetSide.nbChunks; i++)
            {
                for (int j = 0; j < planetSide.nbChunks; j++)
                {
                    for (int k = 0; k < planetSide.nbChunks / 2; k++)
                    {
                        chuncksToInstantiate.Add(new ChunckDataToInstantiate(i, j, k, planetSide));
                    }
                }
            }
            EditorApplication.update -= InstantiatePlanetChunck;
            EditorApplication.update += InstantiatePlanetChunck;
        }
示例#14
0
        public void Initialize()
        {
            Debug.Log("Planet Initialize");
            PlanetSide[] planetSidesInChildren = this.GetComponentsInChildren <PlanetSide>();

            this.ReadPlanetInfoFile();
            this.planetSides = new Dictionary <Planet.Side, PlanetSide>();
            this.size        = Mathf.FloorToInt(Mathf.Pow(2f, this.degree));
            this.rMin        = Mathf.FloorToInt((2 / Mathf.PI - 1 / 8f) * size);

            // Search for PlanetSide in Children. If any cannot be found, it is instantiated.
            foreach (Side side in Enum.GetValues(typeof(Planet.Side)))
            {
                bool       instantiatePlanetSide = true;
                PlanetSide newPlanetSide         = null;

                // Search for an already existing planetSide.
                foreach (PlanetSide planetSide in planetSidesInChildren)
                {
                    if (planetSide.side == side)
                    {
                        newPlanetSide         = planetSide;
                        instantiatePlanetSide = false;
                    }
                }

                // If no existing PlanetSide has been found, create one.
                if (instantiatePlanetSide)
                {
                    GameObject newSideGameObject = new GameObject();
                    newSideGameObject.name                    = side.ToString();
                    newSideGameObject.transform.parent        = this.transform;
                    newSideGameObject.transform.localPosition = Vector3.zero;
                    newSideGameObject.transform.localRotation = PlanetUtility.LocalRotationFromSide(side);

                    newPlanetSide      = newSideGameObject.AddComponent <PlanetSide>();
                    newPlanetSide.side = side;
                }

                this.planetSides.Add(side, newPlanetSide);
            }
        }
示例#15
0
		public void SearchPlanetSide () {
			if (this.up == null) {
				this.up = this.transform.Find ("up").GetComponent<PlanetSide> ();
			}
			if (this.down == null) {
				this.down = this.transform.Find ("down").GetComponent<PlanetSide> ();
			}
			if (this.right == null) {
				this.right = this.transform.Find ("right").GetComponent<PlanetSide> ();
			}
			if (this.left == null) {
				this.left = this.transform.Find ("left").GetComponent<PlanetSide> ();
			}
			if (this.forward == null) {
				this.forward = this.transform.Find ("forward").GetComponent<PlanetSide> ();
			}
			if (this.back == null) {
				this.back = this.transform.Find ("back").GetComponent<PlanetSide> ();
			}
			if (this.vegetationFolder == null) {
				this.vegetationFolder = this.transform.Find ("Vegetation");
				if (this.vegetationFolder == null) {
					this.vegetationFolder = (new GameObject ()).transform;
					this.vegetationFolder.name = "Vegetation";
					this.vegetationFolder.parent = this.transform;
					this.vegetationFolder.localPosition = Vector3.zero;
					this.vegetationFolder.localRotation = Quaternion.identity;
					this.vegetationFolder.localScale = Vector3.one;
				}
			}
			
			this.planetSides = new List<PlanetSide> ();
			this.planetSides.Add (this.up);
			this.planetSides.Add (this.down);
			this.planetSides.Add (this.right);
			this.planetSides.Add (this.left);
			this.planetSides.Add (this.forward);
			this.planetSides.Add (this.back);
		}
示例#16
0
		static public void SetAt (int blockType, int i, int j, int k, PlanetSide planetSide) {
			if ((PlanetCursor.posInPlanetSide != new Vector3 (i, j, k)) || (planetSide.transform != PlanetCursor.CursorTransform.parent)) {
				cursorPlanetSide = planetSide;
				PlanetCursor.BuildCursor (blockType, i, j, k, planetSide);
			}
		}
示例#17
0
		public void AddGameObjectAt (int i, int j, int k, PlanetSide pSide, GameObject prefab) {
			GameObject dropped = Instantiate (prefab, Vector3.zero, Quaternion.identity) as GameObject;
			dropped.transform.parent = pSide.transform;
			dropped.transform.localPosition = pSide.GetLocalPosBlockCenter (i, j, k);
			float a = Vector3.Angle (dropped.transform.up, (dropped.transform.position - dropped.transform.parent.position));
			dropped.transform.RotateAround (dropped.transform.position, Vector3.Cross (dropped.transform.up, (dropped.transform.position - dropped.transform.parent.position)), a);
			dropped.transform.RotateAround (dropped.transform.position, dropped.transform.up, UnityEngine.Random.Range (0, 360f));
			dropped.transform.parent = this.vegetationFolder;
		}
示例#18
0
        static public PlanetChunck InstantiatePlanetChunck(Vector3 orientation, Vector3 posInChunck, PlanetSide planetSide)
        {
            GameObject planetChunckInstance = new GameObject();

            PlanetChunck planetChunck = planetChunckInstance.AddComponent <PlanetChunck> ();

            planetChunck.Initialize(orientation, posInChunck, planetSide);

            planetChunckInstance.AddComponent <MeshFilter> ();
            MeshRenderer planetChunckMeshRenderer = planetChunckInstance.AddComponent <MeshRenderer> ();

            planetChunckMeshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
            planetChunckMeshRenderer.receiveShadows    = true;

            // Adding MeshCollider at runtime gives much better performances.
            //planetChunckInstance.AddComponent<MeshCollider> ();

            return(planetChunck);
        }
示例#19
0
        static public PlanetChunck InstantiatePlanetChunck(int iPos, int jPos, int kPos, PlanetSide planetSide)
        {
            GameObject planetChunckInstance = new GameObject();

            PlanetChunck planetChunck = planetChunckInstance.AddComponent <PlanetChunck> ();

            planetChunck.iPos       = iPos;
            planetChunck.jPos       = jPos;
            planetChunck.kPos       = kPos;
            planetChunck.planetSide = planetSide;

            planetChunckInstance.AddComponent <MeshFilter> ();
            MeshRenderer planetChunckMeshRenderer = planetChunckInstance.AddComponent <MeshRenderer> ();

            planetChunckMeshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
            planetChunckMeshRenderer.receiveShadows    = true;

            planetChunckInstance.AddComponent <MeshCollider>();

            return(planetChunck);
        }
示例#20
0
        static void BuildCursor(int blockType, int i, int j, int k, PlanetSide planetSide)
        {
            Mesh cursorMesh = new Mesh();

            List <Vector3> cursorVertices  = new List <Vector3> ();
            List <Vector3> cursorNormals   = new List <Vector3> ();
            List <Vector2> cursorUV        = new List <Vector2> ();
            List <int>     cursorTriangles = new List <int> ();

            Vector3[][] baseMesh = PlanetUtility.GetBaseMesh(cursorPlanetSide.subDegree).vertices;

            int a = 0;
            int b = 0;
            int c = 0;
            int d = 0;

            float blockHeight = 1f;

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add((baseMesh[i][j + 1] - baseMesh[i + 1][j + 1]).normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k + blockHeight) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add((baseMesh[i][j + 1] - baseMesh[i + 1][j + 1]).normalized);
            cursorUV.Add(new Vector2(0.5f, 1));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k + blockHeight) * baseMesh[i][j].normalized);
            cursorNormals.Add((baseMesh[i][j] - baseMesh[i + 1][j]).normalized);
            cursorUV.Add(new Vector2(1, 1));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k) * baseMesh[i][j].normalized);
            cursorNormals.Add((baseMesh[i][j] - baseMesh[i + 1][j]).normalized);
            cursorUV.Add(new Vector2(1, 0.5f));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add((baseMesh[i + 1][j] - baseMesh[i][j]).normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k + blockHeight) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add((baseMesh[i + 1][j] - baseMesh[i][j]).normalized);
            cursorUV.Add(new Vector2(0.5f, 1));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k + blockHeight) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add((baseMesh[i + 1][j + 1] - baseMesh[i][j + 1]).normalized);
            cursorUV.Add(new Vector2(1, 1));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add((baseMesh[i + 1][j + 1] - baseMesh[i][j + 1]).normalized);
            cursorUV.Add(new Vector2(1, 0.5f));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k) * baseMesh[i][j].normalized);
            cursorNormals.Add((baseMesh[i][j] - baseMesh[i][j + 1]).normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k + blockHeight) * baseMesh[i][j].normalized);
            cursorNormals.Add((baseMesh[i][j] - baseMesh[i][j + 1]).normalized);
            cursorUV.Add(new Vector2(0.5f, 1));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k + blockHeight) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add((baseMesh[i + 1][j] - baseMesh[i + 1][j + 1]).normalized);
            cursorUV.Add(new Vector2(1, 1));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add((baseMesh[i + 1][j] - baseMesh[i + 1][j + 1]).normalized);
            cursorUV.Add(new Vector2(1, 0.5f));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add((baseMesh[i + 1][j + 1] - baseMesh[i + 1][j]).normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k + blockHeight) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add((baseMesh[i + 1][j + 1] - baseMesh[i + 1][j]).normalized);
            cursorUV.Add(new Vector2(0.5f, 1));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k + blockHeight) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add((baseMesh[i][j + 1] - baseMesh[i][j]).normalized);
            cursorUV.Add(new Vector2(1, 1));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add((baseMesh[i][j + 1] - baseMesh[i][j]).normalized);
            cursorUV.Add(new Vector2(1, 0.5f));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add(-baseMesh[i][j + 1].normalized);
            cursorUV.Add(new Vector2(0, 0));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k) * baseMesh[i][j].normalized);
            cursorNormals.Add(-baseMesh[i][j].normalized);
            cursorUV.Add(new Vector2(0, 0.5f));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add(-baseMesh[i + 1][j].normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add(-baseMesh[i + 1][j + 1].normalized);
            cursorUV.Add(new Vector2(0.5f, 0));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            a = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j] + (k + blockHeight) * baseMesh[i][j].normalized);
            cursorNormals.Add(baseMesh[i][j].normalized);
            cursorUV.Add(new Vector2(0, 0.5f));

            b = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i][j + 1] + (k + blockHeight) * baseMesh[i][j + 1].normalized);
            cursorNormals.Add(baseMesh[i][j + 1].normalized);
            cursorUV.Add(new Vector2(0, 1));

            c = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j + 1] + (k + blockHeight) * baseMesh[i + 1][j + 1].normalized);
            cursorNormals.Add(baseMesh[i + 1][j + 1].normalized);
            cursorUV.Add(new Vector2(0.5f, 1));

            d = cursorVertices.Count;
            cursorVertices.Add(baseMesh[i + 1][j] + (k + blockHeight) * baseMesh[i + 1][j].normalized);
            cursorNormals.Add(baseMesh[i + 1][j].normalized);
            cursorUV.Add(new Vector2(0.5f, 0.5f));

            cursorTriangles.Add(a);
            cursorTriangles.Add(b);
            cursorTriangles.Add(c);

            cursorTriangles.Add(a);
            cursorTriangles.Add(c);
            cursorTriangles.Add(d);

            if (blockType == 0)
            {
                Vector3 barycenter = Vector3.zero;
                foreach (Vector3 v in cursorVertices)
                {
                    barycenter += v;
                }

                barycenter /= ((float)cursorVertices.Count);

                for (int v = 0; v < cursorVertices.Count; v++)
                {
                    cursorVertices [v] += (cursorVertices [v] - barycenter).normalized * 0.1f;
                }
            }

            cursorMesh.vertices  = cursorVertices.ToArray();
            cursorMesh.triangles = cursorTriangles.ToArray();
            cursorMesh.normals   = cursorNormals.ToArray();
            cursorMesh.uv        = cursorUV.ToArray();

            PlanetCursor.CursorMeshFilter.sharedMesh   = cursorMesh;
            PlanetCursor.posInPlanetSide               = new Vector3(i, j, k);
            PlanetCursor.CursorTransform.parent        = planetSide.transform;
            PlanetCursor.CursorTransform.localPosition = Vector3.zero;
            PlanetCursor.CursorTransform.localRotation = Quaternion.identity;
            PlanetCursor.CursorMeshRenderer.enabled    = true;
        }
示例#21
0
		static void BuildCursor (int blockType, int i, int j, int k, PlanetSide planetSide) {
			Mesh cursorMesh = new Mesh ();
			
			List<Vector3> cursorVertices = new List<Vector3> ();
			List<Vector3> cursorNormals = new List<Vector3> ();
			List<Vector2> cursorUV = new List<Vector2> ();
			List<int> cursorTriangles = new List<int> ();
			
			Vector3[][] baseMesh = PlanetUtility.GetBaseMesh (cursorPlanetSide.subDegree).vertices;
			
			int a = 0;
			int b = 0;
			int c = 0;
			int d = 0;
			
			float blockHeight = 1f;
			
			a = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j + 1] + (k) * baseMesh[i][j + 1].normalized);
			cursorNormals.Add ((baseMesh[i][j + 1] - baseMesh[i + 1][j + 1]).normalized);
			cursorUV.Add (new Vector2 (0.5f, 0.5f));
			
			b = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j + 1] + (k + blockHeight) * baseMesh[i][j + 1].normalized);
			cursorNormals.Add ((baseMesh[i][j + 1] - baseMesh[i + 1][j + 1]).normalized);
			cursorUV.Add (new Vector2 (0.5f, 1));
			
			c = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j] + (k + blockHeight) * baseMesh[i][j].normalized);
			cursorNormals.Add ((baseMesh[i][j] - baseMesh[i + 1][j]).normalized);
			cursorUV.Add (new Vector2 (1, 1));
			
			d = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j] + (k) * baseMesh[i][j].normalized);
			cursorNormals.Add ((baseMesh[i][j] - baseMesh[i + 1][j]).normalized);
			cursorUV.Add (new Vector2 (1, 0.5f));
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (b);
			cursorTriangles.Add (c);
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (c);
			cursorTriangles.Add (d);
			
			a = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j] + (k) * baseMesh[i + 1][j].normalized);
			cursorNormals.Add ((baseMesh[i + 1][j] - baseMesh[i][j]).normalized);
			cursorUV.Add (new Vector2 (0.5f, 0.5f));
			
			b = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j] + (k + blockHeight) * baseMesh[i + 1][j].normalized);
			cursorNormals.Add ((baseMesh[i + 1][j] - baseMesh[i][j]).normalized);
			cursorUV.Add (new Vector2 (0.5f, 1));
			
			c = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j + 1] + (k + blockHeight) * baseMesh[i + 1][j + 1].normalized);
			cursorNormals.Add ((baseMesh[i + 1][j + 1] - baseMesh[i][j + 1]).normalized);
			cursorUV.Add (new Vector2 (1, 1));
			
			d = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j + 1] + (k) * baseMesh[i + 1][j + 1].normalized);
			cursorNormals.Add ((baseMesh[i + 1][j + 1] - baseMesh[i][j + 1]).normalized);
			cursorUV.Add (new Vector2 (1, 0.5f));
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (b);
			cursorTriangles.Add (c);
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (c);
			cursorTriangles.Add (d);
			
			a = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j] + (k) * baseMesh[i][j].normalized);
			cursorNormals.Add ((baseMesh[i][j] - baseMesh[i][j + 1]).normalized);
			cursorUV.Add (new Vector2 (0.5f, 0.5f));
			
			b = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j] + (k + blockHeight) * baseMesh[i][j].normalized);
			cursorNormals.Add ((baseMesh[i][j] - baseMesh[i][j + 1]).normalized);
			cursorUV.Add (new Vector2 (0.5f, 1));
			
			c = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j] + (k + blockHeight) * baseMesh[i + 1][j].normalized);
			cursorNormals.Add ((baseMesh[i + 1][j] - baseMesh[i + 1][j + 1]).normalized);
			cursorUV.Add (new Vector2 (1, 1));
			
			d = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j] + (k) * baseMesh[i + 1][j].normalized);
			cursorNormals.Add ((baseMesh[i + 1][j] - baseMesh[i + 1][j + 1]).normalized);
			cursorUV.Add (new Vector2 (1, 0.5f));
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (b);
			cursorTriangles.Add (c);
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (c);
			cursorTriangles.Add (d);
			
			a = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j + 1] + (k) * baseMesh[i + 1][j + 1].normalized);
			cursorNormals.Add ((baseMesh[i + 1][j + 1] - baseMesh[i + 1][j]).normalized);
			cursorUV.Add (new Vector2 (0.5f, 0.5f));
			
			b = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j + 1] + (k + blockHeight) * baseMesh[i + 1][j + 1].normalized);
			cursorNormals.Add ((baseMesh[i + 1][j + 1] - baseMesh[i + 1][j]).normalized);
			cursorUV.Add (new Vector2 (0.5f, 1));
			
			c = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j + 1] + (k + blockHeight) * baseMesh[i][j + 1].normalized);
			cursorNormals.Add ((baseMesh[i][j + 1] - baseMesh[i][j]).normalized);
			cursorUV.Add (new Vector2 (1, 1));
			
			d = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j + 1] + (k) * baseMesh[i][j + 1].normalized);
			cursorNormals.Add ((baseMesh[i][j + 1] - baseMesh[i][j]).normalized);
			cursorUV.Add (new Vector2 (1, 0.5f));
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (b);
			cursorTriangles.Add (c);
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (c);
			cursorTriangles.Add (d);
			
			a = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j + 1] + (k) * baseMesh[i][j + 1].normalized);
			cursorNormals.Add (-baseMesh[i][j + 1].normalized);
			cursorUV.Add (new Vector2 (0, 0));
			
			b = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j] + (k) * baseMesh[i][j].normalized);
			cursorNormals.Add (-baseMesh[i][j].normalized);
			cursorUV.Add (new Vector2 (0, 0.5f));
			
			c = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j] + (k) * baseMesh[i + 1][j].normalized);
			cursorNormals.Add (-baseMesh[i + 1][j].normalized);
			cursorUV.Add (new Vector2 (0.5f, 0.5f));
			
			d = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j + 1] + (k) * baseMesh[i + 1][j + 1].normalized);
			cursorNormals.Add (-baseMesh[i + 1][j + 1].normalized);
			cursorUV.Add (new Vector2 (0.5f, 0));
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (b);
			cursorTriangles.Add (c);
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (c);
			cursorTriangles.Add (d);
			
			a = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j] + (k + blockHeight) * baseMesh[i][j].normalized);
			cursorNormals.Add (baseMesh[i][j].normalized);
			cursorUV.Add (new Vector2 (0, 0.5f));
			
			b = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i][j + 1] + (k + blockHeight) * baseMesh[i][j + 1].normalized);
			cursorNormals.Add (baseMesh[i][j + 1].normalized);
			cursorUV.Add (new Vector2 (0, 1));
			
			c = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j + 1] + (k + blockHeight) * baseMesh[i + 1][j + 1].normalized);
			cursorNormals.Add (baseMesh[i + 1][j + 1].normalized);
			cursorUV.Add (new Vector2 (0.5f, 1));
			
			d = cursorVertices.Count;
			cursorVertices.Add (baseMesh[i + 1][j] + (k + blockHeight) * baseMesh[i + 1][j].normalized);
			cursorNormals.Add (baseMesh[i + 1][j].normalized);
			cursorUV.Add (new Vector2 (0.5f, 0.5f));
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (b);
			cursorTriangles.Add (c);
			
			cursorTriangles.Add (a);
			cursorTriangles.Add (c);
			cursorTriangles.Add (d);

			if (blockType == 0) {
				Vector3 barycenter = Vector3.zero;
				foreach (Vector3 v in cursorVertices) {
					barycenter += v;
				}

				barycenter /= ((float) cursorVertices.Count);

				for (int v = 0; v < cursorVertices.Count; v++) {
					cursorVertices [v] += (cursorVertices [v] - barycenter).normalized * 0.1f;
				}
			}
			
			cursorMesh.vertices = cursorVertices.ToArray ();
			cursorMesh.triangles = cursorTriangles.ToArray ();
			cursorMesh.normals = cursorNormals.ToArray ();
			cursorMesh.uv = cursorUV.ToArray ();
			
			PlanetCursor.CursorMeshFilter.sharedMesh = cursorMesh;
			PlanetCursor.posInPlanetSide = new Vector3 (i, j, k);
			PlanetCursor.CursorTransform.parent = planetSide.transform;
			PlanetCursor.CursorTransform.localPosition = Vector3.zero;
			PlanetCursor.CursorTransform.localRotation = Quaternion.identity;
			PlanetCursor.CursorMeshRenderer.enabled = true;
		}