Nov
12
2014

Draw Munching Squares

munching_squares_unity_csharp

Converted this php script into unity, while trying to keep it similar to original code..
again some difference in the output but close enough..
http://rosettacode.org/wiki/Munching_squares#PHP

 

Source c# (MunchingSquares.cs)


// Original source: http://rosettacode.org/wiki/Munching_squares#PHP

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MunchingSquares : MonoBehaviour
{
void Start ()
{
MakeMunch();
}

void MakeMunch()
{
int w = 256;
int h = 256;
Texture2D im = new Texture2D(w, h);
if (im==null) Debug.LogError("Cannot Initialize new texture");

List<Color> color = new List<Color>();

for(int i=0;i<256;i++)
{
color.Add(new Color( (Mathf.Sin((i)*(2*3.14f/256))*128+128)/255 , (i/2)/255 , i/255 , 1));
}

for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
im.SetPixel(i,j,color[i^j]);
}
}

im.Apply(false);
renderer.material.mainTexture = im;
}
}

 

 


Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch Instagram

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.