"Unity" is programmed with shader for 3D Hearts

Source: Internet
Author: User
Tags pow

Some shapes, even if no 3D art designers provide models, can be generated in code.

This is a very important way for independent developers who want to remain original and do not want to borrow a model from others.

Today is dedicated to you with shader programming to achieve a heart, pinned down a faint feeling of homesickness.


Post-starter Address:

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


Finally:


Development environment: Unity 4.5.5


First step: Create a standard sphere at the origin point

Gameobject, Create Other, Sphere

(Note: The size is the default)


Step Two: Create the Heartshader.shader file in the shader directory

Source:

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 (def Ault) #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:P Osition; 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.verte X.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-_Lig HTCOLOR.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 Three: Add your own defined material to the sphere

Create a material. Choose Custom-and heart to define your own shader and add the material to the sphere created in the first step


Summary:

The focus of this article is to implement the Hearts shape, the core code in the vertex shader section.

This shader shader color rendering processing is relatively simple, can be based on the actual need for intact color rendering.

Another point to note is that. The model generated by the shader programming is only visually altered, and the collision Detection (Collider) is still the original (spherical type).


"Unity" is programmed with shader for 3D Hearts

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.