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 /
  • Help Room /
avatar image
0
Question by johnmcmahonart · Jul 14, 2016 at 08:10 PM · proceduralgradientlibnoise

Creating a linear gradient without knowing the size

I am trying to create a gradient to feed into Libnoise so I can create a heatmap for my procedural world game and I can't quite come up with a way to create a linear gradient when I don't know how large the gradient will be.

To use libnoise operators I need to subclass from modulebase which means I need to implement

 public override double GetValue(double x, double y, double z)
         {
         
         }

which should return a value between -1 and 1. I can come up with ways to create the gradient if I know the size of the texture but I can't come up with a method if I don't know the resolution of the map. Maybe I am looking at this the wrong way and there is a better way to go about this.

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
0

Answer by johnmcmahonart · Jul 15, 2016 at 02:02 AM

So at the moment the ugly hack I am going with is to pass in the size (in one dimension) to the constructor for my gradient and then linearly interpolate between a fixed set of control points. It's ugly but quick. I'll post terrible code when I get it working.

Comment
Add comment · Show 1 · 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
avatar image johnmcmahonart · Jul 22, 2016 at 04:12 PM 0
Share

Here is the solution I ended up writing. Not great but maybe it will help someone in the future

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using LibNoise;
 using UnityEngine;
 namespace LibNoise.Generator
 { 
     public class gradientHack:$$anonymous$$oduleBase //the ugliest hack in the history of computer graphics
     {
         private double $$anonymous$$;
         private double max;
         private rescale rescaler;
         private double domain$$anonymous$$in = -1;
         private double domain$$anonymous$$ax = 1;
 
         public gradientHack(): base(0)
         {
         }
 
         public gradientHack (double $$anonymous$$, double max):base(0)
         {
             this.$$anonymous$$ = $$anonymous$$;
             this.max = max;
             this.rescaler = new rescale(this.$$anonymous$$, this.max, domain$$anonymous$$in, domain$$anonymous$$ax);
         }
         public override double GetValue(double x, double y, double z)
         {
             var currentPoint = rescaler.Scale(z);
             //Debug.Log("Current point scaled:" + currentPoint);
             double input1 = -0.4;
             double output1 = -0.3;
             double input2 = 0.0f;
             double output2 = 1;
             double input3 = .4f;
             double output3 = -0.3;
 
             if (currentPoint<=input1)
            {
                rescale newScale = new rescale (domain$$anonymous$$in,input1,domain$$anonymous$$in,output1);
                return newScale.Scale(currentPoint);
                 
            }
 
             
             if (currentPoint>=input1 && currentPoint<=input2)
            {
                rescale newScale = new rescale(input1, input2,output1, output2);
                return newScale.Scale(currentPoint);
 
            }
            if (currentPoint >= input2 && currentPoint <= input3)
            {
                rescale newScale = new rescale(input2,input3, output2, output3);
                return newScale.Scale(currentPoint);
 
            }
 
            if (currentPoint >= input3)
            {
                rescale newScale = new rescale(input3, domain$$anonymous$$ax, output3, domain$$anonymous$$in);
                return newScale.Scale(currentPoint);
 
 
     }
 }

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 
 public class rescale
     {
     private double range$$anonymous$$in, range$$anonymous$$ax, domain$$anonymous$$in, domain$$anonymous$$ax; 
     public rescale (double range$$anonymous$$in,double range$$anonymous$$ax, double domain$$anonymous$$in, double domain$$anonymous$$ax)
         {
         if (range$$anonymous$$ax>range$$anonymous$$in)
         {
             this.range$$anonymous$$ax = range$$anonymous$$ax;
 
         }
         if (range$$anonymous$$in<range$$anonymous$$ax)
         {
             this.range$$anonymous$$in = range$$anonymous$$in;
         }
             this.domain$$anonymous$$ax = domain$$anonymous$$ax;
             this.domain$$anonymous$$in = domain$$anonymous$$in;
         }
 
     public double Scale (double value)
     {
        double scaleParm = (domain$$anonymous$$ax - domain$$anonymous$$in) / (range$$anonymous$$ax - range$$anonymous$$in);
        return (domain$$anonymous$$in + ((value - range$$anonymous$$in) * scaleParm));
 }
 }
 
 

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

63 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

Related Questions

Gradient + Transparent 1 Answer

Generating Mesh using Points 1 Answer

[SOLVED]Unexpected results 0 Answers

Help with clouds -- Is it possible to procedurally generate them in a 2d game? 1 Answer

How to make a ui panel with 5 color gradient? 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