Skip to content

Instantly share code, notes, and snippets.

View olegknyazev's full-sized avatar

Oleg Knyazev olegknyazev

View GitHub Profile
@olegknyazev
olegknyazev / GenerateCircleMesh.cs
Last active October 9, 2022 02:31
Generation of a circle mesh (Unity)
private const int CircleSegmentCount = 64;
private const int CircleVertexCount = CircleSegmentCount + 2;
private const int CircleIndexCount = CircleSegmentCount * 3;
private static Mesh GenerateCircleMesh()
{
var circle = new Mesh();
var vertices = new List<Vector3>(CircleVertexCount);
var indices = new int[CircleIndexCount];
var segmentWidth = Mathf.PI * 2f / CircleSegmentCount;
@olegknyazev
olegknyazev / FogOfWar.cginc
Created April 23, 2017 18:48
Simple implementation of a fog of war shader (Unity)
#ifndef FOG_OF_WAR_INCLUDED
#define FOG_OF_WAR_INCLUDED
#ifdef FOG_OF_WAR
# define FOG_OF_WAR_INPUT float3 worldPos;
# define FOG_OF_WAR_SAMPLE(in_) FogOfWar_Sample(in_.worldPos)
#else
# define FOG_OF_WAR_INPUT
# define FOG_OF_WAR_SAMPLE(in_) (1.0f)
#endif
@olegknyazev
olegknyazev / ShaderSample.shader
Last active April 30, 2017 12:53
Example of a shader that uses FogOfWar.cginc (Unity)
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
#pragma multi_compile __ FOG_OF_WAR // <<< 1
#include "FogOfWar.cginc" // <<< 2
sampler2D _MainTex;
@olegknyazev
olegknyazev / TerrainSplatmap.cginc
Last active April 23, 2017 19:13
Version of Unity's TerrainSplatmap.cginc with fog of war support (Unity)
#ifndef TERRAIN_SPLATMAP_INCLUDED
#define TERRAIN_SPLATMAP_INCLUDED
// Copied from Unity's TerrainSplatmapCommon.cginc
#include "../FogOfWar.cginc" // <<<
struct Input
{
float2 uv_Splat0 : TEXCOORD0;
public class FogOfWarRevealable : MonoBehaviour
{
private List<FogOfWarRevealer> m_NearRevealers = new List<FogOfWarRevealer>();
private bool m_Revealed = true;
private float m_NextFullUpdateTime;
...
private void Start()
{
public override bool Reveals(Vector3 position, float radius)
{
var localPosition = m_Rectangle.transform.InverseTransformPoint(position);
localPosition = Vector3.Scale(localPosition, m_Rectangle.transform.localScale);
return localPosition.x >= -m_Extent.x - radius
&& localPosition.x <= m_Extent.x + radius
&& localPosition.z >= -m_Extent.y - radius
&& localPosition.z <= m_Extent.y + radius;
}
inline fixed FogOfWar_Sample(float3 worldPos) {
#ifdef FOG_OF_WAR
float2 uv = worldPos.xz / _MapSize.xy + 0.5f;
fixed fow = tex2D(_FogOfWarTex, uv).r * 0.75f + 0.25f;
#else
fixed fow = 1;
#endif
float SMOOTH_LENGTH = 5.0f; // <<<
float2 dist = min((_MapSize.zw - worldPos.xz), (_MapSize.zw + worldPos.xz)); // <<<
float2 ratio = dist / SMOOTH_LENGTH; // <<<
inline fixed FogOfWar_Sample(float3 worldPos) {
float2 worldPosNorm = worldPos.xz / _MapSize.xy;
#ifdef FOG_OF_WAR
float2 fogOfWarUV = worldPosNorm + 0.5f;
fixed fogOfWar = tex2D(_FogOfWarTex, fogOfWarUV).r * 0.75f + 0.25f;
#else
fixed fogOfWar = 1;
#endif
#ifdef MAP_BORDERS
float noiseU = abs(worldPosNorm.x / worldPosNorm.y) > 1 ? worldPosNorm.y : worldPosNorm.x;
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
static void IndexedGraphicsBlit(RenderTexture source, RenderTexture dest, Material material)
{
RenderTexture.active = dest;
material.SetTexture("_MainTex", source);
GL.PushMatrix();
GL.LoadOrtho();
material.SetPass(0);