public void Refresh()
        {
            if (rtp == null)
            {
                rtp = MapMagic.instance.GetComponent <ReliefTerrain>();
            }

            foreach (Chunk chunk in MapMagic.instance.chunks.All())
            {
                Material mat = chunk.terrain.materialTemplate;

                rtp.RefreshTextures(mat);
                rtp.globalSettingsHolder.Refresh(mat, rtp);
            }
        }
Beispiel #2
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 #3
0
 public void SetupValues()
 {
     if (this.blendedObject && (this.blendedObject.GetComponent(typeof(MeshRenderer)) != null || this.blendedObject.GetComponent(typeof(Terrain)) != null))
     {
         if (this.underlying_transform == null)
         {
             this.underlying_transform = base.transform.Find("RTP_blend_underlying");
         }
         if (this.underlying_transform != null)
         {
             GameObject gameObject = this.underlying_transform.gameObject;
             this.underlying_renderer = (MeshRenderer)gameObject.GetComponent(typeof(MeshRenderer));
         }
         if (this.underlying_renderer != null && this.underlying_renderer.sharedMaterial != null)
         {
             ReliefTerrain reliefTerrain = (ReliefTerrain)this.blendedObject.GetComponent(typeof(ReliefTerrain));
             if (reliefTerrain)
             {
                 Material sharedMaterial = this.underlying_renderer.sharedMaterial;
                 reliefTerrain.RefreshTextures(sharedMaterial, false);
                 reliefTerrain.globalSettingsHolder.Refresh(sharedMaterial, null);
                 if (sharedMaterial.HasProperty("RTP_DeferredAddPassSpec"))
                 {
                     sharedMaterial.SetFloat("RTP_DeferredAddPassSpec", this._DeferredBlendGloss);
                 }
                 if (reliefTerrain.controlA)
                 {
                     sharedMaterial.SetTexture("_Control", reliefTerrain.controlA);
                 }
                 if (reliefTerrain.ColorGlobal)
                 {
                     sharedMaterial.SetTexture("_Splat0", reliefTerrain.ColorGlobal);
                 }
                 if (reliefTerrain.NormalGlobal)
                 {
                     sharedMaterial.SetTexture("_Splat1", reliefTerrain.NormalGlobal);
                 }
                 if (reliefTerrain.TreesGlobal)
                 {
                     sharedMaterial.SetTexture("_Splat2", reliefTerrain.TreesGlobal);
                 }
                 if (reliefTerrain.BumpGlobalCombined)
                 {
                     sharedMaterial.SetTexture("_Splat3", reliefTerrain.BumpGlobalCombined);
                 }
             }
             Terrain terrain = (Terrain)this.blendedObject.GetComponent(typeof(Terrain));
             if (terrain)
             {
                 this.underlying_renderer.lightmapIndex               = terrain.lightmapIndex;
                 this.underlying_renderer.lightmapScaleOffset         = terrain.lightmapScaleOffset;
                 this.underlying_renderer.realtimeLightmapIndex       = terrain.realtimeLightmapIndex;
                 this.underlying_renderer.realtimeLightmapScaleOffset = terrain.realtimeLightmapScaleOffset;
             }
             else
             {
                 this.underlying_renderer.lightmapIndex               = this.blendedObject.GetComponent <Renderer>().lightmapIndex;
                 this.underlying_renderer.lightmapScaleOffset         = this.blendedObject.GetComponent <Renderer>().lightmapScaleOffset;
                 this.underlying_renderer.realtimeLightmapIndex       = this.blendedObject.GetComponent <Renderer>().realtimeLightmapIndex;
                 this.underlying_renderer.realtimeLightmapScaleOffset = this.blendedObject.GetComponent <Renderer>().realtimeLightmapScaleOffset;
             }
             if (this.Sticked)
             {
                 if (terrain)
                 {
                     base.GetComponent <Renderer>().lightmapIndex               = terrain.lightmapIndex;
                     base.GetComponent <Renderer>().lightmapScaleOffset         = terrain.lightmapScaleOffset;
                     base.GetComponent <Renderer>().realtimeLightmapIndex       = terrain.realtimeLightmapIndex;
                     base.GetComponent <Renderer>().realtimeLightmapScaleOffset = terrain.realtimeLightmapScaleOffset;
                     return;
                 }
                 base.GetComponent <Renderer>().lightmapIndex               = this.blendedObject.GetComponent <Renderer>().lightmapIndex;
                 base.GetComponent <Renderer>().lightmapScaleOffset         = this.blendedObject.GetComponent <Renderer>().lightmapScaleOffset;
                 base.GetComponent <Renderer>().realtimeLightmapIndex       = this.blendedObject.GetComponent <Renderer>().realtimeLightmapIndex;
                 base.GetComponent <Renderer>().realtimeLightmapScaleOffset = this.blendedObject.GetComponent <Renderer>().realtimeLightmapScaleOffset;
             }
         }
     }
 }