OSL – CamMapScale

Specially for the max people! The Camera Map Per Pixel projection map scales the input image to the size of the render output. So if you want to project a square image, you’d have to set your render resolution to square as well. Pretty annoying, yes? This OSL shader will scale your image back so square really means square!

cammapscale_rndr

Download link
http://www.rensheeren.com/osl/camMapScale_tex_v001.osl

Features
– Scales the input image back to its original aspect ratio when using Camera Map Per Pixel.

How to use
VRay:
– Create a VRay OSL Texture map.
– Load in the download OSL shader.
– Plug in the map and set your render and map resolution. The shader will scale the image back to the map resolution.
– VRay — make sure ‘wrap texture coordinates’ (General tab) is off in the OSL shader!

cammapscale_node

cammapscale_ui

showudim_wrapdisable

Properties
input_tex
– The texture to scale.
render_width
– Render output width. Only the w/h ratio matters, not pixels. For example you could use width 2 / height 1 instead of 2048 / 1024.
render_height
– Render output height. Only the w/h ratio matters, not pixels.
image_width
– Texture width. Only the w/h ratio matters, not pixels.
image_height
– Texture height. Only the w/h ratio matters, not pixels.
fit_to_width
– Scale to fit to the width when on, when off it scales to match the height.

Code

/*

camMapScale_tex_v001


By Rens Heeren (mail [at] rensheeren [dot] com)

Info Page: http://www.rensheeren.com/blog/osl-cammapscale/


Use as an OSL texture, not as an OSL material.


Simple shader to scale a map back to its original size when using Camera Map (Per Pixel) in 3ds max, as max scales the image
to fit the render size... for some obscure reason.

Usage:
- Plug in the map and set your render and map resolution. The shader will scale the image back to the map resolution.
- VRay -- make sure 'wrap texture coordinates' (General tab) is off in the OSL shader!



Use at your own risk.



2016/05/28 - Created (rh)

*/


//////////////////////////////


shader
camMapScale_tex_v001
(

	string input_tex = ""
		[[ string description = "The texture to scale." ]],

	int render_width = 1920
		[[ string description = "Render output width. Only the w/h ratio matters, not pixels." ]],
	int render_height = 1080
		[[ string description = "Render output height. Only the w/h ratio matters, not pixels." ]],
	int image_width = 2048
		[[ string description = "Texture width. Only the w/h ratio matters, not pixels." ]],
	int image_height = 2048
		[[ string description = "Texture height. Only the w/h ratio matters, not pixels." ]],

	int fit_to_width = 1
		[[ string widget = "checkBox",
		string description = "Scale to fit to the width when on, when off it scales to match the height." ]],
	
	string info_page = "www.rensheeren.com/blog/osl-cammapscale/"
	  [[ string widget = "string" ]],

	output color col_out = color(0),
	output float alpha_out = 0
	
)

//////////////////////////////////////

{
	
	color c_tex = color(0);
	float f_alpha = 0;
	int i_renW = render_width;
	int i_renH = render_height;
	int i_texW = image_width;
	int i_texH = image_height;
	int i_scaleH = fit_to_width;
	float f_U = u;
	float f_V = v;

	float f_rendRatio = (float)i_renW / (float)i_renH;

	if (i_scaleH)
	{
		f_V = ((f_V - 0.5) / f_rendRatio) + 0.5;
	}
	else
	{
		f_U = ((f_U - 0.5) * f_rendRatio) + 0.5;
	}

	c_tex = texture(input_tex, f_U, f_V, "alpha", f_alpha);
	
	col_out = c_tex;
	alpha_out = f_alpha;

}