Skip to content

Commit

Permalink
Chapter 14
Browse files Browse the repository at this point in the history
  • Loading branch information
MUDV587 committed Oct 29, 2018
1 parent d41de01 commit 9fdce9d
Show file tree
Hide file tree
Showing 447 changed files with 30,236 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Assets/Materials/Chapter14/ToonShading.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: ToonShading
m_Shader: {fileID: 4800000, guid: c7003a2e6a2f78542a4f7afbb06ffd7c, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _Ramp:
m_Texture: {fileID: 2800000, guid: f628a9d765d21417995f27cbf8e44c2c, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Outline: 0.04
- _Parallax: 0.02
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SpecularScale: 0.018
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0.9411765, g: 0.8235294, b: 0.63529414, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _OutlineColor: {r: 0, g: 0, b: 0, a: 1}
- _Specular: {r: 1, g: 1, b: 1, a: 1}
8 changes: 8 additions & 0 deletions Assets/Materials/Chapter14/ToonShading.mat.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/PostProcessing.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/PostProcessing/Editor Resources.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/PostProcessing/Editor Resources/Monitors.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "UnityCG.cginc"

RWStructuredBuffer<uint4> _Histogram;
Texture2D<float4> _Source;

CBUFFER_START (Params)
uint _IsLinear;
float4 _Res;
uint4 _Channels;
CBUFFER_END

groupshared uint4 gs_histogram[256];

#define GROUP_SIZE 16

#pragma kernel KHistogramGather
[numthreads(GROUP_SIZE, GROUP_SIZE,1)]
void KHistogramGather(uint2 dispatchThreadId : SV_DispatchThreadID, uint2 groupThreadId : SV_GroupThreadID)
{
const uint localThreadId = groupThreadId.y * GROUP_SIZE + groupThreadId.x;

if (localThreadId < 256)
gs_histogram[localThreadId] = uint4(0, 0, 0, 0);

GroupMemoryBarrierWithGroupSync();

if (dispatchThreadId.x < (uint)_Res.x && dispatchThreadId.y < (uint)_Res.y)
{
// We want a gamma histogram (like Photoshop & all)
float3 color = saturate(_Source[dispatchThreadId].xyz);
if (_IsLinear > 0)
color = LinearToGammaSpace(color);

// Convert color & luminance to histogram bin
uint3 idx_c = (uint3)(round(color * 255.0));
uint idx_l = (uint)(round(dot(color.rgb, float3(0.2125, 0.7154, 0.0721)) * 255.0));

// Fill the group shared histogram
if (_Channels.x > 0u) InterlockedAdd(gs_histogram[idx_c.x].x, 1); // Red
if (_Channels.y > 0u) InterlockedAdd(gs_histogram[idx_c.y].y, 1); // Green
if (_Channels.z > 0u) InterlockedAdd(gs_histogram[idx_c.z].z, 1); // Blue
if (_Channels.w > 0u) InterlockedAdd(gs_histogram[idx_l].w, 1); // Luminance
}

GroupMemoryBarrierWithGroupSync();

// Merge
if (localThreadId < 256)
{
uint4 h = gs_histogram[localThreadId];
if (_Channels.x > 0u && h.x > 0) InterlockedAdd(_Histogram[localThreadId].x, h.x); // Red
if (_Channels.y > 0u && h.y > 0) InterlockedAdd(_Histogram[localThreadId].y, h.y); // Green
if (_Channels.z > 0u && h.z > 0) InterlockedAdd(_Histogram[localThreadId].z, h.z); // Blue
if (_Channels.w > 0u && h.w > 0) InterlockedAdd(_Histogram[localThreadId].w, h.w); // Luminance
}
}

// Scaling pass
groupshared uint4 gs_pyramid[256];

#pragma kernel KHistogramScale
[numthreads(16,16,1)]
void KHistogramScale(uint2 groupThreadId : SV_GroupThreadID)
{
const uint localThreadId = groupThreadId.y * 16 + groupThreadId.x;
gs_pyramid[localThreadId] = _Histogram[localThreadId];

GroupMemoryBarrierWithGroupSync();

// Parallel reduction to find the max value
UNITY_UNROLL
for(uint i = 256 >> 1; i > 0; i >>= 1)
{
if(localThreadId < i)
gs_pyramid[localThreadId] = max(gs_pyramid[localThreadId], gs_pyramid[localThreadId + i]);

GroupMemoryBarrierWithGroupSync();
}

// Actual scaling
float4 factor = _Res.y / (float4)gs_pyramid[0];
_Histogram[localThreadId] = (uint4)round(_Histogram[localThreadId] * factor);
}

#pragma kernel KHistogramClear
[numthreads(GROUP_SIZE, GROUP_SIZE, 1)]
void KHistogramClear(uint2 dispatchThreadId : SV_DispatchThreadID)
{
if (dispatchThreadId.x < (uint)_Res.x && dispatchThreadId.y < (uint)_Res.y)
_Histogram[dispatchThreadId.y * _Res.x + dispatchThreadId.x] = uint4(0u, 0u, 0u, 0u);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

141 changes: 141 additions & 0 deletions Assets/PostProcessing/Editor Resources/Monitors/HistogramRender.shader
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
Shader "Hidden/Post FX/Monitors/Histogram Render"
{
SubShader
{
ZTest Always Cull Off ZWrite Off
Fog { Mode off }

CGINCLUDE

#pragma fragmentoption ARB_precision_hint_fastest
#pragma target 5.0
#include "UnityCG.cginc"

StructuredBuffer<uint4> _Histogram;
float2 _Size;
uint _Channel;
float4 _ColorR;
float4 _ColorG;
float4 _ColorB;
float4 _ColorL;

float4 FragSingleChannel(v2f_img i) : SV_Target
{
const float4 COLORS[4] = { _ColorR, _ColorG, _ColorB, _ColorL };

float remapI = i.uv.x * 255.0;
uint index = floor(remapI);
float delta = frac(remapI);
float v1 = _Histogram[index][_Channel];
float v2 = _Histogram[min(index + 1, 255)][_Channel];
float h = v1 * (1.0 - delta) + v2 * delta;
uint y = (uint)round(i.uv.y * _Size.y);

float4 color = float4(0.1, 0.1, 0.1, 1.0);
float fill = step(y, h);
color = lerp(color, COLORS[_Channel], fill);
return color;
}

float4 FragRgbMerged(v2f_img i) : SV_Target
{
const float4 COLORS[3] = { _ColorR, _ColorG, _ColorB };

float4 targetColor = float4(0.1, 0.1, 0.1, 1.0);
float4 emptyColor = float4(0.0, 0.0, 0.0, 1.0);

float remapI = i.uv.x * 255.0;
uint index = floor(remapI);
float delta = frac(remapI);

for (int j = 0; j < 3; j++)
{
float v1 = _Histogram[index][j];
float v2 = _Histogram[min(index + 1, 255)][j];
float h = v1 * (1.0 - delta) + v2 * delta;
uint y = (uint)round(i.uv.y * _Size.y);
float fill = step(y, h);
float4 color = lerp(emptyColor, COLORS[j], fill);
targetColor += color;
}

return saturate(targetColor);
}

float4 FragRgbSplitted(v2f_img i) : SV_Target
{
const float4 COLORS[3] = {_ColorR, _ColorG, _ColorB};

const float limitB = round(_Size.y / 3.0);
const float limitG = limitB * 2;

float4 color = float4(0.1, 0.1, 0.1, 1.0);
uint channel;
float offset;

if (i.pos.y < limitB)
{
channel = 2;
offset = 0.0;
}
else if (i.pos.y < limitG)
{
channel = 1;
offset = limitB;
}
else
{
channel = 0;
offset = limitG;
}

float remapI = i.uv.x * 255.0;
uint index = floor(remapI);
float delta = frac(remapI);
float v1 = offset + _Histogram[index][channel] / 3.0;
float v2 = offset + _Histogram[min(index + 1, 255)][channel] / 3.0;
float h = v1 * (1.0 - delta) + v2 * delta;
uint y = (uint)round(i.uv.y * _Size.y);

float fill = step(y, h);
color = lerp(color, COLORS[channel], fill);
return color;
}

ENDCG

// (0) Channel
Pass
{
CGPROGRAM

#pragma vertex vert_img
#pragma fragment FragSingleChannel

ENDCG
}

// (1) RGB merged
Pass
{
CGPROGRAM

#pragma vertex vert_img
#pragma fragment FragRgbMerged

ENDCG
}

// (2) RGB splitted
Pass
{
CGPROGRAM

#pragma vertex vert_img
#pragma fragment FragRgbSplitted

ENDCG
}
}
FallBack off
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9fdce9d

Please sign in to comment.