Bitmap Random Tiling OSL

Maxon Cinema 4D (Export script developed by abstrax, Integrated Plugin developed by aoktar)

Moderators: ChrisHekman, aoktar

Post Reply
RTF
Licensed Customer
Posts: 7
Joined: Mon Mar 30, 2020 9:20 am

Recently,I find 3dmax 2021.2 update a osl named 'Bitmap random tiling' which can solve texture repetition.
And I extracted the OSL from 3DMAX,but can't compilation and show something wrong.
I don't know much about OSL.Can anyone compile the OSL to work with OCTANE?
This is the Bitmap random tiling effect.
Image
https://www.stateofartacademy.com/3ds-m ... -osl-maps/

This is the code

Code: Select all

// Lookup a bitmap and make random variations so no tiling is evident
// RandomTilingBitmap.osl, by Zap Andersson
// Modified: 2020-06-23
// Copyright 2020 Autodesk Inc, All rights reserved. This file is licensed under Apache 2.0 license
//    https://github.com/ADN-DevTech/3dsMax-OSL-Shaders/blob/master/LICENSE.txt

shader RandomTilingBitmap
[[ string help  =
    "<h3>Bitmap Random Tiling</h3>"
	"Look up a bitmap from passed in UV coordinates and modify each "
	"repetition of the bitmap such that tiling is not evident. Has "
	"a mode to properly transform normal maps. Outputs the "
	"modified UV so it can be hooked up to other Bitmap Lookup shaders "
	"to apply the identical randomness, plus outputs the Random Index "
	"itself to drive downstream modifications that are in sync with it.",
   string label = "Bitmap Random Tiling",
   string version = "1.0" ]]
(
  point  Pos   = point(u,v,0)
  	[[  string label= "UV Coordinate",
  		string help = "The 2D coordinate at which the texture is looked up." ]],
  float  Scale  = 0.25
  	[[  string help = "A linear scale factor. For more complex UV manipulation, connect the UVWTransform." ]],

  string Filename = "uv-grid.png"
  	[[ string widget="filename",
  	   string label="File name",
  	   string help="The name of the texture to look up" ]],
  int   NormalMap = 0
  	[[ string widget="checkBox",
  	   string packName = "Normal Map",
  	   string label    = "Normal Map",
  	   int    connectable = 0,
  	   string help="If the texture is a normal map, special transformations have to be applied to retain the proper orientation upon rotation. Check this box if the texture is a normal map." ]],
  int   NormalFlipR = 0
  	// Metadata changed by Save to Previous
		[[ string widget="checkBox",
		string packName = "Normal Map",
		string label    = "Flip Red",
		int connectable =  1,
  	    string help="Invert red component for rotation of normal maps." ]],
  int   NormalFlipG = 0
  	// Metadata changed by Save to Previous
		[[ string widget="checkBox",
		string packName = "Normal Map",
		string label    = "Flip Green",
		int connectable =  1,
	  	string help="Invert green component for rotation of normal maps." ]],
  string Filename_UDIMList = ""
  	[[ string label="Viewport UDIM List",
  	   int    connectable = 0,
  	   string help="The list of UDIM items to load into the viewport. If empty, will be deduced from the file system automatically. " ]],
  string LoadUDIM = "Load UDIM..."
  	[[ string widget="max:actionButton",
  	   string actionID="loadUDIM(\"Filename\")",
  	   string help="Select a set of files to load as an UDIM.",
  	   int    connectable = 0 ]],
  int    UDIM   = 0
  	// Metadata changed by Save to Previous
		[[ string widget="checkBox", string label="UDIM-compatible lookup",
  	   int connectable =  1,
  	   string help  ="Modifies the UV coordinate so that UDIM's are looked up similar to the max MultiTile map" ]],
  string WrapMode = "periodic"
  	[[ string widget="popup", string options = "default|black|clamp|periodic|mirror",
  	   string label="Wrap Mode",
  	   string help="How the texture wraps: (black, clamp, periodic or mirror)." ]],

  int   Randomize    = 1
    // Metadata changed by Save to Previous
		[[ string help =
    	  "Enables all randomization. Can be disabled for debugging purposes. ",
    	  int connectable= 1 ]],
  int   RandomizePos    = 1
    // Metadata changed by Save to Previous
		[[ string help =
    	  "Enables randomization in UV space by making a 'distorted grid' and  "
    	  "apply one variation in each 'square' of the grid. "
    	  "If this is OFF, no spatially varying randomness occurs. "
    	  "There is still variation per Seed value. If Seed is driven from "
    	  "e.g. a Node Handle map, this gives one random value per object.",
    	  int connectable= 1 ]],
  float RandScale    = 1.0
    [[ string help =
    	  "Scale of the 'distorted grid' in relation to UV space. "
    	  "A value of 1.0 means the 'distorted grid' is the same size as a "
    	  "unit square in UV space, a smaller number makes the grid smaller and "
    	  "a larger value makes it larger. "
    	  "A value of zero disables spatially varying randomness "
    	  "but still makes a variation per Seed value. If Seed is driven from "
    	  "e.g. a Node Handle map, this gives one random value per object." ]],


  float WiggleScale  = 0.2  [[ string help="Scale of the 'wiggliness' of edges, in relation to the UV space" ]],
  float WiggleAmount = 0.25
  	[[ float min=0.0, float max=10.0,
  	   string help="Amount of 'wiggliness' on the edge of the randomness grid, to obscure the fact that it is a grid."
  	]],
  float EdgeFuzz     = 0.1
  	[[ float min=0.0, float max=10.0,
  	   string help="Amount of 'crossfade' at the edge of the randomness grid. Note this is computed stochastically and may introduce some noisiness. "
  	]],


  float RotMin = 0.0     [[ float min=-360.0, float max=360.0 ]],
  float RotMax   = 360.0 [[ float min=-360.0, float max=360.0 ]],
  float ScaleMin = 1.0   [[ float min= 0.1, float max=10.0 ]],
  float ScaleMax = 1.0   [[ float min= 0.1, float max=10.0 ]],
  float UOffset = 0.0,
  float VOffset = 0.0,

  int   Seed = 0 [[ string help = "Random seed. Connect to e.g. Node Handle map to have per-object randomness" ]],

  vector HSVMin   = vector(0.0,1.0,1.0),
  vector HSVMax   = vector(0.0,1.0,1.0),


  int    AutoGamma   = 1
  	// Metadata changed by Save to Previous
		[[ string widget="checkBox", int connectable= 1 ]],
  float  ManualGamma = 1.0
  	[[ string label="Manual Gamma" ]],



  output color Col = 0     [[ string label="Col (RGB)" ]],
  output float R = 0,
  output float G = 0,
  output float B = 0,
  output float A = 1,
  output float Luminance = 0,
  output float Average = 0,
  output point UV = 0,
  output int   RandomIndex = 0,
)
{
	// Skip empty files
	if (Filename == "")
		return;

	point p = Pos  / Scale;

	point noisepoint = 0;

	if (RandomizePos)
	{
		point wigglepoint = p / WiggleScale;
		noisepoint = p / RandScale + (noise(wigglepoint, Seed)-point(0.5,0.5,0.0)) * WiggleAmount + noise("hash", p) * EdgeFuzz;
	}

	point rotscale  = Randomize?noise("cell", noisepoint, Seed + 10):0;
	point position  = Randomize?noise("cell", noisepoint, Seed + 11) - point(0.5,0.5,0.0):0;
	point hsvchange = Randomize?noise("cell", noisepoint, Seed + 12):0;

	// Output one of the randomizers so people can do external randomization
	// based on these
	RandomIndex = int(rotscale[2] * 500000000);

	// Compute the actual rotation (in radians)
	float rotation = radians(mix(RotMin, RotMax, rotscale[0]));

	vector hsvTweak = mix(HSVMin, HSVMax, hsvchange);

	p -= point(0.5,0.5,0.0);

	p  = rotate(p, rotation, 0.0, vector(0.0,0.0,1.0));
	p /= mix(ScaleMin, ScaleMax, rotscale[1]);
	p += point(position[0] * UOffset, position[1] * VOffset, 0.0);

	p += point(0.5,0.5,0.0);

	UV = p;

	// Default lookup, just use u and inverted v...
	float ulookup = p[0];
	float vlookup = 1.0 - p[1];

	// But for UDIM compatibility and max's idea that 0,0 is in
	// lower left corner, we need this juggling of v....
	if (UDIM)
	{
		float vfloor  = floor(p[1]);
		float vfrac   = p[1] - vfloor;
		vlookup = vfloor + (1.0 - vfrac);
	}

	Col  = texture(Filename, ulookup, vlookup, "wrap", WrapMode, "alpha", A);

	if (NormalMap  == 0)
	{
		color hsv = transformc("rgb", "hsv", Col);
		hsv[0] += hsvTweak[0]; // Offset the hue
		//hsv[1] *= hsvTweak[1]; // Scale the saturation
		hsv[2] *= hsvTweak[2]; // Scale the value
		Col = transformc("hsv", "rgb", hsv);
		// Simulate saturation via gamma - more visually pleasing
		Col = pow(Col, hsvTweak[1]);
	}
	else
	{
		// Flip components if needed
		if (NormalFlipR) Col[0] = 1.0 - Col[0];
		if (NormalFlipG) Col[1] = 1.0 - Col[1];

		Col = rotate(Col, rotation, point(0.5,0.5,0.0), point(0.5,0.5,1.0));

		// Flip components back if needed
		if (NormalFlipR) Col[0] = 1.0 - Col[0];
		if (NormalFlipG) Col[1] = 1.0 - Col[1];
	}


	int channels;
	gettextureinfo(Filename, "channels", channels);
	if (channels < 4) // No alpha? Set it to opaque
	{
		A = 1.0;

		if (WrapMode == "black") // 2018-04-25: Allow Decals
		{
			if (p[0] < 0.0 || p[0] > 1.0 ||
			    p[1] < 0.0 || p[1] > 1.0)
			    A = 0.0;
		}
	}

	if (!NormalMap)
	{
		// Temporary workaround for color space issues
		if (AutoGamma)
		{
			if (!endswith(Filename, ".exr") &&
				!endswith(Filename, ".EXR") &&
				!endswith(Filename, ".hdr") &&
				!endswith(Filename, ".HDR") &&
				!endswith(Filename, ".tx")  &&
				!endswith(Filename, ".TX"))
			{
				// Gamma 2.2 for sRGB approx)
				Col = pow(Col, 2.2);
			}
		}
		else
		{
			if (ManualGamma != 1.0)
				Col = pow(Col, ManualGamma);
		}
	}

	R = Col[0];
	G = Col[1];
	B = Col[2];
	Luminance = luminance(Col);
	Average   = (R + G + B) / 3.0;
}
:shock: :shock: :shock:
Attachments
RandomTilingBitmap.osl
RandomTilingBitmap.osl
(8.7 KiB) Downloaded 243 times
User avatar
bepeg4d
Octane Guru
Posts: 10340
Joined: Wed Jun 02, 2010 6:02 am
Location: Italy
Contact:

Hi,
you cannot have multiple output pins with Octane OSL nodes, but... do you know that Octane 2020.2 has a new native Chaos node for this?
F678F74E-D81E-46F7-B777-66FECCA1AF9A.jpeg
ciao Beppe
RTF
Licensed Customer
Posts: 7
Joined: Mon Mar 30, 2020 9:20 am

Thanks,I'll try the Chaos node. :D :D :D
ceen
Licensed Customer
Posts: 110
Joined: Wed Jun 10, 2015 9:43 pm

It does not work wih displacement.
3080ti
User avatar
bepeg4d
Octane Guru
Posts: 10340
Joined: Wed Jun 02, 2010 6:02 am
Location: Italy
Contact:

Hi,
probably you are using the Displacement node in Texture mode, that does not allow any texture transformation.

You need to switch to Vertex Displacement, to make it works:
EEFC5C6C-0941-4D07-A1E6-5D972A148BF3.jpeg
Happy GPU rendering,
ciao Beppe
ceen
Licensed Customer
Posts: 110
Joined: Wed Jun 10, 2015 9:43 pm

bepeg4d wrote:Hi,
probably you are using the Displacement node in Texture mode, that does not allow any texture transformation.

You need to switch to Vertex Displacement, to make it works:
EEFC5C6C-0941-4D07-A1E6-5D972A148BF3.jpeg
Happy GPU rendering,
ciao Beppe
Thank you!
3080ti
Post Reply

Return to “Maxon Cinema 4D”