"OpenGL" Dota2 Shader Analysis (1)

Source: Internet
Author: User

Forwarding, please keep address: http://blog.csdn.net/stalendp/article/details/50953869

Recently in summing up OpenGL knowledge, thought of previously searched for DOTA2 related articles: "Dota2-character Shader Masks Guide", and DOTA2 model can also be downloaded on the Internet, so it is well worth to be used as a summary of light-related knowledge. I use the ogre model to do the experiment, the effect is as follows:


This shader just to summarize shader knowledge points, for the implementation may be more rough point, so forgive me.

I'm going to put this Shader in two parts: 1) Use surface Shader method to display the Dota2-character Shader masks Guide algorithm. 2) Using Vertex/fragment method to explain, will be more inclined to surface shader for us to save the tedious transactions (such as how to correctly calculate the normal, etc.). In addition, the download links for the resources involved in this article are as follows: Dota2 Shader for Unity. OK, let's get into the first part.

Shader requires four posters, such as:


This paper mainly introduces the corresponding algorithm of two mask maps.

The graphical algorithm first uses the graphic method to realize the effect, mainly is to emphasize: 1 can use the layer to contain the special meaning information; 2) to adjust the effect, the illumination model is very important, the effect here is Dota2 proprietary, the relatively simple illumination model also has the Phong/blinn model, the BRDF model and so on. We use the material ball of the character weapon as a reference, the original map color:
A. Diffuse reflection section (MASK1.G)
FLOAT3 albedo = tex2d (_maintex, I.UV). rgb;float diff = max (0.05, Dot (worldn, lightdirection)); Float3 diffusereflection = a Lbedo * diff;
Diffuse wrap texture (related concepts: Half-lambert):
FLOAT3 diffusereflection = albedo * tex2d (_diffuse, Float2 (diff, 0.2));
Results of Diffuse

B. High-light correlation (mask2.a,MASK2.R)
Different materials, the size of the high-light point is not the same, the more coarse, the higher the high light point but the smaller the brightness, rough to a certain extent is diffuse (in contrast, mirror reflection). Here are two posters showing the high view size (mask2.a) and intensity (MASK2.R):
We found that the same material has a different intensity of light, which is due to the consideration of AO (Ambient occlusion);
FLOAT3 specularreflection =  pow (max (0.0, dot (reflect (-lightdirection, worldn), viewdirection)), shininess) * specintensity;
Plus Diffuse reflection:
Diffuse + Highlight results:

C. rimlighting MASK2.G

The rimlighting is used to brighten object edges, just as there are light sources behind objects, making objects more prominent. The edge light intensity of different materials is not the same, the metal will be weaker, leather skin and so on will be stronger, here with MASK2.G to control the light of the edge. Mask is as follows:


FLOAT3 rim = rimintensity * Saturate (1-saturate (dot (worldn, viewdirection)) *1.8);

Diffuse + Highlight +rim results:

D. Spontaneous light (mask1.a) is used to simulate the effect of heat radiation, etc., mask is as follows:
Diffusereflection = Albedo  * LERP (tex2d (_diffuse, Float2 (diff, 0.2)), FLOAT3 (1, 1, 1) * 2, selfillu);
Diffuse + specular +rim+ self-luminous results:


D. More realistic metals (mask1.b, mask2.b) F. Detail map slightly
Algorithm principle

Here the principle of shader and the principle of phong/blinn is similar, the Phone/blinn model is mainly using three vectors (light, View, Normal Direction) to calculate the environmental lights (ambient), Diffuse reflection (diffuse) and highlights (specular). And the DOTA2 model is based on this, using mask to distinguish the various types of light, coupled with Rimlight, detailmap and other effects. In addition, the Blinn model is an optimization scheme for the Phong model (refer to the article: Shading model), and of course the physical rendering models (after I understand it, write an article:)

Here's a look at the Phong/blinn model:

Diffuse reflection
Highlights
Rimlight

Full code

Shader "Stalendp/dotashadersuf" {Properties {_maintex ("Albedo (RGB)", 2D) = "White" {}_normal ("Normal", 2D) = "White" {}_m Ask1 ("Mask1", 2D) = "White" {}_mask2 ("Mask2", 2D) = ' White ' {}_diffuse ("diffuse", 2D) = "White" {}}subshader {Tags {"Rend Ertype "=" Opaque "}lod 200cgprogram//physically based standard lighting model, and enable shadows on all light TYPES#PRAGM A surface surf dotaspecular noforwardadd Noshadow//Use Shader Model 3.0 target, to get nicer looking lighting#pragma ta Rget 3.0sampler2d _maintex;sampler2d _normal;sampler2d _mask1;sampler2d _mask2;sampler2d _Diffuse;struct Input {float2 UV_MAINTEX;FLOAT2 uv_normal;float2 uv_mask1;float2 uv_mask2;float2 uv_diffuse;}; struct Mysurfaceoutput {fixed3 albedo;fixed3 normal;fixed3 emission;half specular;fixed gloss;fixed Alpha;float2 myuv;}; Half4 Lightingdotaspecular (mysurfaceoutput s, half3 Lightdir, Half3 viewdir, half atten) {fixed4 tex = tex2d (_maintex, S.M YUV); S.albedo = Tex.rgb;float4 Mask1 = tex2d (_mask1, S.MYUV); Float4 MasK2 = tex2d (_mask2, S.MYUV); float specintensity = mask2.r;float rimintensity = mask2.g;float tint = mask2.b;float shininess = mask2.a * 10;float metalness = mask1.b;float Selfillu = mask1.a;metalness = 1-metalness*0.7;float3 ViewDirection = VI Ewdir; Normalize (Unityworldspaceviewdir (worldpos.xyz)); Float3 lightdirection = Lightdir; Normalize (_WORLDSPACELIGHTPOS0.XYZ); Directional Lightfloat dr = max (0.05, Dot (S.normal, lightdirection)); FLOAT3 diffusereflection = S.albedo * metalness * Lerp (tex2d (_diffuse, FLOAT2 (DR, 0.2)), FLOAT3 (1, 1, 1) * 2, selfillu); FLOAT3 rim = rimintensity * Saturate (1-saturate (do T (S.normal, viewdirection)) *1.8) FLOAT3 specularreflection = specintensity * metalness * 4 * LERP (_LIGHTCOLOR0.RGB, _Ligh TCOLOR0.W * S.albedo, tint) * POW (max (0.0, dot (reflect (-lightdirection, s.normal), viewdirection)), shininess*metalness ); return Half4 ((diffusereflection + rim + specularreflection) * atten, 1);} void Surf (Input in, InOut mysurfaceoutput o) {O.MYUV = In.uv_mainTex;o. Normal = Unpacknormal (tex2d (_normal, In.uv_maintex));} Endcg}fallback "Diffuse"}

Reference

Phone VS Blinn Model: Http://tech-artists.org/wiki/Shading_models

The materials involved in this article (including Unity files and PDFs, need to be opened with Unity5): http://download.csdn.net/detail/stalendp/9469097

= = Specular ======
1) Vertex and Fragment Shader examples:http://docs.unity3d.com/manual/sl-vertexfragmentshaderexamples.html
2) Diffuse reflection:https://en.wikibooks.org/wiki/cg_programming/unity/diffuse_reflection
3) Smooth Specular highlights:https://en.wikibooks.org/wiki/cg_programming/unity/smooth_specular_highlights
4) Specular Highlights:https://en.wikibooks.org/wiki/cg_programming/unity/specular_highlights
5) Silhouette Enhancement:https://en.wikibooks.org/wiki/cg_programming/unity/silhouette_enhancement
6) Applying Matrix transformations:https://en.wikibooks.org/wiki/cg_programming/applying_matrix_transformations
====== Rim ======
7) Rim light:http://in2gpu.com/2014/07/02/rim-lighting/
8) Bump Spec Rim:http://wiki.unity3d.com/index.php?title=bumpspecrim
9) OpenGL Rim Shader:http://www.roxlu.com/2014/037/opengl-rim-shader
) Velvet:http://wiki.unity3d.com/index.php/velvet
======normal mapping======
One) Lighting of bumpy surfaces:https://en.wikibooks.org/wiki/cg_programming/unity/lighting_of_bumpy_surfaces
Lightmode is very important: http://docs.unity3d.com/432/Documentation/Components/ Rendertech-forwardrendering.html (Please learn more about Unity's lighting model!!) )
3D Texture Technology: http://docs.unity3d.com/Manual/class-Texture3D.html
Http://docs.unity3d.com/Manual/script-ColorCorrectionLookup.html
Half-lambert:https://developer.valvesoftware.com/wiki/half_lambert)
15) refer to Unity Shaders and Effects Cookbook Chapter 2 Packing and blending textures
() Light Attenuation:https://en.wikibooks.org/wiki/glsl_programming/unity/light_attenuation
Http://ws.cis.sojo-u.ac.jp/~izumi/Unity_Documentation_jp/Documentation/Components/SL-Attenuation.html
http://blog.csdn.net/candycat1992/article/details/41605257
http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/


"OpenGL" Dota2 Shader Analysis (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.