Skip to content

Commit dbe549a

Browse files
committed
better drawing chain
1 parent 8ef663a commit dbe549a

7 files changed

Lines changed: 66 additions & 56 deletions

File tree

1.3/Assemblies/RW_NodeTree.dll

512 Bytes
Binary file not shown.

1.4/Assemblies/RW_NodeTree.dll

512 Bytes
Binary file not shown.

1.5/Assemblies/RW_NodeTree.dll

512 Bytes
Binary file not shown.

1.6/Assemblies/RW_NodeTree.dll

0 Bytes
Binary file not shown.

src/RW_NodeTree/Graphic_ChildNode.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44
using System.Collections.Generic;
55
using System.Collections.ObjectModel;
66
using UnityEngine;
7+
using UnityEngine.Experimental.Rendering;
78
using Verse;
89

910
namespace RW_NodeTree
1011
{
1112
public class Graphic_ChildNode : Graphic
1213
{
13-
public Graphic_ChildNode(INodeProcesser proccesser, Action<RenderTexture>? PostFX = null)
14+
public Graphic_ChildNode(INodeProcesser proccesser, uint textureSizeFactor = RenderingTools.DefaultTextureSizeFactor, float exceedanceFactor = 1f, float exceedanceOffset = 1f, GraphicsFormat textureFormat = GraphicsFormat.R8G8B8A8_SRGB, FilterMode textureFilterMode = FilterMode.Bilinear, Action<RenderTexture>? PostFX = null)
1415
{
1516
if (proccesser == null) throw new ArgumentNullException(nameof(proccesser));
1617
if (proccesser is not Thing) throw new ArgumentException("Invalid processer type", nameof(proccesser));
1718
currentProcesser = proccesser;
1819
postFX = PostFX;
20+
this.textureSizeFactor = textureSizeFactor;
21+
this.exceedanceFactor = exceedanceFactor;
22+
this.exceedanceOffset = exceedanceOffset;
23+
this.textureFormat = textureFormat;
24+
this.textureFilterMode = textureFilterMode;
1925
//base.drawSize = _THING.DrawSize(_THING.parent.Rotation);
2026
//base.data = _GRAPHIC.data;
2127
}
@@ -48,10 +54,19 @@ public override Material MatAt(Rot4 rot, Thing? thing = null)
4854

4955
if (needUpdate || material == null || texture == null || cachedRenderTarget == null)
5056
{
51-
currentProcesser.ChildNodes.GetNodeRenderingResult(rot, ref cachedRenderTarget, ref texture, this, postFX);
57+
Dictionary<string, List<RenderInfo>> nodeRenderingInfos = currentProcesser.ChildNodes.GetNodeRenderingInfos(rot, this);
58+
59+
List<RenderInfo> final = new List<RenderInfo>();
60+
foreach (var infos in nodeRenderingInfos)
61+
{
62+
if (!infos.Value.NullOrEmpty()) final.AddRange(infos.Value);
63+
}
64+
65+
RenderingTools.RenderToTarget(final, ref cachedRenderTarget, ref texture, textureFormat, default, textureSizeFactor, exceedanceFactor, exceedanceOffset, postFX);
66+
5267
Shader shader = thing.DefaultGraphic.Shader;
5368
texture!.wrapMode = TextureWrapMode.Clamp;
54-
texture.filterMode = currentProcesser.TextureFilterMode;
69+
texture.filterMode = textureFilterMode;
5570

5671
if (material == null)
5772
{
@@ -65,10 +80,16 @@ public override Material MatAt(Rot4 rot, Thing? thing = null)
6580
defaultRenderingCache[rot] = (false, material, texture, cachedRenderTarget);
6681
}
6782

68-
drawSize = new Vector2(texture.width, texture.height) / currentProcesser.TextureSizeFactor;
83+
drawSize = new Vector2(texture.width, texture.height) / textureSizeFactor;
6984
return material;
7085
}
7186

87+
public override Mesh MeshAt(Rot4 rot)
88+
{
89+
MatAt(rot);
90+
return base.MeshAt(rot);
91+
}
92+
7293
public override void DrawWorker(Vector3 loc, Rot4 rot, ThingDef? thingDef, Thing? thing, float extraRotation)
7394
{
7495
MatAt(rot);
@@ -174,8 +195,14 @@ private class OffScreenRenderingCache
174195
public RenderTexture? cachedRenderTargetNorth, cachedRenderTargetEast, cachedRenderTargetSouth, cachedRenderTargetWest;
175196
}
176197

177-
private readonly INodeProcesser currentProcesser;
178-
private readonly Action<RenderTexture>? postFX;
198+
199+
public readonly uint textureSizeFactor;
200+
public readonly float exceedanceFactor;
201+
public readonly float exceedanceOffset;
202+
public readonly GraphicsFormat textureFormat;
203+
public readonly FilterMode textureFilterMode;
204+
public readonly INodeProcesser currentProcesser;
205+
public readonly Action<RenderTexture>? postFX;
179206
private readonly OffScreenRenderingCache defaultRenderingCache = new OffScreenRenderingCache();
180207
}
181208
}

src/RW_NodeTree/INodeProccesser.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ namespace RW_NodeTree
99
{
1010
public partial interface INodeProcesser : IThingHolder
1111
{
12-
uint TextureSizeFactor { get; }
13-
14-
float ExceedanceFactor { get; }
15-
16-
float ExceedanceOffset { get; }
17-
18-
GraphicsFormat TextureFormat { get; }
19-
20-
FilterMode TextureFilterMode { get; }
2112

2213
/// <summary>
2314
/// node container

src/RW_NodeTree/NodeContainer.cs

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -367,30 +367,13 @@ public void CachedRootNodeNeedUpdate()
367367
}
368368

369369

370-
public void GetNodeRenderingResult(Rot4 rot, ref RenderTexture? cachedRenderTarget, ref Texture2D? target, Graphic_ChildNode invokeSource, Action<RenderTexture>? PostFX = null)
370+
internal Dictionary<string, List<RenderInfo>> GetNodeRenderingInfos(Rot4 rot, Graphic_ChildNode invokeSource)
371371
{
372372
UpdateNode();
373373
if (!UnityData.IsInMainThread) throw new InvalidOperationException("not in main thread");
374374
Dictionary<string, List<RenderInfo>> nodeRenderingInfos = new Dictionary<string, List<RenderInfo>>(Count + 1);
375375
Thing parent = (Thing)Processer;
376376

377-
//if (Prefs.DevMode)
378-
//{
379-
// StackTrace stack = new StackTrace();
380-
// string stackReport = "";
381-
// for(int i =0; i < 8; i++)
382-
// {
383-
// StackFrame sf = stack.GetFrame(i);
384-
// MethodBase method = sf.GetMethod();
385-
// stackReport += method.DeclaringType + " -> " + method + " " + sf + "\n";
386-
// }
387-
// Log.Message(parent + " graphic : " + parent.Graphic + ";\nstack : " + stackReport);
388-
//}
389-
390-
391-
//ORIGIN
392-
393-
394377
Dictionary<string, object?> cachingData = new Dictionary<string, object?>();
395378

396379
HashSet<string>? ChildForDraw = null;
@@ -413,36 +396,45 @@ public void GetNodeRenderingResult(Rot4 rot, ref RenderTexture? cachedRenderTarg
413396
Thing? child = this[id];
414397
if (child != null && id != null)
415398
{
416-
RenderingTools.StartOrEndDrawCatchingBlock = true;
417-
try
399+
INodeProcesser? childProcesser = child as INodeProcesser;
400+
if(childProcesser != null)
418401
{
419-
Rot4 rotCache = child.Rotation;
420-
child.Rotation = new Rot4((rot.AsInt + rotCache.AsInt) & 3);
421-
#if V13 || V14
422-
child.DrawAt(Vector3.zero);
423-
#else
424-
child.DrawNowAt(Vector3.zero);
425-
#endif
426-
child.Rotation = rotCache;
427-
nodeRenderingInfos[id] = RenderingTools.RenderInfos!;
402+
List<RenderInfo> renderInfos = new List<RenderInfo>();
403+
foreach(var kv in childProcesser.ChildNodes.GetNodeRenderingInfos(rot, invokeSource))
404+
{
405+
renderInfos.AddRange(kv.Value);
406+
}
407+
nodeRenderingInfos[id] = renderInfos;
428408
}
429-
catch (Exception ex)
409+
else
430410
{
431-
Log.Error(ex.ToString());
411+
RenderingTools.StartOrEndDrawCatchingBlock = true;
412+
try
413+
{
414+
Rot4 rotCache = child.Rotation;
415+
child.Rotation = new Rot4((rot.AsInt + rotCache.AsInt) & 3);
416+
#if V13 || V14
417+
child.DrawAt(Vector3.zero);
418+
#else
419+
child.DrawNowAt(Vector3.zero);
420+
#endif
421+
child.Rotation = rotCache;
422+
nodeRenderingInfos[id] = RenderingTools.RenderInfos!;
423+
}
424+
catch (Exception ex)
425+
{
426+
Log.Error(ex.ToString());
427+
}
428+
RenderingTools.StartOrEndDrawCatchingBlock = false;
429+
432430
}
433-
RenderingTools.StartOrEndDrawCatchingBlock = false;
434431
}
435432
}
436433
}
437-
nodeRenderingInfos = Processer.PostGenRenderInfos(rot, invokeSource, nodeRenderingInfos, cachingData) ?? nodeRenderingInfos;
438-
List<RenderInfo> final = new List<RenderInfo>();
439-
foreach (var infos in nodeRenderingInfos)
440-
{
441-
if (!infos.Value.NullOrEmpty()) final.AddRange(infos.Value);
442-
}
443-
444-
RenderingTools.RenderToTarget(final, ref cachedRenderTarget, ref target, Processer.TextureFormat, default, Processer.TextureSizeFactor, Processer.ExceedanceFactor, Processer.ExceedanceOffset, PostFX);
445-
return;
434+
return Processer.PostGenRenderInfos(rot, invokeSource, nodeRenderingInfos, cachingData) ?? nodeRenderingInfos;
435+
//Humm, I think i should not use state mechine to handle this...
436+
//Time to instead every state check by using various specific method.
437+
//But that has too many parts to refactor, FXXK!
446438
}
447439

448440

0 commit comments

Comments
 (0)