GetPosition() public method

Get the position of the terrain.

public GetPosition ( ) : Vector3
return Vector3
    void OnTriggerEnter(Collider col)
    {
        if(col.gameObject.tag == "Terrain"){

            prevTerrain = Instantiate(TerrainPieces[(int)Random.Range(0, TerrainPieces.Length)], new Vector3(0, 0, prevTerrain.GetPosition().z + prevTerrain.terrainData.size.z), Quaternion.identity)as Terrain;
            GameObject temp = Instantiate(TerrainObjects, new Vector3(0, 0, prevTerrain.GetPosition().z), Quaternion.identity)as GameObject;

        }
    }
Beispiel #2
0
    public void AddGrave(int x, int y, Terrain terrain)
    {
        TerrainData data = terrain.terrainData;
        float xNormalized = x/(float)data.size.x;
        float yNormalized = x/(float)data.size.y;
        float scaleRatio = Random.Range(0.8f,1.2f);

        Vector3 spawnPos = Vector3.zero;
        spawnPos.x = x + terrain.GetPosition().x;
        spawnPos.z = y + terrain.GetPosition().z;
        spawnPos.y = terrain.SampleHeight(spawnPos)-0.1f;

        GameObject go = GameObject.Instantiate(gravePrefabs[Random.Range(0,gravePrefabs.Length)],spawnPos,Quaternion.identity) as GameObject;
        go.transform.localScale = Vector3.one * scaleRatio;
        go.transform.parent = terrain.transform;
        RaycastHit hitInfo;
        if(Physics.Linecast(go.transform.position + Vector3.up,go.transform.position - Vector3.up * 50.0f,out hitInfo)){
            go.transform.up = hitInfo.normal;
        }

        go.transform.RotateAround(go.transform.up,Random.Range(0.0f,360.0f));
    }
    public Vector3 InitializePosition(Terrain terrain)
    {
        var terrainStartPosition = terrain.GetPosition ();
        var xPosition = (terrainStartPosition.x + terrain.terrainData.size.x) / 2.0f;
        var zPosition = (terrainStartPosition.z + terrain.terrainData.size.z) / 2.0f;

        Vector3 airplanePosition = new Vector3 ();
        airplanePosition.x = xPosition;
        airplanePosition.z = zPosition;
        airplanePosition.y = terrain.SampleHeight (airplanePosition) + StartHeightDistanceBetweenTerrainAdnAirplane;

        gameObject.transform.position = airplanePosition;

        return airplanePosition;
    }
Beispiel #4
0
	static void SnapBig(Terrain t, Camera cam, int size, int wCount, string path)
	{

		float nearClip = 0.3f;
		float camSize = t.terrainData.size.x /2;
		Vector3 offset = t.GetPosition() + new Vector3(camSize, 0, camSize) + (t.terrainData.size.y  + nearClip) * Vector3.up;
		cam.nearClipPlane = nearClip;
		cam.farClipPlane = t.terrainData.size.y + nearClip * 2;
		cam.orthographicSize = camSize;
		cam.transform.position = offset;
		cam.depth = int.MaxValue;
		

		cam.targetTexture = new RenderTexture(512, 512, 32, RenderTextureFormat.ARGB32);
		RenderTexture.active = cam.targetTexture;

		string filePath = System.IO.Path.Combine(path, "snapMap.png");
		SnapShot(cam, filePath);
		Debug.Log("Snap done: @ " + filePath);
	}
Beispiel #5
0
        public Grid(Terrain terrain, float sample, float heightCost)
        {
            _terrain = terrain;

            _worldPosition = terrain.GetPosition();
            var width = (int)terrain.terrainData.size.x;
            var height = (int)terrain.terrainData.size.z;

            SampledWidth = (int)Mathf.Ceil(width / sample);
            SampledHeight = (int)Mathf.Ceil(height / sample);
            _sample = sample;
            _heightCost = heightCost;
            InternalGrid = new Node[SampledWidth, SampledHeight];
            InternalGridWithUnwalkable = new Node[SampledWidth, SampledHeight];
            //Build the Grid [width,height] --> O(n^2)
            InitGrid();
            InitGridWithUnWalkable();

            //Initialize the edges/neighboots --> O(n^2)
            InitEdges();
            InitEdgesWithUnWalkable();
        }
Beispiel #6
0
	void SampleHeightInfos(Terrain terrain, int widthGridCount, int lengthGridCount)
	{
		Vector3 terrainPos = terrain.GetPosition();
		terrain.transform.position = Vector3.zero;
		GameObject meshObj;
		MeshCollider cld;
		GenMeshColliderFromNavMesh(out meshObj, out cld);

		frees = new bool[widthGridCount][];
		for (int i = 0; i < widthGridCount; i++)
		{
			frees[i] = new bool[lengthGridCount];
		}

		xCount = widthGridCount + 1;
		zCount = lengthGridCount + 1;

		hitPositions = new Vector3[xCount][];
		for (int i = 0; i < xCount; i++)
		{
			hitPositions[i] = new Vector3[zCount];
		}

		__notHitCount = 0;

		float stepX = terrain.terrainData.size.x / widthGridCount;
		float stepZ = terrain.terrainData.size.z / lengthGridCount;
		centerOffset = new Vector3(stepX * 0.5f, 0, stepZ * 0.5f);

		float y = terrain.terrainData.size.y + 1;
		float distance = y + 2;
		// heights
		for (int z = 0; z < zCount; z++)
		{
			for (int x = 0; x < xCount; x++)
			{
				Vector3 sourcePoint = new Vector3(x * stepX, y, z * stepZ);
				bool hit = SampleHeight(sourcePoint, distance * 2, out hitPositions[x][z], 1 << layerID);
				if (!hit)
				{
					__notHitCount++;
				}
				hitPositions[x][z] += terrainPos;
			}
		}
		centerPositions = CenterPositionsFromHitPositions(hitPositions);
		// reachable
		for (int z = 0; z < lengthGridCount; z++)
		{
			for (int x = 0; x < widthGridCount; x++)
			{
				Vector3 sourcePoint = new Vector3(x * stepX, y, z * stepZ) + centerOffset;
				Vector3 tempHitPos;
				bool hit = SampleHeight(sourcePoint, distance * 2, out tempHitPos, 1 << layerID);
				Ray ray = new Ray(sourcePoint, Vector3.down);
				RaycastHit raycastHit;
				frees[x][z] = cld.Raycast(ray, out raycastHit, distance);
			}
		}
		////

		GameObject.DestroyImmediate(meshObj);
		terrain.transform.position = terrainPos;
	}
Beispiel #7
0
 public void SpawnGameOverSwitch(Terrain terrain)
 {
     Vector3 spawnPos = terrain.GetPosition() + new Vector3(128.0f, 0.0f, 256.0f);
     GameObject go = GameObject.Instantiate(gameOverSwitchPrefab,spawnPos,Quaternion.Euler(new Vector3(0.0f,0.0f,180.0f))) as GameObject;
     UserInterface.SetText2("Find the light, and be free...");
     Vector3 pos = go.transform.position;
     pos.y = terrain.SampleHeight(go.transform.position) + 0.8f;
     go.transform.position = pos;
 }
    void SplitIt()
    {
        if ( Selection.activeGameObject == null )
        {
            Debug.LogWarning("No terrain was selected");
            return;
        }

        parentTerrain = Selection.activeGameObject.GetComponent(typeof(Terrain)) as Terrain;

        if ( parentTerrain == null )
        {
            Debug.LogWarning("Current selection is not a terrain");
            return;
        }

        //Split terrain
        for ( int i=0; i< terrainsCount; i++)
        {

            EditorUtility.DisplayProgressBar("Split terrain","Process " + i, (float) i / terrainsCount );

            TerrainData td = new TerrainData();
            GameObject tgo = Terrain.CreateTerrainGameObject( td );

            tgo.name = parentTerrain.name + " " + i;

            terrainData.Add( td );
            terrainGo.Add ( tgo );

            Terrain genTer = tgo.GetComponent(typeof(Terrain)) as Terrain;
            genTer.terrainData = td;

            AssetDatabase.CreateAsset(td, "Assets/" + genTer.name+ ".asset");

            // Assign splatmaps
            genTer.terrainData.splatPrototypes = parentTerrain.terrainData.splatPrototypes;

            // Assign detail prototypes
            genTer.terrainData.detailPrototypes = parentTerrain.terrainData.detailPrototypes;

            // Assign tree information
            genTer.terrainData.treePrototypes = parentTerrain.terrainData.treePrototypes;

            // Copy parent terrain propeties
            #region parent properties
            genTer.basemapDistance = parentTerrain.basemapDistance;
            genTer.castShadows = parentTerrain.castShadows;
            genTer.detailObjectDensity = parentTerrain.detailObjectDensity;
            genTer.detailObjectDistance = parentTerrain.detailObjectDistance;
            genTer.heightmapMaximumLOD = parentTerrain.heightmapMaximumLOD;
            genTer.heightmapPixelError = parentTerrain.heightmapPixelError;
            genTer.treeBillboardDistance = parentTerrain.treeBillboardDistance;
            genTer.treeCrossFadeLength = parentTerrain.treeCrossFadeLength;
            genTer.treeDistance = parentTerrain.treeDistance;
            genTer.treeMaximumFullLODCount = parentTerrain.treeMaximumFullLODCount;

            #endregion

            //Start processing it

            // Translate peace to position
            #region translate peace to right position

            Vector3 parentPosition = parentTerrain.GetPosition();

            int terraPeaces = (int) Mathf.Sqrt( terrainsCount );

            float spaceShiftX = parentTerrain.terrainData.size.z / terraPeaces;
            float spaceShiftY = parentTerrain.terrainData.size.x / terraPeaces;

            float xWShift = (i % terraPeaces ) * spaceShiftX;
            float zWShift = ( i / terraPeaces ) * spaceShiftY;

            tgo.transform.position = new Vector3( tgo.transform.position.x + zWShift,
                                                  tgo.transform.position.y,
                                                  tgo.transform.position.z + xWShift );

            // Shift last position
            tgo.transform.position = new Vector3( tgo.transform.position.x + parentPosition.x,
                                                  tgo.transform.position.y + parentPosition.y,
                                                  tgo.transform.position.z + parentPosition.z
                                                 );

            #endregion

            // Split height
            #region split height

            Debug.Log ( "Split height" );

            //Copy heightmap
            td.heightmapResolution = parentTerrain.terrainData.heightmapResolution /  terraPeaces;

            //Keep y same
            td.size = new Vector3( parentTerrain.terrainData.size.x / terraPeaces,
                                   parentTerrain.terrainData.size.y,
                                   parentTerrain.terrainData.size.z / terraPeaces
                                  );

            float[,] parentHeight = parentTerrain.terrainData.GetHeights(0,0, parentTerrain.terrainData.heightmapResolution, parentTerrain.terrainData.heightmapResolution );

            float[,] peaceHeight = new float[ parentTerrain.terrainData.heightmapResolution / terraPeaces + 1,
                                              parentTerrain.terrainData.heightmapResolution / terraPeaces + 1
                                            ];

            // Shift calc
            int heightShift = parentTerrain.terrainData.heightmapResolution / terraPeaces;

            int startX = 0;
            int startY = 0;

            int endX = 0;
            int endY = 0;

            if ( i==0 )
            {
                startX = startY = 0;
                endX = endY = parentTerrain.terrainData.heightmapResolution / terraPeaces + 1;
            }

            if ( i==1 )
            {
                startX = startY = 0;
                endX = parentTerrain.terrainData.heightmapResolution / terraPeaces + 1;
                endY = parentTerrain.terrainData.heightmapResolution / terraPeaces + 1;
            }

            if ( i==2 )
            {
                startX = startY = 0;
                endX = parentTerrain.terrainData.heightmapResolution / terraPeaces + 1;
                endY = parentTerrain.terrainData.heightmapResolution / terraPeaces + 1;
            }

            if ( i==3 )
            {
                startX = startY = 0;
                endX = parentTerrain.terrainData.heightmapResolution / terraPeaces + 1;
                endY = parentTerrain.terrainData.heightmapResolution / terraPeaces + 1;
            }

            // iterate
            for ( int x=startX;x< endX;x++)
            {

                EditorUtility.DisplayProgressBar("Split terrain","Split height", (float) x / ( endX - startX ));

                for ( int y=startY;y< endY;y++)
                {

                    int xShift=0;
                    int yShift=0;

                    //
                    if ( i==0 )
                    {
                        xShift = 0;
                        yShift = 0;
                    }

                    //
                    if ( i==1 )
                    {
                        xShift = heightShift;
                        yShift = 0;
                    }

                    //
                    if ( i==2 )
                    {
                        xShift = 0;
                        yShift = heightShift;
                    }

                    if ( i==3 )
                    {
                        xShift = heightShift;
                        yShift = heightShift;
                    }

                    float ph = parentHeight[ x + xShift,y + yShift];

                    peaceHeight[x ,y ] = ph;

                }

            }

            EditorUtility.ClearProgressBar();

            // Set heightmap to child
            genTer.terrainData.SetHeights( 0,0, peaceHeight );
            #endregion

            // Split splat map
            #region split splat map

            td.alphamapResolution = parentTerrain.terrainData.alphamapResolution /  terraPeaces;

            float[,,] parentSplat = parentTerrain.terrainData.GetAlphamaps(0,0, parentTerrain.terrainData.alphamapResolution, parentTerrain.terrainData.alphamapResolution );

            float[,,] peaceSplat = new float[ parentTerrain.terrainData.alphamapResolution / terraPeaces ,
                                              parentTerrain.terrainData.alphamapResolution / terraPeaces,
                                              parentTerrain.terrainData.alphamapLayers
                                            ];

            // Shift calc
            int splatShift = parentTerrain.terrainData.alphamapResolution / terraPeaces;

            if ( i==0 )
            {
                startX = startY = 0;
                endX = endY = parentTerrain.terrainData.alphamapResolution / terraPeaces;
            }

            if ( i==1 )
            {
                startX = startY = 0;
                endX = parentTerrain.terrainData.alphamapResolution / terraPeaces;
                endY = parentTerrain.terrainData.alphamapResolution / terraPeaces;
            }

            if ( i==2 )
            {
                startX = startY = 0;
                endX = parentTerrain.terrainData.alphamapResolution / terraPeaces;
                endY = parentTerrain.terrainData.alphamapResolution / terraPeaces;
            }

            if ( i==3 )
            {
                startX = startY = 0;
                endX = parentTerrain.terrainData.alphamapResolution / terraPeaces;
                endY = parentTerrain.terrainData.alphamapResolution / terraPeaces;
            }

            // iterate
            for ( int s=0;s<parentTerrain.terrainData.alphamapLayers;s++)
            {
                for ( int x=startX;x< endX;x++)
                {

                    EditorUtility.DisplayProgressBar("Split terrain","Split splat", (float) x / ( endX - startX ));

                    for ( int y=startY;y< endY;y++)
                    {

                        int xShift=0;
                        int yShift=0;

                        //
                        if ( i==0 )
                        {
                            xShift = 0;
                            yShift = 0;
                        }

                        //
                        if ( i==1 )
                        {
                            xShift = splatShift;
                            yShift = 0;
                        }

                        //
                        if ( i==2 )
                        {
                            xShift = 0;
                            yShift = splatShift;
                        }

                        if ( i==3 )
                        {
                            xShift = splatShift;
                            yShift = splatShift;
                        }

                        float ph = parentSplat[x + xShift,y + yShift, s];
                        peaceSplat[x ,y, s] = ph;

                    }

                }
            }

            EditorUtility.ClearProgressBar();

            // Set heightmap to child
            genTer.terrainData.SetAlphamaps( 0,0, peaceSplat );
            #endregion

            // Split detail map
            #region split detail map

            td.SetDetailResolution( parentTerrain.terrainData.detailResolution / terraPeaces, 8 );

            for ( int detLay=0; detLay< parentTerrain.terrainData.detailPrototypes.Length; detLay++)
            {
                int[,] parentDetail = parentTerrain.terrainData.GetDetailLayer(0,0, parentTerrain.terrainData.detailResolution, parentTerrain.terrainData.detailResolution, detLay );

                int[,] peaceDetail = new int[ parentTerrain.terrainData.detailResolution / terraPeaces,
                                              parentTerrain.terrainData.detailResolution / terraPeaces
                                            ];

                // Shift calc
                int detailShift = parentTerrain.terrainData.detailResolution / terraPeaces;

                if ( i==0 )
                {
                    startX = startY = 0;
                    endX = endY = parentTerrain.terrainData.detailResolution / terraPeaces;
                }

                if ( i==1 )
                {
                    startX = startY = 0;
                    endX = parentTerrain.terrainData.detailResolution / terraPeaces;
                    endY = parentTerrain.terrainData.detailResolution / terraPeaces;
                }

                if ( i==2 )
                {
                    startX = startY = 0;
                    endX = parentTerrain.terrainData.detailResolution / terraPeaces;
                    endY = parentTerrain.terrainData.detailResolution / terraPeaces;
                }

                if ( i==3 )
                {
                    startX = startY = 0;
                    endX = parentTerrain.terrainData.detailResolution / terraPeaces;
                    endY = parentTerrain.terrainData.detailResolution / terraPeaces;
                }

                // iterate
                    for ( int x=startX;x< endX;x++)
                    {

                        EditorUtility.DisplayProgressBar("Split terrain","Split detail", (float) x / (endX - startX ));

                        for ( int y=startY;y< endY;y++)
                        {

                            int xShift=0;
                            int yShift=0;

                            //
                            if ( i==0 )
                            {
                                xShift = 0;
                                yShift = 0;
                            }

                            //
                            if ( i==1 )
                            {
                                xShift = detailShift;
                                yShift = 0;
                            }

                            //
                            if ( i==2 )
                            {
                                xShift = 0;
                                yShift = detailShift;
                            }

                            if ( i==3 )
                            {
                                xShift = detailShift;
                                yShift = detailShift;
                            }

                            int ph = parentDetail[x + xShift,y + yShift];
                            peaceDetail[x ,y] = ph;

                        }

                }
                EditorUtility.ClearProgressBar();

                // Set heightmap to child
                genTer.terrainData.SetDetailLayer( 0,0, detLay, peaceDetail );

            }
                #endregion

            // Split tree data
            #region  split tree data

            for( int t=0; t< parentTerrain.terrainData.treeInstances.Length;t++)
            {

                EditorUtility.DisplayProgressBar("Split terrain","Split trees "  , (float) t / parentTerrain.terrainData.treeInstances.Length );

                // Get tree instance
                TreeInstance ti = parentTerrain.terrainData.treeInstances[t];

                // First section
                if ( i==0 &&
                     ti.position.x > 0f &&	ti.position.x < 0.5f &&
                     ti.position.z > 0f &&	ti.position.z < 0.5f
                    )
                {
                    // Recalculate new tree position
                    ti.position = new Vector3( ti.position.x * 2f, ti.position.y, ti.position.z * 2f );

                    // Add tree instance
                    genTer.AddTreeInstance( ti );
                }

                // Second section
                if ( i==1 &&
                     ti.position.x > 0.0f &&ti.position.x < 0.5f &&
                     ti.position.z >= 0.5f &&	ti.position.z <= 1.0f
                    )
                {
                    // Recalculate new tree position
                    ti.position = new Vector3( (ti.position.x ) * 2f, ti.position.y, ( ti.position.z - 0.5f ) * 2f );

                    // Add tree instance
                    genTer.AddTreeInstance( ti );
                }

                // Third section
                if ( i==2 &&
                     ti.position.x >= 0.5f && ti.position.x <= 1.0f &&
                     ti.position.z > 0.0f && ti.position.z < 0.5f
                    )
                {
                    // Recalculate new tree position
                    ti.position = new Vector3( (ti.position.x - 0.5f ) * 2f, ti.position.y, ( ti.position.z ) * 2f );

                    // Add tree instance
                    genTer.AddTreeInstance( ti );
                }

                // Fourth section
                if ( i==3 &&
                     ti.position.x >= 0.5f && ti.position.x <= 1.0f &&
                     ti.position.z >= 0.5f && ti.position.z <= 1.0f
                    )
                {
                    // Recalculate new tree position
                    ti.position = new Vector3( (ti.position.x - 0.5f ) * 2f, ti.position.y, ( ti.position.z - 0.5f ) * 2f );

                    // Add tree instance
                    genTer.AddTreeInstance( ti );
                }

            }
            #endregion

            AssetDatabase.SaveAssets();

        }

        EditorUtility.ClearProgressBar();
    }
	void SampleHeightInfos(Terrain terrain, int widthGridCount, int lengthGridCount)
	{
		Vector3 terrainPos = terrain.GetPosition();
		terrain.transform.position = Vector3.zero;
		GameObject meshObj;
		MeshCollider cld;
		GenMeshColliderFromNavMesh(out meshObj, out cld);

		frees = new bool[widthGridCount][];
		isWayPoints = new bool[widthGridCount][];
		for (int i = 0; i < widthGridCount; i++)
		{
			frees[i] = new bool[lengthGridCount];
			isWayPoints[i] = new bool[lengthGridCount];
		}

		xCount = widthGridCount + 1;
		zCount = lengthGridCount + 1;

		hitPositions = new Vector3[xCount][];
		for (int i = 0; i < xCount; i++)
		{
			hitPositions[i] = new Vector3[zCount];
		}

		__notHitCount = 0;

		float stepX = terrain.terrainData.size.x / widthGridCount;
		float stepZ = terrain.terrainData.size.z / lengthGridCount;
		centerOffset = new Vector3(stepX * 0.5f, 0, stepZ * 0.5f);

		float y = terrain.terrainData.size.y + 1;
		float distance = y + 2;
		// heights
		for (int z = 0; z < zCount; z++)
		{
			for (int x = 0; x < xCount; x++)
			{
				Vector3 sourcePoint = new Vector3(x * stepX, y, z * stepZ);
				bool hit = SampleHeight(sourcePoint, distance * 2, out hitPositions[x][z], 1 << layerID);
				if (!hit)
				{
					__notHitCount++;
				}
				hitPositions[x][z] += terrainPos;
			}
		}
		centerPositions = CenterPositionsFromHitPositions(hitPositions);
		// reachable
		/*
		for (int z = 0; z < lengthGridCount; z++)
		{
			for (int x = 0; x < widthGridCount; x++)
			{
				Vector3 sourcePoint = new Vector3(x * stepX, y, z * stepZ) + centerOffset;
				Vector3 tempHitPos;
				bool hit = SampleHeight(sourcePoint, distance * 2, out tempHitPos, 1 << layerID);
				Ray ray = new Ray(sourcePoint, Vector3.down);
				RaycastHit raycastHit;
				frees[x][z] = cld.Raycast(ray, out raycastHit, distance);
			}
		}
		 */
		////
		//// new 
		bool[][] hitNav = new bool[xCount][];
		bool[][] hitCld = new bool[xCount][];
		for (int i = 0; i < hitNav.Length; i++)
		{
			hitNav[i] = new bool[zCount];
			hitCld[i] = new bool[zCount];
		}
		for (int z = 0; z < zCount; z++)
		{
			for (int x = 0; x < xCount; x++)
			{
				Vector3 sourcePoint = new Vector3(x * stepX, y, z * stepZ);
				Vector3 tempHitPos;
				//hitNav[x][z] = SampleHeight(sourcePoint, distance * 2, out tempHitPos, 1 << layerID);
				hitCld[x][z] = SampleHeight(sourcePoint, distance * 2, out tempHitPos, 1 << colliderID);
				Ray ray = new Ray(sourcePoint, Vector3.down);
				RaycastHit raycastHit;
				hitNav[x][z] = cld.Raycast(ray, out raycastHit, distance);
			}
		}
		for (int z = 0; z < zCount - 1; z++)
		{
			for (int x = 0; x < xCount - 1; x++)
			{
				bool b0 = hitCld[x][z];
				bool b1 = hitCld[x + 1][z];
				bool b2 = hitCld[x + 1][z + 1];
				bool b3 = hitCld[x][z + 1];
				frees[x][z] = !(b0 && b1 && b2 && b3);

				bool f0 = hitNav[x][z];
				bool f1 = hitNav[x + 1][z];
				bool f2 = hitNav[x + 1][z + 1];
				bool f3 = hitNav[x][z + 1];
				if (frees[x][z])
				{
					if (!(f0 || f1 || f2 || f3))
					{
						frees[x][z] = false;
					}
				}
			}
		}
		//board
		for (int x = 0; x < xCount - 1; x++)
		{
			frees[x][0] = false;
			frees[x][zCount - 2] = false;
		}
		for (int z = 0; z < zCount - 1; z++)
		{
			frees[0][z] = false;
			frees[xCount - 2][z] = false;
		}



		GameObject.DestroyImmediate(meshObj);
		terrain.transform.position = terrainPos;
	}