- Home /
Unity crashes on play when generating texture.
Hi! Recently I have been playing around with graphics programming in unity and while attempting to create a c# script to generate a LUT unity crashes everytime I hit play ONLY when the script is enabled. I'm sure its down to dodgy programming as my c# is a bit meh. Heres the code:
using UnityEngine;
using System.Collections;
public class GenerateBeckmann : MonoBehaviour {
public int size = 64;
private Material currentMaterial;
public Texture2D generatedTex;
float PHBeckmann(float ndoth,float m){
float alpha = Mathf.Acos (ndoth);
float ta = Mathf.Tan(alpha);
float val = 1f/(m*m*Mathf.Pow(ndoth,4f)) * Mathf.Exp(-(ta * ta) / (m * m));
return val;
}
private Texture2D Beckmann(){
Texture2D beckmann = new Texture2D(size,size,TextureFormat.ARGB32,false);
for(int i=0;i<size;i++){
for(int j = 0;j<size;i++){
float val = 0.5f * Mathf.Pow(PHBeckmann(i/(float) size, j/(float)size), 0.1f);
beckmann.SetPixel(j, i, new Color(val,val,val,val));
}
}
beckmann.Apply ();
return beckmann;
}
// Use this for initialization
void Start () {
if(!currentMaterial){
currentMaterial = transform.renderer.sharedMaterial;
if(!currentMaterial){
Debug.LogWarning ("Cannot find a material on: "+transform.name);
}
}
if(currentMaterial)
{
generatedTex = Beckmann ();
currentMaterial.SetTexture ("_MainTex",generatedTex);
}
}
// Update is called once per frame
void Update () {
}
}
You are not iterating j in your Beckmann function:
for(int i=0;i<size;i++){
for(int j = 0;j<size;i++){
I suppose this causes an infinite loop leading to your crash
Yeah I feel so stupid now but then I have the excuse of writing this script at 11 o'clock last night
Taking short breaks (even 5 $$anonymous$$utes) often help with these kind of problems when you get back from the break xD
Answer by JecoGames · Apr 21, 2014 at 02:09 PM
This is strange...The script runs perfectly now after using unity 2 days later...weird.
Your answer
Follow this Question
Related Questions
Texture2D to Texture3D 2 Answers
Color specific sections of a texture 1 Answer
Applying decals / textures to uneven surfaces runtime 1 Answer
Texture2D.Resize and Shader 0 Answers
Light based texture change on material 0 Answers