Skip to content

Commit

Permalink
Add Support for Shell Textures (#6)
Browse files Browse the repository at this point in the history
* add shell textures
  • Loading branch information
andyoneal authored Feb 2, 2024
1 parent d5ad6ba commit e18c0c6
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 59 deletions.
19 changes: 12 additions & 7 deletions InstDysonShellLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ public struct HexProgressData
{
public float progress;
}
public static int HEXPROGRESSDATA_STRIDE = 4;

public struct PolygonData {
public Vector3 pos;
public Vector3 normal;
}
public static int POLYGONDATA_STRIDE = 24;

public struct HexData
{
Expand All @@ -25,6 +27,7 @@ public struct HexData
public int closestPolygon;
public uint axialCoords_xy;
}
public static int HEXDATA_STRIDE = 32;

public struct ShellData
{
Expand All @@ -34,7 +37,9 @@ public struct ShellData
public int polyCount;
public int polygonIndex;
public Vector3 center;
public int protoId;
}
public static int SHELLDATA_STRIDE = 36;

public int layerId;

Expand Down Expand Up @@ -79,9 +84,9 @@ public InstDysonShellLayer(int layerId)
hexPool = new List<HexData>();
shellPool = new ShellData[11];
polygonPool = new PolygonData[64];
hexProgressBuffer = new ComputeBuffer(64, 4);
shellBuffer = new ComputeBuffer(11, 32);
polygonBuffer = new ComputeBuffer(64, 24);
hexProgressBuffer = new ComputeBuffer(64, HEXPROGRESSDATA_STRIDE);
shellBuffer = new ComputeBuffer(11, SHELLDATA_STRIDE);
polygonBuffer = new ComputeBuffer(64, POLYGONDATA_STRIDE);
SetProps();
}

Expand Down Expand Up @@ -135,7 +140,7 @@ public void SetCapacityPolygonPool(int nextCount)
}
polygonPool = destinationArray;
polygonBuffer?.Release();
polygonBuffer = new ComputeBuffer(newCap, 24);
polygonBuffer = new ComputeBuffer(newCap, POLYGONDATA_STRIDE);
props.SetBuffer(PolygonBuffer, polygonBuffer);
polygonBufferIsDirty = true;
}
Expand Down Expand Up @@ -175,7 +180,7 @@ public void SetCapacityShellPool(int newCap)
}
shellPool = destinationArray;
shellBuffer?.Release();
shellBuffer = new ComputeBuffer(newCap, 32);
shellBuffer = new ComputeBuffer(newCap, SHELLDATA_STRIDE);
props.SetBuffer(ShellBuffer, shellBuffer);
shellBufferIsDirty = true;
}
Expand All @@ -196,7 +201,7 @@ public void SetCapacityHexProgressPool(int newCap)
}
hexProgressPool = destinationArray;
hexProgressBuffer?.Release();
hexProgressBuffer = new ComputeBuffer(newCap, 4);
hexProgressBuffer = new ComputeBuffer(newCap, HEXPROGRESSDATA_STRIDE);
props.SetBuffer(HexProgressBuffer, hexProgressBuffer);
hexProgressBufferIsDirty = true;
}
Expand Down Expand Up @@ -227,7 +232,7 @@ public void SyncBufferData()
if (hexBuffer == null || hexBuffer.count != hexPool.Count)
{
hexBuffer?.Release();
hexBuffer = new ComputeBuffer(hexPool.Count, 32);
hexBuffer = new ComputeBuffer(hexPool.Count, HEXDATA_STRIDE);
props.SetBuffer(HexBuffer, hexBuffer);
}

Expand Down
47 changes: 45 additions & 2 deletions InstDysonShellRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,52 @@ private static void SetupMesh()
_HexMesh.vertices = newVerts;
_HexMesh.triangles = newTris;

_HexMat = UnityEngine.Object.Instantiate(
Resources.Load<Material>("Dyson Sphere/Materials/dyson-shell-unlit-0"));
Material material = Resources.Load<Material>("Dyson Sphere/Materials/dyson-shell-unlit-0");
Texture2D cc2 = (Texture2D)material.GetTexture("_ColorControlTex2");
Texture2D emm2 = (Texture2D)material.GetTexture("_EmissionTex2");

Texture2DArray colorControlTex2 = new Texture2DArray(cc2.width, cc2.height, 7, cc2.format, true);
Texture2DArray emissionTex2 = new Texture2DArray(emm2.width, emm2.height, 7, emm2.format, true);
float[] emissionMultiplier = new float[7];

for (int m = 0; m < cc2.mipmapCount; m++)
{
Graphics.CopyTexture(cc2, 0, m, colorControlTex2, 0, m);
}

for (int m = 0; m < emm2.mipmapCount; m++)
{
Graphics.CopyTexture(emm2, 0, m, emissionTex2, 0, m);
}
// colorControlTex2.SetPixels(cc2.GetPixels(), 0);
// emissionTex2.SetPixels(emm2.GetPixels(), 0);
emissionMultiplier[0] = material.GetFloat("_EmissionMultiplier");

for (int i = 1; i < 7; i++) {
material = Resources.Load<Material>($"Dyson Sphere/Materials/dyson-shell-unlit-{i}");
cc2 = (Texture2D)material.GetTexture("_ColorControlTex2");
emm2 = (Texture2D)material.GetTexture("_EmissionTex2");

for (int m = 0; m < cc2.mipmapCount; m++)
{
Graphics.CopyTexture(cc2, 0, m, colorControlTex2, i, m);
}

for (int m = 0; m < emm2.mipmapCount; m++)
{
Graphics.CopyTexture(emm2, 0, m, emissionTex2, i, m);
}
// colorControlTex2.SetPixels(cc2.GetPixels(), i);
// emissionTex2.SetPixels(emm2.GetPixels(), i);
emissionMultiplier[i] = material.GetFloat("_EmissionMultiplier");
}

_HexMat = UnityEngine.Object.Instantiate(material);
CustomShaderManager.ApplyCustomShaderToMaterial(_HexMat, "dysonshell-inst");

_HexMat.SetTexture("_ColorControlTex2", colorControlTex2);
_HexMat.SetTexture("_EmissionTex2", emissionTex2);
_HexMat.SetFloatArray("_EmissionMultiplier", emissionMultiplier);
}

public void Free()
Expand Down
3 changes: 2 additions & 1 deletion Patch_DysonShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ private static void DysonShell_GenerateModelObjects(DysonShell __instance)
state = __instance.state,
polygonIndex = polygonIndex,
polyCount = polygon.Count,
center = __instance.center
center = __instance.center,
protoId = __instance.protoId
};
instShellLayer.AddShellData(shellIndex, shellData);
instShellLayer.shellBufferIsDirty = true;
Expand Down
13 changes: 11 additions & 2 deletions SphereOpt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</PackageReference>
<PackageReference Include="BepInEx.Core" Version="5.4.17" />
<PackageReference Include="BepInEx.PluginInfoProps" Version="1.*" />
<PackageReference Include="DysonSphereProgram.GameLibs" Version="0.10.28.20779-r.0" />
<PackageReference Include="DysonSphereProgram.GameLibs" Version="0.10.29.21869-r.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.7.0" />
<PackageReference Include="Roslynator.Analyzers" Version="4.4.0">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -33,7 +33,7 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand All @@ -42,5 +42,14 @@
<None Remove="shaders\Frame Inst REPLACE.shader" />
<None Remove="shaders\Frame LOD.compute" />
<None Remove="shaders\Node Inst REPLACE.shader" />
<None Remove="unity\**" />
</ItemGroup>

<ItemGroup>
<Compile Remove="unity\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Remove="unity\**" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@
<Reference Include="UnityEditor.WindowsStandalone.Extensions">
<HintPath>C:/Program Files/Unity/Editor/Data/PlaybackEngines/windowsstandalonesupport/UnityEditor.WindowsStandalone.Extensions.dll</HintPath>
</Reference>
<Reference Include="JetBrains.Rider.Unity.Editor.Plugin.Repacked">
<HintPath>C:/Users/Shadow/RiderProjects/SphereOpt/unity/sphereopt-bundle/Assets/Plugins/Editor/JetBrains/JetBrains.Rider.Unity.Editor.Plugin.Repacked.dll</HintPath>
</Reference>
<Reference Include="UnityEditor.Advertisements">
<HintPath>C:/Users/Shadow/RiderProjects/SphereOpt/unity/sphereopt-bundle/Library/PackageCache/[email protected]/Editor/UnityEditor.Advertisements.dll</HintPath>
</Reference>
Expand All @@ -319,9 +322,6 @@
<Reference Include="UnityEditor.Purchasing">
<HintPath>C:/Users/Shadow/RiderProjects/SphereOpt/unity/sphereopt-bundle/Library/PackageCache/[email protected]/Editor/UnityEditor.Purchasing.dll</HintPath>
</Reference>
<Reference Include="JetBrains.Rider.Unity.Editor.Plugin.Repacked">
<HintPath>C:/Users/Shadow/RiderProjects/SphereOpt/unity/sphereopt-bundle/Assets/Plugins/Editor/JetBrains/JetBrains.Rider.Unity.Editor.Plugin.Repacked.dll</HintPath>
</Reference>
<Reference Include="mscorlib">
<HintPath>C:/Program Files/Unity/Editor/Data/MonoBleedingEdge/lib/mono/4.7.1-api/mscorlib.dll</HintPath>
</Reference>
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ManifestFileVersion: 0
CRC: 4241671993
CRC: 2232847047
AssetBundleManifest:
AssetBundleInfos:
Info_0:
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
ManifestFileVersion: 0
CRC: 2782471473
CRC: 2185452619
Hashes:
AssetFileHash:
serializedVersion: 2
Hash: be1c200c42965698114ce8be88e6f1a6
Hash: b83789f80e30bb78799cf82b1d9f4901
TypeTreeHash:
serializedVersion: 2
Hash: 44d87ff5fe9d9e8c6f146a352e3e72cf
Expand All @@ -14,8 +14,8 @@ ClassTypes:
- Class: 72
Script: {instanceID: 0}
Assets:
- Assets/sphereopt-bundle/shaders/Frame LOD.compute
- Assets/sphereopt-bundle/shaders/Dyson Shell Unlit Instanced.shader
- Assets/sphereopt-bundle/shaders/Node Inst REPLACE.shader
- Assets/sphereopt-bundle/shaders/Dyson Shell Unlit Instanced.shader
- Assets/sphereopt-bundle/shaders/Frame Inst REPLACE.shader
- Assets/sphereopt-bundle/shaders/Frame LOD.compute
Dependencies: []
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
Shader "VF Shaders/Dyson Sphere/Dyson Shell Unlit Instanced" {
Properties {
_Color ("Color", Vector) = (1,1,1,1)
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_NormalTex ("Normal Map", 2D) = "bump" {}
_MSTex ("Metallic Smoothness (RA)", 2D) = "white" {}
_EmissionTex ("Emission (RGB)", 2D) = "black" {}
_EmissionTex2 ("Emission Large (RGB)", 2D) = "black" {}
_NoiseTex ("Noise Texture (R)", 2D) = "gray" {}
_ColorControlTex ("Color Control", 2D) = "black" {}
_ColorControlTex2 ("Color Control Large", 2D) = "black" {}
_AlbedoMultiplier ("漫反射倍率", Float) = 1
_NormalMultiplier ("法线倍率", Float) = 1
_EmissionMultiplier ("自发光倍率", Float) = 5.5
_CellSize ("细胞大小(是否有间隙)", Float) = 1
}
SubShader {
Expand Down Expand Up @@ -61,6 +58,7 @@ Shader "VF Shaders/Dyson Sphere/Dyson Shell Unlit Instanced" {
int polyCount;
int polygonIndex;
float3 center;
int protoId;
};

struct appdata_part {
Expand All @@ -82,7 +80,7 @@ Shader "VF Shaders/Dyson Sphere/Dyson Shell Unlit Instanced" {
int _Global_DS_PaintingGridMode;
float _AlbedoMultiplier;
float _NormalMultiplier;
float _EmissionMultiplier;
float _EmissionMultiplier[7];
int _Global_IsMenuDemo;
int _Global_DS_RenderPlace;
float _CellSize;
Expand All @@ -96,32 +94,14 @@ Shader "VF Shaders/Dyson Sphere/Dyson Shell Unlit Instanced" {
sampler2D _NormalTex;
sampler2D _MSTex;
sampler2D _EmissionTex;
sampler2D _EmissionTex2;
UNITY_DECLARE_TEX2DARRAY(_EmissionTex2);
//sampler2DArray _EmissionTex2;
sampler2D _ColorControlTex;
sampler2D _ColorControlTex2;
UNITY_DECLARE_TEX2DARRAY(_ColorControlTex2);
//sampler2DArray _ColorControlTex2;

#include "UnityCG.cginc"

float3 rotate_vector_fast(float3 v, float4 r){
return v + cross(2.0 * r.xyz, cross(r.xyz, v) + r.w * v);
}

int isInside(int nextLineIsConvex, int prevLineIsConvex, int nextLineInside, int thisLineInside, int prevLineInside)
{
bool nextConvex = nextLineIsConvex > 0;
bool prevConvex = prevLineIsConvex > 0;

bool nextInside = nextLineInside > 0;
bool thisInside = thisLineInside > 0;
bool prevInside = prevLineInside > 0;

return nextConvex && prevConvex && nextInside && thisInside && prevInside ? 1 :
nextConvex && !prevConvex && nextInside && (prevInside || thisInside) ? 1 :
!nextConvex && prevConvex && prevInside && (thisInside || nextInside) ? 1 :
!nextConvex && !prevConvex && (nextInside || prevInside || thisInside) ? 1 :
-1;
}

struct v2f
{
float4 vertex : SV_POSITION;
Expand All @@ -132,7 +112,7 @@ struct v2f
float3 binormal : TEXCOORD4;
float3 normal : TEXCOORD5;
float4 pidx_close_pct_cnt : TEXCOORD6;
float2 state_clock : TEXCOORD7;
float3 state_clock_protoid : TEXCOORD7;
float4 color : TEXCOORD8;
};

Expand All @@ -158,7 +138,8 @@ v2f vert(appdata_part v, uint instanceID : SV_InstanceID)
float distFromCenter = dot(normalize(shellCenterPos.xyz), normalize(hexMidPtPos));
distFromCenter = renderPlace > 1.5 ? distFromCenter : distFromCenter * scaleGrid;
float viewDistFalloff = 1 - min(4, max(0, 0.0001 * (length(_WorldSpaceCameraPos - hexMidPtPos) - 3000))) * 0.25;
float cellSize = _CellSize;
int protoId = _ShellBuffer[shellIndex].protoId + 0.5;
float cellSize = protoId > 0.5 ? 1.0 : 0.94;
float scaledCellSize = distFromCenter * lerp(1, cellSize, viewDistFalloff) * _Scale;

float3 z_axis = normalize(shellCenterPos);
Expand Down Expand Up @@ -226,8 +207,9 @@ v2f vert(appdata_part v, uint instanceID : SV_InstanceID)
o.pidx_close_pct_cnt.z = scaleProgress;
o.pidx_close_pct_cnt.w = polyCount;

o.state_clock.x = state;
o.state_clock.y = 1;
o.state_clock_protoid.x = state;
o.state_clock_protoid.y = 1;
o.state_clock_protoid.z = protoId;

o.color.xyzw = linear_color; //move to frag?

Expand All @@ -250,7 +232,8 @@ v2f vert(appdata_part v, uint instanceID : SV_InstanceID)
}

//uint polyCount = i.polyGroup_pctComplete_polyCount_state.z;
float state = i.state_clock.x;
float state = i.state_clock_protoid.x;
int protoId = i.state_clock_protoid.z + 0.5;

/* remove pixels that fall outside the bounds of the frame that surrounds this shell */
int polyCount = (int)(i.pidx_close_pct_cnt.w + 0.5); //clamp(i.polyCount, 1, 380);
Expand All @@ -267,7 +250,7 @@ v2f vert(appdata_part v, uint instanceID : SV_InstanceID)
int nextIndex = polygonBaseIndex + nextIndexOffset;
int nextnextIndex = polygonBaseIndex + nextnextIndexOffset;

int clockwise = 1;//sign(i.state_clock.y);
int clockwise = 1;//sign(i.state_clock_protoid.y);

//TODO: discard pixels
float3 prevLineNormal = _PolygonBuffer[prevIndex].normal;
Expand Down Expand Up @@ -399,23 +382,23 @@ v2f vert(appdata_part v, uint instanceID : SV_InstanceID)
float3 emissionTex_A = tex2Dbias(_EmissionTex, float4(uv.xy, 0, lodBias)).xyz;
float3 emissionTex_B = tex2Dbias(_EmissionTex, float4(float2(1,1) - uv.yx, 0, lodBias)).xyz;
float3 emissionTex = lerp(emissionTex_A.xyz, emissionTex_B.xyz, sin(_Time.y + _Time.y) * 0.5 + 0.5);
float3 emissionTexTwo = tex2Dbias(_EmissionTex2, float4(scaledUV.xy, 0, lodBias)).xyz;
float3 emissionTexTwo = UNITY_SAMPLE_TEX2DARRAY(_EmissionTex2, float3(scaledUV.xy, protoId)).xyz;
//bool viewingSunFacingSide = dot(i.normal.xyz, viewDir.xyz) < 0;
emissionAnim = viewingOutwardFacingSide ? 0 : saturate((emissionTexTwo.y * 2 + emissionTex.y) * emissionAnim);

float colorControlTex_A = tex2Dbias(_ColorControlTex, float4(uv.xy, 0, lodBias)).x;
float colorControlTex_B = tex2Dbias(_ColorControlTex, float4(float2(1,1) - uv.yx, 0, lodBias)).x;
float colorControlTex = lerp(colorControlTex_A, colorControlTex_B, sin(_Time.y + _Time.y) * 0.5 + 0.5);
float colorControlTexTwo = tex2Dbias(_ColorControlTex2, float4(scaledUV.xy, 0, lodBias)).x;
float colorControlTexTwo = UNITY_SAMPLE_TEX2DARRAY(_ColorControlTex2, float3(scaledUV.xy, protoId)).x;
float colorControl = saturate(colorControlTex + colorControlTexTwo);

float3 colorOutwardFacing = lerp(colorControl * i.color.xyz, i.color.xyz, 0.01 / _EmissionMultiplier);
float3 colorOutwardFacing = lerp(colorControl * i.color.xyz, i.color.xyz, 0.01 / _EmissionMultiplier[protoId]);
float3 emissionOutwardFacing = lerp(emissionTexTwo.xyz * float3(0.3, 0.3, 0.3) + emissionTex.xyz, colorOutwardFacing, i.color.w);
float3 dysonEmission = viewingOutwardFacingSide ? float3(1,1,1) : _DysonEmission.xyz;
float3 emissionSunFacing = float3(3,3,3) * (emissionTexTwo.x + emissionTex.x) * dysonEmission.xyz;
float3 emission = viewingOutwardFacingSide ? emissionOutwardFacing.xyz : emissionSunFacing.xyz;

emission = _EmissionMultiplier * lerp(emission.xyz, dysonEmission.xyz, emissionAnim);
emission = _EmissionMultiplier[protoId] * lerp(emission.xyz, dysonEmission.xyz, emissionAnim);

float scaledDistancePosToCamera = 0.5 < asuint(_Global_IsMenuDemo) ? distancePosToCamera : renderPlace > 0.5 ? 3999.9998 * distancePosToCamera : distancePosToCamera;
float scaleMetallic = 0.5 < asuint(_Global_IsMenuDemo) ? 0.1 : renderPlace > 0.5 ? 0.93 : 0.7;
Expand Down Expand Up @@ -443,7 +426,7 @@ v2f vert(appdata_part v, uint instanceID : SV_InstanceID)
state > 0.5 ? float3(1.05, 1.05, 1.05) :
shellEmissionColor;
float emissionFactor = state > 0.5 || cutOut > 0.5 ? 1.0 : colorControl;
finalAlpha = _EmissionMultiplier * emissionFactor;
finalAlpha = _EmissionMultiplier[protoId] * emissionFactor;
metallicFactor = saturate(metallic * 0.85 + 0.149);
float perceptualRoughness = min(1, 1 - smoothness * 0.97);
roughnessSqr = pow(min(1, 1 - smoothness * 0.97), 4);
Expand All @@ -453,7 +436,7 @@ v2f vert(appdata_part v, uint instanceID : SV_InstanceID)
specularStrength = 0.8 * specularStrength;
fadeOut = 0.03;
finalColor.xyz = emission.xyz * multiplyEmission;
finalAlpha = _EmissionMultiplier * colorControl;
finalAlpha = _EmissionMultiplier[protoId] * colorControl;
metallicFactor = saturate(msTex.x * (0.85 - 0.85 * scaleMetallic) + 0.149);
roughnessSqr = pow(1 - 0.97 * min(0.8, msTex.y), 4); //pow(min(1, 1 - min(0.8, msTex.y) * 0.97), 2);
}
Expand Down

0 comments on commit e18c0c6

Please sign in to comment.