void GetAndSetActiveRenderPipelineSettings() { ToolboxHelper.RenderPipeline m_ActiveRenderPipeline = ToolboxHelper.GetRenderPipeline(); m_VisualizationMaterial = AssetDatabase.LoadAssetAtPath <Material>("Packages/com.unity.terrain-tools/editor/terraintoolbox/materials/terrainvisualization.mat"); switch (m_ActiveRenderPipeline) { case ToolboxHelper.RenderPipeline.HD: m_VisualizationMaterial.shader = AssetDatabase.LoadAssetAtPath <Shader>(k_HDRPShaderPath); break; case ToolboxHelper.RenderPipeline.Universal: m_VisualizationMaterial.shader = AssetDatabase.LoadAssetAtPath <Shader>(k_URPShaderPath); break; default: if (m_Terrains == null || m_Terrains.Count == 0) { break; } #if UNITY_2019_2_OR_NEWER #else m_TerrainMaterialType = m_Terrains[0].materialType; if (m_TerrainMaterialType == Terrain.MaterialType.BuiltInLegacySpecular) { m_TerrainLegacyShininess = m_Terrains[0].legacyShininess; m_TerrainLegacySpecular = m_Terrains[0].legacySpecular; } #endif m_VisualizationMaterial.shader = Shader.Find("Hidden/Builtin_TerrainVisualization"); break; } }
public void GetSettings(bool settingsEditor) { if (!CheckValidUnityTerrain()) { return; } materialType = terrain.materialType; // materialTemplate = terrain.materialTemplate; basemapDistance = terrain.basemapDistance; castShadows = terrain.castShadows; treeCrossFadeLength = terrain.treeCrossFadeLength; #if UNITY_EDITOR bakeLightProbesForTrees = terrain.bakeLightProbesForTrees; #endif reflectionProbeUsage = terrain.reflectionProbeUsage; thickness = terrain.terrainData.thickness; collectDetailPatches = terrain.collectDetailPatches; legacyShininess = terrain.legacyShininess; legacySpecular = terrain.legacySpecular; wavingGrassSpeed = terrain.terrainData.wavingGrassSpeed; wavingGrassAmount = terrain.terrainData.wavingGrassAmount; wavingGrassStrength = terrain.terrainData.wavingGrassStrength; wavingGrassTint = terrain.terrainData.wavingGrassTint; if (settingsEditor) { heightmapPixelError = terrain.heightmapPixelError; heightmapMaximumLOD = terrain.heightmapMaximumLOD; drawTreesAndFoliage = terrain.drawTreesAndFoliage; treeDistance = terrain.treeDistance; detailObjectDistance = terrain.detailObjectDistance; detailObjectDensity = terrain.detailObjectDensity; treeBillboardDistance = terrain.treeBillboardDistance; treeMaximumFullLODCount = terrain.treeMaximumFullLODCount; } else { terrainSettingsScript = terrain.gameObject.GetComponent <TC_TerrainSettings>(); if (terrainSettingsScript == null) { terrainSettingsScript = terrain.gameObject.AddComponent <TC_TerrainSettings>(); } heightmapPixelError = terrainSettingsScript.heightmapPixelError; heightmapMaximumLOD = terrainSettingsScript.heightmapMaximumLOD; drawTreesAndFoliage = terrainSettingsScript.drawTreesAndFoliage; treeDistance = terrainSettingsScript.treeDistance; detailObjectDistance = terrainSettingsScript.detailObjectDistance; detailObjectDensity = terrainSettingsScript.detailObjectDensity; treeBillboardDistance = terrainSettingsScript.treeBillboardDistance; treeMaximumFullLODCount = terrainSettingsScript.treeMaximumFullLODCount; } }
public void CopySettingsFrom(Terrain terrain) { if (terrain == null) { return; } // base settings GroupingID = terrain.groupingID; AutoConnect = terrain.allowAutoConnect; DrawHeightmap = terrain.drawHeightmap; DrawInstanced = terrain.drawInstanced; PixelError = terrain.heightmapPixelError; BaseMapDistance = terrain.basemapDistance; ShadowCastingMode = terrain.shadowCastingMode; MaterialTemplate = terrain.materialTemplate; ReflectionProbeUsage = terrain.reflectionProbeUsage; #if UNITY_2019_2_OR_NEWER #else MaterialType = terrain.materialType; LegacySpecular = terrain.legacySpecular; LegacyShininess = terrain.legacyShininess; #endif // mesh resolution TerrainWidth = terrain.terrainData.size.x; TerrainHeight = terrain.terrainData.size.y; TerrainLength = terrain.terrainData.size.z; DetailResolutaion = terrain.terrainData.detailResolution; DetailResolutionPerPatch = terrain.terrainData.detailResolutionPerPatch; // texture resolution BaseTextureResolution = terrain.terrainData.baseMapResolution; AlphaMapResolution = terrain.terrainData.alphamapResolution; HeightMapResolution = terrain.terrainData.heightmapResolution; // tree and details DrawTreesAndFoliage = terrain.drawTreesAndFoliage; BakeLightProbesForTrees = terrain.bakeLightProbesForTrees; DeringLightProbesForTrees = terrain.deringLightProbesForTrees; PreserveTreePrototypeLayers = terrain.preserveTreePrototypeLayers; DetailObjectDistance = terrain.detailObjectDistance; CollectDetailPatches = terrain.collectDetailPatches; DetailObjectDensity = terrain.detailObjectDensity; TreeDistance = terrain.treeDistance; TreeBillboardDistance = terrain.treeBillboardDistance; TreeCrossFadeLength = terrain.treeCrossFadeLength; TreeMaximumFullLODCount = terrain.treeMaximumFullLODCount; // grass wind WavingGrassStrength = terrain.terrainData.wavingGrassStrength; WavingGrassSpeed = terrain.terrainData.wavingGrassSpeed; WavingGrassAmount = terrain.terrainData.wavingGrassAmount; WavingGrassTint = terrain.terrainData.wavingGrassTint; }
public void CopySettingsFrom(TerrainSettings other) { if (other == null) { return; } GroupingID = other.GroupingID; AutoConnect = other.AutoConnect; DrawHeightmap = other.DrawHeightmap; DrawInstanced = other.DrawInstanced; PixelError = other.PixelError; BaseMapDistance = other.BaseMapDistance; ShadowCastingMode = other.ShadowCastingMode; MaterialTemplate = other.MaterialTemplate; ReflectionProbeUsage = other.ReflectionProbeUsage; #if UNITY_2019_2_OR_NEWER #else MaterialType = other.MaterialType; LegacySpecular = other.LegacySpecular; LegacyShininess = other.LegacyShininess; #endif // mesh resolution TerrainWidth = other.TerrainWidth; TerrainHeight = other.TerrainHeight; TerrainLength = other.TerrainLength; DetailResolutaion = other.DetailResolutaion; DetailResolutionPerPatch = other.DetailResolutionPerPatch; // texture resolution BaseTextureResolution = other.BaseTextureResolution; AlphaMapResolution = other.AlphaMapResolution; HeightMapResolution = other.HeightMapResolution; // tree and details DrawTreesAndFoliage = other.DrawTreesAndFoliage; BakeLightProbesForTrees = other.BakeLightProbesForTrees; DeringLightProbesForTrees = other.DeringLightProbesForTrees; PreserveTreePrototypeLayers = other.PreserveTreePrototypeLayers; DetailObjectDistance = other.DetailObjectDistance; CollectDetailPatches = other.CollectDetailPatches; DetailObjectDensity = other.DetailObjectDensity; TreeDistance = other.TreeDistance; TreeBillboardDistance = other.TreeBillboardDistance; TreeCrossFadeLength = other.TreeCrossFadeLength; TreeMaximumFullLODCount = other.TreeMaximumFullLODCount; // grass wind WavingGrassStrength = other.WavingGrassStrength; WavingGrassSpeed = other.WavingGrassSpeed; WavingGrassAmount = other.WavingGrassAmount; WavingGrassTint = other.WavingGrassTint; }
private static bool IsUsingSmoothness(Terrain.MaterialType materialType, Material materialTemplate) { if (materialType == Terrain.MaterialType.BuiltInStandard) { return(true); } if (materialType == Terrain.MaterialType.Custom && (UnityEngine.Object)materialTemplate != (UnityEngine.Object)null) { return(materialTemplate.HasProperty("_Smoothness0")); } return(false); }
void GetAndSetActiveRenderPipelineSettings() { ToolboxHelper.RenderPipeline m_ActiveRenderPipeline = ToolboxHelper.GetRenderPipeline(); m_VisualizationMaterial = AssetDatabase.LoadAssetAtPath <Material>("Packages/com.unity.terrain-tools/editor/terraintoolbox/materials/terrainvisualization.mat"); //Get terrains if (m_Terrains == null || GameObject.FindObjectsOfType <Terrain>().Length != m_Terrains.Count || m_Terrains[0] == null) { m_Terrains.Clear(); m_Terrains.AddRange(ToolboxHelper.GetAllTerrainsInScene()); m_Settings.TerrainMaxHeight = m_Terrains[0].terrainData.size.y; } //Get materials to revert to m_TerrainMaterials.Clear(); foreach (Terrain terrain in m_Terrains) { m_TerrainMaterials.Add(terrain.materialTemplate); } switch (m_ActiveRenderPipeline) { case ToolboxHelper.RenderPipeline.HD: m_VisualizationMaterial.shader = Shader.Find("Hidden/HDRP_TerrainVisualization"); break; case ToolboxHelper.RenderPipeline.LW: m_VisualizationMaterial.shader = Shader.Find("Hidden/LWRP_TerrainVisualization"); break; default: if (m_Terrains == null || m_Terrains.Count == 0) { break; } #if UNITY_2019_2_OR_NEWER #else m_TerrainMaterialType = m_Terrains[0].materialType; if (m_TerrainMaterialType == Terrain.MaterialType.BuiltInLegacySpecular) { m_TerrainLegacyShininess = m_Terrains[0].legacyShininess; m_TerrainLegacySpecular = m_Terrains[0].legacySpecular; } #endif m_VisualizationMaterial.shader = Shader.Find("Hidden/Builtin_TerrainVisualization"); break; } }
private static bool IsUsingSpecular(Terrain.MaterialType materialType, Material materialTemplate) => ((materialType == Terrain.MaterialType.BuiltInStandard) || (((materialType == Terrain.MaterialType.Custom) && (materialTemplate != null)) && materialTemplate.HasProperty("_Specular0")));
private static bool IsUsingSmoothness(Terrain.MaterialType materialType, Material materialTemplate) { return(materialType == Terrain.MaterialType.BuiltInStandard || (materialType == Terrain.MaterialType.Custom && materialTemplate != null && materialTemplate.HasProperty("_Smoothness0"))); }
void Update() { //FOG OF WAR BUTTON if (Input.GetKeyDown(KeyCode.F) || justDisabled) { if (!revealed) { temp = this.Terrain.materialTemplate; temp2 = this.Terrain.materialType; if (this.Terrain.materialType != Terrain.MaterialType.BuiltInStandard) { this.Terrain.materialType = Terrain.MaterialType.BuiltInStandard; foreach (var entity in Entities) entity.show(); } revealed = !revealed; } else { print("HOLA DEBUG!" + Entities.Count()); this.Terrain.materialTemplate = temp;//Resources.Load("Materials/Terrain", typeof(Material)) as Material; this.Terrain.materialType = temp2;//Terrain.MaterialType.Custom; foreach (LOSEntity g in Entities) { print(g.gameObject.name); //g.RevealState = LOSEntity.RevealStates.Fogged; g.disable(); } revealed = !revealed; } } //THE UPDATE GOES ON #if UNITY_EDITOR if (!Application.isPlaying) { if (PreviewInEditor) { // Make sure we have a valid texture if (losTexture == null || previewParameterHash != GenerateParameterHash()) { InitializeTexture(); previewParameterHash = GenerateParameterHash(); } } else { // Or just use a white texture as placeholder Shader.SetGlobalTexture("_FOWTex", UnityEditor.EditorGUIUtility.whiteTexture); if (losTexture != null) DestroyImmediate(losTexture); losTexture = null; } } #endif if (losTexture != null) { // Update any animating entities (update their FOW color) for (int e = AnimatingEntities.Count-1; e >=0 ; e--) { if (AnimatingEntities[e].UpdateFOWColor()) AnimatingEntities.RemoveAt(e); } // If in editor mode if (!Application.isPlaying) { // Refresh the map each frame for (int p = 0; p < pixels.Length; ++p) { pixels[p] = new Color32(0, 255, 0, 255); } // Add LOS and AO for all entities foreach (var entity in Entities) { RevealLOS(entity, entity.IsRevealer ? 255 : 0, 255, 255); if (entity.EnableAO && (AOIntensity > 0 || EnableHeightBlockers)) { var bounds = entity.Bounds; AddAO(bounds, entity.Height); } } } else { bool forceFullUpdate = Time.frameCount == 1; // Reset all entities to be invisible if (forceFullUpdate) { int revealerCount = 0; foreach (var entity in Entities) { entity.RevealState = LOSEntity.RevealStates.Hidden; if (entity.IsRevealer) revealerCount++; } if (revealerCount == 0) { Debug.LogError("No LOSEntity items were marked as revealers! Tick the 'Is Revealed' checkbox for at least 1 item."); } } // Ensure we have space to store blocking heights (if enabled) if (blockHeights == null && EnableHeightBlockers) { blockHeights = new float[losTexture.height, losTexture.width]; forceFullUpdate = true; } // Decay grayscale if (GrayscaleDecayDuration > 0) { const int GrayscaleGranularity = 4; int oldGrayDecay = (int)(256 / GrayscaleGranularity * timer / GrayscaleDecayDuration) * GrayscaleGranularity; timer += Time.deltaTime; int newGrayDecay = (int)(256 / GrayscaleGranularity * timer / GrayscaleDecayDuration) * GrayscaleGranularity; int grayDecayCount = newGrayDecay - oldGrayDecay; if (grayDecayCount != 0) { for (int p = 0; p < pixels.Length; ++p) { pixels[p].b = (byte)Mathf.Max(pixels[p].b - grayDecayCount, 0); } } } ++frameId; // Reset AO and LOS bool updateAo = (frameId % 2) == 0; if (updateAo || forceFullUpdate) { for (int p = 0; p < pixels.Length; ++p) { pixels[p].r = 0; //pixels[p].a = (byte)Mathf.Clamp(Costs.Costs[p / Costs.Width, p % Costs.Width] * 10 - 10, 0, 255); pixels[p].a = 255; } if (AOIntensity > 0 || EnableHeightBlockers) { if (Terrain != null && EnableHeightBlockers && blockHeights != null) { if (terrainHeightsCache == null) { terrainHeightsCache = (float[,])blockHeights.Clone(); for (int y = 0; y < blockHeights.GetLength(0); ++y) { for (int x = 0; x < blockHeights.GetLength(1); ++x) { var terrainData = Terrain.terrainData; int tx = Mathf.RoundToInt(x * terrainData.heightmapWidth / terrainData.size.x / Scale); int ty = Mathf.RoundToInt(y * terrainData.heightmapHeight / terrainData.size.z / Scale); terrainHeightsCache[y, x] = terrainData.GetHeight(tx, ty); } } } for (int y = 0; y < blockHeights.GetLength(0); ++y) { for (int x = 0; x < blockHeights.GetLength(1); ++x) { blockHeights[y, x] = terrainHeightsCache[y, x]; } } } foreach (var entity in Entities) { if (!entity.Equals(null)) { var bounds = entity.Bounds; if (entity.EnableAO && AOIntensity > 0) AddAO(bounds, entity.Height); if (EnableHeightBlockers && (AllowOwnTeamHeightBlockers || !entity.IsRevealer)) AddHeightBlocker(bounds, entity.transform.position.y + entity.Height); } } } } // Reveal LOS from all entities foreach (var entity in Entities) { if (entity.IsRevealer) RevealLOS(entity, 255, 255, 330); } int count = 0; foreach (var entity in Entities) { ++count; var rect = entity.Bounds; var fowColor = GetFOWColor(rect); var visible = GetRevealFromFOW(fowColor); if (entity.RevealState != visible && !(entity.RevealState == LOSEntity.RevealStates.Hidden && visible == LOSEntity.RevealStates.Fogged)) { entity.RevealState = visible; if (visible == LOSEntity.RevealStates.Unfogged && RevealOnEntityDiscover) { RevealLOS(rect, 0, entity.Height + entity.transform.position.y, 0, 255, 255); } } if (visible != LOSEntity.RevealStates.Hidden || forceFullUpdate) { entity.SetFOWColor(GetQuantizedFOW(fowColor), !forceFullUpdate); // Queue the item for FOW animation if (entity.RequiresFOWUpdate && !AnimatingEntities.Contains(entity)) AnimatingEntities.Add(entity); } } //Manage the fact that resources and enemies behave in a different way. foreach (var entity in Entities) { if (!entity.IsRevealer && (entity.RevealState == LOSEntity.RevealStates.Fogged || entity.RevealState == LOSEntity.RevealStates.Hidden) ) { entity.setActive(false); } } } bool isChanged = true; if (InterpolationRate > 0 && Application.isPlaying) { if (lerpPixels == null) lerpPixels = pixels.ToArray(); else { int rate = Mathf.Max(Mathf.RoundToInt(InterpolationRate * Time.deltaTime), 1); for (int p = 0; p < lerpPixels.Length; ++p) { byte r = EaseToward(lerpPixels[p].r, pixels[p].r, rate), g = EaseToward(lerpPixels[p].g, pixels[p].g, rate), b = EaseToward(lerpPixels[p].b, pixels[p].b, rate), a = EaseToward(lerpPixels[p].a, pixels[p].a, rate); if (isChanged || lerpPixels[p].a != a || lerpPixels[p].r != r || lerpPixels[p].g != g || lerpPixels[p].b != b) { isChanged = true; lerpPixels[p] = new Color32(r, g, b, a); } } } } else lerpPixels = null; if (isChanged) { losTexture.SetPixels32(lerpPixels ?? pixels); losTexture.Apply(); } } }
private static bool IsUsingMetallic(Terrain.MaterialType materialType, Material materialTemplate) { return((materialType == Terrain.MaterialType.BuiltInStandard) || (((materialType == Terrain.MaterialType.Custom) && (materialTemplate != null)) && materialTemplate.HasProperty("_Metallic0"))); }