Learn about the overall structure of an OSL shader.
- [Instructor] Over the next few weeks, we will explore coding a procedural map in the Open Shading Language, or OSL. OSL is a very exciting development. It allows us to author our own maps. It was introduced in 3ds Max 2019. And we're gonna spend the next few weeks learning how to author a procedural fractal noise map. OSL really has no limits. You could write your own functions, and go crazy.
We're going to start simply by using one of the built-in noise pattern generators, which is a simplex noise. It's an updated version of the familiar Perlin noise, or fractal pattern. The first thing we want to do is simply get acquainted with the syntax, or the overall structure of an OSL map. I got a empty scene here. The only thing I've done is assign the Production Renderer to Arnold. Let's open up the Material Editor from its button on the main tool bar.
I don't need the Navigator, so I'm gonna close that. And let's create an OSL map. If you look in the map section, you'll see a subsection labeled OSL, and within these are many maps created by Autodesk, specifically Mr. Zap Andersson authored all of these. We could take one of these, and modify it, but we want to start fresh from a blank OSL document. And to do that, we would use this OSL map which is found in the General section.
So drag OSL map over, and then double-click it to load its parameters. Double-click its preview, or sample swatch in order to make it larger. And I'm not going to be using the Material Map Browser again, so I can close that. If I want to get the Material Map Browser, or the Navigator open again, I can do that from the Tools Menu here. Let's take a look at the sample code provided with this example map. That's done by clicking on the icon that looks like a pencil.
And we click on that, we get the OSL code editor opening up. You notice by the way that in this example map, the icons indicate link to a file is grayed out, or darkened. So in other words, we're not currently linked to a file. Anything we do inside this code editor window is going to be local to the current map instance. And additionally of course local to the current 3ds Max scene.
So anything we do in here is not going to affect anything outside of this particular map in this particular scene. We will look at how to set up the linking later. For now we just want to get familiar with the overall structure of an OSL shader. In the code editor window, we can see that it's basically divided into a few sections. At the top are some comments. Anything that follows two forward slashes is a comment. At the top we usually provide some information to describe what this shader is.
In this case there's also a link to the OSL language specification document which is a must if you're going to be authoring any OSL shaders, you'll definitely want to go, and download that. Below those comments, we see the shader declaration. The word shader followed by the name of a shader, in this case, the map, which is called Example. We can have exactly one shader per OSL document. Below the shader declaration, we see some stuff here in double angle brackets.
And these are metadata strings. We've got a help string, that prints out some text here, so that the user has some idea of what this particular map or shader is going to do. We've also got a URL here, which links to the 3ds Max shader repository on GitHub, and that's what this more button is here. Alright, so that is the metadata for the shader declaration. Below that we have the input, and output parameters declarations enclosed in parenthesis.
There's an open parenthesis here, and then we have input declarations, output declarations, and a closed parenthesis. And just for clarity here, I'm going to insert a carriage return, and delete that tab, and then add another carriage return just to space things out. Everything within these parenthesis is an input, or output parameter. The inputs are of course the user values, that you can plug in.
And those values can be numeric, or they can be other maps, or other shaders. And the we have the outputs, and those of course are what feed into materials, or other map notes. Below the input, and output parameter declarations, is a section enclosed by curly braces, and this is the statements area. The actual mathematical operations of the shader are enclosed in these curly braces.
Also notice that the lines are separated by separator characters within the input, and output parameters, the separator character is a comma, but within the statement section, the separator is a semicolon. Notice also, in the statements, the last line of the statements has a semicolon after it. And that seems to be a requirement. If I remove that, if I delete that, and click the compile shader button down here, then I get a syntax error, and we get this line lighting up in red.
So that's a little bit of an idiosyncrasy here, we have to have a semicolon at the end of the last line of statements, but we don't need to have a comma at the end of the last line of input, and output parameter declarations. So that is the overall structure. I can go ahead, and click compile shader here again, and it says compilation okay. Now that we know how the shader is structured, we can make some simple changes here. First thing I would do is just change the description at the top.
I'll select everything after those first two forward slashes, and give this a description. I'm going to call it a basic iterated simplex noise pattern generator. That will only bee seen by someone opening up this editor window. But if we want to, we can also copy that into the string help, so I can just select that, right click, and copy. I can paste it over this line here, which is enclosed in double quotes. I'll paste that in with control V.
So the string that will be displayed to the user will be basic iterated simplex noise pattern generator, and I don't want the string URL here. So I can just delete everything after that, including the comma, because we don't need a separator. So I'll just delete that with the backspace key. And we'll rename this shader as well. We'll call it simplex noise. Capital S i m p l e x. No space, capital N o i s e, and then click the compile shader button again, and we don't see any errors.
We haven't changed the input, and output. We haven't changed the statements at all. All we've done is change the name, and the metadata here. So now that we can see that that's been adjusted. We've got some metadata printed out, and we've also got a new name for the OSL noise map. So we wanna save our scene at this point. Next week we will look at plugging in some input, and output declarations, and our journey to creating our own procedural noise map.
So check in next week as the exercise continues.