{"id":374256,"date":"2023-07-25T14:20:56","date_gmt":"2023-07-25T13:20:56","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=dotnet&#038;p=374256"},"modified":"2023-07-25T14:23:27","modified_gmt":"2023-07-25T13:23:27","slug":"improvements-to-unity-shader-files-in-rider-2023-2","status":"publish","type":"dotnet","link":"https:\/\/blog.jetbrains.com\/zh-hans\/dotnet\/2023\/07\/25\/improvements-to-unity-shader-files-in-rider-2023-2","title":{"rendered":"Improvements to Unity shader files in Rider 2023.2"},"content":{"rendered":"\n<p>With the release of Rider 2023.2 just around the corner, it\u2019s time to take a look at all the new features we\u2019ve added for <a href=\"https:\/\/docs.unity3d.com\/Manual\/Shaders.html\" target=\"_blank\" rel=\"noopener\">Unity shader<\/a> files.<\/p>\n\n\n\n<p>This release builds on the existing support for ShaderLab <code>.shader<\/code> files as well as Rider\u2019s deep understanding of the HLSL shader language to provide a <strong>more productive editing experience<\/strong>. It introduces a wealth of new features like typing assistance, live templates, online help, breadcrumbs, the <em>Structure<\/em> tool window and even Ctrl+Click navigation and renaming of shaders and shader passes.<\/p>\n\n\n\n<p>Let\u2019s dig in and see what\u2019s new.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Typing Assistance<\/h2>\n\n\n\n<p>One of the most basic features you\u2019d expect from a smart IDE like Rider is <strong>typing assistance<\/strong>, and we\u2019ve made a lot of changes to ShaderLab support to provide this. Simply type an opening <code>{<\/code> brace,  and Rider will automatically insert the closing <code>}<\/code> brace. Hit enter inside the braces to insert a new line, with correct auto-indent and formatting. ShaderLab is a very block-oriented language, so this will save you a lot of boilerplate keystrokes!<\/p>\n\n\n\n<p>Rider now supports completion of block keywords, command keywords, and even command values. Just start typing, and <strong>Rider will automatically show a popup<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-24-at-5.22.57-pm-1.png\" alt=\"Completion of ShaderLab keywords and live templates\" class=\"wp-image-375378\" width=\"600\" height=\"300\"\/><\/figure>\n\n\n\n<p>As you can see in the image above, we\u2019ve also added <a href=\"https:\/\/www.jetbrains.com\/help\/rider\/Using_Live_Templates.html\" target=\"_blank\" rel=\"noopener\">live templates<\/a>. These allow you to quickly create various properties, as well as more complex shaders that will create a surface shader (<code>surf<\/code>), a vertex and fragment shader (<code>vfshader<\/code>), or \u2013 if you\u2019re already inside a subshader block \u2013 create a new pass (<code>vfpass<\/code>). And once inside a pass, there are a number of templates for creating common <code>Blend<\/code> commands.<\/p>\n\n\n\n<p>Naturally, the templates are all fully editable, and you can even create your own, specifying the appropriate scope to ensure they only show in the right place.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"2024\" height=\"1624\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-24-at-5.00.58-pm.png\" alt=\"The Live Templates editor showing the vfshader template\" class=\"wp-image-375219\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Online Help<\/h2>\n\n\n\n<p>Rider will show you a tooltip for all ShaderLab keywords, with a link to external documentation. This will <strong>open the correct page in the locally installed documentation<\/strong>, if available, or a search page on Unity\u2019s help site if not.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-24-at-5.02.55-pm.png\" alt=\"A tooltip for the SubShader command keyword, showing a link to external documentation\" class=\"wp-image-375231\" width=\"800\" height=\"300\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><em>Structure<\/em> Tool Window<\/h2>\n\n\n\n<p>ShaderLab files can be very large. With the <a href=\"https:\/\/www.jetbrains.com\/help\/idea\/structure-tool-window-file-structure-popup.html\" target=\"_blank\" rel=\"noopener\"><em>Structure<\/em> tool window<\/a> you can see an overview of your ShaderLab file and quickly navigate around. The block structure of the file is shown as a tree, and clicking a node will take you to the ShaderLab block or command. It will also show you the structure of the embedded HLSL programs.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-24-at-5.20.22-pm.png\" alt=\"The Structure tool window, showing an outline of the contents of a ShaderLab file in a tree view. The outline of the CGPROGRAM block is also shown\" class=\"wp-image-375354\" width=\"600\" height=\"550\"\/><\/figure>\n\n\n\n<p>To give you a more compact view of where you are in the file, Rider shows the file\u2019s structure in the breadcrumbs control at the bottom of the editor or in the status bar in the <a href=\"https:\/\/www.jetbrains.com\/help\/rider\/New_UI.html\" target=\"_blank\" rel=\"noopener\">new UI<\/a>. Here too, you can simply click the nodes to navigate around the file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-24-at-5.09.19-pm.png\" alt=\"The breadcrumbs control at the bottom of the editor provides a compact view of the outline of the ShaderLab file's structure\" class=\"wp-image-375275\" width=\"800\" height=\"200\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Shader and Pass Names<\/h2>\n\n\n\n<p>These are all nice features, but let\u2019s up the ante a little and look at something even smarter!<\/p>\n\n\n\n<p>Each shader you create has a name, declared in the <code>Shader<\/code> block command at the top of the file. This name can be used in a few places, such as the <code>Fallback<\/code> command. Moreover, you can give each pass a name and use that in the <code>UsePass<\/code> command. Wouldn\u2019t it be great if your IDE could provide completion for these names?<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-24-at-5.10.40-pm.png\" alt=\"Completion of the shader name in the Fallback command\" class=\"wp-image-375286\" width=\"800\" height=\"300\"\/><\/figure>\n\n\n\n<p>Rider will not only provide completion, it also provides <em>Ctrl+Click <\/em>navigation, <em>Find Usages<\/em>, and rename refactoring. And when you rename a pass, Rider will make sure that <code>UsePass<\/code> refers to it in uppercase, an important ShaderLab requirement.<\/p>\n\n\n\n<p>Your C# code doesn\u2019t miss out here, either. If you\u2019re using <code>Shader.Find<\/code>, Rider will provide <strong>completion of the shader name<\/strong>, with working navigation, <em>Find Usages<\/em>, and rename support.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-24-at-5.12.42-pm.png\" alt=\"Completion of shader name in a call to Shader.Find\" class=\"wp-image-375297\" width=\"800\" height=\"300\"\/><\/figure>\n\n\n\n<p>Rider doesn\u2019t currently show built-in shaders, but we hope to add this in a future release, at which point we can add validation highlighting if the shader doesn\u2019t exist \u2013 a perfect way to help avoid typos.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HLSL features<\/h2>\n\n\n\n<p>To round things off, there are also a couple of updates within HLSL blocks and files. Firstly, Rider understands the <code>#pragma surface<\/code> and <code>#pragma vertex<\/code> directives and will resolve the surface or vertex shader method name, making sure you\u2019ve declared it correctly, <strong>highlighting it as an error if it can\u2019t be found<\/strong>. If the method name is valid, Rider will provide the usual <em>Ctrl+Click<\/em> navigation, <em>Find Usages<\/em>, and rename support.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"600\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/pragma_references_preview.png\" data-gif-src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/pragma_references.gif\" alt=\"Rider resolves the method name in pragma directives\" class=\"wp-image-375401\"\/><\/figure>\n\n\n\n<p>And speaking of pragma directives, how does parameter completion sound?<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-24-at-5.13.33-pm-1.png\" alt=\"Completion of pragma directive keywords\" class=\"wp-image-375319\" width=\"800\" height=\"300\"\/><\/figure>\n\n\n\n<p>We\u2019ve fixed the IDE\u2019s handling of the <code>Packages<\/code> folder in <code>#include<\/code> statements. Unity treats this as a kind of \u201cvirtual\u201d folder, and you can use it to include files from the <code>Packages<\/code> folder in your project or from packages stored in the package cache. Rider will now do the same and supports completion and <em>Ctrl+Click<\/em> navigation for all files.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-24-at-5.14.31-pm.png\" alt=\"\" class=\"wp-image-375330\" width=\"800\" height=\"300\"\/><\/figure>\n\n\n\n<p>And finally, Rider now understands the <code>UNITY_VERSION<\/code> predefined macro when analysing your HLSL files. If you hover over the macro, <strong>you\u2019ll see the current value<\/strong>, based on the version of Unity you\u2019re using.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/07\/Screenshot-2023-07-24-at-5.15.16-pm.png\" alt=\"The UNITY_VERSION predefined macro, with a tooltip showing the actual value\" class=\"wp-image-375341\" width=\"600\" height=\"200\"\/><\/figure>\n\n\n\n<p>We hope you\u2019ll agree that 2023.2 is a great release for Unity shader file support! We\u2019d love to hear what you think. <a href=\"https:\/\/www.jetbrains.com\/rider\/nextversion\/\" target=\"_blank\" rel=\"noopener\">Download Rider 2023.2<\/a>, try it out with your own shaders, and let us know how you get on in the comments below.<\/p>\n","protected":false},"author":96,"featured_media":375489,"comment_status":"closed","ping_status":"closed","template":"","categories":[4992,1401],"tags":[477,1978,2031,3772,1941],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/dotnet\/374256"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/dotnet"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/dotnet"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/96"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=374256"}],"version-history":[{"count":10,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/dotnet\/374256\/revisions"}],"predecessor-version":[{"id":375512,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/dotnet\/374256\/revisions\/375512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media\/375489"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=374256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=374256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=374256"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=374256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}