private void Form_Animal(animal_object animal, float bodySize, float greediness, int seed) { // // give appropriate name // int d0 = UnityEngine.Random.Range(0, adjectives.Length); // int d1 = (int)(bodySize*.999f * nounsConsumer.Length); // int d2 = UnityEngine.Random.Range(0, nounsConsumer[d1].Length); // animal.name = adjectives[d0] + " " + nounsConsumer[d1][d2]; animal.name = name_generator.GetName6(seed); // assign mesh animal.Renderer.sharedMesh = Consumer_Meshs[(int)(bodySize * .999f * Consumer_Meshs.Length)]; // generate texture and material // var yuv = new Vector3(.8f-.5f*bodySize, .4f, .8f*greediness-.4f); // // convert yuv to rgb // Color rgb = (Vector4)(AnimalTexture.yuv_to_rgb.MultiplyVector(yuv)) + new Vector4(0,0,0,1); // // scale mesh animal.transform.localScale = Vector3.one;// * (1+bodySize*.1f); // var lab = new LABColor(70-50*bodySize, 10+40*greediness, -50); float L = 70 - 50 * bodySize; var lab1 = new LABColor(L, 50, -80); var lab2 = new LABColor(L, 30, 20); var lab = LABColor.Lerp(lab1, lab2, greediness); Color rgb = lab.ToColor(); Generate_and_Apply(animal, seed, rgb); }
private void Form_Plant(animal_object plant, float bodySize, float greediness, int seed) { // give appropriate name // int d0 = UnityEngine.Random.Range(0, adjectives.Length); // int d1 = (int)(bodySize*.999f * nounsProducer.Length); // int d2 = UnityEngine.Random.Range(0, nounsProducer[d1].Length); // plant.name = adjectives[d0] + " " + nounsProducer[d1][d2]; plant.name = name_generator.GetName1(seed); // assign mesh plant.Renderer.sharedMesh = Producer_Meshs[(int)(bodySize * .999f * Producer_Meshs.Length)]; // generate texture and material // var yuv = new Vector3(.7f-.7f*bodySize, -.4f, .8f*greediness-.4f); // Color rgb = (Vector4)(AnimalTexture.yuv_to_rgb.MultiplyVector(yuv)) + new Vector4(0,0,0,1); // // scale mesh plant.transform.localScale = Vector3.one;// * (1+bodySize*.1f); // var lab = new LABColor(80-50*bodySize, -50+40*greediness, 50); float L = 80 - 50 * bodySize; var lab1 = new LABColor(L, -50, 10); var lab2 = new LABColor(L, -5, 65); var lab = LABColor.Lerp(lab1, lab2, greediness); Color rgb = lab.ToColor(); Generate_and_Apply(plant, seed, rgb); }
// static function for converting from Color to LABColor public static LABColor FromColor(Color c) { float D65x = 0.9505f; float D65y = 1.0f; float D65z = 1.0890f; float rLinear = c.r; float gLinear = c.g; float bLinear = c.b; float r = (rLinear > 0.04045f)? Mathf.Pow((rLinear + 0.055f) / (1f + 0.055f), 2.2f) : (rLinear / 12.92f); float g = (gLinear > 0.04045f)? Mathf.Pow((gLinear + 0.055f) / (1f + 0.055f), 2.2f) : (gLinear / 12.92f); float b = (bLinear > 0.04045f)? Mathf.Pow((bLinear + 0.055f) / (1f + 0.055f), 2.2f) : (bLinear / 12.92f); float x = (r * 0.4124f + g * 0.3576f + b * 0.1805f); float y = (r * 0.2126f + g * 0.7152f + b * 0.0722f); float z = (r * 0.0193f + g * 0.1192f + b * 0.9505f); x = (x > 0.9505f)? 0.9505f : ((x < 0f)? 0f : x); y = (y > 1.0f)? 1.0f : ((y < 0f)? 0f : y); z = (z > 1.089f)? 1.089f : ((z < 0f)? 0f : z); LABColor lab = new LABColor(0f, 0f, 0f); float fx = x / D65x; float fy = y / D65y; float fz = z / D65z; fx = ((fx > 0.008856f)? Mathf.Pow(fx, (1.0f / 3.0f)) : (7.787f * fx + 16.0f / 116.0f)); fy = ((fy > 0.008856f)? Mathf.Pow(fy, (1.0f / 3.0f)) : (7.787f * fy + 16.0f / 116.0f)); fz = ((fz > 0.008856f)? Mathf.Pow(fz, (1.0f / 3.0f)) : (7.787f * fz + 16.0f / 116.0f)); lab.L = 116.0f * fy - 16f; lab.A = 500.0f * (fx - fy); lab.B = 200.0f * (fy - fz); return(lab); }
// constructor - takes a Color public LABColor(Color col) { LABColor temp = FromColor(col); L = temp.L; A = temp.A; B = temp.B; }
// static function for converting from LABColor to Color public static Color ToColor(LABColor lab) { float D65x = 0.9505f; float D65y = 1.0f; float D65z = 1.0890f; float delta = 6.0f / 29.0f; float fy = (lab.L + 16f) / 116.0f; float fx = fy + (lab.A / 500.0f); float fz = fy - (lab.B / 200.0f); float x = (fx > delta)? D65x * (fx * fx * fx) : (fx - 16.0f / 116.0f) * 3f * (delta * delta) * D65x; float y = (fy > delta)? D65y * (fy * fy * fy) : (fy - 16.0f / 116.0f) * 3f * (delta * delta) * D65y; float z = (fz > delta)? D65z * (fz * fz * fz) : (fz - 16.0f / 116.0f) * 3f * (delta * delta) * D65z; float r = x * 3.2410f - y * 1.5374f - z * 0.4986f; float g = -x * 0.9692f + y * 1.8760f - z * 0.0416f; float b = x * 0.0556f - y * 0.2040f + z * 1.0570f; r = (r <= 0.0031308f)? 12.92f * r : (1f + 0.055f) * Mathf.Pow(r, (1.0f / 2.4f)) - 0.055f; g = (g <= 0.0031308f)? 12.92f * g : (1f + 0.055f) * Mathf.Pow(g, (1.0f / 2.4f)) - 0.055f; b = (b <= 0.0031308f)? 12.92f * b : (1f + 0.055f) * Mathf.Pow(b, (1.0f / 2.4f)) - 0.055f; r = (r < 0)? 0 : r; g = (g < 0)? 0 : g; b = (b < 0)? 0 : b; return(new Color(r, g, b)); }
// static function for returning the color difference in a normalized colorspace (Delta-E) public static float Distance(LABColor a, LABColor b) { return(Mathf.Sqrt(Mathf.Pow((a.L - b.L), 2f) + Mathf.Pow((a.A - b.A), 2f) + Mathf.Pow((a.B - b.B), 2f))); }
// public static LABColor LerpLCH(LABColor a, LABColor b, float t){ // float C0 = Mathf.Sqrt(a.A*a.A + a.B*a.B); // float C1 = Mathf.Sqrt(b.A*b.A + b.B*b.B); // float h0 = Mathf.Atan2(a.B, a.A); // float h1 = Mathf.Atan2(b.B, b.A); // float C = Mathf.Lerp(C0, C1, t); // float h = Mathf.Lerp(h0, h1, t); // float L = Mathf.Lerp(a.L, b.L, t); // float A = C*Mathf.Cos(h); // float B = C*Mathf.Sin(h); // return new LABColor(L, A, B); // } // static function for interpolation between two Unity Colors through normalized colorspace public static Color Lerp(Color a, Color b, float t) { return((LABColor.Lerp(LABColor.FromColor(a), LABColor.FromColor(b), t)).ToColor()); }
// static function for linear interpolation between two LABColors public static LABColor Lerp(LABColor a, LABColor b, float t) { return(new LABColor(Mathf.Lerp(a.L, b.L, t), Mathf.Lerp(a.A, b.A, t), Mathf.Lerp(a.B, b.B, t))); }
// function for converting an instance of LABColor to Color public Color ToColor() { return(LABColor.ToColor(this)); }