• Stars
    star
    445
  • Rank 98,085 (Top 2 %)
  • Language ShaderLab
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Miscellaneous Shaders in Unity, WIP(maybe?)

Unity-Miscellaneous-Shaders

一个管理小shader的仓库,慢慢添加。

Update 2018/1/1 GlassDragon

Description:

run

Update 2017/12/3:Explosion and sand effect

Description:

run and click the model

Blog:

Using the geometry shader to achieve model explosion effect

Update 2017/11/23:更新一个斯坦福兔子生成皮毛的demo

Description:

Using the Geometry Shader to generate fur on GPU.

Update 2017/11/23:更新一个雪地印痕的效果小demo。

小随笔:写一个基于几何生成方法的描边效果

0x00 前言

进入金秋九月之后,周末参加的社区活动反而多了起来。因此不像之前一样有富余的时间来写一些长文了,在考虑写点什么的时候突然想到了上一篇文章《利用GPU实现翻页效果》中利用shader实现了一个有趣的翻书的效果。那么这篇文章不妨也来效仿一下写一个shader来实现某种效果,只不过篇幅上可能更短、效果更简单,当然写作的时间也更碎片化了,所以《小随笔》似乎是一个不错的标题。

0x01 先来点理论知识

本文要实现的内容是一个很常见的描边效果。 (本文的模型来自:RTS Mini Legion Lich) 实现的思路来自《Real Time Rendering》的相关章节,即基于几何生成方法的描边。相关的理论内容已经有不少文章都提到过,这里简单概况一下就是在绘制模型时用两个pass,第一遍正常绘制模型;第二遍绘制则要将模型正面剔除——正面剔除的原因在下面的演示中我会告诉各位原因——接着在vs中修改顶点位置,将顶点沿着法线方向膨胀一定距离,然后在fs中将模型用纯色输出即可。

(图片来自:【翻译】西川善司「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,前篇(2))

0x02 再来点实际操作

好了,现在就让我们来实现这个效果吧。 首先我们显然总共需要两个pass,但是我们先实现一个pass,将模型正常的绘制出来。

	// 第一个pass用来渲染正常的模型
	Pass
	{
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		
		#include "UnityCG.cginc"

		struct appdata
		{
			float4 vertex : POSITION;
			float2 uv : TEXCOORD0;
		};

		struct v2f
		{
			float2 uv : TEXCOORD0;
			float4 vertex : SV_POSITION;
		};

		sampler2D _MainTex;
		float4 _MainTex_ST;
		
		v2f vert (appdata v)
		{
			v2f o;
			o.vertex = UnityObjectToClipPos(v.vertex);
			o.uv = TRANSFORM_TEX(v.uv, _MainTex);
			return o;
		}
		
		fixed4 frag (v2f i) : SV_Target
		{
			fixed4 col = tex2D(_MainTex, i.uv);
			return col;
		}
		ENDCG

经过这次绘制,屏幕上出现了正常的模型。

QQ截图20170913221207.png

OK,下面第二个pass要来了。 由于这次我们需要使用法线信息,所以我们可以直接使用Unity内建的appdata_base作为vs的输入,它包含了顶点的法线信息。而由于这次vs和fs之间并没有数据的传递,因此vs只需要输出位置到SV_POSITION,而fs只需要输出纯色到SV_Target即可。

		float4 vert(appdata_base v) : SV_POSITION
		{
			...
		}

		fixed4 frag() : SV_Target {
			return _OutlineColor;
		}

除此之外,在vs中我们不能直接使用在model空间的法线信息,因此还要将顶点的法线信息从model空间转换到clip空间。

			float3 normal = mul((float3x3) UNITY_MATRIX_MVP, v.normal);

然后将顶点沿着法线方向膨胀一定距离:

			pos.xy += _OutlineFactor * normal.xy;

嗯。现在的效果有点赞了。 2b.gif

最后再来看看为什么要打开正面剔除,如果没有正面剔除我们将看到的是一个颜色错误的模型。 就像下面这样: QQ截图20170913224809.png

好了,到此一个常见而又简单的效果就实现了。 祝各位早安~

More Repositories

1

Render-Crowd-Of-Animated-Characters

Animation Baker and Instancing for Animated Characters: Using GPU to implement large-amount animation characters rendering. The animation map for vertex shader to modify the vertex position of the mesh at runtime. Using GPU instancing to reduce draw calls.
C#
1,301
star
2

Realistic-Real-Time-Grass-Rendering-With-Unity

Realistic Real Time Grass Rendering With Unity. Using the Geometry Shader to generate millions of grass on GPU. ref:Edward Lee
C#
572
star
3

UnitySpecialEffectWithDepth

Using depth data to achieve scanning effects, x-ray effects, and shield energy field effects. Version Unity5.6 +
ShaderLab
449
star
4

Unity-Boids-Behavior-on-GPGPU

Boids behavior calculation code is modified to utilize Microsoft Direct Compute resulting significant improvements in Performance. 利用Compute Shader实现Boids模拟。
C#
229
star
5

Unity-Signed-Distance-Field-Shadow

Signed Distance Field Shadow in Unity
GLSL
122
star
6

Unity-ARFoundation-HandDetection

Hand Detection and Tracking with Unity ARFoundation and CoreML.
C#
113
star
7

Unity-Flip-Book-With-Shader

Unity Flip Book With Shader.利用shader实现翻页的效果。
ShaderLab
108
star
8

UsefulWeb

一些有用的网站收藏。
107
star
9

LightProbeEditor

Unity LightProbe Editor
C#
68
star
10

Stanford-Bunny-Fur-With-Unity

Using the Geometry Shader to generate fur on GPU.
ShaderLab
40
star
11

Unity3D_XMLToEgg

基于C#编写的Unity3D工具,利用反射和泛型通过读取XML文件创建新的实例并且为实例赋值。原型,具体内容需要具体配置。
C#
23
star
12

Unity_UI_Gamma

A project that solves the the UI rendering issue in the linear space.
ShaderLab
21
star
13

TTBT-Framework

可供Unity3D使用的行为树框架。TTBT is a C# framework for easily building and running behavior trees. Here, "behavior tree" refers to the technique used to control the behaviour of characters in games.
C#
19
star
14

TTUI-Framework

基于注册分发机制的Unity3D的UI消息框架,目的是便于管理消息以及和MonoBehaviour进行解耦。
C#
18
star
15

azure-openai-gpt4-voice-chatbot

Python
14
star
16

Unity3D_EggToolkit

基于C#的Unity3D小工具箱,namespace为EggToolkit.
C#
10
star
17

AzureFunction.UserCRUD

A sample CRUD project demonstrates the process of Azure Function using EntityFramework Core to operate the Azure SQL database.
C#
10
star
18

SBP_AssetBundles_Demo

Using SBP to build Assetbundles.
C#
7
star
19

VSCode-StandardHeader

VSCode-StandardHeader
TypeScript
6
star
20

Unity.DetectPneumoniaWithAzure

The world has changed due to the COVID-19 pandemic in 2020. Medical staff around the world have shown bravery and resilience in the fight against COVID-19, and some even sacrificed their lives while performing their duties. As an ordinary person, as a programmer, I also want to help. Therefore, using AI to try to help diagnose lung diseases, such as viral pneumonia, bacterial pneumonia, is an idea that I am interested in trying.
ShaderLab
5
star
21

Algorithms

一些基本算法的实现。慢慢更新中。
C#
4
star
22

pyuml

A toy project that generates UML diagrams of Python code by parsing the abstract syntax tree of the target Python code.
Python
4
star
23

Unity-Hello-Swift

This project demonstrates how to create a Swift plugin for Unity and 'automatically' calls some callback such as OnUnityPluginLoad etc.
C++
4
star
24

Awesome-Azure-Skilling

This repository is a collection of free resources and tips for those working with or learning about Azure.
3
star
25

iOS-Hybrid-Sample

iOS 9 Hybrid Sample
JavaScript
3
star
26

Unity-Test-Framework-with-UITest

Add UI Test in the Test Framework
C#
2
star
27

FanyoyBird

Flappy Bird written in cocos2d-js-v3.0-beta
C
2
star
28

CodeStripExample

C#
2
star
29

chenjd

1
star
30

AugmentedReality_Whale_Flying

C#
1
star
31

Unity3D_EggArray

Sort-of like underscore.js, but for CSharp
C#
1
star
32

PerformanceTester

Unity相关操作的性能测试
C#
1
star
33

Microsoft-Azure-Cloud-CheatSheets

The repo that stores the cheat sheets I made about Azure services.
1
star