Last active
December 8, 2019 20:18
-
-
Save nomelif/567bb30e31e43c643021 to your computer and use it in GitHub Desktop.
Osl brick shader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2016 Théo Friberg | |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
// .----------------. .----------------. .----------------. .----------------. .----------------. | |
// | .--------------. || .--------------. || .--------------. || .--------------. || .--------------. | | |
// | | _________ | || | _____ | || | | || | ________ | || | _______ | | | |
// | | | _ _ | | || | |_ _| | || | _ | || | |_ ___ `. | || | |_ __ \ | | | |
// | | |_/ | | \_| | || | | | | || | (_) | || | | | `. \ | || | | |__) | | | | |
// | | | | | || | | | _ | || | _ | || | | | | | | || | | __ / | | | |
// | | _| |_ | || | _| |__/ | | || | | ) | || | _| |___.' / | || | _| | \ \_ | | | |
// | | |_____| | || | |________| | || | )/ | || | |________.' | || | |____| |___| | | | |
// | | | || | | || | | || | | || | | | | |
// | '--------------' || '--------------' || '--------------' || '--------------' || '--------------' | | |
// '----------------' '----------------' '----------------' '----------------' '----------------' | |
// Do with this shader as you will, share it and make the world a better place with it. Live long and prosper. | |
// OSl Mason 1.0 | |
// ---------------------------------------------------------- // | |
// ================ | |
// Helper functions | |
// ================ | |
// ---------------------------------------------------------- // | |
// Saw wave normalized to the range 0-1 | |
float saw( | |
float coordinate, // Position along the wave | |
float wavelength | |
) | |
{ | |
return fmod(coordinate, wavelength)/wavelength; | |
} | |
// Is this a row divisible by n? | |
int divisibleByN( | |
float coordinate, // Position of a point on a row | |
float length, // length of the column | |
int n | |
) | |
{ | |
return (int)(coordinate/length) % n; | |
} | |
// Cutoff | |
// Return the value if it is smaller than the limit | |
float cut(float value, float limit){ | |
if(value > limit){ | |
return value; | |
}else{ | |
return 0; | |
} | |
} | |
// 1D Brick texture | |
float brick( | |
float coordinate, // Position | |
float length, // Length of the brick | |
) | |
{ | |
float sawWave = saw(coordinate, length); // The basic wave | |
return (abs(sawWave-1)*abs(sawWave)); // Make the gradients radieate from the centers of the bricks | |
} | |
// Brick color | |
float brickColor( | |
float coordinate, | |
float length, | |
float variation // How different are individual bricks | |
) | |
{ | |
return cut(0.5+noise("perlin", (int)(coordinate / length)/1.5, 10)*variation/10, 0); | |
} | |
shader Brick | |
( | |
float width = 0.07, // Width of the even rows of bricks | |
float height = 0.025, // Height of the bricks | |
float displace = 0.025, // Displacement between rows of bricks | |
float secondWith = 0.07, // Width of the odd rows of bricks | |
float chips = 0.350, // Amount of the bricks chipped off | |
float normalStrength = 1.1, // Strength of the generated normalmap | |
float brickLimit = 0.16, // What elevation is considered brick | |
float brickPeek = 20.590, // How much bricks stick out? | |
float wavyness = 0, // How irregularly the bricks arel laid out | |
float chipSize = 1.8, // How big bits are chipped off | |
float wetness = 0, // How wet are the bricks; applies mostly to pavements. | |
float mortar = 0.08, // Width of the mortar | |
float colorVariation = 6, // How much does the color vary between | |
color base = color(1, 0.173, 0.043), // The basic color of the bricks | |
color cracked = color(0.314, 0.262, 0.216), // The basic color of the broken parts | |
color mortarColor = color(0.058, 0.058, 0.058), // The basic color of the mortar | |
point Vector = P, // Input texture coordinate | |
output color col = (0.5, 0.5, 0.5), | |
output float spec = 0.0, // Specular map | |
output float bump = 0.0, // Bump map | |
output float roughness = 0.5, // Roughness of the specular data and diffuse surfaces | |
output float distance = 0.0, // Distance from the border of the brick | |
output float isBrick = 0.0, // Is this point on a brick | |
output float chippedOff = 0.0 // Is this part of the brick chipped off? | |
) | |
{ | |
// Displace the texture coordinates according to the wavyness value | |
point displaced = Vector; | |
displaced[0] += noise("perlin", Vector, 10)*wavyness/5; | |
displaced[1] += noise("perlin", Vector, 500)*wavyness/5; | |
displaced[2] += noise("perlin", Vector, 1000)*wavyness/5; | |
// Check if it is an odd or an even row | |
if(divisibleByN(displaced[1],height,2)){ // Even row | |
// Basic black and white brick data | |
distance = brick(displaced[0],width)*brick(displaced[1],height)*abs(brickPeek); | |
// Check if we are in a layer of mortar | |
if(brick(displaced[0],width) < mortar){ | |
distance = 0; | |
} | |
if(brick(displaced[1],height) < mortar){ | |
distance = 0; | |
} | |
// Generate the color for the brick | |
col = (brickColor(displaced[0],width,colorVariation)+brickColor(displaced[1],height,colorVariation)*abs(brickPeek))/2; | |
}else{ // Odd row | |
// Basic black and white brick data | |
distance = brick(displaced[0]+displace,secondWith)*brick(displaced[1],height)*abs(brickPeek); | |
// Check if we are in a layer of mortar | |
if(brick(displaced[0]+displace,secondWith) < mortar){ | |
distance = 0; | |
} | |
if(brick(displaced[1],height) < mortar){ | |
distance = 0; | |
} | |
// Generate the color for the brick | |
col = (brickColor(displaced[0]+displace,secondWith,colorVariation)+brickColor(displaced[1],height,colorVariation)*abs(brickPeek))/2; | |
} | |
// Calculate the specularity of this point (supposing it is on a brick) | |
spec = (1-abs(dot(-I,N)))/4; | |
// Check if this part of this brick is chipped off | |
if((noise("perlin", displaced*1800/chipSize, 10) + (noise("perlin", displaced*12000/chipSize, 200)+0.5)*0.1) + 0.5 < chips*0.1){ | |
// Give the chipped part a noise texture | |
distance -= (noise("perlin", displaced*1500/chipSize, 60)+0.5)*0.4; | |
distance -= (noise("perlin", displaced*6000/chipSize, 400)+0.5)*0.1; | |
distance -= (noise("perlin", displaced*18000/chipSize, 800)+0.5)*0.01; | |
// Ugly hack for using only the intensity of the base color | |
col = (1.0*col)*cracked; | |
// This part is indeed chipped off | |
chippedOff = 1; | |
// It is a rough and diffuse surface | |
spec = 0; | |
roughness = 1; | |
} | |
// Check if this is part of a brick | |
if(distance > brickLimit){ | |
isBrick = 1; | |
// Calculate the bump map | |
float tmpBump = distance*normalStrength; | |
if(brickPeek < 0){ | |
tmpBump = -1*tmpBump; | |
} | |
bump = tmpBump; | |
// Tint the brick | |
col = base*col; | |
}else{ | |
// If this is mortar, calculate the bump and color | |
col = mortarColor; | |
bump = (noise("perlin", displaced*600, 400)+0.5)*0.03; | |
} | |
// If we are part of an inset brick | |
if(!chippedOff && isBrick && brickPeek < 0){ | |
distance = -distance; | |
} | |
// If this part of the material is underwater | |
if(distance < wetness && wetness > 0){ | |
spec = 1-abs(dot(-I,N)); | |
bump = 1; | |
roughness = 0.005; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment