Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class CreateAssetBundles
static void BuildAllAssetBundles ()
{
// Put the bundles in a folder called "AssetBundles"
//var outDir = "Assets/AssetBundles";
var outDir = "C:/Steam/steamapps/common/Kerbal Space Program/GameData/Singularity/shaders";
var outDir = "Assets/AssetBundles";
//var outDir = "C:/Steam/steamapps/common/Kerbal Space Program/GameData/Singularity/shaders";
var outDir2 = "C:/Steam/steamapps/common/Kerbal Space Program 1.8.1/GameData/Singularity/shaders";

if (!Directory.Exists (outDir))
Expand Down
21 changes: 21 additions & 0 deletions Singularity/Shaders/SingularityShaders/Assets/Scripts/BlackHole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class BlackHole : MonoBehaviour
[SerializeField]
float gravity;

[SerializeField]
float schwarzschildRadius;

[SerializeField]
Camera sceneCam;

Expand All @@ -42,6 +45,18 @@ public class BlackHole : MonoBehaviour
[SerializeField]
float rotationSpeed;

[SerializeField]
float dopplerIntensityRate;

[SerializeField]
float dopplerIntensityFactor;

[SerializeField]
float dopplerIntensityOffset;

[SerializeField]
float dopplerColorFactor;

RenderTexture screenBuffer;
CommandBuffer screenCopyCommandBuffer;

Expand Down Expand Up @@ -74,11 +89,17 @@ void Update()
//blackHoleMaterial.SetVector("blackhole", new Vector4(gameObject.transform.position.x,gameObject.transform.position.y,gameObject.transform.position.z,radius));
blackHoleMaterial.SetFloat("blackHoleRadius",radius);
blackHoleMaterial.SetFloat("gravity", gravity);
blackHoleMaterial.SetFloat("schwarzschildRadius", schwarzschildRadius);
blackHoleMaterial.SetVector("blackholeDisk", new Vector4(DiskInnerRadius*DiskNormal.x,DiskInnerRadius*DiskNormal.y,DiskInnerRadius*DiskNormal.z,DiskOuterRadius));
blackHoleMaterial.SetVector("diskNormal", DiskNormal);
blackHoleMaterial.SetFloat("diskInnerRadius", DiskInnerRadius);
blackHoleMaterial.SetFloat("diskOuterRadius", DiskOuterRadius);

blackHoleMaterial.SetFloat ("dopplerIntensityRate", dopplerIntensityRate);
blackHoleMaterial.SetFloat ("dopplerIntensityFactor", dopplerIntensityFactor);
blackHoleMaterial.SetFloat ("dopplerIntensityOffset", dopplerIntensityOffset);
blackHoleMaterial.SetFloat ("dopplerColorFactor", dopplerColorFactor);

//gameObject.transform.position = new Vector3(Mathf.Sin(0.53f*Time.time), 2f+3f*Mathf.Cos(0.74f*Time.time), Mathf.Cos(0.22f*Time.time));

sceneCam.transform.RotateAround(gameObject.transform.position, Vector3.up, 10 * Time.deltaTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
uniform float enclosingMeshRadius;
uniform float blackHoleRadius;
uniform float gravity;
uniform float schwarzschildRadius;

uniform sampler2D _CameraDepthTexture;
uniform sampler2D SingularityDepthTexture;
Expand All @@ -66,6 +67,11 @@
uniform float diskInnerRadius;
uniform float diskOuterRadius;

uniform float dopplerIntensityRate;
uniform float dopplerIntensityFactor;
uniform float dopplerIntensityOffset;
uniform float dopplerColorFactor;

uniform float rotationSpeed;
uniform float universalTime;

Expand All @@ -91,30 +97,29 @@
return o;
}


float3 accretionDiskColor(float3 pos, float3 base1, float3 base2, float3 blackHoleOrigin)
{

pos = pos - blackHoleOrigin;

float3 posOrigin = pos - blackHoleOrigin;
float dist = length(posOrigin);
#if defined (RADIAL_DISK_MAPPING_ON)
//TODO: make this rotate, move TWOPI to defines
//need to rotate base1 and base2?
float dist = length(pos);


float v = clamp((dist - diskInnerRadius) / (diskOuterRadius - diskInnerRadius), 0.0, 1.0);

// float3 base = cross(blackholeDisk.xyz, float3(0.0, 0.0, 1.0));
float angle = acos(dot(normalize(base1), normalize(pos)));
if (dot(cross(base1, pos), diskNormal) < 0.0) angle = -angle;
float angle = acos(dot(normalize(base1), normalize(posOrigin)));
if (dot(cross(base1, posOrigin), diskNormal) < 0.0) angle = -angle;
angle-= universalTime * rotationSpeed;

float u = 0.5 - angle / TWOPI;

float4 color = tex2Dlod(AccretionDisk, float4(u, v,0.0,0.0));
#else
float u = dot (base1,normalize(pos)) * (length(pos)-diskInnerRadius) / (diskOuterRadius-diskInnerRadius);
float v = dot (base2,normalize(pos)) * (length(pos)-diskInnerRadius) / (diskOuterRadius-diskInnerRadius);
float u = dot (base1,normalize(posOrigin)) * (length(posOrigin)-diskInnerRadius) / (diskOuterRadius-diskInnerRadius);
float v = dot (base2,normalize(posOrigin)) * (length(posOrigin)-diskInnerRadius) / (diskOuterRadius-diskInnerRadius);

float2 UV = float2(u,v);

Expand All @@ -133,6 +138,25 @@

float4 color = tex2Dlod(AccretionDisk, float4(UV,0.0,0.0));
#endif
float3 posCamera = pos - _WorldSpaceCameraPos.xyz;
float dopplerIntensity = dot(diskNormal, cross(normalize(posOrigin), normalize(posCamera))) / pow(dist / diskInnerRadius, 0.25);
/*
color.r *= ((dopplerIntensityFactor - dopplerColorFactor) * tanh(dopplerIntensityRate * dopplerIntensity) + dopplerIntensityOffset);
color.g *= (dopplerIntensityFactor * tanh(dopplerIntensityRate * dopplerIntensity) + dopplerIntensityOffset);
color.b *= ((dopplerIntensityFactor + dopplerColorFactor) * tanh(dopplerIntensityRate * dopplerIntensity) + dopplerIntensityOffset);
*/
if (dopplerIntensity>=0)
{
color.r *= ((dopplerIntensityFactor - dopplerColorFactor) * (-1.0f/(dopplerIntensityRate * dopplerIntensity + 1.0f) + 1.0f) + dopplerIntensityOffset);
color.g *= (dopplerIntensityFactor * (-1.0f/(dopplerIntensityRate * dopplerIntensity + 1.0f) + 1.0f) + dopplerIntensityOffset);
color.b *= ((dopplerIntensityFactor + dopplerColorFactor) * (-1.0f/(dopplerIntensityRate * dopplerIntensity + 1.0f) + 1.0f) + dopplerIntensityOffset);
}
else
{
color.r *= ((dopplerIntensityFactor - dopplerColorFactor) * (-1.0f/(dopplerIntensityRate * dopplerIntensity - 1.0f) - 1.0f) + dopplerIntensityOffset);
color.g *= (dopplerIntensityFactor * (-1.0f/(dopplerIntensityRate * dopplerIntensity - 1.0f) - 1.0f) + dopplerIntensityOffset);
color.b *= ((dopplerIntensityFactor + dopplerColorFactor) * (-1.0f/(dopplerIntensityRate * dopplerIntensity - 1.0f) - 1.0f) + dopplerIntensityOffset);
}
return color.rgb*color.a;
}

Expand Down Expand Up @@ -263,7 +287,8 @@
// 0.05: rate of smaller steps when approaching blackhole
stepSize = rayDistance - (blackHoleRadius *0.05);

rayAccel = normalize(gravityVector) * gravity / (rayDistance * rayDistance);
//rayAccel = normalize(gravityVector) * gravity / (rayDistance * rayDistance);
rayAccel = normalize(gravityVector) * schwarzschildRadius*schwarzschildRadius/1.04976E9 / (rayDistance * rayDistance);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest precomputing this value before passing it to the shader.

Also there is now blackHoleRadius and schwarzschildRadius, what's the difference between both? Can one be expressed in terms of the other?

Also remove the gravity parameter if it's no longer used

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In v0.994, I switched back to gravity in the shader. Now gravity can be pre-computed using schwarzschildRadius.

blackHoleRadius is used in the original code. It's the scaled space radius of the black hole and equals to schwarzschildRadius/6000. schwarzschildRadius functions as an alternative to gravity, which equals the actual Schwarzschild radius in meters.

Copy link
Owner

@LGhassen LGhassen May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I just didn't want there to be multiple radius variables in the shader code. So schwarzschildRadius is basically just the radius in local space units but exposed to the user? Sounds good.


float accelFadeOut = clamp( ( length(rayAccel) - 0.001 ) / 0.001, 0.0, 1.0); //we fade out acceleration over the last 0.001 so we can have a smaller enclosing mesh
rayAccel = lerp(0.0, rayAccel, accelFadeOut);
Expand Down
4 changes: 2 additions & 2 deletions Singularity/Singularity/Singularity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Reflection;
using UnityEngine;

[assembly: AssemblyVersion("0.991.*")]
[assembly: AssemblyVersion("0.993.*")]
namespace Singularity
{
[KSPAddon(KSPAddon.Startup.AllGameScenes, false)]
Expand Down Expand Up @@ -178,7 +178,7 @@ void LoadSingularities()

void AddSingularityObject (ConfigNode _cn)
{
if (_cn.HasValue ("name") && _cn.HasValue ("gravity"))
if (_cn.HasValue ("name") && (_cn.HasValue ("gravity") || _cn.HasValue ("schwarzschildRadius")))
{
Transform scaledBodyTransform = ScaledSpace.Instance.transform.FindChild (_cn.GetValue ("name"));
if (!ReferenceEquals (scaledBodyTransform, null))
Expand Down
33 changes: 29 additions & 4 deletions Singularity/Singularity/SingularityObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class SingularityObject : MonoBehaviour
[Persistent] public string name;

[Persistent] public float gravity = 1f;
[Persistent] public float schwarzschildRadius = 0f;

[Persistent] public bool hideCelestialBody = true;

Expand All @@ -30,6 +31,11 @@ public class SingularityObject : MonoBehaviour

[Persistent] public bool depthWrite = true;

[Persistent] public float dopplerEffectIntensityRate = 0f;
[Persistent] public float dopplerEffectIntensityFactor = 0.8f;
[Persistent] public float dopplerEffectIntensityOffset = 1.0f;
[Persistent] public float dopplerEffectColorFactor = 0f;

float scaledRadius = 1f;
float enclosingMeshRadius = 1f;

Expand Down Expand Up @@ -58,13 +64,20 @@ public void Init(ConfigNode _cn)

singularityMaterial = new Material(Singularity.LoadedShaders ["Singularity/BlackHoleAccretionDisk"]);

scaledRadius = Mathf.Sqrt (Mathf.Max(gravity,0f)) * 5f; // The apparent radius (in scaled Space) of the black hole (or event horizon), not physically correct
if (!_cn.HasValue ("schwarzschildRadius"))
{
schwarzschildRadius = 32400f * Mathf.Sqrt(Mathf.Abs(gravity));
}
//scaledRadius = Mathf.Sqrt (Mathf.Max(gravity,0f)) * 5f; // The apparent radius (in scaled Space) of the black hole (or event horizon), not physically correct
scaledRadius = schwarzschildRadius / 6000f * 0.926f; // The apparent radius (in scaled Space) of the black hole (or event horizon), not physically correct
singularityMaterial.SetFloat("blackHoleRadius", scaledRadius);

enclosingMeshRadius = scaleEnclosingMesh * Mathf.Sqrt (Mathf.Abs(gravity)) * 120f; // The radius (in scaled Space) at which the gravity no longer warps the image
//enclosingMeshRadius = scaleEnclosingMesh * Mathf.Sqrt (Mathf.Abs(gravity)) * 120f; // The radius (in scaled Space) at which the gravity no longer warps the image
enclosingMeshRadius = schwarzschildRadius / 27f; // The radius (in scaled Space) at which the gravity no longer warps the image
// Serves as the radius of our enclosing mesh, value finetuned manually
singularityMaterial.SetFloat("enclosingMeshRadius", enclosingMeshRadius);
singularityMaterial.SetFloat("gravity", gravity);
singularityMaterial.SetFloat("schwarzschildRadius", schwarzschildRadius);
singularityMaterial.renderQueue = 2501; //No need to be same renderqueue as scatterer atmos, i's treated as an opaque object, when atmos/clouds are behind it they are included in the re-rendered scaledSpace scene
//Otherwise they are handled by depth-testing

Expand Down Expand Up @@ -160,6 +173,11 @@ void ConfigureAccretionDisk ()
singularityMaterial.SetFloat ("diskInnerRadius", accretionDiskInnerRadius / 6000f); //change to scaledSpace scale
singularityMaterial.SetFloat ("diskOuterRadius", accretionDiskOuterRadius / 6000f);

singularityMaterial.SetFloat ("dopplerIntensityRate", dopplerEffectIntensityRate);
singularityMaterial.SetFloat ("dopplerIntensityFactor", dopplerEffectIntensityFactor);
singularityMaterial.SetFloat ("dopplerIntensityOffset", dopplerEffectIntensityOffset);
singularityMaterial.SetFloat ("dopplerColorFactor", dopplerEffectColorFactor);

//convert from RPM to rad/s
singularityMaterial.SetFloat("rotationSpeed", accretionDiskRotationSpeed * (Mathf.PI * 2) / 60);

Expand Down Expand Up @@ -287,10 +305,16 @@ public void ApplyFromUI(ConfigNode _cn)
return;
}

scaledRadius = Mathf.Sqrt (Mathf.Max(gravity,0f)) * 5f;
//scaledRadius = Mathf.Sqrt (Mathf.Max(gravity,0f)) * 5f;
if (!_cn.HasValue ("schwarzschildRadius"))
{
schwarzschildRadius = 32400f * Mathf.Sqrt(Mathf.Abs(gravity));
}
scaledRadius = schwarzschildRadius / 6000f * 0.926f;
singularityMaterial.SetFloat("blackHoleRadius", scaledRadius);

singularityMaterial.SetFloat("gravity", gravity);
singularityMaterial.SetFloat("schwarzschildRadius", schwarzschildRadius);

ConfigureAccretionDisk ();

Expand All @@ -303,7 +327,8 @@ public void ApplyFromUI(ConfigNode _cn)
UnHideCelestialBody();
}

enclosingMeshRadius = scaleEnclosingMesh * Mathf.Sqrt (Mathf.Abs(gravity)) * 120f;
//enclosingMeshRadius = scaleEnclosingMesh * Mathf.Sqrt (Mathf.Abs(gravity)) * 120f;
enclosingMeshRadius = schwarzschildRadius / 27f;
singularityMaterial.SetFloat("enclosingMeshRadius", enclosingMeshRadius);
singularityGO.transform.localScale = new Vector3 (enclosingMeshRadius / gameObject.transform.localScale.x, enclosingMeshRadius / gameObject.transform.localScale.y, enclosingMeshRadius / gameObject.transform.localScale.z);

Expand Down
2 changes: 1 addition & 1 deletion Singularity/Singularity/SingularityUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void OnGUI ()
{
if (uiVisible)
{
windowRect = GUILayout.Window (windowId, windowRect, DrawWindow,"Singularity 0.991");
windowRect = GUILayout.Window (windowId, windowRect, DrawWindow,"Singularity 0.993");

//prevent window from going offscreen
windowRect.x = Mathf.Clamp(windowRect.x,0,Screen.width-windowRect.width);
Expand Down
Binary file not shown.
Binary file added Singularity/Singularity/obj/Debug/Singularity.dll
Binary file not shown.