Friday, April 15, 2011

Writing mental ray shaders: Mosaic Tiles

Here is a little something thats detours from the book. Combining quantization and texture uv, I can create a mosaic effect. 

1. Have an input texture
2. quantize the uv_coordinates
3. use the quantized_uv_coordinates inside mi_lookup_texture_color

From top to bottom: Original, tile = 20, tile = 10, Porn?!
MI Source
declare shader
 color "mosaic" (
  color texture "tex",
  scalar "tile_count" default 4
apply material
end declare

C Source
#include "shader.h"

struct mosaic {
 miTag tex;
 miScalar tile_count;

miBoolean mosaic(miColor *result, miState *state, struct mosaic *params) {
 miVector uv_coord = {0,0,0};
 miTag tex = *mi_eval_tag(¶ms->tex);
 miScalar tile_count = *mi_eval_scalar(¶ms->tile_count);
 uv_coord.x = fmod((miScalar)((int)(state->tex_list[0].x * tile_count))/tile_count, 1.0);
 uv_coord.y = fmod((miScalar)((int)(state->tex_list[0].y * tile_count))/tile_count, 1.0);
 mi_lookup_color_texture(result, state, tex, &uv_coord);
 return miTRUE;


Post a Comment