Thursday, April 7, 2011

Writing mental ray shaders: Normals as Colors Part II

Goal
A shader that visualizes the world space, object space, and camera space normals.


Methodology
Same as in part I, but use a switch statement with different mi_vector_**** functions.




MI File
declare shader
 color "normals_as_colors_spaces" (
  integer "spaces" default 1
 )
apply material
end declare


C File
#include "shader.h"

struct normals_as_colors_spaces{
 miInteger spaces;
 };

DLLEXPORT
miBoolean normals_as_colors_spaces(miColor *result, miState *state, struct normals_as_colors_spaces *params) {
 miInteger spaces = *mi_eval_integer(&params->spaces);
 miVector normal;
 switch(spaces) {
  case 1: 
   mi_vector_to_world(state, &normal, &state->normal);
   mi_vector_normalize(&normal);
   result->r = (normal.x + 1) / 2;
   result->g = (normal.y + 1) / 2;
   result->b = (normal.z + 1) / 2;
   result->a = 1.0;
   break;
  case 2:
   mi_vector_to_object(state, &normal, &state->normal);
   mi_vector_normalize(&normal);
   result->r = (normal.x + 1) / 2;
   result->g = (normal.y + 1) / 2;
   result->b = (normal.z + 1) / 2;
   result->a = 1.0;
   break;
  case 3:
   mi_vector_to_camera(state, &normal, &state->normal);
   mi_vector_normalize(&normal);
   result->r = (normal.x + 1) / 2;
   result->g = (normal.y + 1) / 2;
   result->b = (normal.z + 1) / 2;
   result->a = 1.0;
   break;
  default :
   result->r = (state->normal.x + 1) / 2;
   result->g = (state->normal.y + 1) / 2;
   result->b = (state->normal.z + 1) / 2;
   result->a = 1.0;
 };

 return miTRUE;
}

Notes
Viewport & Hypershade

Render


From left to right, world space, object space, camera space, default internal space. All the spheres are rotated 45 degrees on the Y-axis. It would be clearer to look at each individual color separetly.

0 comments:

Post a Comment