public void Editor_DrawLivePreview(GStylizedTerrain terrain, GTexturePainterArgs args, Camera cam) { #if UNITY_EDITOR Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Shading.AlbedoMap; int albedoResolution = terrain.TerrainData.Shading.AlbedoMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(terrain, albedoResolution); GCommon.CopyToRT(bg, rt); Material mat = GInternalMaterials.AlbedoPainterMaterial; mat.SetColor("_Color", args.Color); mat.SetTexture("_MainTex", bg); SetupTextureGrid(terrain, mat); mat.SetTexture("_Mask", args.Mask); mat.SetFloat("_Opacity", args.Opacity); int pass = args.ActionType == GPainterActionType.Normal ? 0 : args.ActionType == GPainterActionType.Negative ? 1 : args.ActionType == GPainterActionType.Alternative ? 2 : 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); GLivePreviewDrawer.DrawAlbedoLivePreview(terrain, cam, rt, dirtyRect); #endif }
public void Internal_ApplyMetallic(GStylizedTerrain t, List <Vector4> worldPoints, RenderTexture rtMetallic) { GCommon.CopyToRT(t.TerrainData.Shading.MetallicMap, rtMetallic); Material mat = GInternalMaterials.PathPainterMaterial; mat.SetTexture("_MainTex", t.TerrainData.Shading.MetallicMap); mat.SetFloat("_Metallic", Metallic); mat.SetFloat("_Smoothness", Smoothness); mat.SetTexture("_FalloffNoise", FalloffNoise); mat.SetTextureScale("_FalloffNoise", new Vector2( FalloffNoiseSize.x != 0 ? 1f / FalloffNoiseSize.x : 0, FalloffNoiseSize.y != 0 ? 1f / FalloffNoiseSize.y : 0)); mat.SetTextureOffset("_FalloffNoise", Vector2.zero); int pass = 1; DrawOnTexture(rtMetallic, mat, pass, worldPoints, t); if (SplineCreator.EnableTerrainMask) { mat.SetTexture("_TerrainMask", t.TerrainData.Mask.MaskMap); } else { mat.SetTexture("_TerrainMask", Texture2D.blackTexture); } t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading); }
public void Generate(RenderTexture targetRt) { GBlendMapGeneratorParams param = GTextureToolParams.Instance.Blend; RenderTexture bg = new RenderTexture(targetRt); GCommon.CopyToRT(targetRt, bg); GCommon.FillTexture(targetRt, Color.black); for (int i = 0; i < param.Layers.Count; ++i) { GBlendLayer l = param.Layers[i]; Mat.SetTexture("_Background", bg); Mat.SetTexture("_Foreground", l.Texture); Mat.SetFloat("_Number", l.Number); Mat.SetVector("_Vector", l.Vector); Mat.SetInt("_Ops", (int)l.BlendOps); Mat.SetFloat("_LerpFactor", l.LerpFactor); Mat.SetTexture("_LerpMask", l.LerpMask); Mat.SetInt("_Saturate", l.Saturate ? 1 : 0); GCommon.DrawQuad(targetRt, GCommon.FullRectUvPoints, Mat, (int)l.DataSource); GCommon.CopyToRT(targetRt, bg); } bg.Release(); GUtilities.DestroyObject(bg); }
public void Editor_DrawLivePreview(GStylizedTerrain terrain, GTexturePainterArgs args, Camera cam) { #if UNITY_EDITOR Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Geometry.HeightMap; int heightMapResolution = terrain.TerrainData.Geometry.HeightMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(terrain, heightMapResolution); GCommon.CopyToRT(bg, rt); Vector3 localSamplePoint = terrain.transform.InverseTransformPoint(args.SamplePoint); Material mat = GInternalMaterials.HeightSamplingPainterMaterial; mat.SetTexture("_MainTex", bg); mat.SetTexture("_Mask", args.Mask); mat.SetFloat("_Opacity", args.Opacity); mat.SetFloat("_TargetGray", Mathf.InverseLerp(0, terrain.TerrainData.Geometry.Height, localSamplePoint.y)); int pass = 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); GLivePreviewDrawer.DrawGeometryLivePreview(terrain, cam, rt, dirtyRect); #endif }
private void RenderGeometryHeightMap(GStylizedTerrain terrain, RenderTexture targetRt) { GGeneralParams param = GTextureToolParams.Instance.General; RenderTexture rt = terrain.GetHeightMap(param.Resolution); GCommon.CopyToRT(rt, targetRt); }
public void Internal_ApplySplat(GStylizedTerrain t, List <Vector4> worldPoints, RenderTexture[] rtControls) { Material mat = GInternalMaterials.PathPainterMaterial; mat.SetTexture("_Falloff", falloffTexture); mat.SetTexture("_FalloffNoise", FalloffNoise); mat.SetTextureScale("_FalloffNoise", new Vector2( FalloffNoiseSize.x != 0 ? 1f / FalloffNoiseSize.x : 0, FalloffNoiseSize.y != 0 ? 1f / FalloffNoiseSize.y : 0)); mat.SetTextureOffset("_FalloffNoise", Vector2.zero); int pass = 2; for (int i = 0; i < rtControls.Length; ++i) { Texture2D splatControl = t.TerrainData.Shading.GetSplatControl(i); GCommon.CopyToRT(splatControl, rtControls[i]); mat.SetTexture("_MainTex", splatControl); if (SplatIndex / 4 == i) { mat.SetInt("_ChannelIndex", SplatIndex % 4); } else { mat.SetInt("_ChannelIndex", -1); } DrawOnTexture(rtControls[i], mat, pass, worldPoints, t); } t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading); }
public void Internal_Apply(GStylizedTerrain t, RenderTexture rt, List <Vector4> vertices) { GCommon.CopyToRT(t.TerrainData.Geometry.HeightMap, rt); Material mat = GInternalMaterials.RampMakerMaterial; mat.SetTexture("_HeightMap", t.TerrainData.Geometry.HeightMap); mat.SetTexture("_Falloff", falloffTexture); mat.SetInt("_LowerHeight", LowerHeight ? 1 : 0); mat.SetInt("_RaiseHeight", RaiseHeight ? 1 : 0); mat.SetFloat("_AdditionalMeshResolution", GCommon.SUB_DIV_STEP * AdditionalMeshResolution); mat.SetTexture("_FalloffNoise", FalloffNoise); mat.SetTextureScale("_FalloffNoise", new Vector2( FalloffNoiseSize.x != 0 ? 1f / FalloffNoiseSize.x : 0, FalloffNoiseSize.y != 0 ? 1f / FalloffNoiseSize.y : 0)); mat.SetTextureOffset("_FalloffNoise", Vector2.zero); if (SplineCreator.EnableTerrainMask) { mat.SetTexture("_TerrainMask", t.TerrainData.Mask.MaskMap); } else { mat.SetTexture("_TerrainMask", Texture2D.blackTexture); } mat.SetInt("_StepCount", StepCount); int pass = 0; DrawOnTexture(rt, mat, pass, vertices, t); }
public void Editor_DrawLivePreview(GStylizedTerrain terrain, GTexturePainterArgs args, Camera cam) { #if UNITY_EDITOR Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Geometry.HeightMap; int heightMapResolution = terrain.TerrainData.Geometry.HeightMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(terrain, heightMapResolution); GCommon.CopyToRT(bg, rt); Texture2D remapTex = GCommon.CreateTextureFromCurve(GTexturePainterCustomParams.Instance.Remap.Curve, 512, 1); Material mat = GInternalMaterials.RemapPainterMaterial; mat.SetTexture("_MainTex", bg); mat.SetTexture("_Mask", args.Mask); mat.SetFloat("_Opacity", args.Opacity); mat.SetTexture("_RemapTex", remapTex); int pass = 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); GLivePreviewDrawer.DrawGeometryLivePreview(terrain, cam, rt, dirtyRect); GUtilities.DestroyObject(remapTex); #endif }
public void Paint(Pinwheel.Griffin.GStylizedTerrain terrain, GTexturePainterArgs args) { if (terrain.TerrainData == null) { return; } if (args.MouseEventType == GPainterMouseEventType.Up) { return; } Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Shading.AlbedoMap; int albedoResolution = terrain.TerrainData.Shading.AlbedoMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(albedoResolution); GCommon.CopyToRT(bg, rt); Material mat = GInternalMaterials.AlbedoPainterMaterial; mat.SetColor("_Color", args.Color); mat.SetTexture("_MainTex", bg); SetupTextureGrid(terrain, mat); mat.SetTexture("_Mask", args.Mask); mat.SetFloat("_Opacity", args.Opacity); int pass = args.ActionType == GPainterActionType.Normal ? 0 : args.ActionType == GPainterActionType.Negative ? 1 : args.ActionType == GPainterActionType.Alternative ? 2 : 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); RenderTexture.active = rt; terrain.TerrainData.Shading.AlbedoMap.ReadPixels( new Rect(0, 0, albedoResolution, albedoResolution), 0, 0); terrain.TerrainData.Shading.AlbedoMap.Apply(); RenderTexture.active = null; terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading); if (!args.ForceUpdateGeometry) { return; } terrain.TerrainData.Geometry.SetRegionDirty(dirtyRect); terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Geometry); }
public void Editor_DrawLivePreview(GStylizedTerrain terrain, GTexturePainterArgs args, Camera cam) { #if UNITY_EDITOR Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Mask.MaskMap; int maskResolution = terrain.TerrainData.Mask.MaskMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(terrain, maskResolution); GCommon.CopyToRT(bg, rt); Material mat = GInternalMaterials.MaskPainterMaterial; mat.SetTexture("_MainTex", bg); SetupTextureGrid(terrain, mat); mat.SetTexture("_Mask", args.BrushMask); mat.SetFloat("_Opacity", args.Opacity); GMaskPainterParams param = GTexturePainterCustomParams.Instance.Mask; Vector4 channel; if (param.Channel == GTextureChannel.R) { channel = new Vector4(1, 0, 0, 0); } else if (param.Channel == GTextureChannel.G) { channel = new Vector4(0, 1, 0, 0); } else if (param.Channel == GTextureChannel.B) { channel = new Vector4(0, 0, 1, 0); } else { channel = new Vector4(0, 0, 0, 1); } mat.SetVector("_Channel", channel); int pass = args.ActionType == GPainterActionType.Normal ? 0 : args.ActionType == GPainterActionType.Negative ? 1 : args.ActionType == GPainterActionType.Alternative ? 2 : 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); GLivePreviewDrawer.DrawMask4ChannelsLivePreview( terrain, cam, rt, GCommon.UnitRect); #endif }
public void Paint(Pinwheel.Griffin.GStylizedTerrain terrain, GTexturePainterArgs args) { if (terrain.TerrainData == null) { return; } if (args.MouseEventType == GPainterMouseEventType.Up) { return; } if (!args.Transform.IsChildOf(terrain.transform)) { return; } if (!(args.Collider is MeshCollider)) { return; } MeshCollider mc = args.Collider as MeshCollider; if (mc.sharedMesh == null) { return; } Mesh m = mc.sharedMesh; Vector2[] uvs = m.uv; int[] tris = m.triangles; int trisIndex = args.TriangleIndex; Vector2 v0 = uvs[tris[trisIndex * 3 + 0]]; Vector2 v1 = uvs[tris[trisIndex * 3 + 1]]; Vector2 v2 = uvs[tris[trisIndex * 3 + 2]]; int albedoMapResolution = terrain.TerrainData.Shading.AlbedoMapResolution; RenderTexture rt = new RenderTexture(albedoMapResolution, albedoMapResolution, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB); GCommon.CopyToRT(terrain.TerrainData.Shading.AlbedoMap, rt); Color c = args.Color; if (args.ActionType == GPainterActionType.Negative) { if (!ColorUtility.TryParseHtmlString(args.CustomArgs, out c)) { c = Color.white; } } GCommon.DrawTriangle(rt, v0, v1, v2, c); RenderTexture.active = rt; terrain.TerrainData.Shading.AlbedoMap.ReadPixels( new Rect(0, 0, albedoMapResolution, albedoMapResolution), 0, 0); terrain.TerrainData.Shading.AlbedoMap.Apply(); RenderTexture.active = null; rt.Release(); Object.DestroyImmediate(rt); terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading); }
public void Paint(GStylizedTerrain terrain, GTexturePainterArgs args) { if (terrain.TerrainData == null) { return; } if (args.MouseEventType == GPainterMouseEventType.Down) { terrain.ForceLOD(0); GGriffinSettings.Instance.IsHidingFoliageOnEditing = true; } if (args.MouseEventType == GPainterMouseEventType.Up) { terrain.ForceLOD(-1); GGriffinSettings.Instance.IsHidingFoliageOnEditing = false; return; } Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Geometry.HeightMap; int heightMapResolution = terrain.TerrainData.Geometry.HeightMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(heightMapResolution); GCommon.CopyToRT(bg, rt); Material mat = GInternalMaterials.VisibilityPainterMaterial; mat.SetTexture("_MainTex", bg); mat.SetTexture("_Mask", args.Mask); mat.SetFloat("_Opacity", args.Opacity); int pass = args.ActionType == GPainterActionType.Normal ? 0 : args.ActionType == GPainterActionType.Negative ? 1 : 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); RenderTexture.active = rt; terrain.TerrainData.Geometry.HeightMap.ReadPixels( new Rect(0, 0, heightMapResolution, heightMapResolution), 0, 0); terrain.TerrainData.Geometry.HeightMap.Apply(); RenderTexture.active = null; terrain.TerrainData.Geometry.SetRegionDirty(dirtyRect); terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Geometry); }
public bool Internal_ApplySplat(GStylizedTerrain t, RenderTexture[] rtControls) { GetQuad(worldPoints); GetUvPoints(t, worldPoints, uvPoints); Rect dirtyRect = GUtilities.GetRectContainsPoints(uvPoints); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return(false); } int brushResolution = t.TerrainData.Shading.SplatControlResolution; RenderTexture[] brushes = RenderBrushes(t, uvPoints, brushResolution); RenderTexture[] bg = new RenderTexture[rtControls.Length]; for (int i = 0; i < bg.Length; ++i) { bg[i] = GetRenderTexture("bg" + i.ToString(), brushResolution); Texture2D splatControl = t.TerrainData.Shading.GetSplatControl(i); GCommon.CopyToRT(splatControl, bg[i]); GCommon.CopyToRT(splatControl, rtControls[i]); } Material paintMaterial = GInternalMaterials.SplatPainterMaterial; paintMaterial.SetFloat("_Opacity", 1); for (int i = 0; i < brushes.Length; ++i) { if (Layers[i].Ignore) { continue; } paintMaterial.SetTexture("_Mask", brushes[i]); int controlMapCount = rtControls.Length; for (int controlIndex = 0; controlIndex < controlMapCount; ++controlIndex) { GCommon.CopyToRT(bg[controlIndex], rtControls[controlIndex]); paintMaterial.SetTexture("_MainTex", bg[controlIndex]); if (Layers[i].SplatIndex / 4 == controlIndex) { paintMaterial.SetInt("_ChannelIndex", Layers[i].SplatIndex % 4); } else { paintMaterial.SetInt("_ChannelIndex", -1); } int pass = 0; GCommon.DrawQuad(rtControls[controlIndex], GCommon.FullRectUvPoints, paintMaterial, pass); GCommon.CopyToRT(rtControls[controlIndex], bg[controlIndex]); } } t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading); return(true); }
private void DoImportSplats() { GSplatPrototypeGroup splatGroup = DesData.Shading.Splats; if (splatGroup == null || splatGroup == GGriffinSettings.Instance.TerrainDataDefault.Shading.Splats) { CreateNewSplatPrototypesGroup = true; } if (CreateNewSplatPrototypesGroup) { splatGroup = ScriptableObject.CreateInstance <GSplatPrototypeGroup>(); #if UNITY_EDITOR string path = AssetDatabase.GetAssetPath(DesData); string directory = Path.GetDirectoryName(path); string filePath = Path.Combine(directory, string.Format("Splats_{0}_{1}.asset", DesData.Id, System.DateTime.Now.Ticks)); AssetDatabase.CreateAsset(splatGroup, filePath); #endif DesData.Shading.Splats = splatGroup; } List <LPTSplatInfo> layers = SrcData.SurfaceSettings.Splats; GSplatPrototype[] prototypes = new GSplatPrototype[layers.Count]; for (int i = 0; i < layers.Count; ++i) { GSplatPrototype p = (GSplatPrototype)layers[i]; prototypes[i] = p; } splatGroup.Prototypes = new List <GSplatPrototype>(prototypes); int controlResolution = SrcData.SurfaceSettings.ControlResolution; DesData.Shading.SplatControlResolution = controlResolution; Texture[] alphaMaps = SrcData.SurfaceSettings.GetSplatControlTextures(); int maxControlCount = Mathf.Min(alphaMaps.Length, DesData.Shading.SplatControlMapCount); RenderTexture rt = new RenderTexture(controlResolution, controlResolution, 0, RenderTextureFormat.ARGB32); for (int i = 0; i < maxControlCount; ++i) { Texture2D controlMap = DesData.Shading.Internal_GetSplatControl(i); GCommon.CopyToRT(alphaMaps[i], rt); GCommon.CopyFromRT(controlMap, rt); controlMap.filterMode = alphaMaps[i].filterMode; } rt.Release(); GUtilities.DestroyObject(rt); DesData.SetDirty(GTerrainData.DirtyFlags.Shading); GC.Collect(); }
public void Editor_DrawLivePreview(GStylizedTerrain terrain, GTexturePainterArgs args, Camera cam) { #if UNITY_EDITOR Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Material mat = GInternalMaterials.SplatPainterMaterial; mat.SetTexture("_Mask", args.Mask); mat.SetFloat("_Opacity", args.Opacity); int controlMapResolution = terrain.TerrainData.Shading.SplatControlResolution; int controlMapCount = terrain.TerrainData.Shading.SplatControlMapCount; for (int i = 0; i < controlMapCount; ++i) { Texture2D splatControl = terrain.TerrainData.Shading.GetSplatControl(i); RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(terrain, controlMapResolution, i); GCommon.CopyToRT(splatControl, rt); mat.SetTexture("_MainTex", splatControl); if (args.SplatIndex / 4 == i) { mat.SetInt("_ChannelIndex", args.SplatIndex % 4); } else { mat.SetInt("_ChannelIndex", -1); } int pass = args.ActionType == GPainterActionType.Normal ? 0 : args.ActionType == GPainterActionType.Negative ? 1 : args.ActionType == GPainterActionType.Alternative ? 2 : 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); } Texture[] controls = new Texture[controlMapCount]; for (int i = 0; i < controlMapCount; ++i) { controls[i] = GTerrainTexturePainter.Internal_GetRenderTexture(terrain, controlMapResolution, i); } GLivePreviewDrawer.DrawSplatLivePreview(terrain, cam, controls, dirtyRect); #endif }
private RenderTexture CloneBg(RenderTexture targetRt) { if (bgRt == null) { bgRt = new RenderTexture(targetRt); } else if (bgRt.width != targetRt.width || bgRt.height != targetRt.height || bgRt.format != targetRt.format) { bgRt.Release(); GUtilities.DestroyObject(bgRt); bgRt = new RenderTexture(targetRt); } GCommon.CopyToRT(targetRt, bgRt); return(bgRt); }
public void Internal_Apply(GStylizedTerrain t, RenderTexture rt) { GCommon.CopyToRT(t.TerrainData.Geometry.HeightMap, rt); Vector3[] worldCorner = GetQuad(); Vector2[] uvCorners = new Vector2[worldCorner.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = t.WorldPointToUV(worldCorner[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Vector3 normalizedPos = t.WorldPointToNormalized(Position); float stampHeight = GUtilities.InverseLerpUnclamped(0, t.TerrainData.Geometry.Height, Scale.y); Material mat = GInternalMaterials.StamperMaterial; mat.SetTexture("_HeightMap", t.TerrainData.Geometry.HeightMap); mat.SetTexture("_Stamp", Stamp); mat.SetTexture("_Falloff", falloffTexture); mat.SetInt("_Operation", (int)Operation); mat.SetFloat("_LerpFactor", LerpFactor); mat.SetFloat("_StampHeight", stampHeight); mat.SetFloat("_StampPositionY", normalizedPos.y); mat.SetFloat("_Inverse", InverseStamp ? 1 : 0); mat.SetFloat("_UseFalloffAsBlendFactor", UseFalloffAsBlendFactor ? 1 : 0); mat.SetFloat("_AdditionalMeshResolution", GCommon.SUB_DIV_STEP * AdditionalMeshResolution); if (EnableTerrainMask) { mat.SetTexture("_TerrainMask", t.TerrainData.Mask.MaskMap); } else { mat.SetTexture("_TerrainMask", Texture2D.blackTexture); } int pass = (int)Channel; GCommon.DrawQuad(rt, uvCorners, mat, pass); }
public void Editor_DrawLivePreview(GStylizedTerrain terrain, GTexturePainterArgs args, Camera cam) { #if UNITY_EDITOR Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Geometry.HeightMap; int heightMapResolution = terrain.TerrainData.Geometry.HeightMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(terrain, heightMapResolution); GCommon.CopyToRT(bg, rt); RenderTexture noiseMap = GTerrainTexturePainter.Internal_GetRenderTexture(terrain, heightMapResolution, 1); RenderNoiseTexture(noiseMap, terrain); Material mat = GInternalMaterials.NoisePainterMaterial; mat.SetTexture("_MainTex", bg); mat.SetTexture("_Mask", args.BrushMask); mat.SetFloat("_Opacity", Mathf.Pow(args.Opacity, GTerrainTexturePainter.GEOMETRY_OPACITY_EXPONENT)); mat.SetTexture("_NoiseMap", noiseMap); if (args.EnableTerrainMask) { mat.SetTexture("_TerrainMask", terrain.TerrainData.Mask.MaskMap); } else { mat.SetTexture("_TerrainMask", Texture2D.blackTexture); } GCommon.SetMaterialKeywordActive(mat, "USE_WORLD_SPACE", GTexturePainterCustomParams.Instance.Noise.UseWorldSpace); int pass = args.ActionType == GPainterActionType.Normal ? 0 : args.ActionType == GPainterActionType.Negative ? 1 : 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); GLivePreviewDrawer.DrawGeometryLivePreview(terrain, cam, rt, dirtyRect); #endif }
public void Editor_DrawLivePreview(GStylizedTerrain terrain, GTexturePainterArgs args, Camera cam) { #if UNITY_EDITOR Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Geometry.HeightMap; int heightMapResolution = terrain.TerrainData.Geometry.HeightMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(terrain, heightMapResolution); GCommon.CopyToRT(bg, rt); Material mat = GInternalMaterials.SubDivPainterMaterial; mat.SetTexture("_MainTex", bg); mat.SetTexture("_Mask", args.BrushMask); mat.SetFloat("_Opacity", Mathf.Pow(args.Opacity, GTerrainTexturePainter.GEOMETRY_OPACITY_EXPONENT)); if (args.EnableTerrainMask) { mat.SetTexture("_TerrainMask", terrain.TerrainData.Mask.MaskMap); } else { mat.SetTexture("_TerrainMask", Texture2D.blackTexture); } int pass = args.ActionType == GPainterActionType.Normal ? 0 : args.ActionType == GPainterActionType.Negative ? 1 : 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); Matrix4x4 worldToMaskMatrix = Matrix4x4.TRS( args.WorldPointCorners[0], Quaternion.Euler(0, args.Rotation, 0), args.Radius * 2 * Vector3.one).inverse; GLivePreviewDrawer.DrawSubdivLivePreview(terrain, cam, rt, dirtyRect, args.BrushMask, worldToMaskMatrix); #endif }
public void Generate(RenderTexture targetRt) { GHeightMapGeneratorParams param = GTextureToolParams.Instance.HeightMap; if (param.Terrain == null || param.Terrain.TerrainData == null) { GCommon.CopyToRT(Texture2D.blackTexture, targetRt); } else { if (param.UseRealGeometry) { RenderGeometryHeightMap(param.Terrain, targetRt); } else { RenderPixelHeightMap(param.Terrain, targetRt); } } }
public void Internal_ApplyAlbedo(GStylizedTerrain t, List <Vector4> worldPoints, RenderTexture rtAlbedo) { GCommon.CopyToRT(t.TerrainData.Shading.AlbedoMap, rtAlbedo); Material mat = GInternalMaterials.PathPainterMaterial; mat.SetTexture("_MainTex", t.TerrainData.Shading.AlbedoMap); mat.SetFloat("_Metallic", Metallic); mat.SetFloat("_Smoothness", Smoothness); mat.SetTexture("_Falloff", falloffTexture); mat.SetColor("_Color", Color); mat.SetTexture("_FalloffNoise", FalloffNoise); mat.SetTextureScale("_FalloffNoise", new Vector2( FalloffNoiseSize.x != 0 ? 1f / FalloffNoiseSize.x : 0, FalloffNoiseSize.y != 0 ? 1f / FalloffNoiseSize.y : 0)); mat.SetTextureOffset("_FalloffNoise", Vector2.zero); int pass = 0; DrawOnTexture(rtAlbedo, mat, pass, worldPoints, t); t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading); }
public void Apply(RenderTexture targetRt, GTextureFilterParams param) { GHydraulicErosionParams erosionParam = param.HydraulicErosion; RenderTexture writeRt = null; //buffer to write to RenderTexture readRt = null; //buffer to read from RenderTexture tmp = null; //temp buffer for swapping CloneBg(targetRt, out writeRt, out readRt); //init height field & water level Mat.SetTexture("_HeightMap", targetRt); GCommon.DrawQuad(writeRt, GCommon.FullRectUvPoints, Mat, 0); GCommon.DrawQuad(readRt, GCommon.FullRectUvPoints, Mat, 0); //simulate for (int i = 0; i < erosionParam.Iteration; ++i) { Mat.SetTexture("_HeightMap", readRt); Mat.SetVector("_Dimension", erosionParam.Dimension); Mat.SetFloat("_Rain", erosionParam.Rain); Mat.SetFloat("_Transportation", erosionParam.Transportation); Mat.SetFloat("_MinAngle", erosionParam.AngleMin); Mat.SetFloat("_Evaporation", erosionParam.Evaporation); Mat.SetTexture("_WaterSourceMap", erosionParam.WaterSourceMap); Mat.SetTexture("_HardnessMap", erosionParam.HardnessMap); //Mat.SetFloat("_Seed", Random.value); GCommon.DrawQuad(writeRt, GCommon.FullRectUvPoints, Mat, 1); //swap buffer tmp = readRt; readRt = writeRt; writeRt = tmp; } //copy result GCommon.CopyToRT(writeRt, targetRt); }
public void Paint(Pinwheel.Griffin.GStylizedTerrain terrain, GTexturePainterArgs args) { if (terrain.TerrainData == null) { return; } if (args.MouseEventType == GPainterMouseEventType.Down) { terrain.ForceLOD(0); GRuntimeSettings.Instance.isEditingGeometry = true; } if (args.MouseEventType == GPainterMouseEventType.Up) { terrain.ForceLOD(-1); GRuntimeSettings.Instance.isEditingGeometry = false; terrain.UpdateTreesPosition(); terrain.UpdateGrassPatches(); terrain.TerrainData.Foliage.ClearTreeDirtyRegions(); terrain.TerrainData.Foliage.ClearGrassDirtyRegions(); return; } Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Geometry.HeightMap; int heightMapResolution = terrain.TerrainData.Geometry.HeightMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(heightMapResolution); GCommon.CopyToRT(bg, rt); Texture2D remapTex = GCommon.CreateTextureFromCurve(GTexturePainterCustomParams.Instance.Remap.Curve, 512, 1); Material mat = GInternalMaterials.RemapPainterMaterial; mat.SetTexture("_MainTex", bg); mat.SetTexture("_Mask", args.BrushMask); mat.SetFloat("_Opacity", Mathf.Pow(args.Opacity, GTerrainTexturePainter.GEOMETRY_OPACITY_EXPONENT)); mat.SetTexture("_RemapTex", remapTex); if (args.EnableTerrainMask) { mat.SetTexture("_TerrainMask", terrain.TerrainData.Mask.MaskMap); } else { mat.SetTexture("_TerrainMask", Texture2D.blackTexture); } int pass = 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); RenderTexture.active = rt; terrain.TerrainData.Geometry.HeightMap.ReadPixels( new Rect(0, 0, heightMapResolution, heightMapResolution), 0, 0); terrain.TerrainData.Geometry.HeightMap.Apply(); RenderTexture.active = null; GUtilities.DestroyObject(remapTex); terrain.TerrainData.Geometry.SetRegionDirty(dirtyRect); terrain.TerrainData.Foliage.SetTreeRegionDirty(dirtyRect); terrain.TerrainData.Foliage.SetGrassRegionDirty(dirtyRect); terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Geometry); }
public void Paint(Pinwheel.Griffin.GStylizedTerrain terrain, GTexturePainterArgs args) { if (terrain.TerrainData == null) { return; } if (args.ActionType != GPainterActionType.Normal) { terrain.ForceLOD(-1); return; } if (args.MouseEventType == GPainterMouseEventType.Down) { terrain.ForceLOD(0); GGriffinSettings.Instance.IsHidingFoliageOnEditing = true; } if (args.MouseEventType == GPainterMouseEventType.Up) { GGriffinSettings.Instance.IsHidingFoliageOnEditing = false; terrain.UpdateTreesPosition(); terrain.UpdateGrassPatches(); terrain.TerrainData.Foliage.ClearTreeDirtyRegions(); terrain.TerrainData.Foliage.ClearGrassDirtyRegions(); return; } Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Geometry.HeightMap; int heightMapResolution = terrain.TerrainData.Geometry.HeightMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(heightMapResolution); GCommon.CopyToRT(bg, rt); Vector3 localSamplePoint = terrain.transform.InverseTransformPoint(args.SamplePoint); Material mat = GInternalMaterials.HeightSamplingPainterMaterial; mat.SetTexture("_MainTex", bg); mat.SetTexture("_Mask", args.Mask); mat.SetFloat("_Opacity", args.Opacity); mat.SetFloat("_TargetGray", Mathf.InverseLerp(0, terrain.TerrainData.Geometry.Height, localSamplePoint.y)); int pass = 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); RenderTexture.active = rt; terrain.TerrainData.Geometry.HeightMap.ReadPixels( new Rect(0, 0, heightMapResolution, heightMapResolution), 0, 0); terrain.TerrainData.Geometry.HeightMap.Apply(); RenderTexture.active = null; terrain.TerrainData.Geometry.SetRegionDirty(dirtyRect); terrain.TerrainData.Foliage.SetTreeRegionDirty(dirtyRect); terrain.TerrainData.Foliage.SetGrassRegionDirty(dirtyRect); terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Geometry); }
public void Paint(Pinwheel.Griffin.GStylizedTerrain terrain, GTexturePainterArgs args) { if (terrain.TerrainData == null) { return; } if (terrain.TerrainData.Shading.Splats == null) { return; } if (args.MouseEventType == GPainterMouseEventType.Up) { return; } Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Material mat = GInternalMaterials.SplatPainterMaterial; mat.SetTexture("_Mask", args.Mask); mat.SetFloat("_Opacity", args.Opacity); int controlMapResolution = terrain.TerrainData.Shading.SplatControlResolution; int controlMapCount = terrain.TerrainData.Shading.SplatControlMapCount; for (int i = 0; i < controlMapCount; ++i) { Texture2D splatControl = terrain.TerrainData.Shading.GetSplatControl(i); RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(controlMapResolution); GCommon.CopyToRT(splatControl, rt); mat.SetTexture("_MainTex", splatControl); if (args.SplatIndex / 4 == i) { mat.SetInt("_ChannelIndex", args.SplatIndex % 4); } else { mat.SetInt("_ChannelIndex", -1); } int pass = args.ActionType == GPainterActionType.Normal ? 0 : args.ActionType == GPainterActionType.Negative ? 1 : args.ActionType == GPainterActionType.Alternative ? 2 : 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); RenderTexture.active = rt; splatControl.ReadPixels(new Rect(0, 0, controlMapResolution, controlMapResolution), 0, 0); splatControl.Apply(); RenderTexture.active = null; } terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading); if (!args.ForceUpdateGeometry) { return; } terrain.TerrainData.Geometry.SetRegionDirty(dirtyRect); terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Geometry); }
public bool Internal_ApplyAlbedoMetallicSmoothness(GStylizedTerrain t, RenderTexture albedoRt, RenderTexture metallicRt) { GetQuad(worldPoints); GetUvPoints(t, worldPoints, uvPoints); Rect dirtyRect = GUtilities.GetRectContainsPoints(uvPoints); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return(false); } int brushResolution = Mathf.Max(t.TerrainData.Shading.AlbedoMapResolution, t.TerrainData.Shading.MetallicMapResolution); RenderTexture[] brushes = RenderBrushes(t, uvPoints, brushResolution); RenderTexture bg0 = GetRenderTexture("bg0", brushResolution); Texture2D albedoMap = t.TerrainData.Shading.AlbedoMap; GCommon.CopyToRT(albedoMap, bg0); GCommon.CopyToRT(albedoMap, albedoRt); Material mat = GInternalMaterials.AlbedoPainterMaterial; mat.SetFloat("_Opacity", 1); for (int i = 0; i < brushes.Length; ++i) { if (Layers[i].Ignore) { continue; } mat.SetTexture("_MainTex", bg0); mat.SetTexture("_Mask", brushes[i]); mat.SetColor("_Color", Layers[i].Color); int pass = 0; GCommon.DrawQuad(albedoRt, GCommon.FullRectUvPoints, mat, pass); GCommon.CopyToRT(albedoRt, bg0); } RenderTexture bg1 = GetRenderTexture("bg1", brushResolution); Texture2D metallicMap = t.TerrainData.Shading.MetallicMap; GCommon.CopyToRT(metallicMap, bg1); GCommon.CopyToRT(metallicMap, metallicRt); mat = GInternalMaterials.MetallicPainterMaterial; mat.SetFloat("_Opacity", 1); for (int i = 0; i < brushes.Length; ++i) { if (Layers[i].Ignore) { continue; } mat.SetTexture("_MainTex", bg1); mat.SetTexture("_Mask", brushes[i]); mat.SetColor("_Color", Color.white * Layers[i].Metallic); int pass = 3; //fragSet GCommon.DrawQuad(metallicRt, GCommon.FullRectUvPoints, mat, pass); GCommon.CopyToRT(metallicRt, bg1); } mat = GInternalMaterials.SmoothnessPainterMaterial; mat.SetFloat("_Opacity", 1); for (int i = 0; i < brushes.Length; ++i) { if (Layers[i].Ignore) { continue; } mat.SetTexture("_MainTex", bg1); mat.SetTexture("_Mask", brushes[i]); mat.SetColor("_Color", Color.white * Layers[i].Smoothness); int pass = 3; //fragSet GCommon.DrawQuad(metallicRt, GCommon.FullRectUvPoints, mat, pass); GCommon.CopyToRT(metallicRt, bg1); } t.TerrainData.SetDirty(GTerrainData.DirtyFlags.Shading); return(true); }
public void Paint(GStylizedTerrain terrain, GTexturePainterArgs args) { if (terrain.TerrainData == null) { return; } if (args.MouseEventType == GPainterMouseEventType.Down) { } if (args.MouseEventType == GPainterMouseEventType.Up) { return; } Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Mask.MaskMap; int maskResolution = terrain.TerrainData.Mask.MaskMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(maskResolution); GCommon.CopyToRT(bg, rt); Material mat = GInternalMaterials.MaskPainterMaterial; mat.SetTexture("_MainTex", bg); SetupTextureGrid(terrain, mat); mat.SetTexture("_Mask", args.BrushMask); mat.SetFloat("_Opacity", args.Opacity); GMaskPainterParams param = GTexturePainterCustomParams.Instance.Mask; Vector4 channel; if (param.Channel == GTextureChannel.R) { channel = new Vector4(1, 0, 0, 0); } else if (param.Channel == GTextureChannel.G) { channel = new Vector4(0, 1, 0, 0); } else if (param.Channel == GTextureChannel.B) { channel = new Vector4(0, 0, 1, 0); } else { channel = new Vector4(0, 0, 0, 1); } mat.SetVector("_Channel", channel); int pass = args.ActionType == GPainterActionType.Normal ? 0 : args.ActionType == GPainterActionType.Negative ? 1 : args.ActionType == GPainterActionType.Alternative ? 2 : 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); RenderTexture.active = rt; terrain.TerrainData.Mask.MaskMap.ReadPixels( new Rect(0, 0, maskResolution, maskResolution), 0, 0); terrain.TerrainData.Mask.MaskMap.Apply(); RenderTexture.active = null; if (!args.ForceUpdateGeometry) { return; } terrain.TerrainData.Geometry.SetRegionDirty(dirtyRect); terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Geometry); }
public void Paint(Pinwheel.Griffin.GStylizedTerrain terrain, GTexturePainterArgs args) { if (terrain.TerrainData == null) { return; } if (args.MouseEventType == GPainterMouseEventType.Down) { terrain.ForceLOD(0); GGriffinSettings.Instance.IsHidingFoliageOnEditing = true; } if (args.MouseEventType == GPainterMouseEventType.Up) { terrain.ForceLOD(-1); GGriffinSettings.Instance.IsHidingFoliageOnEditing = false; terrain.UpdateTreesPosition(); terrain.UpdateGrassPatches(); terrain.TerrainData.Foliage.ClearTreeDirtyRegions(); terrain.TerrainData.Foliage.ClearGrassDirtyRegions(); return; } Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length]; for (int i = 0; i < uvCorners.Length; ++i) { uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]); } Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners); if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1))) { return; } Texture2D bg = terrain.TerrainData.Geometry.HeightMap; int heightMapResolution = terrain.TerrainData.Geometry.HeightMapResolution; RenderTexture rt = GTerrainTexturePainter.Internal_GetRenderTexture(heightMapResolution); GCommon.CopyToRT(bg, rt); RenderTexture noiseMap = GTerrainTexturePainter.Internal_GetRenderTexture(terrain, heightMapResolution, 1); RenderNoiseTexture(noiseMap, terrain); Material mat = GInternalMaterials.NoisePainterMaterial; mat.SetTexture("_MainTex", bg); mat.SetTexture("_Mask", args.Mask); mat.SetFloat("_Opacity", args.Opacity); mat.SetTexture("_NoiseMap", noiseMap); GCommon.SetMaterialKeywordActive(mat, "USE_WORLD_SPACE", GTexturePainterCustomParams.Instance.Noise.UseWorldSpace); int pass = args.ActionType == GPainterActionType.Normal ? 0 : args.ActionType == GPainterActionType.Negative ? 1 : 0; GCommon.DrawQuad(rt, uvCorners, mat, pass); RenderTexture.active = rt; terrain.TerrainData.Geometry.HeightMap.ReadPixels( new Rect(0, 0, heightMapResolution, heightMapResolution), 0, 0); terrain.TerrainData.Geometry.HeightMap.Apply(); RenderTexture.active = null; terrain.TerrainData.Geometry.SetRegionDirty(dirtyRect); terrain.TerrainData.Foliage.SetTreeRegionDirty(dirtyRect); terrain.TerrainData.Foliage.SetGrassRegionDirty(dirtyRect); terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Geometry); }