diff --git a/Assets/Scripts/illusions/wheelColor.cs b/Assets/Scripts/illusions/wheelColor.cs new file mode 100644 index 00000000..a12a1b6d --- /dev/null +++ b/Assets/Scripts/illusions/wheelColor.cs @@ -0,0 +1,133 @@ +using System; +using UnityEngine; + +public class wheelColor: WheelLength +{ + [SerializeField] + private float foregroundIntensity = 1.0f; // Initially white + [SerializeField] + private float backgroundIntensity = 0.0f; // Initially black + + // Property to get/set the foreground color intensity + public float ForegroundIntensity + { + get { return foregroundIntensity; } + set + { + if (!Mathf.Approximately(foregroundIntensity, value)) + { + foregroundIntensity = value; + UpdatePatternColors(); + } + } + } + + // Property to get/set the background color intensity + public float BackgroundIntensity + { + get { return backgroundIntensity; } + set + { + if (!Mathf.Approximately(backgroundIntensity, value)) + { + backgroundIntensity = value; + UpdatePatternColors(); + } + } + } + + void Start() + { + texture = GeneratePattern(); // Generate the initial pattern + ApplyTexture(); // Apply texture to a renderer + } + + public wheelColor(float stepSize = 0.01f) + { + texture = GeneratePattern(); + this.stepSize = stepSize; + } + + // Generate the pattern with dynamic color based on intensity + public override Texture2D GeneratePattern() + { + Texture2D texture = new Texture2D(width, height, TextureFormat.RGBA32, false); + + // Define colors based on intensity + Color foregroundColor1 = new Color(foregroundIntensity, foregroundIntensity, foregroundIntensity, 1f); + Color foregroundColor2 = new Color(1f - foregroundIntensity, 1f - foregroundIntensity, 1f - foregroundIntensity, 1f); // The complementary color + Color backgroundColor = new Color(backgroundIntensity, backgroundIntensity, backgroundIntensity, 1f); + + // Fill the background + for (int i = 0; i < width; i++) + { + for (int j = 0; j < height; j++) + { + texture.SetPixel(i, j, backgroundColor); + } + } + + Vector2 center = new Vector2(width / 2, height / 2); + + // Draw rotated squares with alternating line colors + for (int i = 0; i < numSquares; i++) + { + float angle = 2 * Mathf.PI * i / numSquares; + Vector2 squareCenter = center + new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * radius; + DrawRotatedSquare(texture, squareCenter, squareSize, angle + Mathf.PI / 2, foregroundColor1, foregroundColor2); + } + + // Draw the dot at the center using the first foreground color + DrawDot(texture, center, dotDiameter, foregroundColor1); + + texture.Apply(); + return texture; + } + + public override float GetCurrentRatio() + { + Debug.LogWarning("ForegroundColor: " + foregroundColor + " BackgroundColor: " + backgroundColor); + if (foregroundColor == 0) + { + return 10f; + } + else + { + return (float)backgroundColor / foregroundColor; + } + + } + + public override float GetInitRatio() + { + return 10; + } + + public override void DecreasePatternRatio() + { + + if (backgroundColor > foregroundColor) + { + foregroundColor += stepSize; + backgroundColor -= stepSize; + } + else + { + foregroundColor = backgroundColor = 0.5f; + } + + texture = GeneratePattern(); + } + + public override void IncreasePatternRatio() + { + if (foregroundColor - stepSize >= 0.0f) + { + backgroundColor += stepSize; + foregroundColor -= stepSize; + } + texture = GeneratePattern(); + } + + +} diff --git a/Assets/Scripts/illusions/wheelLength.cs b/Assets/Scripts/illusions/wheelLength.cs index b1330f6e..c042653f 100644 --- a/Assets/Scripts/illusions/wheelLength.cs +++ b/Assets/Scripts/illusions/wheelLength.cs @@ -49,7 +49,7 @@ public override Texture2D GeneratePattern() { float angle = 2 * Mathf.PI * i / numSquares; Vector2 squareCenter = center + new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * adjustedRadius; - DrawRotatedSquare(texture, squareCenter, CurrentPatternHeight, angle + Mathf.PI / 2); // Use CurrentPatternHeight for square size + DrawRotatedSquare(texture, squareCenter, CurrentPatternHeight, angle + Mathf.PI / 2, color.black, color.white); // Use CurrentPatternHeight for square size } // Draw the dot at the center @@ -60,18 +60,24 @@ public override Texture2D GeneratePattern() } - void DrawRotatedSquare(Texture2D texture, Vector2 center, float size, float angle) + void DrawRotatedSquare(Texture2D texture, Vector2 center, float size, float angle, Color color1, Color color2) { Vector2 halfSize = Vector2.one * (size / 2); Vector2[] corners = new Vector2[4]; + + // Define each corner of the square relative to the center corners[0] = RotatePoint(center, center - halfSize, angle); corners[1] = RotatePoint(center, new Vector2(center.x + halfSize.x, center.y - halfSize.y), angle); corners[2] = RotatePoint(center, center + halfSize, angle); corners[3] = RotatePoint(center, new Vector2(center.x - halfSize.x, center.y + halfSize.y), angle); + // Colors for the lines, alternating between two provided colors + Color[] lineColors = { color1, color2, color1, color2 }; + + // Draw lines between each corner point for (int i = 0; i < 4; i++) { - DrawLine(texture, corners[i], corners[(i + 1) % 4], Color.white); + DrawLine(texture, corners[i], corners[(i + 1) % 4], lineColors[i]); } } @@ -84,21 +90,6 @@ Vector2 RotatePoint(Vector2 pivot, Vector2 point, float angle) return rotated + pivot; } - void DrawDot(Texture2D texture, Vector2 position, int diameter, Color color) - { - int radius = diameter / 2; - for (int x = -radius; x <= radius; x++) - { - for (int y = -radius; y <= radius; y++) - { - if (x * x + y * y <= radius * radius) - { - texture.SetPixel((int)(position.x + x), (int)(position.y + y), color); - } - } - } - } - void DrawLine(Texture2D texture, Vector2 start, Vector2 end, Color color) { int x0 = (int)start.x; @@ -123,6 +114,7 @@ void DrawLine(Texture2D texture, Vector2 start, Vector2 end, Color color) } } + public override float GetCurrentRatio() { return ((float)CurrentPatternWidth) / CurrentPatternHeight;