Unity中Shader URP最简Shader框架(ShaderGraph 转 URP Shader)
文章目录
- 前言
- 一、 我们先了解一下 Shader Graph 怎么操作
- 1、了解一下 Shader Graph 的面板信息
- 2、修改Shader路径
- 3、鼠标中键 或 Alt + 鼠标左键 移动画布
- 4、鼠标右键 打开创建节点菜单
- 5、把ShaderGraph节点转化为 Shader 代码
- 6、可以看出 URP 和 BuildIn RP 大体框架一致
- 二、把ShaderGraph转化后的 Shader 只保留最基础的通用前向渲染Pass
- 1、我们看一下 URP Pass不可用后使用的默认Shader
前言
在之前的文章中,我们学习了 ShaderLab 中 BuildIn Render Pipeline 下的Shader书写。在这篇文章中,我们来了解一下 URP 下 的最简Shader怎么编写。
一、 我们先了解一下 Shader Graph 怎么操作
1、了解一下 Shader Graph 的面板信息
2、修改Shader路径
3、鼠标中键 或 Alt + 鼠标左键 移动画布
4、鼠标右键 打开创建节点菜单
5、把ShaderGraph节点转化为 Shader 代码
- 可以直接 查看 或 复制 编译后的Shader
6、可以看出 URP 和 BuildIn RP 大体框架一致
二、把ShaderGraph转化后的 Shader 只保留最基础的通用前向渲染Pass
因为是由ShaderGraph转化过来的。所以,这里虽然简化了。但是,还是过于臃肿,在之后的文章中,我们来优化一下
Shader "MyShader/P2_10" { Properties {} SubShader { Tags { "RenderPipeline"="UniversalPipeline" "RenderType"="Opaque" "Queue"="Geometry" } Pass { Name "Universal Forward" Tags { // LightMode: } // Render State Cull Back Blend One Zero ZTest LEqual ZWrite On // Debug // // -------------------------------------------------- // Pass HLSLPROGRAM // Pragmas #pragma target 2.0 #pragma multi_compile_instancing #pragma multi_compile_fog #pragma instancing_options renderinglayer #pragma vertex vert #pragma fragment frag // Keywords #pragma multi_compile _ LIGHTMAP_ON #pragma multi_compile _ DIRLIGHTMAP_COMBINED #pragma shader_feature _ _SAMPLE_GI #pragma multi_compile_fragment _ _DBUFFER_MRT1 _DBUFFER_MRT2 _DBUFFER_MRT3 #pragma multi_compile_fragment _ DEBUG_DISPLAY #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION // GraphKeywords: // Defines #define ATTRIBUTES_NEED_NORMAL #define ATTRIBUTES_NEED_TANGENT #define VARYINGS_NEED_POSITION_WS #define VARYINGS_NEED_NORMAL_WS #define FEATURES_GRAPH_VERTEX /* WARNING: $splice Could not find named fragment 'PassInstancing' */ #define SHADERPASS SHADERPASS_UNLIT #define _FOG_FRAGMENT 1 /* WARNING: $splice Could not find named fragment 'DotsInstancingVars' */ // custom interpolator pre-include /* WARNING: $splice Could not find named fragment 'sgci_CustomInterpolatorPreInclude' */ // Includes #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl" #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureStack.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DBuffer.hlsl" #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShaderPass.hlsl" // -------------------------------------------------- // Structs and Packing // custom interpolators pre packing /* WARNING: $splice Could not find named fragment 'CustomInterpolatorPrePacking' */ struct Attributes { float3 positionOS : POSITION; float3 normalOS : NORMAL; float4 tangentOS : TANGENT; #if UNITY_ANY_INSTANCING_ENABLED uint instanceID : INSTANCEID_SEMANTIC; #endif }; struct Varyings { float4 positionCS : SV_POSITION; float3 positionWS; float3 normalWS; #if UNITY_ANY_INSTANCING_ENABLED uint instanceID : CUSTOM_INSTANCE_ID; #endif #if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE))) uint stereoTargetEyeIndexAsBlendIdx0 : BLENDINDICES0; #endif #if (defined(UNITY_STEREO_INSTANCING_ENABLED)) uint stereoTargetEyeIndexAsRTArrayIdx : SV_RenderTargetArrayIndex; #endif #if defined(SHADER_STAGE_FRAGMENT) && defined(VARYINGS_NEED_CULLFACE) FRONT_FACE_TYPE cullFace : FRONT_FACE_SEMANTIC; #endif }; struct SurfaceDescriptionInputs { }; struct VertexDescriptionInputs { float3 ObjectSpaceNormal; float3 ObjectSpaceTangent; float3 ObjectSpacePosition; }; struct PackedVaryings { float4 positionCS : SV_POSITION; float3 positionWS : INTERP0; float3 normalWS : INTERP1; #if UNITY_ANY_INSTANCING_ENABLED uint instanceID : CUSTOM_INSTANCE_ID; #endif #if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE))) uint stereoTargetEyeIndexAsBlendIdx0 : BLENDINDICES0; #endif #if (defined(UNITY_STEREO_INSTANCING_ENABLED)) uint stereoTargetEyeIndexAsRTArrayIdx : SV_RenderTargetArrayIndex; #endif #if defined(SHADER_STAGE_FRAGMENT) && defined(VARYINGS_NEED_CULLFACE) FRONT_FACE_TYPE cullFace : FRONT_FACE_SEMANTIC; #endif }; PackedVaryings PackVaryings(Varyings input) { PackedVaryings output; ZERO_INITIALIZE(PackedVaryings, output); output.positionCS = input.positionCS; output.positionWS.xyz = input.positionWS; output.normalWS.xyz = input.normalWS; #if UNITY_ANY_INSTANCING_ENABLED output.instanceID = input.instanceID; #endif #if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE))) output.stereoTargetEyeIndexAsBlendIdx0 = input.stereoTargetEyeIndexAsBlendIdx0; #endif #if (defined(UNITY_STEREO_INSTANCING_ENABLED)) output.stereoTargetEyeIndexAsRTArrayIdx = input.stereoTargetEyeIndexAsRTArrayIdx; #endif #if defined(SHADER_STAGE_FRAGMENT) && defined(VARYINGS_NEED_CULLFACE) output.cullFace = input.cullFace; #endif return output; } Varyings UnpackVaryings(PackedVaryings input) { Varyings output; output.positionCS = input.positionCS; output.positionWS = input.positionWS.xyz; output.normalWS = input.normalWS.xyz; #if UNITY_ANY_INSTANCING_ENABLED output.instanceID = input.instanceID; #endif #if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE))) output.stereoTargetEyeIndexAsBlendIdx0 = input.stereoTargetEyeIndexAsBlendIdx0; #endif #if (defined(UNITY_STEREO_INSTANCING_ENABLED)) output.stereoTargetEyeIndexAsRTArrayIdx = input.stereoTargetEyeIndexAsRTArrayIdx; #endif #if defined(SHADER_STAGE_FRAGMENT) && defined(VARYINGS_NEED_CULLFACE) output.cullFace = input.cullFace; #endif return output; } // -------------------------------------------------- // Graph // Graph Properties CBUFFER_START(UnityPerMaterial) CBUFFER_END // Object and Global properties // Graph Includes // GraphIncludes: // -- Property used by ScenePickingPass #ifdef SCENEPICKINGPASS float4 _SelectionID; #endif // -- Properties used by SceneSelectionPass #ifdef SCENESELECTIONPASS int _ObjectId; int _PassValue; #endif // Graph Functions // GraphFunctions: // Custom interpolators pre vertex /* WARNING: $splice Could not find named fragment 'CustomInterpolatorPreVertex' */ // Graph Vertex struct VertexDescription { float3 Position; float3 Normal; float3 Tangent; }; VertexDescription VertexDescriptionFunction(VertexDescriptionInputs IN) { VertexDescription description = (VertexDescription)0; description.Position = IN.ObjectSpacePosition; description.Normal = IN.ObjectSpaceNormal; description.Tangent = IN.ObjectSpaceTangent; return description; } // Custom interpolators, pre surface #ifdef FEATURES_GRAPH_VERTEX Varyings CustomInterpolatorPassThroughFunc(inout Varyings output, VertexDescription input) { return output; } #define CUSTOMINTERPOLATOR_VARYPASSTHROUGH_FUNC #endif // Graph Pixel struct SurfaceDescription { float3 BaseColor; }; SurfaceDescription SurfaceDescriptionFunction(SurfaceDescriptionInputs IN) { SurfaceDescription surface = (SurfaceDescription)0; surface.BaseColor = IsGammaSpace() ? float3(0.5, 0.5, 0.5) : SRGBToLinear(float3(0.5, 0.5, 0.5)); return surface; } // -------------------------------------------------- // Build Graph Inputs #ifdef HAVE_VFX_MODIFICATION #define VFX_SRP_ATTRIBUTES Attributes #define VFX_SRP_VARYINGS Varyings #define VFX_SRP_SURFACE_INPUTS SurfaceDescriptionInputs #endif VertexDescriptionInputs BuildVertexDescriptionInputs(Attributes input) { VertexDescriptionInputs output; ZERO_INITIALIZE(VertexDescriptionInputs, output); output.ObjectSpaceNormal = input.normalOS; output.ObjectSpaceTangent = input.tangentOS.xyz; output.ObjectSpacePosition = input.positionOS; return output; } SurfaceDescriptionInputs BuildSurfaceDescriptionInputs(Varyings input) { SurfaceDescriptionInputs output; ZERO_INITIALIZE(SurfaceDescriptionInputs, output); #ifdef HAVE_VFX_MODIFICATION #if VFX_USE_GRAPH_VALUES uint instanceActiveIndex = asuint(UNITY_ACCESS_INSTANCED_PROP(PerInstance, _InstanceActiveIndex)); /* WARNING: $splice Could not find named fragment 'VFXLoadGraphValues' */ #endif /* WARNING: $splice Could not find named fragment 'VFXSetFragInputs' */ #endif #if UNITY_UV_STARTS_AT_TOP #else #endif #if defined(SHADER_STAGE_FRAGMENT) && defined(VARYINGS_NEED_CULLFACE) #define BUILD_SURFACE_DESCRIPTION_INPUTS_OUTPUT_FACESIGN output.FaceSign = IS_FRONT_VFACE(input.cullFace, true, false); #else #define BUILD_SURFACE_DESCRIPTION_INPUTS_OUTPUT_FACESIGN #endif #undef BUILD_SURFACE_DESCRIPTION_INPUTS_OUTPUT_FACESIGN return output; } // -------------------------------------------------- // Main #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl" #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/UnlitPass.hlsl" // -------------------------------------------------- // Visual Effect Vertex Invocations #ifdef HAVE_VFX_MODIFICATION #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/VisualEffectVertex.hlsl" #endif ENDHLSL } } FallBack "Hidden/Shader Graph/FallbackError" }1、我们看一下 URP Pass不可用后使用的默认Shader
使用的是内置渲染管线,返回颜色为洋葱紫
Shader "Hidden/Universal Render Pipeline/FallbackError" { SubShader { Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" } Pass { HLSLPROGRAM #pragma target 2.0 #pragma editor_sync_compilation // ------------------------------------- // Shader Stages #pragma vertex vert #pragma fragment frag // ------------------------------------- // Unity defined keywords #pragma multi_compile _ STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON //-------------------------------------- // GPU Instancing #include_with_pragmas "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DOTS.hlsl" // ------------------------------------- // Includes #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl" struct appdata_t { float4 vertex : POSITION; UNITY_VERTEX_INPUT_INSTANCE_ID }; struct v2f { float4 vertex : SV_POSITION; UNITY_VERTEX_OUTPUT_STEREO }; v2f vert (appdata_t v) { v2f o; UNITY_SETUP_INSTANCE_ID(v); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); o.vertex = TransformObjectToHClip(v.vertex.xyz); return o; } float4 frag (v2f i) : SV_Target { return float4(1,0,1,1); } ENDHLSL } } Fallback "Hidden/Core/FallbackError" }
- 可以直接 查看 或 复制 编译后的Shader
- 1、我们看一下 URP Pass不可用后使用的默认Shader
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!





