- Home /
Logarithmic Spiral Code Assistance
I have been trying to create a spiral texture using code and eventually managed to find any excellent example in a blog post I found which gives the exact effect I'm trying to accomplish and kindly supplied some PYTHON source code too.
I took to converting the python code into UnityScript/JS and for the most part was pretty successful...
#pragma strict
var resolution : int = 1000;
var settingOne : float = 1.0;
var settingTwo : float = 0.5;
var texture : Texture2D;
function Start(){
texture = new Texture2D(resolution, resolution);
DrawSpiral();
}
function Spiral(x : int, y : int, a : float, b : float){
//Define "Euler's Constant", calculate the target radius and theta
var mathfE : float = 2.718281;
var r : float = Mathf.Sqrt(x * x + y * y);
var t : float = Mathf.Atan2(y, x);
/*Early exit if the point requested is the origin itself
to avoid taking the logarithm of zero in the next step*/
if(r == 0){
return 0;
}else{
//Calculate the floating point approximation for n
var n : float = (Mathf.Log(r / a) / b - t) / (2.0 * Mathf.PI);
//Find the two possible radii for the closest point
var upperR : float = a * Mathf.Pow(mathfE, b * (t + 2.0 * Mathf.PI * Mathf.Ceil(n)));
var lowerR : float = a * Mathf.Pow(mathfE, b * (t + 2.0 * Mathf.PI * Mathf.Floor(n)));
return Mathf.Min(Mathf.Abs(upperR - r), Mathf.Abs(r - lowerR));
}
}
function DrawSpiral(){
var sample : float;
for(var i : int = 0; i < texture.height; i++){
for(var j : int = 0; j < texture.width; j++){
sample = Mathf.Min(255, Mathf.Floor(Spiral(i, j, settingOne, settingTwo)));
texture.SetPixel(i, j, Color(sample, sample, sample));
}
}
texture.Apply();
}
THE PROBLEM
Where I start to get lost is the section of python code that actually generates the image. My code as it stands generates very thin spirals from each of the four corners rather than the centre...and it certainly doesn't look nice and interpolated/smooth like in the example shown, which is the most important part. I'm sure this is a problem with how I have written the code to generate the texture in my own version.
Would anybody with some python experience be kind enough to offer some insight here? Or anybody with an idea of what I'm doing wrong?
Thanks in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220613153340im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Procedural or Animated Textur in AirAttack Zeppelin Wreck (IphoneGame)? 1 Answer
Calling "Generate Alpha From Grayscale" from scripting?? 2 Answers
Adding multiple textures to a procedurally generated terrain mesh? 0 Answers
How do you add terrain textures at runtime? 1 Answer
Setting mipmaps 0 Answers