AlgebraMaster

Modern C++ 创造非凡 . 改变世界

导航

OSL

1,SimpleColorShader:

 

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

 

Katana启动时候会自动编译:

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 56
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.0-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(0,0,0)[[string help="InputColor",float min = 0, float max = 1]],output color Cout=color(0,0,0))
{
    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 =0)
{   
   bsdf = diffuse_color * reflection(N);
}

 

diffuse(N):

 5,Depth Color Channel:

#include <stdosl.h>

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

 

 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 = 0;
    vector sum=0;
    vector sample_p = (inputPos + offset) * freq;
    float contrib = 1;
    for(int i=0;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 = 0,
    float amplitude = 1,
    float roughness = 0.55,
    int turbulence = 5,
    vector offset  = 0,
    vector frequency = 1,
    int useRestNoise = 0,
    output color bsdf =0)
{  
   if(useRestNoise)
    bsdf = getNoise(inputPosition,amplitude,offset,frequency,turbulence,roughness);
   else{
    bsdf = getNoise(P,amplitude,offset,frequency,turbulence,roughness);
   }

}

 

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 = 0 [[ string widget = "boolean" ]],
    int enumvalue = 0 [[ string widget = "popup", string options = "OptionA|OptionB|OptionC" ]],
    output color cout=0
    )
{
    if (booleanvalue)
        cout=color(1,0,0);
    if (enumvalue == OPTION_B)
        cout=color(0,1,0);
}
View Code

 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=0
    )
{

    point Po = transform("object", P);
    
    point rbound[2];
    getattribute("geom:objbounds", rbound);
    
    
    point pmin = rbound[0];
    point pmax = rbound[1];
    
    float bx = fit(Po[0],pmin[0],pmax[0],0,1);
    float by = fit(Po[1],pmin[1],pmax[1],0,1);
    float bz = fit(Po[2],pmin[2],pmax[2],0,1);
 
    cout = color(bx,by,bz);
}
View Code

 9,Fake Caustic In Katana

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

 

10,Gradient Voronoise

If F(x,y,z) =

 

 WIKI Tell us :

 

So GradF will display this picture.

 

 

Display the Grad vector:

 

REF:

https://blog.csdn.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

 

posted on 2018-10-09 11:23  gearslogy  阅读(1766)  评论(0编辑  收藏  举报