Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by dbrue · May 04, 2017 at 09:41 AM · generating

Efficent generation of moving grating image

How to efficently generate and visualize a moving grating image with adjustable resolution?

  • The distance (frequency ) of the patern has to be adjustable.

  • And the whole pattern has to be moving with adjustable speed.

  • Idealy the profile should be adaptable as shown under a), and b) in the figure with a square and a sinusoidal wave.

http://webvision.med.utah.edu/imageswv/KallSpat14.jpg

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · May 04, 2017 at 10:07 AM

The question is a bit unspecific about the actual purpose and what the texture should look like. Where and how is it used? For example if it's just vertical bars, generating a texture that is only one pixel high is enough. "Moving" (i guess horizontally?) can be done by simply using the texture offset in the material which will basically "scroll" the texture.

As for the actual generation of the texture, for most flexibility you could simply use an AnimationCurve. This let you specify the pattern that should be used, just like your two graphs. Though if you prefer exact results you can directly use Mathf.Sin

So you could simply create a texture that is 2048x1 pixel and just iterate over the pixel array and set the color based on the sampled value (either from the AnimationCurve or your preferred mathematical function) based on the x index of the pixel.

So inside a script you could simply do:

 public AnimationCurve pattern;
 public Texture2D texture;
 public int resolution = 2048;
 public float frequencyMultiplier = 1.0f;
 public float  minValue = 0f;
 public float  maxValue = 1.0f;
 
 void Start()
 {
     Color[] colors = new Color[resolution];
     for(int i = 0; i < resolution; i++)
     {
         float t = ((float)i / resolution) * frequencyMultiplier;
         t = Mathf.Repeat(t, 1.0f);
         float v = minValue + pattern.Evaluate(t) * (maxValue - minValue);
         colors[i] = new Color(v, v, v, 1f);
     }
     texture = new Texture2D(resolution, 1);
     texture.SetPixels(colors);
     texture.Apply();
     // use your texture somewhere
 }

Note if instead you want to use Mathf.Sin you can replace pattern.Evaluate(t) with Mathf.Sin(t * Mathf.PI * 2f).

By default the "frequencyMultiplier" is set to 1, so it will perform one period over the whole image. If you set it to 2.0 there will be two periods

If you use an animationcurve, make sure your curve uses the range 0.0 - 1.0 on both axis. If you want to be able to define a curve that is longer on the x axis, you have to multiply "t" inside the Evaluate method by the length of your curve. The length of the curve can be determined by:

 float L = pattern[pattern.length-1].time;
 // [ ... ]
 pattern.Evaluate(t * L);

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Generating with scripts turned off? 2 Answers

Generating Textures in Shaders? 1 Answer

Snow Effect Map Wide 0 Answers

Generating a 2D map out of a tileset problem. Array and If statements. HELP! 0 Answers

Generate a random number that is different every time? 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