The Shader programming is used in Unity to realize the 3D red heart and unityshader3d red heart.

Source: Internet
Author: User

The Shader programming is used in Unity to realize the 3D red heart and unityshader3d red heart.

Some shapes can be generated using code even if no 3D artist provides a model.

For independent developers who want to maintain originality and do not want to borrow others' models, this is nothing more than an important way.

Today, we are dedicated to the use of Shader programming to achieve a red heart, with a faint sense of nostalgia.


Blog Post address:

Http://blog.csdn.net/duzixi/article/details/41221647


Finally:


Development Environment: Unity 4.5.5


Step 1: create a standard sphere at the origin

GameObject-> Create Other-> Sphere

(Note: The default size is used)


Step 2: Create the HeartShader. Shader file in the shader folder.

Source code:

Shader "Custom/Heart"{Properties {_LightColor("Light Color", COLOR) = (1,1,1,1)_DarkColor("Dark Color", COLOR) = (1,1,1,1)}SubShader {// vertex// fragmentPass{CGPROGRAM   // -> GPU begin// define a function  type:vertex name:vert(default)#pragma vertex   vert// define another function type:fragment   name:frag(default)#pragma fragment fraguniform half4 _LightColor;uniform half4 _DarkColor;// typedef vertex struct// v2f: vertex to fragmentstruct VertexInput{fixed4 vertex:POSITION; // must have};struct FragmentInput{fixed4 pos:SV_POSITION;// must havefloat4 color:COLOR;};// compute fragment by vertexFragmentInput vert(VertexInput i){FragmentInput o;if (i.vertex.y < 0 && abs(i.vertex.z) <= 0.2 && abs(i.vertex.x) <= 0.2){i.vertex.y -= 0.12 - sqrt(pow(i.vertex.z,2) + pow(i.vertex.x,2)) * 0.65;}if (i.vertex.y > 0 && abs(i.vertex.z) <= 0.5 && abs(i.vertex.x) <= 0.5) {i.vertex.y -= 0.3 - sqrt(pow(i.vertex.z,2) + pow(i.vertex.x,2)) * 0.5;if (abs(i.vertex.z) <= 0.2 && abs(i.vertex.x) < 0.48){i.vertex.y -= 0.2 - sqrt(abs(i.vertex.z)) * 0.45;}}o.pos = mul(UNITY_MATRIX_MVP, i.vertex);o.color = _LightColor;float r = (_DarkColor.r - _LightColor.r) * (1 - i.vertex.y) + _LightColor.r;float g = (_DarkColor.g - _LightColor.g) * (1 - i.vertex.y) + _LightColor.g;float b = (_DarkColor.b - _LightColor.b) * (1 - i.vertex.y) + _LightColor.b;o.color = float4(r,g,b,0.5);return o;}half4 frag(FragmentInput i):COLOR{return i.color;}ENDCG  // -> GPU end}}// get the default shaderFallBack "Diffuse"}

Step 3: add custom materials to the sphere

Create a material, select Custom-> Heart Custom Shader, and add the material to the sphere created in step 1.


Summary:

The focus of this article is to achieve the shape of a red heart. The core code is in the vertex coloring tool section.

The color rendering process of the Shader is relatively simple, and the color rendering can be improved according to actual needs.

Note that the model generated by the Shader programming only changes visually, while the collision detection (Collider) is still the original (ball type ).


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.