当前位置: 首页> 教育> 就业 > 广州市手机网站建设平台_百度一下官网首页_太原关键词优化服务_seo关键词排名优化系统

广州市手机网站建设平台_百度一下官网首页_太原关键词优化服务_seo关键词排名优化系统

时间:2025/7/16 21:57:33来源:https://blog.csdn.net/weixin_43796392/article/details/147638199 浏览次数:0次
广州市手机网站建设平台_百度一下官网首页_太原关键词优化服务_seo关键词排名优化系统

参考

Getting Started With Compute Shaders In Unity

环境

Win10
Unity20194.40

全屏纯色纹理示例

使用ComputerShader逐个像素设置颜色

  1. ComputeShader脚本
    设置纹理颜色
#pragma kernel CSMainRWTexture2D<float4> Result;//纹理
half4 solidColor;//颜色[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{Result[id.xy] = solidColor;//设置颜色 id.xy对应屏幕上的像素点
}
  1. UseSolidComputeShader脚本
    调用ComputeShader,绘制纯色纹理
using UnityEngine;
public class UseSolidComputeShader : MonoBehaviour
{[SerializeField] ComputeShader computeShader;RenderTexture renderTexture;int functionId;int groupsX;int groupsY;public RenderTexture Texture => renderTexture;public void Init(int width, int height){renderTexture = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);renderTexture.enableRandomWrite = true;renderTexture.Create();functionId = computeShader.FindKernel("CSMain");groupsX = Mathf.CeilToInt(renderTexture.width / 8f);//向上取整,避免小于纹理的宽度groupsY = Mathf.CeilToInt(renderTexture.height / 8f);computeShader.SetTexture(functionId, "Result", renderTexture);}public void Draw(Color color){computeShader.SetVector("solidColor", color);computeShader.Dispatch(functionId, groupsX, groupsY, 1);}
}
  1. TestSolidColor脚本
    按下D键,绘制纯色纹理,显示在Rawimage上
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.UI;
public class TestSolidColor : MonoBehaviour
{[SerializeField] UseSolidComputeShader useSolidComputeShader;[SerializeField] RawImage rawImage;private void Awake(){useSolidComputeShader.Init(Screen.width, Screen.height);rawImage.texture = useSolidComputeShader.Texture;}private void Update(){if (Input.GetKeyDown(KeyCode.D)){Profiler.BeginSample("Random Solid Color");useSolidComputeShader.Draw(Random.ColorHSV());Profiler.EndSample();}}
}
  1. 场景层级
    在这里插入图片描述
    Draw对象,添加TestSolidColor脚本,UseSolidComputeShader,设置引用。
    RawImage为全屏大小
  2. 运行,效果图如下
    在这里插入图片描述
关键字:广州市手机网站建设平台_百度一下官网首页_太原关键词优化服务_seo关键词排名优化系统

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: