Skip to content

Instantly share code, notes, and snippets.

@prophetgoddess
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prophetgoddess/6c5f6cbd224f0273dcda to your computer and use it in GitHub Desktop.
Save prophetgoddess/6c5f6cbd224f0273dcda to your computer and use it in GitHub Desktop.
#freecodefriday 3/20/15
using UnityEngine;
using System.Collections;
/*
Copyright (c) 2015 Lycaon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*Helper class for storing and converting HSV color space data.*/
public class HSVColor{
public float s;
public float v;
public float h;
public float brightness{
get{
return (this.getRGB().r + this.getRGB().g + this.getRGB().b);
}
}
public HSVColor(float H, float S, float V){
this.h = H;
this.s = S;
this.v = V;
}
public Color getRGB(){
return HSVToRGB(h, s, v);
}
public override string ToString(){
return "HSVColor: (" + h + ", " + s + ", " + v + "), brightness: " + brightness;
}
public static Color HSVToRGB(float H, float S, float V){
if (S == 0f){
return new Color(V,V,V);
}
else if (V == 0f){
return Color.black;
}
else{
Color col = Color.black;
float Hval = H * 6f;
int sel = Mathf.FloorToInt(Hval);
float mod = Hval - sel;
float v1 = V * (1f - S);
float v2 = V * (1f - S * mod);
float v3 = V * (1f - S * (1f - mod));
switch (sel + 1){
case 0:
col.r = V;
col.g = v1;
col.b = v2;
break;
case 1:
col.r = V;
col.g = v3;
col.b = v1;
break;
case 2:
col.r = v2;
col.g = V;
col.b = v1;
break;
case 3:
col.r = v1;
col.g = V;
col.b = v3;
break;
case 4:
col.r = v1;
col.g = v2;
col.b = V;
break;
case 5:
col.r = v3;
col.g = v1;
col.b = V;
break;
case 6:
col.r = V;
col.g = v1;
col.b = v2;
break;
case 7:
col.r = V;
col.g = v3;
col.b = v1;
break;
}
col.r = Mathf.Clamp(col.r, 0f, 1f);
col.g = Mathf.Clamp(col.g, 0f, 1f);
col.b = Mathf.Clamp(col.b, 0f, 1f);
return col;
}
}
}
using UnityEngine;
using System.Collections;
/*
Copyright (c) 2015 Lycaon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*Attach this script to a GameObject to turn it into a leaf sprite*/
public class LeafGen : MonoBehaviour {
void Start(){
var sr = gameObject.AddComponent<SpriteRenderer>();
sr.sprite = makeLeaves();
sr.sprite.texture.filterMode = FilterMode.Point;
}
public static Sprite makeLeaves(){
Texture2D leaves = new Texture2D(128, 128);
//Pick a random, fully saturated, full value color. This is *way* easier to do with HSV than RGB.
Color leafColor = new HSVColor(Random.value, 1f, 1f).getRGB();
//Set all the pixels in the leaf sprite texture to transparent
var texColors = new Color[leaves.width * leaves.width];
for(int i = 0; i < texColors.Length; i++){
texColors[i] = Color.clear;
}
leaves.SetPixels(texColors);
int radius = 0;
Vector2 location;
//Place between 10 and 20 randomly sized circles (defined here as a point and a radius)
for(int i = 0; i < Random.Range(10, 20); i++){
radius = Random.Range(10, 50);
location = new Vector2(Random.Range(0f + radius, leaves.width - radius), Random.Range(0f + radius, leaves.width - radius));
//Get a color very close to the base color, for variation in leaf tones
Color thisLeafColor = new Color(leafColor.r + Random.Range(-0.1f, 0.1f), leafColor.g + Random.Range(-0.1f, 0.1f), leafColor.b + Random.Range(-0.1f, 0.1f));
//dumb as hell bresenham approximation, goes over some pixels in the circle too many times but w/e
for(int y = -radius; y <= radius; y++){
for(int x = -radius; x <= radius; x++){
if(x * x + y * y <= radius * radius + radius * 0.8f){
leaves.SetPixel((int)(location.x + x), (int)(location.y + y), thisLeafColor);
}
}
}
}
leaves.Apply();
return Sprite.Create(leaves, new Rect(0, 0, leaves.width, leaves.height), new Vector2(0.5f, 0f), 100f);
}
}
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
/*
Copyright (c) 2015 Lycaon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public class TrunkGen : MonoBehaviour {
//helper class for converting hex color codes to rgb unity colors
internal class ColorTranslator
{
public static Color FromHtml(string hexString)
{
return new Color(Convert.ToInt32(hexString.Substring(1, 2), 16) / 255f,
Convert.ToInt32(hexString.Substring(3, 2), 16) / 255f,
Convert.ToInt32(hexString.Substring(5, 2), 16) / 255f);
}
}
public static List<Color> trunkColors = new List<Color>(){ColorTranslator.FromHtml("#836539"),
ColorTranslator.FromHtml("#653700"),
ColorTranslator.FromHtml("#7b5804"),
ColorTranslator.FromHtml("#341c02"),
ColorTranslator.FromHtml("#ad8150")};
// Use this for initialization
void Start () {
var sr = gameObject.AddComponent<SpriteRenderer>();
sr.sprite = makeTrunk();
sr.sprite.texture.filterMode = FilterMode.Point;
}
public static Sprite makeTrunk(){
Texture2D trunk = new Texture2D(128, 128);
//Set all the pixels in the trunk texture to transparent
var texColors = new Color[trunk.width * trunk.width];
for(int i = 0; i < texColors.Length; i++){
texColors[i] = Color.clear;
}
trunk.SetPixels(texColors);
//place between 7 and 13 slightly overlapping randomly sized rectangles of a random color from the list established above
float height = 0f;
for(int i = 0; i < UnityEngine.Random.Range(7, 13); i++){
Color trunkColor = trunkColors[UnityEngine.Random.Range(0, trunkColors.Count)];
Rect section = new Rect(UnityEngine.Random.Range((trunk.width/2) - 1, (trunk.width/2) + 1), i * height/2, UnityEngine.Random.Range(5, 10), UnityEngine.Random.Range(15, 20));
height = section.height;
for(int x = (int)section.x; x < (int)section.xMax; x++){
for(int y = (int)section.y; y < (int)section.yMax; y++){
trunk.SetPixel(x, y, trunkColor);
}
}
}
trunk.Apply();
return Sprite.Create(trunk, new Rect(0, 0, trunk.width, trunk.height), new Vector2(0.5f, 0f), 100f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment