{"TotalCount":15,"Files":[{"Ident":"stellawisps.scenefoliagepainter","Path":"Assembly.cs","FileName":"Assembly.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":74752,"IsPrivate":false,"Code":"global using Sandbox;\r\nglobal using System.Collections.Generic;\r\nglobal using System.Linq;\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"UnitTests/LibraryTest.cs","FileName":"LibraryTest.cs","PackageType":"library","CodeKind":"UnitTest","AssetVersionId":74752,"IsPrivate":false,"Code":"using Sandbox;\r\n\r\n[TestClass]\r\npublic partial class LibraryTests\r\n{\r\n\t[TestMethod]\r\n\tpublic void SceneTest()\r\n\t{\r\n\t\tvar scene = new Scene();\r\n\t\tusing ( scene.Push() )\r\n\t\t{\r\n\t\t\tvar go = new GameObject();\r\n\r\n\t\t\tAssert.AreEqual( 1, scene.Directory.GameObjectCount );\r\n\t\t}\r\n\t}\r\n\r\n}\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"Code/Assembly.cs","FileName":"Assembly.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":74752,"IsPrivate":false,"Code":"global using Sandbox;\r\nglobal using System.Collections.Generic;\r\nglobal using System.Linq;\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":".obj/__compiler_extra.cs","FileName":"__compiler_extra.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":74752,"IsPrivate":false,"Code":"global using static Sandbox.Internal.GlobalGameNamespace;\r\n[assembly: global::System.Reflection.AssemblyMetadata( \u0022AddonTitle\u0022, \u0022Scene Foliage\u0022 )]\r\n[assembly: global::System.Reflection.AssemblyMetadata( \u0022AddonIdent\u0022, \u0022scenefoliagepainter\u0022 )]\r\n[assembly: global::System.Reflection.AssemblyMetadata( \u0022OrgIdent\u0022, \u0022stellawisps\u0022 )]\r\n[assembly: global::System.Reflection.AssemblyMetadata( \u0022Ident\u0022, \u0022stellawisps.scenefoliagepainter\u0022 )]\r\n[assembly: global::System.Reflection.AssemblyMetadata( \u0022CompileTime\u0022, \u002210/15/2024 1:16:41 AM\u0022 )]\r\n[assembly: global::System.Reflection.AssemblyMetadata( \u0022EngineVersion\u0022, \u002217\u0022 )]\r\n[assembly: global::System.Reflection.AssemblyMetadata( \u0022EngineMinorVersion\u0022, \u00221\u0022 )]\r\n\r\n[assembly: System.Runtime.Versioning.TargetFramework( \u0022.NETCoreApp,Version=v7.0\u0022, FrameworkDisplayName = \u0022.NET 7.0\u0022 )]\r\n[assembly: global::System.Reflection.AssemblyVersion(\u00220.0.147.0\u0022)]\r\n[assembly: global::System.Reflection.AssemblyFileVersion(\u00220.0.147.0\u0022)]"},{"Ident":"stellawisps.scenefoliagepainter","Path":"Editor/Assembly.cs","FileName":"Assembly.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":74752,"IsPrivate":false,"Code":"global using Sandbox;\r\nglobal using Editor;\r\nglobal using System.Collections.Generic;\r\nglobal using System.Linq;\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"Editor/Brush.cs","FileName":"Brush.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":74752,"IsPrivate":false,"Code":"using Foliage;\r\n\r\nnamespace Editor.FoliagePainter;\r\n\r\n\r\n/// \u003Csummary\u003E\r\n/// Brushes you can use\r\n/// \u003C/summary\u003E\r\npublic class FoliageList\r\n{\r\n\tpublic FoliageResource Selected { get; set; }\r\n\tpublic List\u003CFoliageResource\u003E AllFoliage = new();\r\n\r\n\tpublic FoliageList()\r\n\t{\r\n\t\tLoadAll();\r\n\t}\r\n\r\n\tpublic void LoadAll()\r\n\t{\r\n\t\tAllFoliage = FoliageResource.All.ToList();\r\n\t\t\r\n\r\n\t\tSelected = AllFoliage.FirstOrDefault();\r\n\t}\r\n}\r\n\r\npublic class FoliageType\r\n{\r\n\tpublic string Name { get; private set; }\r\n\tpublic Texture Texture { get; private set; }\r\n\tpublic Pixmap Pixmap { get; private set; }\r\n\r\n\tpublic void Set( string name )\r\n\t{\r\n\t\tTexture = Texture.Load( FileSystem.Mounted, $\u0022materials/tools/terrain/brushes/{name}.png\u0022 );\r\n\t}\r\n\r\n\tinternal static FoliageType LoadFromFile( string filename )\r\n\t{\r\n\t\tvar foliageType = new FoliageType();\r\n\t\tfoliageType.Name = System.IO.Path.GetFileNameWithoutExtension( filename );\r\n\t\tfoliageType.Texture = Texture.Load( FileSystem.Content, filename );\r\n\t\tfoliageType.Pixmap = Pixmap.FromFile( FileSystem.Content.GetFullPath( filename ) );\r\n\t\treturn foliageType;\r\n\t}\r\n}\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"Editor/FoliageRendererButtons.cs","FileName":"FoliageRendererButtons.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":74752,"IsPrivate":false,"Code":"using Foliage;\r\n\r\nnamespace Editor.FoliagePainter;\r\n\r\n\tusing Editor;\r\nusing Sandbox;\r\n\tusing System;\r\nusing System.IO;\r\nusing System.Linq;\r\n\r\n\r\n[Sandbox.CustomEditor( typeof( FoliageRenderer.OptionsWidget ) )]\r\npublic class FoliageRendererButtons : ControlWidget\r\n{\r\n\tFoliageRenderer FoliageManager { get; set; }\r\n\t\r\n\tpublic FoliageRendererButtons( Sandbox.SerializedProperty property ) : base( property )\r\n    {\r\n        FoliageManager = property.Parent.Targets.First() as FoliageRenderer;\r\n\r\n        Layout = Layout.Column();\r\n        Layout.Spacing = 2;\r\n\r\n        Rebuild();\r\n    }\r\n\r\n    [EditorEvent.Hotload]\r\n    void Rebuild()\r\n    {\r\n        Layout.Clear( true );\r\n\r\n        {\r\n            var button = new Button( this );\r\n            button.Text = \u0022Delete All Foliage\u0022;\r\n            button.ToolTip = \u0022Removes all foliage from this renderer\u0022;\r\n            button.Clicked \u002B= ResetResource;\r\n            Layout.Add( button );\r\n        }\r\n        \r\n        {\r\n\t        var button = new Button( this );\r\n\t        button.Text = \u0022Update Foliage\u0022;\r\n\t        button.ToolTip = \u0022Update all foliage renderers if a model was changed in a .fol asset\u0022;\r\n\t        button.Clicked \u002B= UpdateResource;\r\n\t        Layout.Add( button );\r\n        }\r\n\r\n    }\r\n\r\n    void ResetResource()\r\n    {\r\n\t    FoliageManager.ClearAll();\r\n        Rebuild();\r\n    }\r\n\r\n    void UpdateResource()\r\n    {\r\n\t    FoliageManager.UpdateRenderers();\r\n    }\r\n    \r\n}\r\n\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"Editor/FoliageSettings.cs","FileName":"FoliageSettings.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":74752,"IsPrivate":false,"Code":"using System;\r\nusing Foliage;\r\n\r\nnamespace Editor.FoliagePainter;\r\n\r\npublic class FoliageSettings\r\n{\r\n\t[Property, Range( 8, 512 )] public int Size { get; set; } = 50;\r\n\t[Property, Range( 0, 100 )] public float PaintSpeed { get; set; } = 5;\r\n\t[Property, ResourceType(\u0022.fol\u0022)] public FoliageResource Foliage { get; set; }\r\n\t[Property] public bool EraseOnlySelectedFoliage { get; set; } = true;\r\n}\r\n\r\npublic class FoliageSettingsWidgetWindow : WidgetWindow\r\n{\r\n\tclass FoliageSelectedWidget : Widget\r\n\t{\r\n\t\tpublic FoliageSelectedWidget( Widget parent ) : base( parent )\r\n\t\t{\r\n\t\t\tMinimumSize = new( 48, 48 );\r\n\t\t\tCursor = CursorShape.Finger;\r\n\t\t}\r\n\r\n\t\tprotected override void OnMouseClick( MouseEvent e )\r\n\t\t{\r\n\t\t\tvar popup = new PopupWidget( null );\r\n\t\t\tpopup.Position = Application.CursorPosition;\r\n\t\t\tpopup.Visible = true;\r\n\t\t\tpopup.Layout = Layout.Column();\r\n\t\t\tpopup.Layout.Margin = 10;\r\n\t\t\tpopup.MaximumSize = new Vector2( 300, 150 );\r\n\t\t}\r\n\t}\r\n\r\n\tpublic FoliageSettingsWidgetWindow( Widget parent, SerializedObject so ) : base( parent, \u0022Foliage Settings\u0022 )\r\n\t{\r\n\t\tLayout = Layout.Row();\r\n\t\tLayout.Margin = 4;\r\n\t\tMaximumWidth = 300.0f;\r\n\r\n\t\tvar cs = new ControlSheet();\r\n\t\tcs.AddRow( so.GetProperty( nameof( FoliageSettings.Size ) ) );\r\n\t\tcs.AddRow( so.GetProperty( nameof( FoliageSettings.PaintSpeed) ) );\r\n\t\tcs.AddRow( so.GetProperty( nameof( FoliageSettings.Foliage ) ) );\r\n\t\tcs.AddRow( so.GetProperty( nameof( FoliageSettings.EraseOnlySelectedFoliage ) ) );\r\n\t\tcs.SetMinimumColumnWidth( 0, 50 );\r\n\t\tcs.Margin = new Sandbox.UI.Margin( 8, 0, 4, 0 );\r\n\t\t\r\n\t\tvar text = Layout.Column( );\r\n\t\ttext.Add( new Label.Body( \u0022LMB = paint\u0022 ) );\r\n\t\ttext.Add( new Label.Body( \u0022shift\u002BLMB = erase\u0022 ));\r\n\t\ttext.Alignment = TextFlag.LeftBottom;\r\n\t\ttext.Margin = new Sandbox.UI.Margin( 16, 6, 4, 0 );\r\n\t\t\r\n\t\tvar l = Layout.Column();\r\n\t\tl.Add( cs );\r\n\t\tl.Add( text );\r\n\t\tLayout.Add( l );\r\n\t\t\r\n\t}\r\n}\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"FoliageRenderer.cs","FileName":"FoliageRenderer.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":74752,"IsPrivate":false,"Code":"using System;\r\nusing System.Text.Json.Serialization;\r\nusing Sandbox.Engine.Settings;\r\n\r\nnamespace Foliage;\r\n\r\n\r\npublic class FoliageRenderer : Component, Component.ExecuteInEditor\r\n{\r\n\tpublic List\u003CFoliageSceneObject\u003E Renderers { get; set; } = new();\r\n\t[Property,Hide] Dictionary\u003Cint,List\u003CTransform\u003E\u003E FoliageRenderers { get; set; } = new();\r\n\t[Property] private int ChunkSize { get; set; } = 512;\r\n\t[Property] OptionsWidget Options { get; set; }\r\n\tpublic void PaintFoliage(FoliageResource foliage, Transform transform)\r\n\t{\r\n\t\tif ( FoliageRenderers.TryGetValue(foliage.ResourceId, out var renderer) )\r\n\t\t{\r\n\t\t\trenderer.Add( transform );\r\n\t\t\tUpdateRenderers();\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tFoliageRenderers.Add( foliage.ResourceId, new List\u003CTransform\u003E { transform } );\r\n\t\t\tFoliageRenderers[foliage.ResourceId].Add( transform );\r\n\t\t\tUpdateRenderers();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void EraseFoliage( Vector3 position,float size, int id )\r\n\t{\r\n\t\tList\u003CTransform\u003E toRemove = new();\r\n\r\n\t\tif ( id != 0 )\r\n\t\t{\r\n\t\t\tforeach ( var testPos in FoliageRenderers[id] )\r\n\t\t\t{\r\n\t\t\t\tif ( testPos.Position.DistanceSquared( position ) \u003C= size * size )\r\n\t\t\t\t{\r\n\t\t\t\t\ttoRemove.Add( testPos );\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tforeach ( var remove in toRemove )\r\n\t\t\t{\r\n\t\t\t\tFoliageRenderers[id].Remove( remove );\r\n\t\t\t}\r\n\t\t\treturn;\r\n\t\t}\r\n\t\t\r\n\t\tforeach ( var testVal in FoliageRenderers )\r\n\t\t{\r\n\t\t\tforeach ( var testPos in testVal.Value )\r\n\t\t\t{\r\n\t\t\t\tif ( testPos.Position.DistanceSquared( position ) \u003C= size * size )\r\n\t\t\t\t{\r\n\t\t\t\t\ttoRemove.Add( testPos );\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tforeach ( var remove in toRemove )\r\n\t\t\t{\r\n\t\t\t\ttestVal.Value.Remove( remove );\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t}\r\n\r\n\tprotected override void OnStart()\r\n\t{\r\n\t\tUpdateRenderers();\r\n\t\t\r\n\t\t\r\n\t}\r\n\r\n\tprotected override void OnEnabled()\r\n\t{\r\n\t\tbase.OnEnabled();\r\n\t\tUpdateRenderers();\r\n\t}\r\n\r\n\tprotected override void OnDestroy()\r\n\t{\r\n\t\tforeach ( var val in Renderers )\r\n\t\t{\r\n\t\t\tval.Delete();\r\n\t\t}\r\n\t\tbase.OnDestroy();\r\n\t}\r\n\r\n\tprotected override void OnDisabled()\r\n\t{\r\n\t\tforeach ( var val in Renderers )\r\n\t\t{\r\n\t\t\tval.Delete();\r\n\t\t}\r\n\t\tbase.OnDisabled();\r\n\t\t\r\n\t}\r\n\r\n\tpublic void UpdateRenderers()\r\n\t{\r\n\t\tforeach ( var val in Renderers )\r\n\t\t{\r\n\t\t\tval.Delete();\r\n\t\t\t\r\n\t\t}\r\n\r\n\t\t\r\n\t\t\r\n\t\tRenderers.Clear();\r\n\r\n\t\tforeach ( var folRenderer in FoliageRenderers )\r\n\t\t{\r\n\t\t\tvar transformMins = new Vector3( 0 );\r\n\t\t\tvar transformMaxs = new Vector3( 0 );\r\n\r\n\t\t\tforeach ( var testTrans in folRenderer.Value )\r\n\t\t\t{\r\n\t\t\t\ttransformMins = Vector3.Min( transformMins, testTrans.Position );\r\n\t\t\t\ttransformMaxs = Vector3.Max( transformMaxs, testTrans.Position );\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tvar transformSize = transformMaxs - transformMins;\r\n\r\n\t\t\tvar transformArray = folRenderer.Value.ToArray();\r\n\t\t\tvar folInstance = ResourceLibrary.Get\u003CFoliageResource\u003E( folRenderer.Key );\r\n\r\n\t\t\t\r\n\t\t\tfor ( int x = 0; x \u003C (transformSize.x / ChunkSize).CeilToInt(); x\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tfor ( int y = 0; y \u003C (transformSize.y / ChunkSize).CeilToInt(); y\u002B\u002B )\r\n\t\t\t\t{\r\n\t\t\t\t\tList\u003CTransform\u003E newTransforms = new();\r\n\t\t\t\t\tvar transformedMins = transformMins \u002B new Vector3( (x * ChunkSize)\u002BChunkSize, (y * ChunkSize)\u002BChunkSize, 10000 );\r\n\t\t\t\t\tvar testBBox = new BBox( transformMins\u002B new Vector3( x * ChunkSize, y * ChunkSize, -500 ),transformedMins );\r\n\t\t\t\t\tforeach ( var testTrans in folRenderer.Value )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif ( testBBox.Contains( testTrans.Position ) )\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tnewTransforms.Add( testTrans );\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\r\n\t\t\t\t\tif ( newTransforms.Count \u003C= 0 )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tcontinue;\r\n\t\t\t\t\t}\r\n\r\n\t\t\t\t\tvar renderModelLargestAxis = folInstance.Model.Bounds.Size;\r\n\r\n\t\t\t\t\tvar boundsMin = Vector3.Zero;\r\n\t\t\t\t\tvar boundsMax = Vector3.Zero;\r\n\t\t\t\t\tfor ( int i = 0; i \u003C newTransforms.Count; i\u002B\u002B )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tboundsMin = Vector3.Min( boundsMin, newTransforms[i].Position);\r\n\t\t\t\t\t\tboundsMax = Vector3.Max( boundsMax, newTransforms[i].Position);\r\n\t\t\t\t\t}\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\r\n\r\n\t\t\t\t\t\r\n\t\t\t\t\tvar folSceneObject = new FoliageSceneObject( GameObject.Scene.SceneWorld, folInstance.Model )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tBounds = new BBox( boundsMin-renderModelLargestAxis,boundsMax\u002BrenderModelLargestAxis ),\r\n\t\t\t\t\t\tTransforms = newTransforms.ToArray()\r\n\t\t\t\t\t};\r\n\t\t\t\t\tfolSceneObject.Flags.CastShadows = true;\r\n\t\t\t\r\n\t\t\t\t\tRenderers.Add( folSceneObject );\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\t\r\n\r\n\t\t\t\r\n\r\n\r\n\r\n\r\n\t\t}\r\n\t\t\r\n\t}\r\n\tpublic void ClearAll()\r\n\t{\r\n\t\tforeach ( var val in FoliageRenderers )\r\n\t\t{\r\n\t\t\tval.Value.Clear();\r\n\t\t}\r\n\t\tUpdateRenderers();\r\n\t}\r\n\t\r\n\tpublic class OptionsWidget { }\r\n}\r\npublic class FoliageSceneObject : SceneCustomObject\r\n{\r\n\tpublic Model RenderModel;\r\n\tpublic Transform[] Transforms = Array.Empty\u003CTransform\u003E();\r\n\tpublic FoliageSceneObject(SceneWorld sceneWorld, Model renderModel) : base(sceneWorld)\r\n\t{\r\n\t\tRenderModel = renderModel;\r\n\t}\r\n\r\n\t\r\n\tpublic void AddTransform(Transform transform)\r\n\t{\r\n\t\t//Transforms.Add( transform );\r\n\t}\r\n\r\n\tpublic override void RenderSceneObject()\r\n\t{\r\n\t\tbase.RenderSceneObject();\r\n\t\tGraphics.DrawModelInstanced( RenderModel, Transforms.AsSpan() );\r\n\t\t\r\n\t\t\r\n\t}\r\n}\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"UnitTests/UnitTest.cs","FileName":"UnitTest.cs","PackageType":"library","CodeKind":"UnitTest","AssetVersionId":74752,"IsPrivate":false,"Code":"global using Microsoft.VisualStudio.TestTools.UnitTesting;\r\n\r\n[TestClass]\r\npublic class TestInit\r\n{\r\n\t[AssemblyInitialize]\r\n\tpublic static void ClassInitialize( TestContext context )\r\n\t{\r\n\t\tSandbox.Application.InitUnitTest();\r\n\t}\r\n}\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"Editor/FoliagePainter.cs","FileName":"FoliagePainter.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":74752,"IsPrivate":false,"Code":"using System;\r\n\r\nnamespace Editor.FoliagePainter;\r\nusing Foliage;\r\n[EditorTool] // this class is an editor tool\r\n[Title( \u0022Foliage\u0022 )] // title of your tool\r\n[Icon( \u0022grass\u0022 )] // icon name from https://fonts.google.com/icons?selected=Material\u002BIcons\r\npublic class FoliagePainter : EditorTool\r\n{\r\n\tprivate float PaintProgress { get; set; } = 0f;\r\n\tpublic FoliageSettings FoliageSettings { get; private set; } = new();\r\n\tpublic override void OnEnabled()\r\n\t{\r\n\t\tAllowGameObjectSelection = false;\r\n\t\tif ( GetSelectedComponent\u003CFoliageRenderer\u003E() == null )\r\n\t\t{\r\n\t\t\tSelection.Clear();\r\n\t\t\tvar first = Scene.GetAllComponents\u003CFoliageRenderer\u003E().FirstOrDefault();\r\n\t\t\tif ( first is not null ) Selection.Add( first.GameObject );\r\n\t\t}\r\n\t\t\r\n\t\tvar foliageSettings = new FoliageSettingsWidgetWindow( SceneOverlay, EditorUtility.GetSerializedObject( FoliageSettings ) );\r\n\t\tAddOverlay( foliageSettings, TextFlag.RightBottom, 10 );\r\n\t}\r\n\r\n\tprivate bool Painted { get; set; } = false;\r\n\r\n\r\n\tprivate void Paint(SceneTraceResult tr)\r\n\t{\r\n\r\n\t\tvar paintTarget = GetSelectedComponent\u003CFoliageRenderer\u003E();\r\n\t\tif ( paintTarget == null ) { return; }\r\n\t\t\t\t\t\r\n\t\tvar randomPos =\r\n\t\t\tnew Vector3( Random.Shared.Float( FoliageSettings.Size * -1, FoliageSettings.Size ),\r\n\t\t\t\tRandom.Shared.Float( FoliageSettings.Size * -1, FoliageSettings.Size ), FoliageSettings.Size ).RotateAround( Vector3.Zero,\r\n\t\t\t\tRotation.LookAt( tr.Normal ) * Rotation.FromPitch( 90f ) );\r\n\r\n\t\trandomPos = randomPos.ClampLength( FoliageSettings.Size );\r\n\t\t\t\t\t\r\n\t\tvar paintTr = Scene.Trace.Ray( new Ray( tr.HitPosition \u002B randomPos,Rotation.LookAt( tr.Normal ) * Rotation.FromPitch( 90f ).Down ), FoliageSettings.Size\u002B2 )\r\n\t\t\t.UseRenderMeshes( true )\r\n\t\t\t.UsePhysicsWorld( false )\r\n\t\t\t.WithoutTags( \u0022invisible\u0022 )\r\n\t\t\t.Run();\r\n\r\n\t\tif ( paintTr.Hit )\r\n\t\t{\r\n\t\t\tif ( 1-((paintTr.Normal.Dot( new Vector3( 0, 0, 1 ) ) \u002B 1f) / 2f) \u003E FoliageSettings.Foliage.MaxNormal / 180 )\r\n\t\t\t{\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\r\n\t\t\tvar randomRot =\r\n\t\t\t\tRotation.FromYaw( FoliageSettings.Foliage.RandomRoll ? Random.Shared.Float( 0, 360 ) : 0 );\r\n\t\t\tif ( FoliageSettings.Foliage.RandomRotation )\r\n\t\t\t{\r\n\t\t\t\trandomRot = Rotation.Random;\r\n\t\t\t}\r\n\t\t\t\t\t\t\r\n\t\t\tvar transform = new Transform( paintTr.HitPosition\u002B(tr.Normal* (FoliageSettings.Foliage.ZOffset.GetValue())) );\r\n\t\t\tif ( FoliageSettings.Foliage.AlignToNormal )\r\n\t\t\t{\r\n\t\t\t\ttransform = transform.WithRotation( Rotation.LookAt( paintTr.Normal )*Rotation.FromPitch( 90f )*randomRot );\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\ttransform = transform.WithRotation( randomRot );\r\n\t\t\t}\r\n\t\t\t\t\t\t\r\n\t\t\ttransform = transform.WithScale(\r\n\t\t\t\tFoliageSettings.Foliage.Scale.GetValue());\r\n\t\t\t\t\t\r\n\t\t\tpaintTarget.PaintFoliage( FoliageSettings.Foliage ,transform );\r\n\t\t\tPainted = true;\r\n\t\t}\r\n\t}\r\n\tpublic override void OnUpdate()\r\n\t{\r\n\t\tvar tr = Scene.Trace.Ray( Gizmo.CurrentRay, 5000 )\r\n\t\t\t.UseRenderMeshes( true )\r\n\t\t\t.UsePhysicsWorld( false )\r\n\t\t\t.WithoutTags( \u0022invisible\u0022 )\r\n\t\t\t.Run();\r\n\r\n\r\n\t\tif ( tr.Hit )\r\n\t\t{\r\n\t\t\tusing ( Gizmo.Scope( \u0022cursor\u0022 ) )\r\n\t\t\t{\r\n\t\t\t\tGizmo.Transform = new Transform( tr.HitPosition, Rotation.LookAt( tr.Normal ) );\r\n\t\t\t\tGizmo.Draw.LineCircle( 0, FoliageSettings.Size );\r\n\r\n\t\t\t}\r\n\r\n\t\t\tif ( Gizmo.IsLeftMouseDown \u0026\u0026 Application.KeyboardModifiers.HasFlag( Sandbox.KeyboardModifiers.Shift ) )\r\n\t\t\t{\r\n\t\t\t\tvar paintTarget = GetSelectedComponent\u003CFoliageRenderer\u003E();\r\n\t\t\t\tif ( paintTarget == null ) { return; }\r\n\r\n\t\t\t\tif ( FoliageSettings.EraseOnlySelectedFoliage )\r\n\t\t\t\t{\r\n\t\t\t\t\tpaintTarget.EraseFoliage( tr.HitPosition, FoliageSettings.Size, FoliageSettings.Foliage.ResourceId );\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tpaintTarget.EraseFoliage( tr.HitPosition, FoliageSettings.Size, 0 );\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\tPainted = true;\r\n\t\t\t}\r\n\r\n\t\t\tif ( !Gizmo.IsLeftMouseDown \u0026\u0026 Painted )\r\n\t\t\t{\r\n\t\t\t\tPainted = false;\r\n\t\t\t\tSceneEditorSession.Active.FullUndoSnapshot(\u0022Paint Grass\u0022);\r\n\t\t\t\t\r\n\t\t\t\tvar paintTarget = GetSelectedComponent\u003CFoliageRenderer\u003E();\r\n\t\t\t\tif ( paintTarget == null ) { return; }\r\n\t\t\t\tpaintTarget.UpdateRenderers();\r\n\t\t\t}\r\n\r\n\t\t\tif ( Gizmo.IsLeftMouseDown \u0026\u0026 !Application.KeyboardModifiers.HasFlag( Sandbox.KeyboardModifiers.Shift ))\r\n\t\t\t{\r\n\t\t\t\tif ( GetSelectedComponent\u003CFoliageRenderer\u003E() == null )\r\n\t\t\t\t{\r\n\t\t\t\t\tSelection.Clear();\r\n\t\t\t\t\tvar first = Scene.GetAllComponents\u003CFoliageRenderer\u003E().FirstOrDefault();\r\n\t\t\t\t\tif ( first is not null ) Selection.Add( first.GameObject );\r\n\t\t\t\t\treturn;\r\n\t\t\t\t}\r\n\t\t\t\t\r\n\t\t\t\tif ( FoliageSettings.Foliage == null ) { return;}\r\n\t\t\t\tPaintProgress \u002B= Time.Delta * FoliageSettings.PaintSpeed * 3f;\r\n\t\t\t\tif ( PaintProgress \u003E= 1 )\r\n\t\t\t\t{\r\n\t\t\t\t\tfor ( int i = 0; i \u003C (int)PaintProgress.Floor(); i\u002B\u002B )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tPaint( tr );\r\n\t\t\t\t\t}\r\n\t\t\t\t\tPaintProgress = 0;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t}\r\n\t}\r\n}\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"FoliageResource.cs","FileName":"FoliageResource.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":74752,"IsPrivate":false,"Code":"using System;\r\n\r\nnamespace Foliage;\r\n[Serializable]\r\n[GameResource( \u0022Foliage/Foliage\u0022, \u0022fol\u0022, \u0022Foliage Definition\u0022, Icon = \u0022grass\u0022 )]\r\npublic class FoliageResource : GameResource\r\n{\r\n\tpublic static HashSet\u003CFoliageResource\u003E All { get; set; } = new();\r\n\t[ResourceType( \u0022vmdl\u0022 ),Category(\u0022info\u0022)]\r\n\tpublic Model Model { get; set; }\r\n\tpublic RangedFloat Scale { get; set; } = new RangedFloat( 1f );\r\n\t[Category(\u0022placement\u0022)]\r\n\tpublic bool AlignToNormal { get; set; } = true;\r\n\t[Category(\u0022placement\u0022),Description(\u0022Maximum normal compared to the Up Vector to paint on\u0022)]\r\n\tpublic float MaxNormal { get; set; } = 180;\r\n\t[Category(\u0022placement\u0022)]\r\n\tpublic bool RandomRoll { get; set; } = true;\r\n\t[Category(\u0022placement\u0022)]\r\n\tpublic bool RandomRotation { get; set; } = false;\r\n\t[Category(\u0022placement\u0022)]\r\n\tpublic RangedFloat ZOffset { get; set; } = new RangedFloat( 0f );\r\n\tprotected override void PostLoad()\r\n\t{\r\n\t\tAll.Add( this );\r\n\t}\r\n}\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"__gen_RazorNamespace.cs","FileName":"__gen_RazorNamespace.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":74752,"IsPrivate":false,"Code":"global using Microsoft.AspNetCore.Components; \nglobal using Microsoft.AspNetCore.Components.Rendering;\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"Code/FoliageResource.cs","FileName":"FoliageResource.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":74752,"IsPrivate":false,"Code":"using System;\r\n\r\nnamespace Foliage;\r\n[Serializable]\r\n[GameResource( \u0022Foliage/Foliage\u0022, \u0022fol\u0022, \u0022Foliage Definition\u0022, Icon = \u0022grass\u0022 )]\r\npublic class FoliageResource : GameResource\r\n{\r\n\tpublic static HashSet\u003CFoliageResource\u003E All { get; set; } = new();\r\n\t[ResourceType( \u0022vmdl\u0022 ),Category(\u0022info\u0022)]\r\n\tpublic Model Model { get; set; }\r\n\tpublic RangedFloat Scale { get; set; } = new RangedFloat( 1f );\r\n\t[Category(\u0022placement\u0022)]\r\n\tpublic bool AlignToNormal { get; set; } = true;\r\n\t[Category(\u0022placement\u0022),Description(\u0022Maximum normal compared to the Up Vector to paint on\u0022)]\r\n\tpublic float MaxNormal { get; set; } = 180;\r\n\t[Category(\u0022placement\u0022)]\r\n\tpublic bool RandomRoll { get; set; } = true;\r\n\t[Category(\u0022placement\u0022)]\r\n\tpublic bool RandomRotation { get; set; } = false;\r\n\t[Category(\u0022placement\u0022)]\r\n\tpublic RangedFloat ZOffset { get; set; } = new RangedFloat( 0f );\r\n\tprotected override void PostLoad()\r\n\t{\r\n\t\tAll.Add( this );\r\n\t}\r\n}\r\n"},{"Ident":"stellawisps.scenefoliagepainter","Path":"Code/FoliageRenderer.cs","FileName":"FoliageRenderer.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":74752,"IsPrivate":false,"Code":"using System;\r\nusing System.Text.Json.Serialization;\r\nusing Sandbox.Engine.Settings;\r\n\r\nnamespace Foliage;\r\n\r\n\r\npublic class FoliageRenderer : Component, Component.ExecuteInEditor\r\n{\r\n\tpublic List\u003CFoliageSceneObject\u003E Renderers { get; set; } = new();\r\n\t[Property,Hide] Dictionary\u003Cint,List\u003CTransform\u003E\u003E FoliageRenderers { get; set; } = new();\r\n\t[Property] private int ChunkSize { get; set; } = 512;\r\n\t[Property] OptionsWidget Options { get; set; }\r\n\tpublic void PaintFoliage(FoliageResource foliage, Transform transform)\r\n\t{\r\n\t\tif ( FoliageRenderers.TryGetValue(foliage.ResourceId, out var renderer) )\r\n\t\t{\r\n\t\t\trenderer.Add( transform );\r\n\t\t\tUpdateRenderers();\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tFoliageRenderers.Add( foliage.ResourceId, new List\u003CTransform\u003E { transform } );\r\n\t\t\tFoliageRenderers[foliage.ResourceId].Add( transform );\r\n\t\t\tUpdateRenderers();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void EraseFoliage( Vector3 position,float size, int id )\r\n\t{\r\n\t\tList\u003CTransform\u003E toRemove = new();\r\n\r\n\t\tif ( id != 0 )\r\n\t\t{\r\n\t\t\tforeach ( var testPos in FoliageRenderers[id] )\r\n\t\t\t{\r\n\t\t\t\tif ( testPos.Position.DistanceSquared( position ) \u003C= size * size )\r\n\t\t\t\t{\r\n\t\t\t\t\ttoRemove.Add( testPos );\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tforeach ( var remove in toRemove )\r\n\t\t\t{\r\n\t\t\t\tFoliageRenderers[id].Remove( remove );\r\n\t\t\t}\r\n\t\t\treturn;\r\n\t\t}\r\n\t\t\r\n\t\tforeach ( var testVal in FoliageRenderers )\r\n\t\t{\r\n\t\t\tforeach ( var testPos in testVal.Value )\r\n\t\t\t{\r\n\t\t\t\tif ( testPos.Position.DistanceSquared( position ) \u003C= size * size )\r\n\t\t\t\t{\r\n\t\t\t\t\ttoRemove.Add( testPos );\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\tforeach ( var remove in toRemove )\r\n\t\t\t{\r\n\t\t\t\ttestVal.Value.Remove( remove );\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t}\r\n\r\n\tprotected override void OnStart()\r\n\t{\r\n\t\tUpdateRenderers();\r\n\t\t\r\n\t\t\r\n\t}\r\n\r\n\tprotected override void OnEnabled()\r\n\t{\r\n\t\tbase.OnEnabled();\r\n\t\tUpdateRenderers();\r\n\t}\r\n\r\n\tprotected override void OnDestroy()\r\n\t{\r\n\t\tforeach ( var val in Renderers )\r\n\t\t{\r\n\t\t\tval.Delete();\r\n\t\t}\r\n\t\tbase.OnDestroy();\r\n\t}\r\n\r\n\tprotected override void OnDisabled()\r\n\t{\r\n\t\tforeach ( var val in Renderers )\r\n\t\t{\r\n\t\t\tval.Delete();\r\n\t\t}\r\n\t\tbase.OnDisabled();\r\n\t\t\r\n\t}\r\n\r\n\tpublic void UpdateRenderers()\r\n\t{\r\n\t\tforeach ( var val in Renderers )\r\n\t\t{\r\n\t\t\tval.Delete();\r\n\t\t\t\r\n\t\t}\r\n\r\n\t\t\r\n\t\t\r\n\t\tRenderers.Clear();\r\n\r\n\t\tforeach ( var folRenderer in FoliageRenderers )\r\n\t\t{\r\n\t\t\tvar transformMins = new Vector3( 0 );\r\n\t\t\tvar transformMaxs = new Vector3( 0 );\r\n\r\n\t\t\tforeach ( var testTrans in folRenderer.Value )\r\n\t\t\t{\r\n\t\t\t\ttransformMins = Vector3.Min( transformMins, testTrans.Position );\r\n\t\t\t\ttransformMaxs = Vector3.Max( transformMaxs, testTrans.Position );\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tvar transformSize = transformMaxs - transformMins;\r\n\r\n\t\t\tvar transformArray = folRenderer.Value.ToArray();\r\n\t\t\tvar folInstance = ResourceLibrary.Get\u003CFoliageResource\u003E( folRenderer.Key );\r\n\r\n\t\t\t\r\n\t\t\tfor ( int x = 0; x \u003C (transformSize.x / ChunkSize).CeilToInt(); x\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tfor ( int y = 0; y \u003C (transformSize.y / ChunkSize).CeilToInt(); y\u002B\u002B )\r\n\t\t\t\t{\r\n\t\t\t\t\tList\u003CTransform\u003E newTransforms = new();\r\n\t\t\t\t\tvar transformedMins = transformMins \u002B new Vector3( (x * ChunkSize)\u002BChunkSize, (y * ChunkSize)\u002BChunkSize, 10000 );\r\n\t\t\t\t\tvar testBBox = new BBox( transformMins\u002B new Vector3( x * ChunkSize, y * ChunkSize, -500 ),transformedMins );\r\n\t\t\t\t\tforeach ( var testTrans in folRenderer.Value )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tif ( testBBox.Contains( testTrans.Position ) )\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tnewTransforms.Add( testTrans );\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\r\n\t\t\t\t\tif ( newTransforms.Count \u003C= 0 )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tcontinue;\r\n\t\t\t\t\t}\r\n\r\n\t\t\t\t\tvar renderModelLargestAxis = folInstance.Model.Bounds.Size;\r\n\r\n\t\t\t\t\tvar boundsMin = Vector3.Zero;\r\n\t\t\t\t\tvar boundsMax = Vector3.Zero;\r\n\t\t\t\t\tfor ( int i = 0; i \u003C newTransforms.Count; i\u002B\u002B )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tboundsMin = Vector3.Min( boundsMin, newTransforms[i].Position);\r\n\t\t\t\t\t\tboundsMax = Vector3.Max( boundsMax, newTransforms[i].Position);\r\n\t\t\t\t\t}\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\r\n\r\n\t\t\t\t\t\r\n\t\t\t\t\tvar folSceneObject = new FoliageSceneObject( GameObject.Scene.SceneWorld, folInstance.Model )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tBounds = new BBox( boundsMin-renderModelLargestAxis,boundsMax\u002BrenderModelLargestAxis ),\r\n\t\t\t\t\t\tTransforms = newTransforms.ToArray()\r\n\t\t\t\t\t};\r\n\t\t\t\t\tfolSceneObject.Flags.CastShadows = true;\r\n\t\t\t\r\n\t\t\t\t\tRenderers.Add( folSceneObject );\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\t\r\n\r\n\t\t\t\r\n\r\n\r\n\r\n\r\n\t\t}\r\n\t\t\r\n\t}\r\n\tpublic void ClearAll()\r\n\t{\r\n\t\tforeach ( var val in FoliageRenderers )\r\n\t\t{\r\n\t\t\tval.Value.Clear();\r\n\t\t}\r\n\t\tUpdateRenderers();\r\n\t}\r\n\t\r\n\tpublic class OptionsWidget { }\r\n}\r\npublic class FoliageSceneObject : SceneCustomObject\r\n{\r\n\tpublic Model RenderModel;\r\n\tpublic Transform[] Transforms = Array.Empty\u003CTransform\u003E();\r\n\tpublic FoliageSceneObject(SceneWorld sceneWorld, Model renderModel) : base(sceneWorld)\r\n\t{\r\n\t\tRenderModel = renderModel;\r\n\t}\r\n\r\n\t\r\n\tpublic void AddTransform(Transform transform)\r\n\t{\r\n\t\t//Transforms.Add( transform );\r\n\t}\r\n\r\n\tpublic override void RenderSceneObject()\r\n\t{\r\n\t\tbase.RenderSceneObject();\r\n\t\tGraphics.DrawModelInstanced( RenderModel, Transforms.AsSpan() );\r\n\t\t\r\n\t\t\r\n\t}\r\n}\r\n"}]}