Add Unity fields layer projection
This commit is contained in:
parent
bcee150fd5
commit
9b1d316f68
5 changed files with 117 additions and 2 deletions
|
|
@ -8,7 +8,7 @@
|
|||
"org.gamecult.cultlib": "https://github.com/GameCult/CultLib.git?path=/unity/org.gamecult.cultlib#cultlib-unity-source-v1.0.3-r2",
|
||||
"org.gamecult.eve.plugin-fields": "https://github.com/GameCult/EvePlugins.git?path=/plugins/eve-plugin-fields/unity/org.gamecult.eve.plugin-fields#eve-plugin-fields-unity-v0.1.1",
|
||||
"org.gamecult.eve.surface": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.surface#eveunity-surface-v0.1.0",
|
||||
"org.gamecult.eve.unity-scene": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.1.2",
|
||||
"org.gamecult.eve.unity-scene": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-scene#eveunity-scene-v0.1.3",
|
||||
"org.gamecult.eve.unity-uitoolkit": "https://github.com/GameCult/EveUnity.git?path=/packages/org.gamecult.eve.unity-uitoolkit#eveunity-uitoolkit-v0.1.0"
|
||||
},
|
||||
"testables": [
|
||||
|
|
|
|||
|
|
@ -0,0 +1,104 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using GameCult.Eve.PluginFields;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Experimental.Rendering;
|
||||
|
||||
namespace GameCult.Eve.UnityScene.Fields
|
||||
{
|
||||
public sealed class EveFieldsSplatLayerRenderer : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private EveFieldsSplatRasterizer rasterizer;
|
||||
private readonly Dictionary<string, RenderTexture> textures = new Dictionary<string, RenderTexture>(StringComparer.Ordinal);
|
||||
|
||||
public bool TryGetTexture(string layerKey, out RenderTexture texture) =>
|
||||
textures.TryGetValue(layerKey ?? "", out texture);
|
||||
|
||||
public void Render(IEveFieldsSplatsDocument document, IReadOnlyList<EveFieldsSplatLayerTarget> targets, int width, int height)
|
||||
{
|
||||
if (document == null || targets == null) return;
|
||||
ResolveRasterizer();
|
||||
if (rasterizer == null) return;
|
||||
var layers = document.Layers;
|
||||
for (var layerIndex = 0; layerIndex < layers.Count; layerIndex++)
|
||||
{
|
||||
var layer = layers[layerIndex];
|
||||
if (layer == null || string.IsNullOrWhiteSpace(layer.LayerKey)) continue;
|
||||
for (var targetIndex = 0; targetIndex < targets.Count; targetIndex++)
|
||||
{
|
||||
var target = targets[targetIndex];
|
||||
if (target == null || !target.Enabled || !string.Equals(target.LayerKey, layer.LayerKey, StringComparison.Ordinal)) continue;
|
||||
var texture = EnsureTexture(target, layer, width, height);
|
||||
rasterizer.RenderLayerToTarget(document, texture, layerIndex, MaterialPass(layer.BlendMode), ClearColor(layer));
|
||||
target.TargetTexture = texture;
|
||||
textures[layer.LayerKey] = texture;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ResolveRasterizer()
|
||||
{
|
||||
if (rasterizer != null) return;
|
||||
rasterizer = GetComponent<EveFieldsSplatRasterizer>();
|
||||
if (rasterizer == null) rasterizer = gameObject.AddComponent<EveFieldsSplatRasterizer>();
|
||||
}
|
||||
|
||||
private static RenderTexture EnsureTexture(EveFieldsSplatLayerTarget target, IEveFieldsSplatLayer layer, int width, int height)
|
||||
{
|
||||
var targetWidth = Mathf.Max(1, Mathf.RoundToInt(width * Mathf.Max(0.01f, target.WidthScale)));
|
||||
var targetHeight = Mathf.Max(1, Mathf.RoundToInt(height * Mathf.Max(0.01f, target.HeightScale)));
|
||||
var formatName = string.IsNullOrWhiteSpace(target.GraphicsFormatOverride) ? layer.GraphicsFormat : target.GraphicsFormatOverride;
|
||||
var format = Enum.TryParse(formatName, out GraphicsFormat parsed) ? parsed : GraphicsFormat.R16_SFloat;
|
||||
var existing = target.TargetTexture;
|
||||
if (existing != null && existing.width == targetWidth && existing.height == targetHeight && existing.graphicsFormat == format) return existing;
|
||||
if (existing != null) { existing.Release(); Destroy(existing); }
|
||||
var descriptor = new RenderTextureDescriptor(targetWidth, targetHeight)
|
||||
{
|
||||
depthBufferBits = 0,
|
||||
graphicsFormat = format,
|
||||
msaaSamples = 1,
|
||||
sRGB = false,
|
||||
useMipMap = target.UseMipMaps,
|
||||
autoGenerateMips = target.UseMipMaps
|
||||
};
|
||||
var texture = new RenderTexture(descriptor)
|
||||
{
|
||||
name = $"Eve Fields {layer.LayerKey}",
|
||||
filterMode = target.FilterMode,
|
||||
wrapMode = TextureWrapMode.Clamp
|
||||
};
|
||||
texture.Create();
|
||||
return texture;
|
||||
}
|
||||
|
||||
private static int MaterialPass(string blendMode) =>
|
||||
blendMode == EveFieldsSplatBlendModes.Max ? 1 : blendMode == EveFieldsSplatBlendModes.Alpha ? 2 : 0;
|
||||
|
||||
private static Color ClearColor(IEveFieldsSplatLayer layer) =>
|
||||
new Color((float)layer.ClearR, (float)layer.ClearG, (float)layer.ClearB, (float)layer.ClearA);
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
foreach (var texture in textures.Values)
|
||||
{
|
||||
if (texture == null) continue;
|
||||
texture.Release();
|
||||
Destroy(texture);
|
||||
}
|
||||
textures.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public sealed class EveFieldsSplatLayerTarget
|
||||
{
|
||||
public bool Enabled = true;
|
||||
public string LayerKey = "";
|
||||
public string GraphicsFormatOverride = "";
|
||||
public float WidthScale = 1f;
|
||||
public float HeightScale = 1f;
|
||||
public bool UseMipMaps;
|
||||
public FilterMode FilterMode = FilterMode.Bilinear;
|
||||
[NonSerialized] public RenderTexture TargetTexture;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 917a2499873b4c41be6e10b0bd9cb0c6
|
||||
|
|
@ -28,5 +28,14 @@ namespace GameCult.Eve.UnityScene.Tests
|
|||
Assert.That(Shader.Find("Eve/Fields/Splats"), Is.Not.Null);
|
||||
Assert.That(File.Exists("Packages/org.gamecult.eve.unity-scene/Runtime/Fields/EveFieldsSplatsCore.hlsl"), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LayerRendererConsumesPluginContractsWithoutProviderBindings()
|
||||
{
|
||||
var render = typeof(EveFieldsSplatLayerRenderer).GetMethod("Render");
|
||||
Assert.That(render, Is.Not.Null);
|
||||
Assert.That(render.GetParameters()[0].ParameterType, Is.EqualTo(typeof(IEveFieldsSplatsDocument)));
|
||||
Assert.That(typeof(EveFieldsSplatLayerRenderer).GetField("GlobalTextureName"), Is.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "org.gamecult.eve.unity-scene",
|
||||
"displayName": "Eve Unity Scene Lowering",
|
||||
"version": "0.1.2",
|
||||
"version": "0.1.3",
|
||||
"unity": "2021.3",
|
||||
"description": "Provider-agnostic Unity scene shell for gamecult.eve.surface.v1 interactive world surfaces.",
|
||||
"dependencies": {
|
||||
|
|
|
|||
Reference in a new issue