Skip to content

Commit

Permalink
Make go fast, update to 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanTroyer committed Jul 6, 2021
1 parent 49acb90 commit b84b521
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 88 deletions.
1 change: 1 addition & 0 deletions About/About.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<supportedVersions>
<li>1.1</li>
<li>1.2</li>
<li>1.3</li>
</supportedVersions>
<packageId>neptimus7.progressrenderer</packageId>
<modDependencies>
Expand Down
1 change: 1 addition & 0 deletions About/PublishedFileId.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2010777010
26 changes: 13 additions & 13 deletions Common/Languages/English/Keyed/All.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<LanguageData>

<!-- General -->
<!-- General -->
<LPR_RenderEveryHours>{0} hours</LPR_RenderEveryHours>
<LPR_RenderEveryHour>hour</LPR_RenderEveryHour>
<LPR_RenderEveryDays>{0} days</LPR_RenderEveryDays>
<LPR_RenderEveryDay>day</LPR_RenderEveryDay>
<LPR_RenderEveryDays>{0} days</LPR_RenderEveryDays>
<LPR_RenderEveryDay>day</LPR_RenderEveryDay>

<!-- Messages -->
<!-- Messages -->
<LPR_MessageCornerMarkerAmount>There are now {0} corner markers on this map.</LPR_MessageCornerMarkerAmount>
<LPR_MessageCornerMarkerTooLess>Rendering needs 2 markers to limit the rendered area. Now the full map will be rendered.</LPR_MessageCornerMarkerTooLess>
<LPR_MessageCornerMarkerTooMany>Rendering will create and use a virtual rect around all markers. You may want to remove unnecessary ones.</LPR_MessageCornerMarkerTooMany>
Expand All @@ -17,20 +17,20 @@
<LPR_MessageCornerMarkersRectRatioDefined>Aspect ratio is {0} ({1}).</LPR_MessageCornerMarkersRectRatioDefined>
<LPR_MessageCornerMarkesRectHeightTooLow>Height is too low. This will result in incorrect renderings. More than 20 cells height are recommended.</LPR_MessageCornerMarkesRectHeightTooLow>

<!-- Designation -->
<!-- Designation -->
<DesignatorCornerMarker>Rendering corner marker</DesignatorCornerMarker>
<DesignatorCornerMarkerDesc>Place corner markers for map rendering. Placing 2 will limit the rendering to only render the enclosed area.</DesignatorCornerMarkerDesc>
<DesignatorConerMarkerRemove>Remove corner marker</DesignatorConerMarkerRemove>
<DesignatorCornerMarkerRemoveDesc>Remove corner markers for map rendering. Removing all will let the rendering render the full map.</DesignatorCornerMarkerRemoveDesc>

<!-- Misc -->
<!-- Misc -->
<LPR_Rendering>Rendering map image...</LPR_Rendering>

<!-- Settings -->
<!-- Settings -->
<LPR_SettingsCategory>Progress Renderer</LPR_SettingsCategory>
<LPR_SettingsEnabledLabel>Enabled</LPR_SettingsEnabledLabel>
<LPR_SettingsEnabledDescription>When enabled, this mod takes automatic renderings of current maps while playing and uses the settings below. You can temporary disable this here so you don't have to remove the mod each time.</LPR_SettingsEnabledDescription>
<LPR_SettingsRenderFeedbackLabel>Render feedback</LPR_SettingsRenderFeedbackLabel>
<LPR_SettingsRenderFeedbackLabel>Render feedback</LPR_SettingsRenderFeedbackLabel>
<LPR_RenderFeedback_None>None</LPR_RenderFeedback_None>
<LPR_RenderFeedback_Message>Message</LPR_RenderFeedback_Message>
<LPR_RenderFeedback_Window>Popup</LPR_RenderFeedback_Window>
Expand All @@ -52,14 +52,14 @@
<LPR_SettingsTimeOfDayDescription>Choose at which time (in hours) of the selected days the rendering should happen. If you chose an interval of multiple times per day above, this setting works as an offset.</LPR_SettingsTimeOfDayDescription>
<LPR_SettingsEncodingLabel>Exported image encoding</LPR_SettingsEncodingLabel>
<LPR_ImgEncoding_png_unity>PNG (Unity)</LPR_ImgEncoding_png_unity>
<LPR_ImgEncoding_jpg_unity>JPG (Unity)</LPR_ImgEncoding_jpg_unity>
<LPR_ImgEncoding_jpg_unity>JPG (Unity)</LPR_ImgEncoding_jpg_unity>
<LPR_JPGQualityLabel>JPG Quality</LPR_JPGQualityLabel>
<LPR_JPGQualityDescription>Higher values correspond to higher image quality, lower values correspond to smaller image size</LPR_JPGQualityDescription>
<LPR_SettingsPixelPerCellLabel>Pixel per cell (for image resolution)</LPR_SettingsPixelPerCellLabel>
<LPR_JPGQualityDescription>Higher values correspond to higher image quality, lower values correspond to smaller image size</LPR_JPGQualityDescription>
<LPR_SettingsPixelPerCellLabel>Pixel per cell (for image resolution)</LPR_SettingsPixelPerCellLabel>
<LPR_SettingsPixelPerCellDescription>Define with how many pixels each ingame map cell should be displayed on the rendered image.</LPR_SettingsPixelPerCellDescription>
<LPR_SettingsScaleOutputImageLabel>Rescale to fixed height</LPR_SettingsScaleOutputImageLabel>
<LPR_SettingsScaleOutputImageDescription>Set this if you want the rendered output image to have a fixed height. The width will be calculated to preserve the aspect ratio. Useful when you change the rendered area size mid-game and and plan to animate the image sequence later, so you can work with one fixed image size.</LPR_SettingsScaleOutputImageDescription>
<LPR_SettingsOutputImageFixedHeightLabel>Output height (px)</LPR_SettingsOutputImageFixedHeightLabel>
<LPR_SettingsScaleOutputImageDescription>Set this if you want the rendered output image to have a fixed height. The width will be calculated to preserve the aspect ratio. Useful when you change the rendered area size mid-game and and plan to animate the image sequence later, so you can work with one fixed image size.</LPR_SettingsScaleOutputImageDescription>
<LPR_SettingsOutputImageFixedHeightLabel>Output height (px)</LPR_SettingsOutputImageFixedHeightLabel>
<LPR_SettingsExportPathLabel>Export path</LPR_SettingsExportPathLabel>
<LPR_SettingsExportPathDescription>The path where rendered images will be saved. Note that this must be a valid path to an existing and accessible directory on your computer. You can copy+paste a directory from your file explorer here for simple editing.</LPR_SettingsExportPathDescription>
<LPR_SettingsFileNamePatternLabel>Exported image name pattern</LPR_SettingsFileNamePatternLabel>
Expand Down
20 changes: 12 additions & 8 deletions LoadFolders.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<loadFolders>
<v1.1>
<li>Common</li>
<li>v1.1</li>
</v1.1>
<v1.2>
<li>Common</li>
<li>v1.2</li>
</v1.2>
<v1.1>
<li>Common</li>
<li>v1.1</li>
</v1.1>
<v1.2>
<li>Common</li>
<li>v1.2</li>
</v1.2>
<v1.3>
<li>Common</li>
<li>v1.3</li>
</v1.3>
</loadFolders>
6 changes: 6 additions & 0 deletions News/UpdateFeatureDefs/UpdateFeatures.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@
<assemblyVersion>1.2.0</assemblyVersion>
<content>Update to 1.2</content>
</HugsLib.UpdateFeatureDef>

<HugsLib.UpdateFeatureDef ParentName="UpdateFeatureBase">
<defName>ProgressRenderer_1_3_0</defName>
<assemblyVersion>1.3.0</assemblyVersion>
<content>Update to 1.3. Also makes rendering go way way faster. Crank up those settings people!</content>
</HugsLib.UpdateFeatureDef>
</Defs>
2 changes: 1 addition & 1 deletion Progress-Renderer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>v1.3\Assemblies\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
118 changes: 54 additions & 64 deletions Source/MapComponents/MapComponent_RenderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using HarmonyLib;
using ProgressRenderer.Source.Enum;
using RimWorld;
using RimWorld.Planet;
using RimWorld.Planet;
using UnityEngine;
using Verse;

namespace ProgressRenderer
{

public class MapComponent_RenderManager : MapComponent
{

private const int RenderTextureSize = 4096;

public static int nextGlobalRenderManagerTickOffset = 0;
Expand All @@ -34,14 +32,13 @@ public class MapComponent_RenderManager : MapComponent
private float rsTargetEndZ = -1f;
private float rsCurrentPosition = 1f;

private int imageWidth, imageHeight;
private Texture2D imageTexture;
private byte[] imageTextureRawData;
private int imageTextureWidth;
private int imageTextureHeight;

private Task EncodingTask;

private bool manuallyTriggered = false;
private bool encodingInt = false;
Thread encodeThread;
private bool encoding = false;
private bool ctrlEncodingPost = false;
private SmallMessageBox messageBox;

Expand Down Expand Up @@ -164,7 +161,7 @@ private IEnumerator DoRendering(bool forceRenderFullMap = false)
switchedMap = true;
Current.Game.CurrentMap = map;
}

// Close world view if needed
bool rememberedWorldRendered = WorldRendererUtility.WorldRenderedNow;
if (rememberedWorldRendered)
Expand Down Expand Up @@ -236,35 +233,40 @@ private IEnumerator DoRendering(bool forceRenderFullMap = false)

float distX = endX - startX;
float distZ = endZ - startZ;

// Calculate basic values that are used for rendering
int imageWidth;
int imageHeight;
int newImageWidth;
int newImageHeight;
if (PRModSettings.scaleOutputImage)
{
imageHeight = PRModSettings.outputImageFixedHeight;
imageWidth = (int)((float)imageHeight / distZ * distX);
newImageWidth = PRModSettings.outputImageFixedHeight;
newImageHeight = (int)(imageHeight / distZ * distX);
}
else
{
imageWidth = (int)(distX * PRModSettings.pixelPerCell);
imageHeight = (int)(distZ * PRModSettings.pixelPerCell);
newImageWidth = (int)(distX * PRModSettings.pixelPerCell);
newImageHeight = (int)(distZ * PRModSettings.pixelPerCell);
}
bool mustUpdateTexture = false;
if (newImageWidth != imageWidth || newImageHeight != imageHeight)
{
mustUpdateTexture = true;
imageWidth = newImageWidth;
imageHeight = newImageHeight;
}

int renderCountX = (int)Math.Ceiling((float)imageWidth / RenderTextureSize);
int renderCountZ = (int)Math.Ceiling((float)imageHeight / RenderTextureSize);
int renderWidth = (int)Math.Ceiling((float)imageWidth / renderCountX);
int renderHeight = (int)Math.Ceiling((float)imageHeight / renderCountZ);

float cameraPosX = (float)distX / 2 / renderCountX;
float cameraPosZ = (float)distZ / 2 / renderCountZ;
float orthographicSize = Math.Min(cameraPosX, cameraPosZ);
orthographicSize = cameraPosZ;
float cameraPosX = (float)distX / 2;
float cameraPosZ = (float)distZ / 2;
float orthographicSize = cameraPosZ;
Vector3 cameraBasePos = new Vector3(cameraPosX, 15f + (orthographicSize - 11f) / 49f * 50f, cameraPosZ);

RenderTexture renderTexture = RenderTexture.GetTemporary(renderWidth, renderHeight, 24);
imageTexture = new Texture2D(imageWidth, imageHeight, TextureFormat.RGB24, false);

// Initialize cameras and textures
RenderTexture renderTexture = RenderTexture.GetTemporary(imageWidth, imageHeight, 24);
if (imageTexture == null || mustUpdateTexture)
{
imageTexture = new Texture2D(imageWidth, imageHeight, TextureFormat.RGB24, false);
}

Camera camera = Find.Camera;
CameraDriver camDriver = camera.GetComponent<CameraDriver>();
camDriver.enabled = false;
Expand All @@ -283,19 +285,14 @@ private IEnumerator DoRendering(bool forceRenderFullMap = false)
Traverse camDriverTraverse = Traverse.Create(camDriver);
camDriverTraverse.Field("lastViewRect").SetValue(CellRect.FromLimits(camRectMinX, camRectMinZ, camRectMaxX, camRectMaxZ));
camDriverTraverse.Field("lastViewRectGetFrame").SetValue(Time.frameCount);

yield return new WaitForEndOfFrame();

// Set camera values needed for rendering
camera.orthographicSize = orthographicSize;
camera.farClipPlane = cameraBasePos.y + 6.5f;

// Create a temporary camera for rendering
/*Camera tmpCam = Camera.Instantiate(camera);
tmpCam.orthographicSize = orthographicSize;
tmpCam.farClipPlane = cameraPos.y + 6.5f;*/

// Set render textures
//tmpCam.targetTexture = renderTexture;
camera.targetTexture = renderTexture;
RenderTexture.active = renderTexture;

Expand All @@ -306,18 +303,13 @@ private IEnumerator DoRendering(bool forceRenderFullMap = false)
{
map.weatherManager.DrawAllWeather();
}
for (int i = 0; i < renderCountZ; i++)
{
for (int j = 0; j < renderCountX; j++)
{
camera.transform.position = new Vector3(startX + cameraBasePos.x * (2 * j + 1), cameraBasePos.y, startZ + cameraBasePos.z * (2 * i + 1));
camera.Render();
imageTexture.ReadPixels(new Rect(0, 0, renderWidth, renderHeight), renderWidth * j, renderHeight * i, false);
}
}
} catch(Exception e)
camera.transform.position = new Vector3(startX + cameraBasePos.x, cameraBasePos.y, startZ + cameraBasePos.z);
camera.Render();
imageTexture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0, false);
}
catch (Exception e)
{
Log.Error(e.Message, true);
Log.Error(e.Message);
}

// Restore camera and viewport
Expand All @@ -344,21 +336,29 @@ private IEnumerator DoRendering(bool forceRenderFullMap = false)

// Sinal finished rendering
Rendering = false;
// Hide message box
if (messageBox != null)
{
messageBox.Close();
messageBox = null;
}
yield return null;

// Start encoding
DoEncoding();
if (EncodingTask != null && !EncodingTask.IsCompleted)
EncodingTask.Wait();
EncodingTask = Task.Run(DoEncoding);

yield break;
}

private void DoEncoding()
{
if (encodingInt)
if (encoding)
{
Log.Error("Progress Renderer is still encoding an image while the encoder was called again. This can lead to missing or wrong data.");
}
switch(PRModSettings.encoding)
switch (PRModSettings.encoding)
{
case EncodingType.UnityJPG:
EncodeUnityJpg();
Expand All @@ -376,21 +376,11 @@ private void DoEncoding()
private void DoEncodingPost()
{
// Clean up unused objects
imageTextureRawData = null;
UnityEngine.Object.Destroy(imageTexture);
imageTextureWidth = 0;
imageTextureHeight = 0;

//UnityEngine.Object.Destroy(imageTexture);

// Signal finished encoding
manuallyTriggered = false;
encodingInt = false;

// Hide message box
if (messageBox != null)
{
messageBox.Close();
messageBox = null;
}
encoding = false;
}

private void EncodeUnityPng()
Expand All @@ -409,12 +399,14 @@ private void SaveUnityEncoding(byte[] encodedImage)
{
// Create file and save encoded image
string filePath = CreateCurrentFilePath();

File.WriteAllBytes(filePath, encodedImage);
// Create tmp copy to file if needed
if (!manuallyTriggered && PRModSettings.fileNamePattern == FileNamePattern.BothTmpCopy)
{
File.Copy(filePath, CreateFilePath(FileNamePattern.Numbered, true));
}

DoEncodingPost();
}

Expand Down Expand Up @@ -488,7 +480,5 @@ private string CreateImageNameNumbered()
{
return "rimworld-" + Find.World.info.seedString + "-" + map.Tile + "-" + lastRenderedCounter.ToString("000000");
}

}

}
Binary file modified v1.2/Assemblies/Progress-Renderer.dll
Binary file not shown.
Binary file modified v1.3/Assemblies/Progress-Renderer.dll
Binary file not shown.

0 comments on commit b84b521

Please sign in to comment.