• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Matthew Scott · Jul 06, 2014 at 01:59 AM · textureproceduralpython

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assigning UV Map to model at runtime 0 Answers

Procedural Texturing on Multiple Terrain Mesh 1 Answer

Do I need to split my mesh up to properly UV texture it? 1 Answer

Pixelated Texture Edges on Terrain 0 Answers

Terrain Toolkit and Texturing via Code HELP! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges