• UnityShader编程——内置函数和变量(一)


    内置函数

    radians(degree) : 角度变弧度;
    degrees(radian) : 弧度变角度;
    sin(angle), cos(angle), tan(angle)
    asin(x): arc sine, 返回弧度 [-PI/2, PI/2];
    acos(x): arc cosine,返回弧度 [0, PI];
    atan(y, x): arc tangent, 返回弧度 [-PI, PI];
    atan(y/x): arc tangent, 返回弧度 [-PI/2, PI/2];
    pow(x, y): x的y次方;
    exp(x): 指数, log(x):
    exp2(x): 2的x次方, log2(x):
    sqrt(x): x的根号; inversesqrt(x): x根号的倒数
    abs(x): 绝对值
    sign(x): 符号, 1, 0 或 -1
    floor(x): 底部取整
    ceil(x): 顶部取整
    fract(x): 取小数部分
    mod(x, y): 取模, x - y*floor(x/y)
    min(x, y): 取最小值
    max(x, y): 取最大值
    clamp(x, min, max):  min(max(x, min), max);
    mix(x, y, a): x, y的线性混叠, x(1-a) + y*a;
    step(edge, x): 如 x
    smoothstep(edge0, edge1, x): threshod  smooth transition时使用。 edge0<=edge0时为0.0, x>=edge1时为1.0
    length(x): 向量长度
    distance(p0, p1): 两点距离, length(p0-p1);
    dot(x, y): 点积,各分量分别相乘 后 相加
    cross(x, y): 差积,x[1]*y[2]-y[1]*x[2], x[2]*y[0] - y[2]*x[0], x[0]*y[1] - y[0]*x[1]
    normalize(x): 归一化, length(x)=1;
    faceforward(N, I, Nref): 如 dot(Nref, I)< 0则N, 否则 -N
    reflect(I, N): I的反射方向, I -2*dot(N, I)*N, N必须先归一化
    refract(I, N, eta): 折射,k=1.0-eta*eta*(1.0 - dot(N, I) * dot(N, I)); 如k<0.0 则0.0,否则 eta*I - (eta*dot(N, I)+sqrt(k))*N
    matrixCompMult(matX, matY): 矩阵相乘, 每个分量 自行相乘, 即 r[i][j] = x[i][j]*y[i][j];
                               矩阵线性相乘,直接用 *
    lessThan(vecX, vecY): 向量 每个分量比较 x < y
    lessThanEqual(vecX, vecY): 向量 每个分量比较 x<=y
    greaterThan(vecX, vecY): 向量 每个分量比较 x>y
    greaterThanEqual(vecX, vecY): 向量 每个分量比较 x>=y
    equal(vecX, vecY): 向量 每个分量比较 x==y
    notEqual(vecX, vexY): 向量 每个分量比较 x!=y
    any(bvecX): 只要有一个分量是true, 则true
    all(bvecX): 所有分量是true, 则true
    not(bvecX): 所有分量取反
    texture2D(sampler2D, coord): texture lookup
    texture2D(sampler2D, coord, bias): LOD bias, mip-mapped texture
    texture2DProj(sampler2D, coord):
    texture2DProj(sampler2D, coord, bias):
    texture2DLod(sampler2D, coord, lod):
    texture2DProjLod(sampler2D, coord, lod):
    textureCube(samplerCube, coord):
    textureCube(samplerCube, coord, bias):
    textureCubeLod(samplerCube, coord, lod): 

    内置常量

    const mediump int gl_MaxVertexAttribs = 8;
    const mediump int gl_MaxVertexUniformVectors = 128;
    const mediump int gl_MaxVaryingVectors = 8;
    const mediump int gl_MaxVertexTextureImageUnits = 0;
    const mediump int gl_MaxCombinedTextureImageUnits = 8;
    const mediump int gl_MaxTextureImageUnits = 8;
    const mediump int gl_MaxFragmentUnitformVectors = 16;
    const mediump int gl_MaxDrawBuffers = 1;

     

    内置变量

    const mediu mp int gl_MaxVertexAttribs = 8;
    const mediump int gl_MaxVertexUniformVectors = 128;
    const mediump int gl_MaxVaryingVectors = 8;
    const mediump int gl_MaxVertexTextureImageUnits = 0;
    const mediump int gl_MaxCombinedTextureImageUnits = 8;
    const mediump int gl_MaxTextureImageUnits = 8;
    const mediump int gl_MaxFragmentUnitformVectors = 16;
    const mediump int gl_MaxDrawBuffers = 1;

    精度

    ighp, mediump, lowp

     定义

    默认:无修饰符,普通变量读写, 与外界无连接;
    const:常量 const vec3 zAxis = vec3(0.0, 0.0, 1.0);
    attribute: 申明传给vertex shader的变量;只读;不能为array或struct;attribute vec4 position;
    uniform: 表明整个图元处理中值相同;只读; uniform vec4 lightPos;
    varying: 被差值;读写; varying vec3 normal;
    in, out, inout;

    外部调用

    1uint CreateShader(enum type) : 创建空的shader object; 
    type: VERTEX_SHADER, 
    2void ShaderSource(uint shader, sizeicount, const **string, const int *length):加载shader源码进shader object;可能多个字符串 
    3void CompileShader(uint shader):编译shader object; 
    shader object有状态 表示编译结果 
    4void DeleteShader( uint shader ):删除 shader object; 
    5void ShaderBinary( sizei count, const uint *shaders, 
    enum binaryformat, const void *binary, sizei length ): 加载预编译过的shader 二进制串; 
    6uint CreateProgram( void ):创建空的program object, programe object组织多个shader object,成为executable; 
    7void AttachShader( uint program, uint shader ):关联shader object和program object8void DetachShader( uint program, uint shader ):解除关联; 
    9void LinkProgram( uint program ):program object准备执行,其关联的shader object必须编译正确且符合限制条件; 
    10void UseProgram( uint program ):执行program object11void ProgramParameteri( uint program, enum pname, 
    int value ): 设置program object的参数; 
    12void DeleteProgram( uint program ):删除program object
  • 相关阅读:
    Spring之配置文件bean作用域的详细介绍
    Spring之hello world(Spring入门)
    Spring的控制反转和依赖注入
    c3p0和QueryRunner的结合使用,让开发更加简便
    C3p0数据库连接池的使用
    Hibernate的核心对象关系映射
    Hibernate的主配置文件hibernate.cfg.xml
    鼠标移到导航上面 当前的LI变色 处于当前的位置
    CSS3背景渐变属性 linear-gradient(线性渐变)和radial-gradient(径向渐变)
    【转】Android中dip(dp)与px之间单位转换
  • 原文地址:https://www.cnblogs.com/Roz-001/p/14177096.html
Copyright © 2020-2023  润新知