Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/andyoneal/SphereOpt
Browse files Browse the repository at this point in the history
  • Loading branch information
andyoneal committed Nov 2, 2023
2 parents 0fcaac1 + fc5c5d0 commit 846b03c
Show file tree
Hide file tree
Showing 6 changed files with 411 additions and 385 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SphereOpt

**SphereOpt v0.8.1 should be considered a "beta" release. The worst you should is experience is errors or graphical glitches, in which case you can uninstall the mod. Please report any issues you encounter.**
**SphereOpt v0.8.2 should be considered a "beta" release. The worst you should is experience is errors or graphical glitches, in which case you can uninstall the mod. Please report any issues you encounter.**

Improves rendering of Dyson Shells by drastically reducing the amount of data sent to the gpu per shell. Impact is larger depending on the number of shells in your Dyson Sphere(s).

Expand All @@ -13,6 +13,9 @@ In a new game with a large and densely packed sphere containing 5,240 shells, fr
- does not support painting/colors on shells. this was just an additional layer of complexity that I haven't dug into yet. planning to have this in before 1.0.

## Changelog
- v0.8.2
- fixes bright green glow on unbuilt frames/nodes in the dyson sphere editor
- slight performance increase
- v0.8.1
- fixes error when using shells on the 10th dyson sphere layer.
- v0.8.0
Expand Down
2 changes: 1 addition & 1 deletion SphereOpt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net472</TargetFramework>
<AssemblyName>SphereOpt</AssemblyName>
<Description>Optimize Dyson Sphere rendering</Description>
<Version>0.8.1</Version>
<Version>0.8.2</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<PlatformTarget>x64</PlatformTarget>
Expand Down
107 changes: 107 additions & 0 deletions shaders/CGIncludes/DSPCommon.cginc
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "UnityCG.cginc"

struct GPUOBJECT
{
uint objId;
float3 pos;
float4 rot;
};

struct AnimData
{
float time;
float prepare_length;
float working_length;
uint state;
float power;
};

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

inline float SchlickFresnel_Approx(float F0, float vDotH)
{
return F0 + (1 - F0) * exp2((-5.55473 * vDotH - 6.98316) * vDotH);
}

inline float3 calculateLightFromHeadlamp(float4 headlampPos, float3 upDir, float3 lightDir, float3 worldNormal) {
float isHeadlampOn = headlampPos.w >= 0.5 ? 1.0 : 0.0;
if (headlampPos.w < 0.5) return float3(0, 0, 0);

float distanceFromHeadlamp = length(headlampPos) - 5.0;
float headlampVisibility = saturate(distanceFromHeadlamp);
float daylightDimFactor = saturate(dot(-upDir, lightDir) * 5.0);

float3 directionToPlayer = headlampPos - upDir * distanceFromHeadlamp;
float distObjToPlayer = length(directionToPlayer);
directionToPlayer /= distObjToPlayer;

float falloff = pow(max((20.0 - distObjToPlayer) * 0.05, 0), 2);
float3 lightColor = float3(1.3, 1.1, 0.6);

float lightIntensity = headlampVisibility * daylightDimFactor * falloff * saturate(dot(directionToPlayer, worldNormal));
float3 computedLight = lightIntensity * lightColor;

return distObjToPlayer < 0.001 ? daylightDimFactor * lightColor : computedLight;
}

inline float distributionGGX(float roughness, float nDotH) {
float a = roughness; //NDF formula says `a` should be roughness^2
//"We also adopted Disney’s reparameterization of α = Roughness2."
//but a = Roughness here
float denom = rcp(nDotH * nDotH * (a * a - 1.0) + 1); //r0.w
return denom * denom * a * a; //r0.w
//missing (1/PI) *
}

inline float geometrySchlickGGX(float roughness, float nDotV, float nDotL) {
float k = pow(roughness * roughness + 1.0, 2) * 0.125; //r2.w does "roughness" mean perceptualroughness^2 or ^4?
//"We also chose to use Disney’s modification to reduce “hotness” by remapping roughness using (Roughness+1)/2 before squaring."
//but this is doing (Roughness^2+1)/2 before squaring
float ggxNV = nDotV * (1.0 - k) + k; //r5.x
float ggxNL = nDotL * (1.0 - k) + k; //r1.z
return rcp(ggxNL * ggxNV); //r1.x
//missing (nDotL * nDotV) *
}

inline float GGX(float roughness, float metallic, float nDotH, float nDotV, float nDotL, float vDotH) {

float D = distributionGGX(roughness, nDotH);
float G = geometrySchlickGGX(roughness, nDotV, nDotL); //r1.x
float F = SchlickFresnel_Approx(metallic, vDotH);

return (D * F * G) / 4.0;
//should be (4.0 * nDotV * nDotL)
}

inline float3 calculateBinormal(float4 tangent, float3 normal ) {
float sign = tangent.w * unity_WorldTransformParams.w;
float3 binormal = cross(normal.xyz, tangent.xyz) * sign;
return binormal;
}

UNITY_DECLARE_TEXCUBE(_Global_PGI);

/* What image is reflected in metallic surfaces and how reflective is it? */
inline float3 reflection(float perceptualRoughness, float3 metallicLow, float3 upDir, float3 viewDir, float3 worldNormal, out float reflectivity) {
float upDirMagSqr = dot(upDir, upDir);
bool validUpDirY = upDirMagSqr > 0.01 && upDir.y < 0.9999;
float3 xaxis = validUpDirY ? normalize(cross(upDir.zxy, float3(0, 0, 1))) : float3(0, 1, 0);
bool validUpDirXY = dot(xaxis, xaxis) > 0.01 && upDirMagSqr > 0.01;
float3 zaxis = validUpDirXY ? normalize(cross(xaxis.yzx, upDir)) : float3(0, 0, 1);

float3 worldReflect = reflect(-viewDir, worldNormal);
float3 reflectDir;
reflectDir.x = dot(worldReflect.zxy, -xaxis);
reflectDir.y = dot(worldReflect, upDir);
reflectDir.z = dot(worldReflect, -zaxis);

float reflectLOD = 10.0 * pow(perceptualRoughness, 0.4);
float3 g_PGI = UNITY_SAMPLE_TEXCUBE_LOD(_Global_PGI, reflectDir, reflectLOD);

float scaled_metallicLow = metallicLow * 0.7 + 0.3;
reflectivity = scaled_metallicLow * (1.0 - perceptualRoughness);

return g_PGI * reflectivity;
}
Loading

0 comments on commit 846b03c

Please sign in to comment.