How can I thread this function?
I'm working on a light study using unity that uses the ray function in the physics engine to simulate light rays. I've got to the stage where I can render an image, but it freezes the program up for 20 seconds while it calculates the render. I tried running my render function in a new thread, but then I found out that nothing in Unity is thread safe other than doing 1+1 XD. (Joking)
Under the assumption that I don't care about super high frame rates or any performance issues, is there any way I can run this function in another thread without it complaining? I don't really know much about threads but I would have thought I could have some kind of a work around for this? Thanks
A snippet of code:
function threadCamera() {
var thread = new Thread(renderCamera);
thread.Start();
}
function renderCamera() {
t1 = Time.deltaTime;
C.text = "COMPUTING";
renderRes = float.Parse(renderResText.text);
renderWidth = int.Parse(widthInput.text);
renderHeight = int.Parse(heightInput.text);
renderTexture = new Texture2D(renderWidth, renderHeight, TextureFormat.RGB24, false);
blendFac = 0.5;
bFacText.text = "bFac = " + blendFac.ToString();
mCam.Render();
//emitFac = Color.RGBToHSV(emitFac);
for(var ii = 1; ii < renderHeight; ii ++) {
for(var i = 1; i < renderWidth; i ++) {
ray = Camera.main.ScreenPointToRay(Vector2((i / renderRes) + int.Parse(renderOffX.text), ((ii / renderRes) + int.Parse(renderOffY.text))));
if(Physics.Raycast(ray, hit, 100)) {
tex = hit.transform.GetComponent.<Renderer>().material.mainTexture;
hit.transform.GetComponent.<Renderer>().material.EnableKeyword("_GLOSSINESS");
randfoc = 1 - hit.transform.GetComponent.<Renderer>().material.GetFloat("_Glossiness");
hit.transform.GetComponent.<Renderer>().material.EnableKeyword("_EMISSION");
emitFac = hit.transform.GetComponent.<Renderer>().material.GetColor("_EmissionColor");
pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
difCol = (tex.GetPixel(pixelUV.x, pixelUV.y) + emitFac) / (randfoc + 2);
ray = Ray(hit.point, Vector3.Reflect(ray.direction, hit.normal) + Vector3(Random.Range(-randFoc,randFoc),Random.Range(-randFoc,randFoc),Random.Range(-randFoc,randFoc)));
if(Physics.Raycast(ray, hit, 100)) {
tex = hit.transform.GetComponent.<Renderer>().material.mainTexture;
randfoc = 1 - hit.transform.GetComponent.<Renderer>().material.GetFloat("_Glossiness");
emitFac = hit.transform.GetComponent.<Renderer>().material.GetColor("_EmissionColor");
pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
difCol *= (tex.GetPixel(pixelUV.x, pixelUV.y) + emitFac) / (randfoc + 2);
renderTexture.SetPixel(i, ii, difCol);
ray = Ray(hit.point, Vector3.Reflect(ray.direction, hit.normal) + Vector3(Random.Range(-randFoc,randFoc),Random.Range(-randFoc,randFoc),Random.Range(-randFoc,randFoc)));
if(Physics.Raycast(ray, hit, 100)) {
tex = hit.transform.GetComponent.<Renderer>().material.mainTexture;
randfoc = 1 - hit.transform.GetComponent.<Renderer>().material.GetFloat("_Glossiness");
emitFac = hit.transform.GetComponent.<Renderer>().material.GetColor("_EmissionColor");
pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
difCol *= (tex.GetPixel(pixelUV.x, pixelUV.y) + emitFac) / (randfoc + 2);
renderTexture.SetPixel(i, ii, difCol);
ray = Ray(hit.point, Vector3.Reflect(ray.direction, hit.normal) + Vector3(Random.Range(-randFoc,randFoc),Random.Range(-randFoc,randFoc),Random.Range(-randFoc,randFoc)));
if(Physics.Raycast(ray, hit, 100)) {
tex = hit.transform.GetComponent.<Renderer>().material.mainTexture;
randfoc = 1 - hit.transform.GetComponent.<Renderer>().material.GetFloat("_Glossiness");
emitFac = hit.transform.GetComponent.<Renderer>().material.GetColor("_EmissionColor");
pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
difCol *= (tex.GetPixel(pixelUV.x, pixelUV.y) + emitFac) / (randfoc + 2);
renderTexture.SetPixel(i, ii, difCol);
ray = Ray(hit.point, Vector3.Reflect(ray.direction, hit.normal) + Vector3(Random.Range(-randFoc,randFoc),Random.Range(-randFoc,randFoc),Random.Range(-randFoc,randFoc)));
if(Physics.Raycast(ray, hit, 100)) {
tex = hit.transform.GetComponent.<Renderer>().material.mainTexture;
randfoc = 1 - hit.transform.GetComponent.<Renderer>().material.GetFloat("_Glossiness");
emitFac = hit.transform.GetComponent.<Renderer>().material.GetColor("_EmissionColor");
pixelUV = hit.textureCoord;
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
difCol *= (tex.GetPixel(pixelUV.x, pixelUV.y) + emitFac) / (randfoc + 2);
renderTexture.SetPixel(i, ii, difCol);
}
else {
renderTexture.SetPixel(i, ii, difCol);
}
}
else {
renderTexture.SetPixel(i, ii, difCol);
}
}
else {
renderTexture.SetPixel(i, ii, difCol);
}
}
else {
renderTexture.SetPixel(i, ii, difCol);
}
}
else {
renderTexture.SetPixel(i, ii, Color.black);
}
}
}
i = 1;
ii = 1;
renderTexture.Apply();
renderSprite = Sprite.Create(renderTexture, new Rect(0, 0, renderTexture.width, renderTexture.height), new Vector2(0.5f, 0.5f));
spritePrev.GetComponent("Image").sprite = renderSprite;
C.text = "NULL";
t2 = Time.deltaTime;
tText.text = (t2 - t1).ToString();
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612093530im_/https://answers.unity.com/themes/thub/images/avi.jpg)