Beispiel #1
0
        public static IEnumerator Apply(CoordRect rect, Terrain terrain, object dataBox, Func <float, bool> stop = null)
        {
                        #if RTP
            //guard if old-style rtp approach is used
            ReliefTerrain chunkRTP = terrain.gameObject.GetComponent <ReliefTerrain>();
            if (chunkRTP != null && chunkRTP.enabled)
            {
                Debug.Log("MapMagic: RTP component on terain chunk detected. RTP Output Generator works with one RTP script assigned to main MM object only. Make sure that Copy Components is turned off.");
                chunkRTP.enabled = false;
            }
            yield return(null);

            //loading objects
            RTPTuple tuple = (RTPTuple)dataBox;
            if (tuple == null)
            {
                yield break;
            }

            //creating control textures
            Texture2D controlA = new Texture2D(MapMagic.instance.resolution, MapMagic.instance.resolution);
            controlA.wrapMode = TextureWrapMode.Clamp;
            controlA.SetPixels(0, 0, controlA.width, controlA.height, tuple.colorsA);
            controlA.Apply();
            yield return(null);

            Texture2D controlB = null;
            if (tuple.colorsB != null)
            {
                controlB          = new Texture2D(MapMagic.instance.resolution, MapMagic.instance.resolution);
                controlB.wrapMode = TextureWrapMode.Clamp;
                controlB.SetPixels(0, 0, controlB.width, controlB.height, tuple.colorsB);
                controlB.Apply();
                yield return(null);
            }

            //welding
            if (MapMagic.instance != null && MapMagic.instance.splatsWeldMargins != 0)
            {
                Coord coord = Coord.PickCell(rect.offset, MapMagic.instance.resolution);
                //Chunk chunk = MapMagic.instance.chunks[coord.x, coord.z];

                Chunk neigPrevX = MapMagic.instance.chunks[coord.x - 1, coord.z];
                if (neigPrevX != null && neigPrevX.worker.ready && neigPrevX.terrain.materialTemplate.HasProperty("_Control1"))
                {
                    WeldTerrains.WeldTextureToPrevX(controlA, (Texture2D)neigPrevX.terrain.materialTemplate.GetTexture("_Control1"));
                    if (controlB != null && neigPrevX.terrain.materialTemplate.HasProperty("_Control2"))
                    {
                        WeldTerrains.WeldTextureToPrevX(controlB, (Texture2D)neigPrevX.terrain.materialTemplate.GetTexture("_Control2"));
                    }
                }

                Chunk neigNextX = MapMagic.instance.chunks[coord.x + 1, coord.z];
                if (neigNextX != null && neigNextX.worker.ready && neigNextX.terrain.materialTemplate.HasProperty("_Control1"))
                {
                    WeldTerrains.WeldTextureToNextX(controlA, (Texture2D)neigNextX.terrain.materialTemplate.GetTexture("_Control1"));
                    if (controlB != null && neigNextX.terrain.materialTemplate.HasProperty("_Control2"))
                    {
                        WeldTerrains.WeldTextureToNextX(controlB, (Texture2D)neigNextX.terrain.materialTemplate.GetTexture("_Control2"));
                    }
                }

                Chunk neigPrevZ = MapMagic.instance.chunks[coord.x, coord.z - 1];
                if (neigPrevZ != null && neigPrevZ.worker.ready && neigPrevZ.terrain.materialTemplate.HasProperty("_Control1"))
                {
                    WeldTerrains.WeldTextureToPrevZ(controlA, (Texture2D)neigPrevZ.terrain.materialTemplate.GetTexture("_Control1"));
                    if (controlB != null && neigPrevZ.terrain.materialTemplate.HasProperty("_Control2"))
                    {
                        WeldTerrains.WeldTextureToPrevZ(controlB, (Texture2D)neigPrevZ.terrain.materialTemplate.GetTexture("_Control2"));
                    }
                }

                Chunk neigNextZ = MapMagic.instance.chunks[coord.x, coord.z + 1];
                if (neigNextZ != null && neigNextZ.worker.ready && neigNextZ.terrain.materialTemplate.HasProperty("_Control1"))
                {
                    WeldTerrains.WeldTextureToNextZ(controlA, (Texture2D)neigNextZ.terrain.materialTemplate.GetTexture("_Control1"));
                    if (controlB != null && neigNextZ.terrain.materialTemplate.HasProperty("_Control2"))
                    {
                        WeldTerrains.WeldTextureToNextZ(controlB, (Texture2D)neigNextZ.terrain.materialTemplate.GetTexture("_Control2"));
                    }
                }
            }
            yield return(null);

            //assigning material propery block (not saving for fixed terrains)
            //#if UNITY_5_5_OR_NEWER
            //assign textures using material property
            //MaterialPropertyBlock matProp = new MaterialPropertyBlock();
            //matProp.SetTexture("_Control1", controlA);
            //if (controlB!=null) matProp.SetTexture("_Control2", controlB);
            //#endif

            //duplicating material and assign it's values
            //if (MapMagic.instance.customTerrainMaterial != null)
            //{
            //	//duplicating material
            //	terrain.materialTemplate = new Material(MapMagic.instance.customTerrainMaterial);
            //
            //	//assigning control textures
            //	if (terrain.materialTemplate.HasProperty("_Control1"))
            //		terrain.materialTemplate.SetTexture("_Control1", controlA);
            //	if (controlB != null && terrain.materialTemplate.HasProperty("_Control2"))
            //		terrain.materialTemplate.SetTexture("_Control2", controlB);
            //}

            if (rtp == null)
            {
                rtp = MapMagic.instance.gameObject.GetComponent <ReliefTerrain>();
            }
            if (rtp == null || rtp.globalSettingsHolder == null)
            {
                yield break;
            }

            //getting rtp material
            Material mat = null;
            if (terrain.materialTemplate != null && terrain.materialTemplate.shader.name == "Relief Pack/ReliefTerrain-FirstPas")          //if relief terrain material assigned to terrain
            {
                mat = terrain.materialTemplate;
            }
            //if (mat==null && chunk.previewBackupMaterial!=null && chunk.previewBackupMaterial.shader.name=="Relief Pack/ReliefTerrain-FirstPas") //if it is backed up for preview
            //	mat = chunk.previewBackupMaterial;
            if (mat == null)             //if still could not find material - creating new
            {
                Shader shader = Shader.Find("Relief Pack/ReliefTerrain-FirstPass");
                mat = new Material(shader);

                if (Preview.previewOutput == null)
                {
                    terrain.materialTemplate = mat;
                }
                //else chunk.previewBackupMaterial = mat;
            }
            terrain.materialType = Terrain.MaterialType.Custom;

            //setting
            rtp.RefreshTextures(mat);
            rtp.globalSettingsHolder.Refresh(mat, rtp);
            mat.SetTexture("_Control1", controlA);
            if (controlB != null)
            {
                mat.SetTexture("_Control2", controlB); mat.SetTexture("_Control3", controlB);
            }
                        #else
            yield return(null);
                        #endif
        }
Beispiel #2
0
		public static IEnumerator Apply(CoordRect rect, Terrain terrain, object dataBox, Func<float,bool> stop= null)
		{
			#if __MEGASPLAT__
			//loading objects
			MegaSplatData tuple = (MegaSplatData)dataBox;
			if (tuple == null)
				yield break;

			//terrain.materialType = Terrain.MaterialType.Custom; //it's already done with MapMagic
			//terrain.materialTemplate = new Material(tuple.template);

			// TODO: We should pool these textures instead of creating and destroying them!

			int res = MapMagic.instance.resolution;


			//control texture
			var control = new Texture2D(res, res, MegaSplatOutput.formatARGB? TextureFormat.ARGB32 : TextureFormat.RGBA32, false, true);
			control.wrapMode = TextureWrapMode.Clamp;
			control.filterMode = FilterMode.Point;
 
			control.SetPixels(0, 0, control.width, control.height, tuple.control);
			control.Apply();
			yield return null;
			

			//param texture
			var paramTex = new Texture2D(res, res, MegaSplatOutput.formatARGB? TextureFormat.ARGB32 : TextureFormat.RGBA32, false, true);
			paramTex.wrapMode = TextureWrapMode.Clamp;
			paramTex.filterMode = FilterMode.Point;

			paramTex.SetPixels(0, 0, paramTex.width, paramTex.height, tuple.param);
			paramTex.Apply();
			yield return null;


			//welding
			if (MapMagic.instance != null && MapMagic.instance.splatsWeldMargins!=0)
			{
				Coord coord = Coord.PickCell(rect.offset, MapMagic.instance.resolution);
				//Chunk chunk = MapMagic.instance.chunks[coord.x, coord.z];
				
				Chunk neigPrevX = MapMagic.instance.chunks[coord.x-1, coord.z];
				if (neigPrevX!=null && neigPrevX.worker.ready && neigPrevX.terrain.materialTemplate.HasProperty("_SplatControl")) 
				{
					WeldTerrains.WeldTextureToPrevX(control, (Texture2D)neigPrevX.terrain.materialTemplate.GetTexture("_SplatControl"));
					control.Apply();
				}

				Chunk neigNextX = MapMagic.instance.chunks[coord.x+1, coord.z];
				if (neigNextX!=null && neigNextX.worker.ready && neigNextX.terrain.materialTemplate.HasProperty("_SplatControl")) 
				{
					WeldTerrains.WeldTextureToNextX(control, (Texture2D)neigNextX.terrain.materialTemplate.GetTexture("_SplatControl"));
					control.Apply();
				}
				
				Chunk neigPrevZ = MapMagic.instance.chunks[coord.x, coord.z-1];
				if (neigPrevZ!=null && neigPrevZ.worker.ready && neigPrevZ.terrain.materialTemplate.HasProperty("_SplatControl")) 
				{
					WeldTerrains.WeldTextureToPrevZ(control, (Texture2D)neigPrevZ.terrain.materialTemplate.GetTexture("_SplatControl"));
					control.Apply();
				}

				Chunk neigNextZ = MapMagic.instance.chunks[coord.x, coord.z+1];
				if (neigNextZ!=null && neigNextZ.worker.ready && neigNextZ.terrain.materialTemplate.HasProperty("_SplatControl")) 
				{
					WeldTerrains.WeldTextureToNextZ(control, (Texture2D)neigNextZ.terrain.materialTemplate.GetTexture("_SplatControl"));
					control.Apply();
				}
			}
			yield return null;




			//TODO: weld textures with 1-pixel margin

			//assign textures using material property (not saving for fixed terrains)
			//#if UNITY_5_5_OR_NEWER
			//MaterialPropertyBlock matProp = new MaterialPropertyBlock();
			//matProp.SetTexture("_SplatControl", control);
			//matProp.SetTexture("_SplatParams", paramTex);
			//matProp.SetFloat("_ControlSize", res);
			//terrain.SetSplatMaterialPropertyBlock(matProp);
			//#endif

			//duplicating material
			if (MapMagic.instance.customTerrainMaterial != null)
			{
				terrain.materialTemplate = new Material(MapMagic.instance.customTerrainMaterial);

				//assigning control textures
				if (terrain.materialTemplate.HasProperty("_SplatControl"))
					terrain.materialTemplate.SetTexture("_SplatControl", control);
				if (terrain.materialTemplate.HasProperty("_SplatParams"))
					terrain.materialTemplate.SetTexture("_SplatParams", paramTex);
			}

			#else
			yield return null;
			#endif
		}