Skip to content

Commit

Permalink
updated components to use the standardshader in the main software ins…
Browse files Browse the repository at this point in the history
…tead of using the individual shaders of each component to have a single source of truth, also changed from compatibility to Forward+ which fixed the bug with the not working shader artifacts
  • Loading branch information
aignermax committed Jan 16, 2024
1 parent 07b6f2d commit 9e36b55
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ public class PhaseShiftCalculator
{
public const double TileWidthInNM = 250000.0;
public const double refractionIndexSiliconNitride = 2.0;
public static double GetDegrees(double waveGuideLength, double laserWaveLength)

public static double CalculateInRad(double waveGuideLength, double laserWaveLength)
{
var phaseShift = 360 * refractionIndexSiliconNitride * waveGuideLength / laserWaveLength;
phaseShift %= (360);
var phaseShift = 2 * Math.PI * refractionIndexSiliconNitride * waveGuideLength / laserWaveLength;
phaseShift %= (2 * Math.PI);
return phaseShift;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Connect-A-Pic-Core/Components/Creation/SMatrixFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public static SMatrix GetSMatrix(List<Connection> connections, Part[,] parts , d
var connectionWeights = new Dictionary<(Guid, Guid), Complex>();
foreach (Connection connection in connections)
{
var phaseShiftDegrees = PhaseShiftCalculator.GetDegrees(connection.WireLengthNM, laserWaveLengthNM);
connectionWeights.Add((connection.FromPin, connection.ToPin), Complex.FromPolarCoordinates(connection.Magnitude, phaseShiftDegrees));
var phaseShiftInRad = PhaseShiftCalculator.CalculateInRad(connection.WireLengthNM, laserWaveLengthNM);
connectionWeights.Add((connection.FromPin, connection.ToPin), Complex.FromPolarCoordinates(connection.Magnitude, phaseShiftInRad));
};

componentConnections.SetValues(connectionWeights);
Expand Down
Binary file modified Scenes/Components/DirectionalCouplerPCK.pck
Binary file not shown.
94 changes: 46 additions & 48 deletions Scenes/Components/LightOverlayShaded.tres
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ code = "// Laser Farbe
shader_type canvas_item;
render_mode blend_add;

uniform int version = 1;
uniform vec4 laserColor;

uniform vec4 lightInFlow1; // x = intensity, y = phase but in RAD, z = offsetx, w = offsety
uniform vec4 lightInFlow1; // x = intensity, y = phase in RAD, z = offsetx, w = offsety
uniform vec4 lightOutFlow1;
uniform vec4 lightInFlow2;
uniform vec4 lightOutFlow2;
Expand All @@ -34,70 +35,67 @@ uniform sampler2D animation6;
uniform sampler2D animation7;
uniform sampler2D animation8;
// ... die hälfte von oben, also 8 (in/out ist ja nur umgedreht in der Zeit)
uniform float numAnimationColumns = 4.0;
vec4 getAnimationFrameColor(sampler2D animationTexture, vec2 uvCoord, float elapsedTim2e, float speed, vec4 lightAttributes) {
float elapsedTime = round(TIME * speed);
float lightPhase = lightAttributes.y;
float animationOffset = elapsedTime + round(lightPhase * speed);

// currentColumn is the current Column that we are reading the data from
int currentColumn = int(animationOffset) % int(numAnimationColumns);
// frameshift is the offset of where the image is being read like 0, 0.25, 0.5, 0,75
// because we only want to read the lines of the images in a certain column as the image is divided in 4 columns. (or numAnimationColumns, which is 4)
float frameShift = float(currentColumn) / numAnimationColumns;
uniform float numAnimationColumns = 4.0;

vec4 getAnimationFrameColor(sampler2D animationTexture, vec2 uvCoord, float speed, vec4 lightInOutFlow) {
float phaseShift = lightInOutFlow.y;

int currentFrameIndex = int(TIME * speed + phaseShift) % int(numAnimationColumns);
float frameShift = float(currentFrameIndex) / numAnimationColumns; // frameshift shifts the UV.x so that the starting point is in the proper column
if(speed < 0.0) // if the speed is negative, then the animation should playbackwards, so 0.75, 0.5, 0.25, 0
{
frameShift = 1.0 -abs(frameShift- 1.0/numAnimationColumns);
frameShift = 0.75 - frameShift;
}

vec2 adjustedUV = vec2((uvCoord.x / numAnimationColumns + frameShift), uvCoord.y);
return texture(animationTexture, adjustedUV);
}

float getIntensityOfLight(vec4 currentInflowColor, float lightInflowIntensity){
float subtractBlueFromRedLight(vec4 currentInflowColor, float lightInflowIntensity){

return currentInflowColor.a *(currentInflowColor.r - currentInflowColor.b) * lightInflowIntensity;
return currentInflowColor.a *(currentInflowColor.r - currentInflowColor.b) * abs(lightInflowIntensity);
}
void fragment(){
float animationspeed = 2.0f;
float animationTime = round(TIME * animationspeed);

vec4 col_baseTexture = texture(TEXTURE, UV);
vec4 col_anim1in = getAnimationFrameColor(animation1,UV,animationTime,animationspeed, lightInFlow1);
vec4 col_anim1out = getAnimationFrameColor(animation1,UV,animationTime,-animationspeed, lightOutFlow1);
vec4 col_anim2in = getAnimationFrameColor(animation2,UV,animationTime,animationspeed, lightInFlow2);
vec4 col_anim2out = getAnimationFrameColor(animation2,UV,animationTime,-animationspeed, lightOutFlow2);
vec4 col_anim3in = getAnimationFrameColor(animation3,UV,animationTime,animationspeed, lightInFlow3);
vec4 col_anim3out = getAnimationFrameColor(animation3,UV,animationTime,-animationspeed, lightOutFlow3);
vec4 col_anim4in = getAnimationFrameColor(animation4,UV,animationTime,animationspeed, lightInFlow4);
vec4 col_anim4out = getAnimationFrameColor(animation4,UV,animationTime,-animationspeed, lightOutFlow4);
vec4 col_anim5in = getAnimationFrameColor(animation5,UV,animationTime,animationspeed, lightInFlow5);
vec4 col_anim5out = getAnimationFrameColor(animation5,UV,animationTime,-animationspeed, lightOutFlow5);
vec4 col_anim6in = getAnimationFrameColor(animation6,UV,animationTime,animationspeed, lightInFlow6);
vec4 col_anim6out = getAnimationFrameColor(animation6,UV,animationTime,-animationspeed, lightOutFlow6);
vec4 col_anim7in = getAnimationFrameColor(animation7,UV,animationTime,animationspeed, lightInFlow7);
vec4 col_anim7out = getAnimationFrameColor(animation7,UV,animationTime,-animationspeed, lightOutFlow7);
vec4 col_anim8in = getAnimationFrameColor(animation8,UV,animationTime,animationspeed, lightInFlow8);
vec4 col_anim8out = getAnimationFrameColor(animation8,UV,animationTime,-animationspeed, lightOutFlow8);
vec4 col_anim1in = getAnimationFrameColor(animation1,UV,animationspeed, lightInFlow1);
vec4 col_anim1out = getAnimationFrameColor(animation1,UV,-animationspeed, lightOutFlow1);
vec4 col_anim2in = getAnimationFrameColor(animation2,UV,animationspeed, lightInFlow2);
vec4 col_anim2out = getAnimationFrameColor(animation2,UV,-animationspeed, lightOutFlow2);
vec4 col_anim3in = getAnimationFrameColor(animation3,UV,animationspeed, lightInFlow3);
vec4 col_anim3out = getAnimationFrameColor(animation3,UV,-animationspeed, lightOutFlow3);
vec4 col_anim4in = getAnimationFrameColor(animation4,UV,animationspeed, lightInFlow4);
vec4 col_anim4out = getAnimationFrameColor(animation4,UV,-animationspeed, lightOutFlow4);
vec4 col_anim5in = getAnimationFrameColor(animation5,UV,animationspeed, lightInFlow5);
vec4 col_anim5out = getAnimationFrameColor(animation5,UV,-animationspeed, lightOutFlow5);
vec4 col_anim6in = getAnimationFrameColor(animation6,UV,animationspeed, lightInFlow6);
vec4 col_anim6out = getAnimationFrameColor(animation6,UV,-animationspeed, lightOutFlow6);
vec4 col_anim7in = getAnimationFrameColor(animation7,UV,animationspeed, lightInFlow7);
vec4 col_anim7out = getAnimationFrameColor(animation7,UV,-animationspeed, lightOutFlow7);
vec4 col_anim8in = getAnimationFrameColor(animation8,UV,animationspeed, lightInFlow8);
vec4 col_anim8out = getAnimationFrameColor(animation8,UV,-animationspeed, lightOutFlow8);

// Der rot Kanal = höhen, z.B.
// Der blau Kanal = tiefen

float intensity = getIntensityOfLight( col_anim1in, lightInFlow1.x) +
getIntensityOfLight( col_anim2in, lightInFlow2.x) +
getIntensityOfLight( col_anim3in, lightInFlow3.x) +
getIntensityOfLight( col_anim4in, lightInFlow4.x) +
getIntensityOfLight( col_anim5in, lightInFlow5.x) +
getIntensityOfLight( col_anim6in, lightInFlow6.x) +
getIntensityOfLight( col_anim7in, lightInFlow7.x) +
getIntensityOfLight( col_anim8in, lightInFlow8.x) +
getIntensityOfLight( col_anim1out, lightOutFlow1.x) +
getIntensityOfLight( col_anim2out, lightOutFlow2.x) +
getIntensityOfLight( col_anim3out, lightOutFlow3.x) +
getIntensityOfLight( col_anim4out, lightOutFlow4.x) +
getIntensityOfLight( col_anim5out, lightOutFlow5.x) +
getIntensityOfLight( col_anim6out, lightOutFlow6.x) +
getIntensityOfLight( col_anim7out, lightOutFlow7.x) +
getIntensityOfLight( col_anim8out, lightOutFlow8.x);
float intensity = subtractBlueFromRedLight( col_anim1in, lightInFlow1.x) +
subtractBlueFromRedLight( col_anim2in, lightInFlow2.x) +
subtractBlueFromRedLight( col_anim3in, lightInFlow3.x) +
subtractBlueFromRedLight( col_anim4in, lightInFlow4.x) +
subtractBlueFromRedLight( col_anim5in, lightInFlow5.x) +
subtractBlueFromRedLight( col_anim6in, lightInFlow6.x) +
subtractBlueFromRedLight( col_anim7in, lightInFlow7.x) +
subtractBlueFromRedLight( col_anim8in, lightInFlow8.x) +
subtractBlueFromRedLight( col_anim1out, lightOutFlow1.x) +
subtractBlueFromRedLight( col_anim2out, lightOutFlow2.x) +
subtractBlueFromRedLight( col_anim3out, lightOutFlow3.x) +
subtractBlueFromRedLight( col_anim4out, lightOutFlow4.x) +
subtractBlueFromRedLight( col_anim5out, lightOutFlow5.x) +
subtractBlueFromRedLight( col_anim6out, lightOutFlow6.x) +
subtractBlueFromRedLight( col_anim7out, lightOutFlow7.x) +
subtractBlueFromRedLight( col_anim8out, lightOutFlow8.x);


COLOR = laserColor * 2.5 * abs(intensity);
}"
14 changes: 7 additions & 7 deletions Scripts/View/ComponentViews/ComponentView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public partial class ComponentView : TextureRect
private new float RotationDegrees { get => base.RotationDegrees; set => base.RotationDegrees = value; }
private new float Rotation { get => base.Rotation; set => base.Rotation = value; }
private DiscreteRotation _rotationCC;
public ShaderMaterial LightOverlayShader { get; set; }
public DiscreteRotation RotationCC
{
get => _rotationCC;
Expand All @@ -53,7 +54,7 @@ public override void _Ready()
RotationCC = _rotationCC;
}

private void FindAndAssignOverlay()
private void FindAndAssignOverlayBlueprint()
{
OverlayBluePrint = FindChild("Overlay", true, false) as Sprite2D;

Expand All @@ -76,7 +77,9 @@ public void InitializeComponent(int componentTypeNumber, List<AnimationSlotOverl
this.TypeNumber = componentTypeNumber;
this.WidthInTiles = widthInTiles;
this.HeightInTiles = heightInTiles;
FindAndAssignOverlay();
LightOverlayShader = new ShaderMaterial();
LightOverlayShader.Shader = ResourceLoader.Singleton.Load("res://Scenes/Components/LightOverlayShaded.tres").Duplicate() as Shader;
FindAndAssignOverlayBlueprint();
this.CheckForNull(x => x.OverlayBluePrint);
InitializeLightOverlays();
foreach (var slotData in slotDataSets)
Expand Down Expand Up @@ -146,11 +149,8 @@ protected Sprite2D DuplicateAndConfigureOverlay(Sprite2D overlayDraft, Godot.Col
var newOverlay = overlayDraft.Duplicate() as Sprite2D;
overlayDraft.GetParent().AddChild(newOverlay);
newOverlay.Hide();
if (overlayDraft.Material is ShaderMaterial materialDraft)
{
newOverlay.Material = materialDraft.Duplicate(true) as ShaderMaterial;
(newOverlay.Material as ShaderMaterial).SetShaderParameter("laserColor", laserColor);
}
newOverlay.Material = LightOverlayShader.Duplicate() as ShaderMaterial;
(newOverlay.Material as ShaderMaterial).SetShaderParameter("laserColor", laserColor);
return newOverlay;
}
public void HideLightVector()
Expand Down
6 changes: 2 additions & 4 deletions UnitTests/PhaseShiftTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ public class PhaseShiftTests
[Fact]
public static void ComplexFactorTest()
{
Complex factor1 = PhaseShiftCalculator.GetDegrees(PhaseShiftCalculator.TileWidthInNM, StandardWaveLengths.RedNM); // widthInTiles is 1 for the first component
Complex factor2 = PhaseShiftCalculator.GetDegrees(PhaseShiftCalculator.TileWidthInNM, StandardWaveLengths.RedNM); // widthInTiles is 1 also for the second component

Complex result = factor1 * factor2;
Complex factor1 = PhaseShiftCalculator.CalculateInRad(PhaseShiftCalculator.TileWidthInNM, StandardWaveLengths.RedNM); // widthInTiles is 1 for the first component
Complex factor2 = PhaseShiftCalculator.CalculateInRad(PhaseShiftCalculator.TileWidthInNM, StandardWaveLengths.RedNM); // widthInTiles is 1 also for the second component

}
[Fact]
Expand Down
1 change: 0 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@ enabled=PackedStringArray()

[rendering]

renderer/rendering_method="gl_compatibility"
renderer/rendering_method.mobile="gl_compatibility"

0 comments on commit 9e36b55

Please sign in to comment.