最近在用UE4的Instancing, 发现限制很多。

Unity有instancing的attribute array (uniform/constant buffer),通过InstanceID来访问每个instance的数据,实现每个实例的不同的参数(通常的一种做法)。

然而Unreal没有这样的功能,只有一些instancing的vertex buffer。

shader:

 #if USE_INSTANCING && !USE_INSTANCING_EMULATED
float4 InstanceOrigin : ATTRIBUTE8; // per-instance random in w
half4 InstanceTransform1 : ATTRIBUTE9; // hitproxy.r + 256 * selected in .w
half4 InstanceTransform2 : ATTRIBUTE10; // hitproxy.g in .w
half4 InstanceTransform3 : ATTRIBUTE11; // hitproxy.b in .w
#endif // USE_INSTANCING && !USE_INSTANCING_EMULATED

C++:

 template <class FloatType>
struct FInstanceStream
{
FVector4 InstanceOrigin; // per-instance random in w
FloatType InstanceTransform1[]; // hitproxy.r + 256 * selected in .w
FloatType InstanceTransform2[]; // hitproxy.g in .w
FloatType InstanceTransform3[]; // hitproxy.b in .w
int16 InstanceLightmapAndShadowMapUVBias[];
...
};

在不改动引擎代码的前提下(不熟悉,风险大), 总结了以下workaround:

其中第一种是简单的颜色映射。

后面的几种是为了解决多个参数的方式,因为只有一个随机值,导致随机化不理想。比如一个实例有两个随机颜色,用PerInstanceRandom去查两个颜色表【蓝,黄,绿】和【黑,白,灰】那么第一个颜色是蓝色的时候,第二个颜色一定是黑色,而实际需求的很可能是两个颜色也要随机搭配。

解决方法是把一个随机值映射为多个。前两种比较类似,一个用随机函数,一个用纹理采样。最后一种是hack, 取每一位的数字就不一样。虽然hack,有很多限制,但是目前用起来还算可以。
事实上,后面两种都是第一种的特化,第二种Fn=TextureSample(Texn, x), 第三种Fn=frac(x*10^n),虽然我一开始想到的就是第三种最简单的hack。

  1. Simple senario

    • map to color

      • create an color LUT table (i.e. using photoshop, filter mode need to set as point/nearest if interpolation is not expected)
      • make float2: use PerInstanceRandom as x
      • link to TextureSample node's uv pin
    • map to value
      • PerInstanceRandom * (max-min) + min
    • could be done in blueprint
  2. Multiple variation attributes:(i.e. speed/color/move range/angular speed/etc.)
    • Simply map PerInstanceRandom to multiple attributes works but may not have expected results
    • i.e. instances with 2 different color, using PerInstanceRandom to access 2 color tables, lets say [Blue, Yellow, Cyan] and [Black, White, Grey]
      because using the same PerInstanceRandom as UV, when the first color is "Blue", the second "random" color will always be "Black".
      while the expected result may need be any combinations of the color in the 2 tables, not fixed combinations.
    • Solution: remap PerInstanceRandom to multiple random data
      • 1.multiple custom random function

        • random0 = F0(PerInstanceRandom)
        • random1 = F1(PerInstanceRandom);
        • ...
        • randomn = Fn(PerInstanceRandom);
        • get color using random0;
        • get speed/color2 using random1;
        • ...
        • pros: can map PerInstanceRandom to any count of random numbers
        • cons: custom math/random function need to be written for each attribute variation
      • 2.using random noise texture: similiar as above while using texture sampling
        • random0 = sample(RandomNoiseTexture0, float2( PerInstanceRandom, 0));
        • random1 = sample(RandomNoiseTexture1, float2(PerInstancerandom, 0));
        • ...
        • pros: easy & simple; blueprint friendly; can map PerInstanceRandom to any count of random number
        • cons: one additional random noise texture for each attribute - extra workflow & runtime cost (texture uniform may exceed?)
          - this could be avoid by using a single random texture with multiple rows: randomn = sample(Texture, float2(PerInstanceRandom, (0.5+n)/N));
      • 3.simple hack: most significant digit shift
        • random0 = PerInstanceRandom;
        • random1 = frac(random0*10);
        • random2 = rac(random1*10);
        • ...
        • pros: simple & easy; blueprint friendly
        • cons: precision lost on each iteration; max attribute variations limited to 5-6, due to floating point precision issue. last random number( i.e. random5 ) have lowest resolution: approximately 10 (probably 0.0~0.9 or even less), but still good to be used as index/uv to sampling color table if color count is small.
        • if variation count is small, this method is personally recommended.

note: the last 2 methods can be consider as an speicialization of the first one: 2. Fn=TextureSample(x),  3.Fn=frac(x*10^n)

note2: instance's AbsoluteWorldPosition could be used to generate attribute variations.

[工作积累] Tricks with UE4 PerInstanceRandom的更多相关文章

  1. [工作积累] D3D10+ 中 Pixel Shader 的input semantic和参数顺序

    由于semantic的使用,我们有理由相信 vertex shader的output 和 pixel shader的input是按照semantic来匹配的,而跟传入顺序无关.印象dx9时代是这样. ...

  2. [工作积累] UE4 并行渲染的同步 - Sync between FParallelCommandListSet & FRHICommandListImmediate calls

    UE4 的渲染分为两个模式1.编辑器是同步绘制的 2.游戏里是FParallelCommandListSet并行派发的. mesh渲染也分两类,static mesh 使用TStaticMeshDra ...

  3. [工作积累] UE4 TAA ReProjection的精度处理

    先贴一个UE4 TAA的slidehttps://de45xmedrsdbp.cloudfront.net/Resources/files/TemporalAA_small-59732822.pdf ...

  4. [工作积累] Google/Amazon平台的各种坑

    所谓坑, 就是文档中没有标明的特别需要处理的细节, 工作中会被无故的卡住各种令人恼火的问题. 包括系统级的bug和没有文档化的限制. 继Android的各种坑后, 现在做Amazon平台, 遇到的坑很 ...

  5. [工作积累] 32bit to 64bit: array index underflow

    先贴一段C++标准(ISO/IEC 14882:2003): 5.2.1 Subscripting: 1 A postfix expression followed by an expression ...

  6. [工作积累] bitfield

    ISO/IEC 14882:2003: 9.6 Bit-fields [class.bit] A member-declarator of the form identifieropt : const ...

  7. [工作积累] GCC 4.6 new[] operator内存对齐的BUG

    对于用户没有定义dctor(包括其所有成员)的类来说, new CLASS[n] 可能会直接请求sizeof(CLASS)*n的空间. 而带有dctor的 类, 因为delete[]的时候要逐个调用析 ...

  8. [工作积累] TAA Ghosting 的相关问题

    因为TAA要使用上一帧的历史结果,那么在相机移动的时候,颜色就会有残留,出现ghosting(残影). 由于上一帧历史是累积的,是由上一帧的直接渲染结果和上上帧的结果做了合并,所以ghosting并不 ...

  9. [工作积累] shadow map问题汇总

    1.基本问题和相关 Common Techniques to Improve Shadow Depth Maps: https://msdn.microsoft.com/en-us/library/w ...

随机推荐

  1. win10 如何关掉自带的杀毒软件 window defender

    问题描述: win10系统,自带的杀毒软件 window defender 会实时保护电脑对文件进行检测,将认为的病毒文件自动清除, 造成我想下载的MDK5的注册机一直下载不成功,即使从别处拷贝过来, ...

  2. 《Swell数学》用户故事

    一.用户故事基础知识: 1. 从用户的角度来描述用户渴望得到的功能. 2. 用户故事是描述对用户有价值的功能,好的用户故事应该包括角色.功能和商业价值三个要素. 3. 一个用户故事只是以客户能够明白的 ...

  3. lunx中部分命令总结

    一.文件和目录操作命令ls  全拼list,功能是列出目录的内容及其内容属性信息. cd  全拼change directory,功能是从当前工作目录切换到指定的工作目录. cp  全拼copy,其功 ...

  4. ffmpeg奇数分辨率转码失败

    偶然遇到将目的分辨率设置成奇数(例如:854x481)导致ffmpeg转码失败(错误:width not divisible by 2 (854x481)) 于是去查了一下原因:由于转码后的pix_f ...

  5. sklearn-adaboost

    sklearn中实现了adaboost分类和回归,即AdaBoostClassifier和AdaBoostRegressor, AdaBoostClassifier 实现了两种方法,即 SAMME 和 ...

  6. Spring源码学习(6)——容器的功能扩展

    之前的随笔中借BeanFactory介绍了bean的解析和加载的完整过程,实际上,除了BeanFactory,spring还提供了一种功能更加强大的容器:ApplicationContext Appl ...

  7. Python01(linux基础)

    聊一聊计算机 CPU,电源相关,时钟部分(主频),存储器,输入输出系统 计算机记忆装置:存储器分两大类:1.内存Ram(速度快,容量小(相对),断电后内容丢失) 非持久性记忆存储器2.如:硬盘,(速度 ...

  8. Spring学习札记(一)

    写在前面:spring的两大特点:IOC与aop.IOC(Inverse of Control):控制反转,也可以称为依赖倒置.降低耦合.AOP:即面向切面编程. 从Spring的角度看,AOP最大的 ...

  9. 学习小片段——springboot 错误处理

    一:先看看springboot默认的错误处理机制 springboot默认会判断是否是浏览器(http请求头Accept是否含有 text/html)来选择返回html错误页面或json错误信息 原因 ...

  10. 关于C#鼠标方面的。

    因为,遇到这样的提问,就是当点击按钮后,让按钮的位置,随着鼠标移动而移动. 思路,就是按下后记录鼠标的坐标,然后修改按钮的坐标   0: Control.PointToScreen:将指定工作区点的位 ...