Texture 在Compute Shader 中算是一个特别重要的应用了。
其实原理什么的都和上一篇说的一样。
下面说一些不一样的。
#pragma kernel CSMain
RWTexture2D<float4> Result;
[numthreads(8,8,1)]
void CSMain (uint2 id : SV_DispatchThreadID)
{
float x, y;
Result.GetDimensions(x, y);
Result[id] = float4(id.x/x,id.y/y,1,1);
}
-------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class _t2 : MonoBehaviour {
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()
{
tex.Release();
}
}
1.RWTexture2D<float4> Result; 表示可随机写入的Texture。</