Unity3D rendering important programming - shader programming.

Unity3D rendering important programming - shader programming.

Date: 2017-11-09
Views: 51

As is known to all, all rendering work of Unity3D is inseparable from the Shader, namely Shader. It is actually a small program that is responsible for combining the input Mesh in a specified manner with the input map or color, and then the output. The drawing unit can draw the image to the screen based on this output. The input map or color, plus the corresponding Shader, and the specific parameter Settings for Shader, will be packaged and stored together, resulting in a Material. After that, we can render the material to the appropriate renderer. In the end, Shader is a program that specifies the points and colors that can be read by the input color map and the output renderer.

1. Type of Shader.

Shader generally falls into three categories:

1. Surface Shader -- Surface Shader.

Contains illumination calculations, 3D scenes.

2. Vertex and Fragment Shader -- Vertex/Fragment Shader.

Does not include lighting operation, 2D scene, special effects.

3. Fixed Function Shader -- Fixed Function Shader.

Is not recommended

Unity3D渲染的重要编程——着色器编程


Vertex/fragment shader.

2. Basic structure of Shader.

Open the newly created Shader with a text editor:

 Unity3D渲染的重要编程——着色器编程


Unity3D渲染的重要编程——着色器编程


The attribute Properties {}

Example: _Name(" Display Name ", type) = defaultValue[{options}]

_Name: variable name.

Display Name: Display Name.

Type: there are several types.

Unity3D渲染的重要编程——着色器编程

 DefaultValue: the defaultValue corresponds to the type.

Unity3D渲染的重要编程——着色器编程 

Options: optional

2D/Rect/Cube -- the TexGen mode of OpenGL, at least {}

Example:

Define a Color with a default value of translucent blue (" MainColor ", Color) = (0, 0, 1, 0.5)

Define a default value of pure white Texture Main Texture _MainTex (" Main Texture ", 2D) = "white" {}



SubShader SubShader.

1. Tags

Tags {"TagName1" = "Value1" "TagName2" = "Value2"}

Role: control the rendering engine "when" and "how" to render the subshader content?

Render type:

Tags {" RenderType "=" Opaque "} // render an Opaque object with output.

Tags {" RenderType "=" Transparent "// render outputs Transparent objects.

Render order queue:

Tags {" Queue "=" Transparent "// specify the order Queue to render Transparent objects.

2. Detail level LOD.

LOD: the Level of Detail

The larger the number, the higher the detail, the greater the consumption.

"LOD 100" is a very low value that is the same as the LOD value of the VertexLit (vertex light) Shader.

3, CGPROGRAM... ENDCG

Tag CG program

The implementation language is Microsoft's HLSL language (most of which is the same as NVIDIA's CG language)

4, # pragma

Role: indicates that the compilation corresponds to the Shader function.

The #pragma vertex vert // function is named vertex Shader of vert.

#pragma fragment frag // function name fragment Shader named frag.

5, sampler2D

Sampler2D is a data container interface that is bound to the texture, which is the type of 2D textures in CG/HLSL.

There are also sampler1D, sampler3D, samplerCUBE and other formats.

Note: because the CG program needs to access the variables of the properties (properties), the variables in the CG program must be declared with the same name as the previous variable.

6, the include

Role: import Unity contains files for Shader predefined variables and functions.

#include: "UnityCG. Cginc" // import Unity universal CG predefined files.

7. Appdata and v2f.

Appdata: data entered into Vertex Shader.

V2f: output from Vertex Shader, input to Fragment Shader data.

"Float4" defines a four-dimensional floating-point vector with a floating-point precision of 32-bit.

": POSITION" is the "semantics" of the Shader parameter, representing the spatial coordinates of Vertex.

"SV_POSITION" describes the pixel location of Vertex.

8. Vert and frag.

UnityObjectToClipPos() // convert the object space of Vertex to the clipping space of the camera (screen 2D coordinate)

"Fixed4" defines a four-dimensional floating-point vector with a floating-point precision of 11-bit and range from -2.0 to +2.0.

"SV_Target" describes pixel pixel to Color.

"Return fixed4(1,0,0,1)" indicates that the final frag returns a pure red pixel.

Related news / News More
  • Clicks: 51
    2017 - 11 - 09
    As is known to all, all rendering work of Unity3D is inseparable from the Shader, namely Shader. It is actually a small program that is responsible for combining the input Mesh in a specified manner with the input map or color, and then the output. The drawing unit can draw the image to the screen based on this output. The input map or color, plus the corresponding Shader, and the specific parameter Settings for Shader, will be packaged and stored together, resulting in a Material. After that, we can render the material to the appropriate renderer. In the end, Shader is a program that specifies the points and colors that can be read by the input color map and the output renderer.1. Type of Shader.Shader generally falls into three categories:1. Surface Shader -- Surface Shader.Contains illu...
  • Clicks: 53
    2017 - 12 - 08
    The main interfaceInterface in some function is not perfect, but I'm still posted the latest interface, open to you is to want to tell you that each person has a different tool interface, have their own tools, so I will share here Interface source reference: Transform relative Transform position, direction, size.  Refer to the above script, for example setting location, direction and size. The advantage of using it is that it can ignore the changes caused by the object.Fast switch object activation close.  Shortcut Ctrl+1 to quickly activate or close the selected object. Clicking on the upper-left corner of the property panel with the mouse will be tricky.Fast marshalling objects. Ctrl+4, which adds a parent node to several selected objects, is equivalent...
  • Clicks: 74
    2018 - 01 - 12
    1: pull a "Cube" in the scene and attach the camera and the moving object to the Cube.2: create a lens.3: click on the animation to create a "Matinee".4: select the shot to create a "New Camera Group" on the left side, and set the time for animation on the right time axis.5: select "Movement" to press enter and a keyframe will appear on the right.6: go back to the scene and drag the camera to set the track.7: select the Cube you just created, create a New Empty Group on the right, and right-click to add the New "Movement Track".8: mouse the left key to select key frames 1 press the return key, press and hold Ctrl can appear not to drag another key frames and drag it to the appropriate location, you can create multiple keyframes, create good...
  • Clicks: 126
    2018 - 02 - 02
    Maybe you're a developer who wants to make cool commercials for your HTC Vive game. Or you are the game anchor, want to bring the high quality VR live experience for the audience; Even you are a fan of VR. Anyway, if you're interested in MixedReality, go ahead. This article describes how to use HTC Vive to shoot high quality MR video.What is Mixed Reality(Mixed Reality) film?Mixed Reality film is a film that is synthesized through the images in VR. Mixed Reality video is more suitable for live-streaming and propaganda films than the VR films recorded in general methods.If it is still not clear, you can see the following film, which is the film effect produced by Mixed Reality.http://v.qq.com/x/page/x0194621p8b.htmlMake the props for the Mixed Reality film.1.) green screen environment2.) ca...
  • Name
  • *
  • company name:
  • *
  • Address:
  • *
  • Phone
  • *
  • fax:
  • *
  • E-mail:
  • *
  • Postal code:
  • *
  • Real estate name
  • *
  • Content
  • *
     
Room 301, Bldg 1, No.388 Zhongjiang Road, Shanghai
64683109
18665895116
sales@vrwaibao.com
Follow us
Mobile phone side
WeChat public number
Online message
Contact us
Copyright ©2017 - 2020 Zhi Jing Technology