Community
Arnold General Rendering Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Counting intersections in a shader

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
tomekp1981
457 Views, 4 Replies

Counting intersections in a shader

This is my first post here, so welcome to everyone.

I have a scene with a sphere and some cubes between the sphere and camera. All cubes have primary visibility turned off and double sided turned on. Now, I want my shader to count intersections between shaded point P and camera.

553-pic.jpg

I came up with something like this:

shader_evaluate
{
AtPoint pw = sg->P;
AtVector av = -sg->Rd;
AtVector an = sg->N;
AtPoint pnt = sg->P;
int hitnum = 0;
AtShaderGlobals* hitpoint = AiShaderGlobals();
AtRay ar;
double maxLength = 1000;
AiMakeRay(&ar, AI_RAY_ALL, &pnt, &av, maxLength, sg);
bool hit = AiTraceProbe(&ar, hitpoint);
AtColor ac = AiColor(pw.x, pw.y, pw.z);
while (hit) {
	hitnum++;
	pnt = hitpoint->P;
	av = hitpoint->Rd;
	AiMakeRay(&ar, AI_RAY_ALL, &pnt, &av, maxLength, hitpoint);
	hit = AiTraceProbe(&ar, hitpoint);
}
float res = hitnum;
ac = AiColor(res, res, res);
sg->out.RGB = ac;
}

Unfortunately, I get only 2 intersections in rendered image, even when there are two or more cubes between observer and shaded sphere. Question is, what's wrong with my shader?

Version: MtoA 1.4.2.1

Tags (1)
Labels (1)
4 REPLIES 4
Message 2 of 5
maxtarpini
in reply to: tomekp1981

Just an hint. Are you color picking the color managed result to check your hits ? Because then it's correct.. those are the resulting intersections.. in gamma space however. In facts if you were expecting 6 intersections .. 6^(1/2.2) ~= 2

Message 3 of 5
tomekp1981
in reply to: tomekp1981

559-shd2.jpg

I'm afraid it's not it. Please, have a look at the rendered image. I divided it by 2, to better see the result. As you can see, I get two values - 1.0 and 2.0, arranged in a strange way. I have no idea, why it renders like that. I must have done something wrong, but I can't figure out what 🙂

Message 4 of 5
maxtarpini
in reply to: tomekp1981

Try changing the ray type.. from AI_RAY_ALL to anything like AI_RAY_ALL_DIFFUSE .. it's working here .. the former type probably gets some internal massage. Side view it's just to see the setup, cubes are in front of the camera in the persp view. You see RGBA returns correctly 6, while gamma'ed value is around 2 as pointed above.

564-backtrace.png

Message 5 of 5
tomekp1981
in reply to: tomekp1981

Oh... I haven't thought about changing ray type. Thanks 🙂

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report