Sunday, February 9, 2014

Writing mental ray shaders: Simpler Diffuse!!

Intro

The previous simplest diffuse shader doesn't really take lights in the scene into account. To get that information, a shader parameter declaration of type "light" will be needed. 

Mi Source
declare shader
	color "jc_simpler_diffuse" (
		color "diffuse" default 0 0 0,
		light "onelight")
	version 1
	apply material
end declare

Notes:

The shader parameter declaration of type light is a string, so in the C sturct portion we'll use miTag

C Source

#include "shader.h"

DLLEXPORT

struct jc_simpler_diffuse {
	miColor diffuse;
	miTag light;
	};

DLLEXPORT
int jc_simpler_diffuse_version(void) {return(1);}

DLLEXPORT

miBoolean jc_simpler_diffuse(
	miColor *result,
	miState *state,
	struct jc_simpler_diffuse *params) {
	
	miColor light_color;
	miVector dir;
	miScalar dot_nl;
	int samples;
	miColor *diffuse = mi_eval_color(&params->diffuse);
	miTag *light = mi_eval_tag(&params->light);
	if(mi_sample_light(&light_color, &dir, &dot_nl, state, *light, &samples)) {

	result->r = diffuse->r * dot_nl * light_color.r;
	result->g = diffuse->g * dot_nl * light_color.g;
	result->b = diffuse->b * dot_nl * light_color.b;
	result->a = 1.0;
	}
	
	
	return(miTRUE);
}

Notes

the function mi_sample_light returns light_color, direction, and the dot_nl.
Inside maya, the shader node has a text box that can create a light, or type in the light name. It only accepts a single light, thus, simpler diffuse shader. Regardless, its the first light based shader.

0 comments:

Post a Comment