本文续前一篇《在Unity中使用ComputeShader》
https://blog.csdn.net/tangyin025/article/details/110876760
修改成使用URP环境下实现ComputeShader
Unity2019需要安装Package:Universal RP

安装之后,通过菜单 Asset | Create | Rendering | Universal Render Pipeline | Pipeline Asset (Forward Renderer),创建UniversalRenderPipelineAsset.asset、UniversalRenderPipelineAsset_Renderer.asset

再到菜单 Edit | Project Settings… | Graphics,设置Scriptable Render Pipeline Settings为刚才创建的asset,这样就将现有的工程修改为URP环境了

接下来准备好和上一篇一样的资源:
- Cube.prefab
- NewComputeShader.compute
- NewSurfaceShader 1.shader
- New Material 1.mat
- NewBehaviourScript1.cs
其中NewSurfaceShader 1.shader必须修改为支持URP的HLSL版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | Shader "Custom/NewSurfaceShader 1" {<!-- --> Properties {<!-- --> } SubShader {<!-- --> Tags {<!-- --> "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" } Pass {<!-- --> HLSLPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_instancing #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #pragma instancing_options procedural:ConfigureProcedural #pragma editor_sync_compilation #if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED) StructuredBuffer<float3> _Positions; #endif float2 _Scale; void ConfigureProcedural() {<!-- --> #if defined(UNITY_PROCEDURAL_INSTANCING_ENABLED) float3 position = _Positions[unity_InstanceID]; unity_ObjectToWorld = 0.0f; unity_ObjectToWorld._m03_m13_m23_m33 = float4(position, 1.0f); unity_ObjectToWorld._m00_m11_m22 = _Scale.x; unity_WorldToObject = 0.0f; unity_WorldToObject._m03_m13_m23_m33 = float4(-position, 1.0f); unity_WorldToObject._m00_m11_m22 = _Scale.y; #endif } struct Attributes {<!-- --> float4 positionOS : POSITION; UNITY_VERTEX_INPUT_INSTANCE_ID }; struct Varyings {<!-- --> float4 positionCS : SV_POSITION; float4 color : COLOR; UNITY_VERTEX_INPUT_INSTANCE_ID // UNITY_VERTEX_OUTPUT_STEREO // VR、立体眼镜支持 }; Varyings vert(Attributes input) {<!-- --> Varyings output = (Varyings)0; UNITY_SETUP_INSTANCE_ID(input); UNITY_TRANSFER_INSTANCE_ID(input, output); // UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz); output.positionCS = vertexInput.positionCS; output.color = float4(saturate(vertexInput.positionWS * 0.5f + 0.5f), 1); return output; } half4 frag(Varyings input) : SV_Target {<!-- --> UNITY_SETUP_INSTANCE_ID(input); // UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); return input.color; } ENDHLSL } } FallBack "Diffuse" } |
修改之后,工程Reimport一下,确保没有错误。新建场景、新建Empty Object,挂载NewBehaviourScript1.cs,就能运行了
