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
0
Question by RobAnthem · Jan 14, 2017 at 07:36 AM · texturecolorproceduralprocedural-generationprocedural texturing

Can't find a way to simplify this code... any help appreciated...

So I'm writing an extension for texture generation, and when I ran into bricks I quickly realized that some simple fractal or perlin noise was not enough, I needed to calculate the grout lines accurately. So after a few hours this is what I came up with... I spent the rest of the day combing over it and simplifying and fixing things where I saw it needed it, but I still think I am MASSIVELY overlooking some easy steps...

 else if (textureType == TextureType.Bricks)
 {
     //Calculate the noise scale.
     float xCoord = xOrg + x / (float)noiseTex.width * scale;
     float yCoord = yOrg + y / (float)noiseTex.height * scale;
     //Calculate the linear scale of noise.
     float xyValue = xCoord + addNoise(x / (float)noiseTex.width * noiseScaleX * scale, y / (float)noiseTex.height * noiseScaleY * scale, noiseSize) * noiseStrength;
     float sample = Mathf.Sin(xyValue);
     //Create our ratio sample.
     if (cutoff > 0.0f)
         sample = (sample - cutoff) / (1.0f - cutoff);
     //Declare the noise we are coming from.
     float fromRatio = sample;
     //Declare the noise we are going towards.
     float toRatio = 1.0f - sample;
     //Calculate the height of a brick.
     int bHeight = noiseTex.height / brickCount;
     //Calculate the width of the brick.
     int bWidth = noiseTex.width / (brickCount / 2);
     //Calculate the row of bricks.
     int moveCount = y / bHeight;
     //Prevent the divison of zero.
     if (y > 0)
     {
         //Check if the remainder of the y coordinate is less than or equal to our brick grout lines.
         //Within the confined space of 0 - brickSpacing.
         if (y % bHeight <= brickSpacing && y % bHeight >= 0)
         {
             //Run the method of obtaining the next color from the color generator.
             pix[i] = getNextColor(fromRatio, toRatio, bWidth - x % bWidth, y % bHeight, true);
             //If the current pixel is center of the grout line.
             if (y % bHeight <= brickSpacing / 2)
             {
                 //Darken the pixel by 10%.
                 pix[i] -= Color.white * .1f;
             }
             //Our pixel is not center of the grout line.
             else
             {
                 //Darken the pixel by 5%.
                 pix[i] -= Color.white * 0.05f;
             }
         }
         //If our x coordinate is not on the far left side, and our number of the remainder of our brick rows divided by 2 is 0.
         //And our Width is offset by half the width of a normal brick, then offset again, so that it draws equal size brick lines centered above old bricks
         //Then checking if we are within the confined space of our normally generated grout lines.
         else if (x > brickSpacing && moveCount % 2 == 0 && (x + bWidth / 2) / 2 % (bWidth / 2) <= brickSpacing / 2
             && (x + bWidth / 2) / 2 % (bWidth / 2) >= 0 && y >= bHeight * moveCount && y <= bHeight * (moveCount + 1))
         {
             //Run the method of obtaining the next color from the color generator.
             pix[i] = getNextColor(fromRatio, toRatio, bWidth - x % bWidth, y % bHeight, true);
             //If the current pixel is center of the grout line.
             if ((x + bWidth / 2) / 2 % (bWidth / 2) <= brickSpacing / 2 / 2)
             {
                 //Darken the pixel by 10%.
                 pix[i] -= Color.white * .1f;
             }
             //It is not center of our grout line.
             else
             {
                 //Darken the pixel by 5%.
                 pix[i] -= Color.white * 0.05f;
             }
         }
         //Is this pixel a normal grout line, not offset. Calculated by our normal width remainders and height constraints.
         else if (moveCount % 2 != 0 && x % bWidth <= brickSpacing && x % bWidth >= 0 && y >= bHeight * moveCount && y <= bHeight * (moveCount + 1))
         {
             //Run the method of obtaining the next color from the color generator.
             pix[i] = getNextColor(fromRatio, toRatio, bWidth - x % bWidth, y % bHeight, true);
             if (x % bWidth <= brickSpacing / 2)
             {
                 //Darken the pixel by 10%.
                 pix[i] -= Color.white * .1f;
             }
             else
             {
                 //Darken the pixel by 5%.
                 pix[i] -= Color.white * 0.05f;
             }
         }
         else
         {
             //Run the method of obtaining the next color from the color generator.
             pix[i] = getNextColor(fromRatio, toRatio, x, y);
         }
         //Set the alpha back to max.
         pix[i].a = 1f;
     }
     //This is the first row of pixels.
     else
     {
         //Run the method of obtaining the next color from the color generator.
         pix[i] = getNextColor(fromRatio, toRatio, x, y, true);
         //Darken the pixel by 5%.
         pix[i] -= Color.white * .05f;
         //Set the alpha back to max.
         pix[i].a = 1f;
     }
 

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.

Follow this Question

Answers Answers and Comments

79 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 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 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 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

Set the procedural color texture script to a set pattern? 1 Answer

Would a color based game run better than a texture based game? 2 Answers

Blending a Color with a Material Texture 1 Answer

Colour cubes after CombineMesh() 0 Answers

Assigning UV Map to model at runtime 0 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