最近在用unity的compute shader实现一些效果,遇到一个十分匪夷所思的bug,一开始以为自己写的有问题,但即使把shader改为最直接的采样,也仍然出现了这个问题,所以对这个问题做一个记录。 Compute shader计算得到的Texture尺寸不对 #pragma kernel SampleTextureTexture2D<float4>_InputTe
{ public RawImage ri; public ComputeShader shader; RenderTexture tex; void Start () { tex = new RenderTexture(64,64,0); tex.enableRandomWrite = true; tex.Create(); shader.SetTexture(0, "Result", tex); shader.Dispatch(0, 8, 8, 1); ri.texture = tex; } private void OnDestroy() ...
I remember first time creating a compute shader and not knowing what to do with it. This sample shows how to create and bind a render texture to compute shader and to render it. Procedural Points An example to position points in a compute shader and render them procedurally. 2D Particle Tr...
camera.depthTextureMode = DepthTextureMode.Depth; 一旦设置好上面的摄像机模式后,我们就可以在Shader中声明_CameraDepthTexture来访问它。 Unity为我们提供了统一的宏SAMPLE_DEPTH_TEXTURE用来处理这些平台差异(比如PS3和PS2)造成的问题。例如: float d = SAMPLE DEPTH TEXTURE (_CameraDepthTexture , i.uv) ; flo...
从此图可以看出 CPU和GPU之间的数据传输是瓶颈。故当使用GPGPU时,对Texture的逐像素处理不需要传回CPU,因而速度比较快。 Compute Shader Compute Shader下文简称cs 【概念】 Compute Shaders是在GPU运行却又在普通渲染管线之外的程序。用于运行GPGPU program。
float linearDepth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv_depth)); float3 worldPos = _WorldSpaceCameraPos + linearDepth * i.interpolatedRay.xyz; float2 speed = _Time.y * float2(_FogXSpeed, _FogYSpeed); float noise = (tex2D(_NoiseTex, i.uv + speed).r - 0.5...
对于自身带有ShadowCaster Pass或者FallBack中含有,并且Render Queue小于等于2500的渲染对象才会出现在深度纹理中,详细的测试可以参考:【Unity Shader】Shadow Caster、RenderType和_CameraDepthTexture 在Shader中,需提前定义纹理_CameraDepthTexture ,为了兼容不同的平台,Unity ShaderLab提供了UNITY_SAMPLE_DEPTH、SAMPLE_DEPTH...
当在Shader中访问到深度纹理_CameraDepthTexture 后,我们就可以使用当前像素的纹理坐标对它进行采样。绝大多数情况下,我们直接使用tex2D函数采样即可,但在某些平台上,我们需要一些特殊处理。Unity为我们提供了一个统一的宏SAMPLE_DEPTH_TEXTURE,用来处理这些由于平台差异造成的问题。而我们只需要在Shader中使用SAMPLE_DEPTH...
Unlit Shader 会产生一个不包含光照(但包含雾效)的基本顶点/片元着色器,代码如下: Shader "Unlit/UnlitShader" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM ...
最后,在shader中增加噪波相关的运算: half4 ShadowPassFragment(Varyings input #if !defined(CAN_SKIP_VPOS) , UNITY_VPOS_TYPE vpos : VPOS #endif ) : SV_TARGET { half4 BaseColorAlpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv) * _BaseColor; ...