Few words said shader (ix) diamond

Source: Internet
Author: User
Tags mul

This time the goal is to draw a sparkling diamond, the pursuit of the effect is closer to the real better.


Let's talk about what I've done these days.

1. Read the STALENDP blog in the "Diamond effect" in the back of the reference document

The most valuable is ATI's speech in the 2004 GDC, titled Drawing a Diamond. However, because only PPT, it is difficult to reproduce the project in-depth study. The idea was to prepare a refractive cubemap, a reflective cubemap with simulated dispersion, and finally a flare mix.


2. Some simple specular reflections have been tested

Non-real-time cubemap reflection The simplest nothing to say, the need to pay attention to the precise location of specular reflection or is difficult, accidentally will find that when the object is close to the reflective surface, the size of the position will be seriously wrong.

Like this, the ball's mirror size is obviously too large.



Real-time access to the environment CubeMap reflection also tested under, remember the words cubemap facesize set is 512, in my one plus mobile phone basic on the normal frame, facesize set to 256 frame rate is acceptable, but the picture quality can not be seen, In short, real-time reflection is not affordable to mobile phones.


Here I summarize a little thought:

The game screen performance itself with a lot of false ingredients, because all according to the real to calculate the performance can not withstand, the simplest to take the light, do not talk about the environmental light of multiple reflections, a simple parallel lighting real-time calculation can not meet, all to pre-baked light map, shadow is the same, the real calculation too much cost performance, I usually get a fake.

So we will find that the gap between the game screen and the real picture is always at a glance. So there will be a lot of special cover-up techniques.

How to grab the main part, make a trade-off between effects and performance, and try to achieve near-real results, is probably what graphics engineers are thinking about most of the time.


This demo of the diamond effect, obviously to the idea of a fixed environment, do not need to consider real-time problems, such as mentioned in the size of the distortion of the problem, the basic can be ignored, because the diamond shape like a small ball, the slight distortion of the environment can be easily disguised in the past.


3. Went to Assetstore to search the next related resources

Discover the free gem shaders is officially provided by unity and has been specifically updated for 5.0. I ran down its example scene, feeling not very satisfied with the impression.

Mainly it provides a few Diamond model shape I do not like, so I went online to find a presumably is the legend of the "eight-star Eight arrows" shape model, and then still use this shader tried the effect, the results found still very dick.



My original idea was to try to get the final result with real light refraction and reflection calculations. However, even if the implementation of the code is not considered, regardless of the performance after implementation, first of all, it is very obvious that I can establish a real and accurate optical model with my limited optical posture at present.

So this time we still have to learn the official example of the main, do not want to make big news all day.


The code is as follows:

Shader "Fx/gem" {Properties {_color ("color", color) = (1,1,1,1) _reflectionstrength ("Reflection Strength", Range ( 0.0,2.0)) = 1.0_environmentlight ("Environment Light", range (0.0,2.0)) = 1.0_emission ("Emission", Range (0.0,2.0)) = 0.0[ Noscaleoffset] _refracttex ("Refraction Texture", Cube) = "" {}}subshader {Tags {"Queue" = "Transparent"}//first pass-h Ere we render the backfaces of the Diamonds. Since Those diamonds is more-or-less//convex objects, this is effectively rendering the inside of them. Pass {Cull frontzwrite offcgprogram#pragma vertex vert#pragma fragment frag#include "Unitycg.cginc" struct v2f {Flo AT4 pos:sv_position;float3 uv:texcoord0;}; v2f Vert (float4 v:position, Float3 n:normal) {v2f O;o.pos = Mul (UNITY_MATRIX_MVP, v);//Texgen cubereflect://reflect View direction along the normal, in view space.float3 Viewdir = Normalize (Objspaceviewdir (v)); o.uv =-reflect (Viewdir, N); O.UV = Mul (_object2world, FLOAT4 (o.uv,0)); return o;} Fixed4 _color;samplercube _refRacttex;half _environmentlight;half _emission;half4 Frag (v2f i): sv_target{half3 refraction = TexCUBE (_RefractTex, I.UV ). RGB * _color.rgb;half4 reflection = Unity_sample_texcube (UNITY_SPECCUBE0, i.uv); Reflection.rgb = Decodehdr ( reflection, UNITY_SPECCUBE0_HDR); Half3 multiplier = Reflection.rgb * _environmentlight + _emission;return Half4 ( Refraction.rgb * Multiplier.rgb, 1.0f);} ENDCG}//Second Pass-here We render the front faces of the diamonds. Pass {zwrite onblend one onecgprogram#pragma vertex vert#pragma fragment frag#include "Unitycg.cginc" struct v2f {f LOAT4 pos:sv_position;float3 uv:texcoord0;half fresnel:texcoord1;}; v2f Vert (float4 v:position, Float3 n:normal) {v2f O;o.pos = Mul (UNITY_MATRIX_MVP, v);//Texgen cubereflect://reflect View direction along the normal, in view space.float3 Viewdir = Normalize (Objspaceviewdir (v)); o.uv =-reflect (Viewdir, N); O.UV = Mul (_object2world, FLOAT4 (o.uv,0)); O.fresnel = 1.0-saturate (dot (n,viewdir)); return o;} Fixed4 _coloR;samplercube _refracttex;half _reflectionstrength;half _environmentlight;half _Emission;half4 frag (v2f i): SV_Target {Half3 refraction = Texcube (_refracttex, I.UV). RGB * _color.rgb;half4 reflection = Unity_sample_texcube (UNITY_SPECCUBE0 , i.uv); Reflection.rgb = Decodehdr (reflection, UNITY_SPECCUBE0_HDR); Half3 Reflection2 = Reflection * _ Reflectionstrength * I.fresnel;half3 multiplier = Reflection.rgb * _environmentlight + _emission;return fixed4 ( Reflection2 + Refraction.rgb * multiplier, 1.0f);} endcg}//Shadow Casting & Depth Texture support--so that gems can//cast shadows Usepass "vertexlit/s Hadowcaster "}}

Let me analyze the pose points:

The personal level is limited many of the statements may be wrong, it is stated here first.

1. The simulation of refraction light

First of all, its refractive light is not really calculated, but instead gives such a cubemap diagram, which is then actually calculated by reflection.


2.pass Design

In roughly two steps, the first pass is to draw the back of the diamond, and the second, of course, is ahead. Note the blend one in the second pass, which represents two layer 1:1 blends.

The design is reasonable, considering that the diamond is a transparent object.


3. Lighting Model Design

Refraction light, reflected light, ambient light, spontaneous light are all kinds of superposition of various multiplication. Anyway, because the white is 255, black is 0, here Although the calculation is light, but the actual processing or color, whether it is added or multiplied, are more stacked brighter, in line with the Basic Law of light superposition. I certainly don't know whether the formula is derived from an accurate optical principle or an approximate simulation.


4.HDR

High Dynamic Range, irresponsible say, presumably is if 0 is black, 1 is white, then some parts of the color can be more than 1, so the final picture results will show a bright glow of the effect, specifically, please refer to the Unity Official document description.


5. Fresnel effect

Simply because the light is fired into the glass, part of it is reflected, and part of it is directly in, causing the final reflected light to appear a more parallel to the incident surface, the stronger the reflection effect, the more vertical the weaker the phenomenon.

This factor is introduced in the second pass calculation.

So at first I said that it was difficult to establish accurate models, such as this subtle but real phenomenon, with only a little superficial refraction reflex posture and not to go deep into the study how to consider it?


This shader used a lot of macros, in the Cgincludes folder in those files can be found, these I can generally understand or guess what is going on, but to really make sure that it will take a lot of effort.

Here please allow pig brother to steal a lazy, casually talk about pretending to understand the appearance, specific calculation details will not be unfolded.


Conclusion:

At this early stage of the shader learning program even completed, because I deliberately to make progress, stole some lazy, than the plan ahead of a week. A penny of the harvest, shader this piece of water is very deep, want to be accomplished must spend more time. But I do not want to continue to put energy in this piece for the sake of overall consideration.

The next stage of vision back to the C # language, the most basic field, continue to repair internal forces. Last year bought the "C # essence" to the end, has not turned the "in-depth understanding of C #" also to read.

Talking about language inevitably involves design patterns, but also avoids them as much as possible.

The cycle is set at five weeks.

Few words said shader (ix) diamond

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.