OSL

时间:2023-03-09 16:40:27
OSL

1,SimpleColorShader:

shader gamma(color cin = color(,,),output color Cout=color(,,))
{
Cout = cin;
}

Katana启动时候会自动编译:

OSL

Arnold里bin文件夹里有oslc编译osl文件。但是在centos的GLIB不是2.14,所以单独运行不行。 但是在Katana启动调用时候为何自动编译,猜测是Arnold bin文件夹里已经把GLIB2.14一些符号编译成静态库了。

如果需要手动编译osl.编译GLIBC2.14.

编译glibc2.14 放到/opt/glibc-2.14

cd build
../configure --prefix=/opt/glibc-2.14 make -j
make install
#如果编译错误:
cp /etc/ld.so.conf /opt/glibc-2.14/etc/

再创建个bash文件调用GLIBC2.14,然后执行oslc,起名为:run_oslc

#!/usr/bin/env bash

GLIB_LIB_PATH=/opt/glibc-2.14/lib
KTOA_ROOT=/opt/solidangle/KtoA-2.2.0.2-kat3.-linux/
export LD_LIBRARY_PATH=${GLIB_LIB_PATH}:${LD_LIBRARY_PATH}
exec "${KTOA_ROOT}/bin/oslc" "$@"

则编译shader是:./run_oslc gamma.osl

Katana要加载osl shader 必须:export ARNOLD_PLUGIN_PATH=/mnt/Proj_Lighting/RD/katana3_plugin/Arnold/OSL_Shaders 要指定这个环境变量,

把shader放到这个里面,Katana会自动编译这些oslshader, 不用自己去手动编译

2,添加辅助信息 :

shader gamma[[ string help = "Simple mat" ]]
(color cin = color(,,)[[string help="InputColor",float min = , float max = ]],output color Cout=color(,,))
{
Cout = cin;
}

3,Diffuse with noise

#include <stdosl.h>

shader SimpleShader(
color diffuse_color = color(0.6, 0.8, 0.6),
float noise_factor = 0.5,
output closure color bsdf = diffuse(N))
{
color material_color = diffuse_color * mix(1.0, noise(P * 10.0), noise_factor);
bsdf = material_color * diffuse(N);
}

bsdf默认是个diffuse材质,下面只是乘以一些颜色

4,Metal Reflection

#include <stdosl.h>

shader GOSL_Metal(
color diffuse_color = color(0.6, 0.8, 0.6),
output closure color bsdf =)
{
bsdf = diffuse_color * reflection(N);
}

OSL

diffuse(N):

OSL

5,Depth Color Channel:

#include <stdosl.h>

shader GOSL_Metal(
float divide_value = ,
output color bsdf =[[float min=,float max=]])
{
point camera;
camera = transform("camera","world",point(,,));
bsdf = sqrt(dot(camera-P,camera-P))/divide_value; }

OSL

6,Noise shader:

#include <stdosl.h>

/*
Coding Time:2018/9/10
liuyangping207@qq.com
*/ vector getNoise(vector inputPos,float amp,vector offset,vector freq,int turbulence, float rough){
vector fp = ;
vector sum=;
vector sample_p = (inputPos + offset) * freq;
float contrib = ;
for(int i=;i<turbulence;i++)
{
vector ns = snoise(sample_p);
sum += ns*contrib;
sample_p *=2.6;
contrib *= rough;
}
fp += sum*amp;
return fp;
} shader GOSL_Metal(
vector inputPosition = ,
float amplitude = ,
float roughness = 0.55,
int turbulence = ,
vector offset = ,
vector frequency = ,
int useRestNoise = ,
output color bsdf =)
{
if(useRestNoise)
bsdf = getNoise(inputPosition,amplitude,offset,frequency,turbulence,roughness);
else{
bsdf = getNoise(P,amplitude,offset,frequency,turbulence,roughness);
} }

OSL

7,metadata in katana

#include <stdosl.h>

/*
Coding Time:2018/9/10
liuyangping207@qq.com
*/ #define OPTION_A 0
#define OPTION_B 1
#define OPTION_C 2 shader TestCode(
int booleanvalue = [[ string widget = "boolean" ]],
int enumvalue = [[ string widget = "popup", string options = "OptionA|OptionB|OptionC" ]],
output color cout=
)
{
if (booleanvalue)
cout=color(,,);
if (enumvalue == OPTION_B)
cout=color(,,);
}

OSL

8,Bounding obj color:

#include <stdosl.h>

/*
Coding Time:2018/9/10
liuyangping207@qq.com
*/ int zero_compare(float a ) {
return a >= -0.00001 && a <= 0.00001;
} float fit(float var, float omin, float omax, float nmin, float nmax) {
float d = omax - omin;
if (zero_compare(d)) {
return (nmin + nmax) * 0.5;
}
if (omin < omax) {
if (var < omin) return nmin;
if (var > omax) return nmax;
} else {
if (var < omax) return nmax;
if (var > omin) return nmin;
}
return nmin + (nmax - nmin) * (var - omin) / d;
} shader TestCode(
output color cout=
)
{ point Po = transform("object", P); point rbound[];
getattribute("geom:objbounds", rbound); point pmin = rbound[];
point pmax = rbound[]; float bx = fit(Po[],pmin[],pmax[],,);
float by = fit(Po[],pmin[],pmax[],,);
float bz = fit(Po[],pmin[],pmax[],,); cout = color(bx,by,bz);
}

OSL

9,Fake Caustic In Katana

新项目要用焦散. OSL这个时候快速派上用场。思维就是在灯光上用gobo投射UV Caustic, 顺便也做了个三维的,因为在三维就是x,y,z, uv的就是u,v,0

OSL

10,Gradient Voronoise

If F(x,y,z) =

OSL

WIKI Tell us :

OSL

So GradF will display this picture.

OSL

Display the Grad vector:

OSL

REF:

https://blog.****.net/clirus/article/details/62425498

https://answers.arnoldrenderer.com/questions/489/how-to-setup-osl-shader.html

http://thhube.github.io/tutorials/osl/osl.html

https://docs.arnoldrenderer.com/display/A5ARP/OSL+Shaders

https://blog.selfshadow.com/publications/s2012-shading-course/martinez/s2012_pbs_osl_notes_v3.pdf