Unity Problems

Started by
4 comments, last by Turbo14 5 years, 2 months ago

I'm having some problems with path-tracing code. Not sure if it's from the random number generator or what. but my projected rays don't "stick" to surfaces.

Here's what I'm doing:

what I want is the hit points to appear to "stick" to surfaces but instead they're flying all around. The only time they stick is when I strafe. No idea what I'm doing wrong.

void Update () {
m_Clear_Buff.CopyTo(m_Color_Buff, 0);

m_main_Cam.enabled = false;
m_Trace_Cam.enabled = true;
System.Random r = new System.Random(98);

for (int i = 0; i < 300; i++)
{
Vector3 dir = new Vector3((float)r.Next(360) /360f-.5f, (float)r.Next(360) / 360f - .5f, (float)r.Next(360) / 360f - .5f);

Ray ray = new Ray(new Vector3(0,0,0), dir);
RaycastHit rch;
if (Physics.Raycast(ray, out rch, 50f))
{

Vector3 sp = m_Trace_Cam.WorldToScreenPoint(rch.point);

if (sp.x >=0 && sp.x < m_Trace_Cam.pixelRect.width-1 && sp.y >=0 && sp.y < m_Trace_Cam.pixelRect.height-1)
{
float lv = Mathf.Max(Vector3.Dot(rch.point.normalized, new Vector3(0, 0, 0)), 0);
lv *= 10;

Material m = rch.collider.gameObject.GetComponent().material;
m_Color_Buff[(int)(sp.x + sp.y * m_Trace_Cam.pixelRect.width)] = new Color(m.color.r * lv, m.color.g * lv, m.color.b * lv,1);
}
}
}
m_Trace_Cam.enabled = false;
m_main_Cam.enabled = true;
m_Trace_Tex.SetPixels(m_Color_Buff);
m_Trace_Tex.Apply();
}

Advertisement

void Update () 
{
	m_Clear_Buff.CopyTo(m_Color_Buff, 0);
	m_main_Cam.enabled = false;
	m_Trace_Cam.enabled = true;
	System.Random r = new System.Random(98);

	for (int i = 0; i < 300; i++)
	{
		Vector3 dir = new Vector3((float)r.Next(360) /360f-.5f, (float)r.Next(360) / 360f - .5f, (float)r.Next(360) / 360f - .5f);
		Ray ray = new Ray(new Vector3(0,0,0), dir);
		RaycastHit rch;
		if (Physics.Raycast(ray, out rch, 50f))
		{
			Vector3 sp = m_Trace_Cam.WorldToScreenPoint(rch.point);
			if (sp.x >=0 && sp.x < m_Trace_Cam.pixelRect.width-1 && sp.y >=0 && sp.y < m_Trace_Cam.pixelRect.height-1)
			{
				float lv = Mathf.Max(Vector3.Dot(rch.point.normalized, new Vector3(0, 0, 0)), 0);
				lv *= 10;
				Material m = rch.collider.gameObject.GetComponent().material;
				m_Color_Buff[(int)(sp.x + sp.y * m_Trace_Cam.pixelRect.width)] = new Color(m.color.r * lv, m.color.g * lv, m.color.b * lv,1);
			}
		}
	}
	m_Trace_Cam.enabled = false;
	m_main_Cam.enabled = true;
	m_Trace_Tex.SetPixels(m_Color_Buff);
	m_Trace_Tex.Apply();
}

Okay, that's better..

Now, why are you raycasting 300 times in random directions every update?  That doesn't really sound anything like what you were wanting to do..

Also, what is this script attached to?

It's for a variation of path tracing. Anyways, the script is on an empty game object. I'm having rays shoot outside the box I have the light in, whereas they should all be contained within the box. And they don't appear to stick to the surfaces I need them to for post processing.

What is the purpose of your "path-tracing" variation here, in this scenario?  Can you provide an example of the effect you are trying to achieve?  And the 300 different raycasts?  Is 300 significant or just a test number?

So, you have the script on an empty object.

And a box with a light INSIDE it.  This box is your "scene" then?  And the empty object is inside this box? 

Do you have Project Settings > Physics > Queries Hit Backfaces checked?  Or are the "inside" faces already the "front" faces of this box?

Either way, a little more description of what you are actually trying to achieve and how you are going about it might help.. i.e.  What are these "hit point" objects you describe(are they objects?), and what exactly are they supposed to stick to?  Some comments in the code would be extremely helpful too.

 

I found the problem... it was this line:

 

Here it is fixed  

m_Color_Buff[(int)((int)sp.x + (int)sp.y * (int)m_Trace_Cam.pixelRect.width)] = new Color(m.color.r * lv, m.color.g * lv, m.color.b * lv,0.5f);

turns out I had to cast each variable to an int instead of casting them together.

This topic is closed to new replies.

Advertisement