This is the 8th day of my participation in the August Text Challenge.More challenges in August


📢 preface

  • A few days ago I saw a Unity guru at CSDN using tools to create various Shader effects and so on
  • I was intrigued, even though I hadn’t fully learned what the big guy was saying
  • But I’m going to try and make a few shaders myself
  • Today, use Shader to make a simple rainbow ball as a starter


🍇 Rainbow ball making

First open Unity project inProjectRight click on panelCreateTo create aShader! As you can see, there are a number of categories you can create, but let’s just pick one

  • Double-click on the Shader to open it
  • Mine is like this, looks like C# ordinary Script is still very different!
  • And there is generally no error message in the wrong code, but you can also download some plug-ins to let him display error message!

  • The next step is to delete all this code!
  • Yes, the blogger did not typo, just delete it, our homemade Shader does not need this code!

🏳️🌈 A brief introduction to the Shader structure

And let’s take a look at the Shader code that I wrote down here

// Set the path and name of the Shader
Shader "MyShaders/shader001"
{
	// Set external properties
	Properties
	{
	   _MainColor("Color",Color) = (1.1.1.1)}// Child shader
	SubShader{ Pass { } }
}
Copy the code

The first one is setupShaderThe path and name of this is selected in UnityShaderWill use

  • Then there is the keyword Properties, which is internally used to set the external Properties
  • The Properties we can manipulate in Unity’s Shader panel are set in the Properties section

For example, there is a line in the code above

_MainColor("Color",Color) = (1.1.1.1)
Copy the code

What this line represents isColor SettingsAdd this line to set the colors in Unity


🏳️🌈 Rainbow ball instance Shader code

  • More Shader structures will not be covered; this article is not intended to learn about shaders
  • Because Shader doesn’t know much about it either
  • Look directly at the rainbow ball code! The code is as follows:
Shader "MyShaders/VFShader/VSShader002"
{
   Properties
    {
         _Offset("Rainbow display offset",Range(0.1=))0
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM  // Start with CG language

            // Compile instruction shader name Shader function name
            #pragma// Vertex shader
            #pragmaFragment Frag // Fragment shader

            // Declare external attributes
            fixed4 _MainColor;
            fixed _Offset;

            struct v2f
            {
            // Projection space coordinates
               half4 clipPos:SV_POSITION ;
               // Coordinates in model space
               half4 modelPos:TEXCOORD0;

			};
            // Vertex function: parameter semantically bound model space coordinates, return values corresponding to screen space coordinates
            v2f vert(half4 vertexPos:POSITION)
            {
            // The structure object returned
              v2f o;
             // Convert vertices from model space to projection space coordinates
              o.clipPos=UnityObjectToClipPos(vertexPos);
              // Store the model space coordinates temporarily
              o.modelPos=vertexPos+fixed4(_Offset,_Offset,_Offset,0);// Return the result
               return o; 
			}
            fixed4 frag (v2f o):SV_TARGET
            {
                / / return fixed4,0,0,1 (1);
                return o.modelPos;
			}
            ENDCG   //-- Cg language ending}}}Copy the code

prompt

  • No errors are reported when the editor writes Shader code
  • When an error occurs, click the Shader script in Unity interface, there will be a red error message, according to the error message can be modified!


🏳 ️ 🌈 Unity

To create aMaterialThe materialThe selectedMaterialThe one that we createdShaderAdd, as shown below:Then create a few random game objects in the sceneMaterialAdd it to the gameobjectMaterialDrag toThe game objectIt is good to go


🏳️🌈 Effect display


💬 summary

  • This article briefly introduces a small Demo of a rainbow ball Shader

  • Since blogger Shader is also in the learning stage, he can barely take a look at what he has done

  • Take it one step at a time! Come on, Ollie. Here you go.