• 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 firestorm713q · Feb 05, 2015 at 02:50 AM · c#graphicsprocedural

Why does my Ocean vibrate?

Relavent Code: https://github.com/firestorm713/ShaderExperiments/blob/master/Assets/Scripts/Plane.cs

I'm trying to create a procedural animated ocean based on the work put forth by Jerry Tessendorf(source) and a few various sources from the community shader thread(Wanted: Ocean Shader)(specifically this HeartsOfOak source and the official-ish community version). I've followed them fairly closely, and I think I have the basic algorithm down, but it's not animating correctly. I know that it isn't in any of the plane generation or graphics functions, because if you switch it to a simple sine wave it functions perfectly, so it's definitely in the Ocean bits that it's messing up.

Here are the relevant functions:

 float GaussianRnd()
     {
         //Debug.Log("GaussianRnd Called");
         float x1 = Random.value;
         float x2 = Random.value;
 
         if (x1 == 0)
             x1 = 0.01f;
 
         return Mathf.Sqrt(-2.0f * Mathf.Log(x1) * Mathf.Cos(2.0f * Mathf.PI * x2));
     }
 
     float Dispersion(int n_prime, int m_prime)
     {
         //Debug.Log("Dispersion Called");
         float w_0 = 2.0f * Mathf.PI / 200.0f;
         float kx = Mathf.PI * (2 * n_prime - LengthWidth) / LengthWidth;
         float kz = Mathf.PI * (2 * m_prime - LengthWidth) / LengthWidth;
         return Mathf.Floor(Mathf.Sqrt(g * Mathf.Sqrt(kx * kx + kz * kz)) / w_0) * w_0;
     }
 
     float Phillips(int n_prime, int m_prime)
     {
         //Debug.Log("Phillips Called");
         Vector2 k = new Vector2(Mathf.PI * (2 * n_prime - LengthWidth) / LengthWidth,
             Mathf.PI * (2 * m_prime - LengthWidth) / LengthWidth);
         float k_length = k.magnitude;
         if (k_length < 0.00001f) return 0;
 
         float k_length2 = k_length * k_length;
         float k_length4 = k_length2 * k_length2;
 
         float k_dot_w = Vector2.Dot(k.normalized, Wind.normalized);
         float k_dot_w2 = k_dot_w * k_dot_w;
 
         float w_length = Wind.magnitude;
         float L = w_length * w_length / g;
         float L2 = L * L;
 
         float damping = 0.001f;
         float l2 = L2 * damping * damping;
 
         return Height * Mathf.Exp(-1.0f / (k_length2 * L2)) / k_length4 * k_dot_w2 * Mathf.Exp(-k_length2 * l2);
     }
 
     ComplexF hTilde_0(int n_prime, int m_prime)
     {
         //Debug.Log("hTilde_0 Called");
         ComplexF r = new ComplexF(GaussianRnd(), GaussianRnd());
         return r * Mathf.Sqrt(Phillips(n_prime, m_prime) / 2.0f);
     }
 
     ComplexF hTilde(int n_prime, int m_prime)
     {
         //Debug.Log("hTilde Called");
         int index = m_prime * LengthWidth + 1 + n_prime;
 
         //float t = Time.time;
 
         ComplexF htilde0 = new ComplexF(ComplexDisplacementMap[index].Re, ComplexDisplacementMap[index].Im);
         ComplexF htilde0mkconj = new ComplexF(ComplexDisplacementMapConj[index].Re, ComplexDisplacementMapConj[index].Im);
 
         float omegat = Dispersion(n_prime, m_prime);
         float sin_ = Mathf.Sin(omegat);
         float cos_ = Mathf.Cos(omegat);
 
         ComplexF c0 = new ComplexF(cos_, sin_);
         ComplexF c1 = new ComplexF(cos_, sin_);
 
         ComplexF res = htilde0 * c0 + htilde0mkconj * c1;
 
         return res;
     }
 
     void EvaluateWaves()
     {
         //Debug.Log("Evaluating Waves at " + Time.time);
         float kx, kz, len, lambda = 1.0f;
         int index;
         for (int m_prime = 0; m_prime < TextureResolution; m_prime++)
         {
             kz = Mathf.PI * (2 * m_prime - TextureResolution) / TextureResolution;
             for (int n_prime = 0; n_prime < TextureResolution; n_prime++)
             {
                 kx = Mathf.PI * (2 * n_prime - TextureResolution) / TextureResolution;
                 len = Mathf.Sqrt(kx * kx + kz * kz);
                 index = m_prime * TextureResolution + n_prime;
 
                 ComplexDisplacementMap[index] = hTilde(n_prime, m_prime);
                 SlopeX[index] = ComplexDisplacementMap[index] * new ComplexF(0, kx);
                 SlopeZ[index] = ComplexDisplacementMap[index] * new ComplexF(0, kz);
                 if (len < 0.000001f)
                 {
                     DisplaceX[index] = new ComplexF(0, 0);
                     DisplaceZ[index] = new ComplexF(0, 0);
                 }
                 else
                 {
                     DisplaceX[index] = ComplexDisplacementMap[index] * new ComplexF(0, -kx / len);
                     DisplaceZ[index] = ComplexDisplacementMap[index] * new ComplexF(0, -kz / len);
                 }
             }
         }
 
         //Debug.Log("Evaluating FFT at " + Time.time);
         Fourier.FFT2(ComplexDisplacementMap, TextureResolution, TextureResolution, FourierDirection.Backward);
         Fourier.FFT2(SlopeX, TextureResolution, TextureResolution, FourierDirection.Backward);
         Fourier.FFT2(SlopeZ, TextureResolution, TextureResolution, FourierDirection.Backward);
         Fourier.FFT2(DisplaceX, TextureResolution, TextureResolution, FourierDirection.Backward);
         Fourier.FFT2(DisplaceZ, TextureResolution, TextureResolution, FourierDirection.Backward);
         //Debug.Log("FFT Complete, now assigning values to displace map at " + Time.time);
         int sign;
         float[] signs = { 1.0f, -1.0f};
         for (int m_prime = 0; m_prime < TextureResolution; m_prime++)
         {
             for (int n_prime = 0; n_prime < TextureResolution; n_prime++)
             {
                 index = m_prime * TextureResolution + n_prime;
                 Debug.Log(ComplexDisplacementMap[index].Re);
                 Debug.Log(DisplaceX[index].Re);
                 Debug.Log(DisplaceZ[index].Re);
                 Debug.Log(SlopeX[index].Re);
                 Debug.Log(SlopeZ[index].Re);
                 sign = (int)signs[(n_prime + m_prime) & 1];
 
                 ComplexDisplacementMap[index] *= sign;
                 DisplaceX[index] *= sign;
                 DisplaceZ[index] *= sign;
                 SlopeX[index] *= sign;
                 SlopeZ[index] *= sign;
                 float xoffset = n_prime % ((TextureResolution - 1) / (LengthWidth - 1));
                 float zoffset = m_prime % ((TextureResolution - 1) / (LengthWidth - 1));
                 DisplacementMap[index].y = ComplexDisplacementMap[index].Re;
                 DisplacementMap[index].x = xoffset * PlaneScale + DisplaceX[index].Re *-1;
                 DisplacementMap[index].z = zoffset * PlaneScale + DisplaceZ[index].Re *-1;
                 Vector3 n = new Vector3(0.0f - SlopeX[index].Re, 0.0f, 0.0f - SlopeZ[index].Re).normalized;
                 Normal[index] = new Color(n.x, n.y, n.z);
 
 
                 if (n_prime == 0 && m_prime == 0)
                 {
                     float tempOffsetX = (TextureResolution-1) % ((TextureResolution - 1) / (LengthWidth - 1));
                     float tempOffsetZ = (TextureResolution-1) % ((TextureResolution - 1) / (LengthWidth - 1));
                     DisplacementMap[index + TextureResolution-1 + (TextureResolution-1) * TextureResolution].y = ComplexDisplacementMap[index].Re;
                     DisplacementMap[index + TextureResolution - 1 + (TextureResolution - 1) * TextureResolution].x = tempOffsetX * PlaneScale + DisplaceX[index].Re * -1;
                     DisplacementMap[index + TextureResolution - 1 + (TextureResolution - 1) * TextureResolution].z = tempOffsetZ * PlaneScale + DisplaceZ[index].Re * -1;
 
                     Normal[index + TextureResolution - 1 + (TextureResolution - 1) * TextureResolution] = new Color(n.x, n.y, n.z);
                 }
                 if (n_prime == 0)
                 {
                     float tempOffsetX = (TextureResolution - 1) % ((TextureResolution - 1) / (LengthWidth - 1));
                     float tempOffsetZ = m_prime % ((TextureResolution - 1) / (LengthWidth - 1));
                     DisplacementMap[index + TextureResolution - 1].y = ComplexDisplacementMap[index].Re;
                     DisplacementMap[index + TextureResolution - 1].x = tempOffsetX * PlaneScale + DisplaceX[index].Re;
                     DisplacementMap[index + TextureResolution - 1].z = tempOffsetZ * PlaneScale + DisplaceZ[index].Re;
 
                     Normal[index + TextureResolution - 1] = new Color(n.x, n.y, n.z);
 
                 }
                 if (m_prime == 0)
                 {
                     float tempOffsetX = n_prime % ((TextureResolution - 1) / (LengthWidth - 1));
                     float tempOffsetZ = (TextureResolution - 1) % ((TextureResolution - 1) / (LengthWidth - 1));
                     DisplacementMap[index + (TextureResolution - 1) * TextureResolution].y = ComplexDisplacementMap[index].Re;
                     DisplacementMap[index + (TextureResolution - 1) * TextureResolution].x = tempOffsetX * PlaneScale + DisplaceX[index].Re;
                     DisplacementMap[index + (TextureResolution - 1) * TextureResolution].z = tempOffsetZ * PlaneScale + DisplaceZ[index].Re;
                     Normal[index + (TextureResolution-1)*TextureResolution] = new Color(n.x, n.y, n.z);
                 }
             }
         }
         //Debug.Log("Complex Displacement Map applied to Displacement Map at " + Time.time);
         NormalMap.SetPixels(Normal);
         NormalMap.Apply();
         DebugNormal.SetPixels(Normal);
         DebugNormal.Apply();
         material.SetTexture("_BumpMap", NormalMap);
         GenerateColorFromHeight();
         GenerateTransparencyFromHeight();
         ApplyHeightMap();
         //GenerateNormalMap();
     }

What did I do wrong?

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Visualizing a Height Field in Real time & in C# 0 Answers

2D Firing an object in the direction the Player is facing! 2 Answers

Generate a mesh around a LineRenderer 2 Answers


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