Compute Shader in Unity Part2 ——Texture

本文介绍了在Unity中如何使用Compute Shader处理Texture,强调了RWTexture2D<float4>的作用,获取Texture尺寸的方法以及开启RenderTexture的随机读写。还讨论了Texture在Compute Shader中的采样方式,包括基本采样、带Mipmap的采样以及使用SamplerState进行高级采样的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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。</

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值