• Stars
    star
    1,189
  • Rank 39,340 (Top 0.8 %)
  • Language HLSL
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

Raymarching Shader Generator in Unity

uRaymarching

uRaymarching is a raymarching shader templates using uShaderTemplate. The following functions are implemented:

  • Create a ray-marching object by simply writing a distance function
  • Legacy pipelines (Forward / Deferred) and URP (Forward / Deferred) are supported
    • HDRP is not yet supported
  • Intersects polygonal objects because it writes depth
  • VR support
  • Lit / Unlit (+ Transparent)
  • Shadows for Directional / Spot / Point lights
  • Object-space and full-screen (Full screen only for legacy pipelines)

Screenshots

The following shapes are rendered inside a 12-polygon cube.

Check more examples here:

Install

  • Unity Package
  • Git URL (UPM)
    • Add the following git URLs to Package Manager.
      • https://github.com/hecomi/uShaderTemplate.git#upm
      • https://github.com/hecomi/uRaymarching.git#upm
  • Scoped Registry (UPM)
    • Add a scoped registry to your project.
      • URL: https://registry.npmjs.com
      • Scope: com.hecomi
    • Install uRaymarching in Package Manager.

How to use

  1. Select Create > Shader > uShaderTemplate > Generator in the Project view.
  2. Input Shader Name and select Shader Template from Inspector.
  3. Edit items in Conditions, Variables, Properties, Distance Function, and Post Effect.
  4. Press Export button or Ctrl + R to create shader from the Generator.
  5. Create material in the Project view (or press Create Material button).
  6. Create Cube in the Hierarchy view.
  7. Apply the generated material to the cube.

Please also see uShaderTemplate to learn the detail of shader generation function.

Inspector

The UI is generated by uShaderTemplate automatically from template files located in the Assets/uRaymarching/Editor/Resources/ShaderTemplates.

Shader Templates

  • Forward > Standard
    • The lighting is done by the same method as a standard surface shader in ForwardBase/Add pass.
  • Forward > Unlit
    • No lighting by default so you have to write output colors manually.
  • Deferred > Standard
    • The lighting is done by the same method as a standard surface shader.
  • Deferred > Direct GBuffer
    • Write values directly into GBuffers without effects like GI and LightProbe.
  • UniversalRP > Lit
    • Same lighting as the built-in Universal Render Pipelin/Lit shader.
  • UniversalRP > Unlit
    • No lighting, and same as the built-in Universal Render Pipelin/Unlit shader.

The items in Conditions and Variables are different depending on the selected template. Please see each page for further details:

Properties

This block is inserted into a Property section in a shader.

[Header(Additional Parameters)]
_Grid("Grid", 2D) = "" {}

Distance Function

Write a distance function here. The following code is the one generating the example of morphing sphere in Screenshots section in this document.

inline float DistanceFunction(float3 pos)
{
    float r = abs(sin(2 * PI * _Time.y / 2.0));
    float d1 = RoundBox(Repeat(pos, float3(6, 6, 6)), 1 - r, r);
    float d2 = Sphere(pos, 3.0);
    float d3 = Plane(pos - float3(0, -3, 0), float3(0, 1, 0));
    return SmoothMin(SmoothMin(d1, d2, 1.0), d3, 1.0);
}

Math.cginc and Primitives.cginc are included in the generated shader, so in this example some functions like RoundBox() and Repeat() come from these include files (of cource you can write them by yourself).

Post Effect

Post Effect is similar to a surface function in a surface shader. The following code is used in the hexagon-tile example in Screenshots section.

float4 _TopColor;

inline void PostEffect(RaymarchInfo ray, inout PostEffectOutput o)
{
    float3 localPos = ToLocal(ray.endPos);
    o.Emission += smoothstep(0.48, 0.50, localPos.y) * _TopColor;
    o.Occlusion *= 1.0 - 1.0 * ray.loop / ray.maxLoop;
}

RaymarchInfo is the input and the output of a raymarching calculation and this is defined in Struct.cginc.

struct RaymarchInfo
{
    // Input
    float3 startPos;    // start position of ray
    float3 rayDir;      // ray direction
    float3 projPos;     // ComputeScreenPos-applied position
    float3 polyNormal;  // normal on polygon surface
    float minDistance;  // minimum ray distance (comes from material setting)
    float maxDistance;  // maximum ray distance (changes by the raymarching setting)
    int maxLoop;        // maximum number of loop (comes from material setting)

    // Output
    int loop;           // total number of loop of the calculation (<= maxLoop)
    float3 endPos;      // last position (= surface of the distance function)
    float lastDistance; // the final distance of the raymarching
    float totalLength;  // total ray length
    float depth;        // depth (encoded)
    float3 normal;      // normal (encoded)
};

So ray.loop / ray.maxLoop is a normalized value and becomes close to 0.0 on the position where a ray reaches easily and becomes close to 1.0 when hard. So you can use it as a factor of a rechability or 1.0 - ray.loop / ray.maxLoop as an simple and a light-weight occlusion factor.

PostEffectOutput is defferent depending on the selected shader template. For example, it is an alias of SurfaceOutputStandard in Standard template. Please see the following pages for more details.

Please see each template file by clicking Edit button on the right side of the Shader Template drop-down list for more details.

Export

Press Export button or Ctrl + R to export shader. Then, press Create Material button to generate a material which uses the shader (or create a material manually from the Project pane).

Material

  • Loop
    • The maximum number of loops of raymarching in basic passes.
  • Minimum Distance
    • If the distance returned by a distance function becomes lower than this value, the loop finishes.
  • Distance Multiplier
    • This value is multiplied by the output of a distance function.
  • Shadow Loop
    • The maximum number of loops of raymarching in shadow pass.
  • Shadow Minimum Distance
    • Minimum Distance in shadow pass.
  • Shadow Extra Bias
    • Additional shadow bias to avoid shadow acne.

More Repositories

1

UnityFurURP

Fur shader implementation for URP
HLSL
659
star
2

uLipSync

MFCC-based LipSync plug-in for Unity using Job System and Burst Compiler
C#
556
star
3

uREPL

In-game powerful REPL environment for Unity3D.
C#
533
star
4

uDesktopDuplication

Desktop Duplication API implementation for Unity (only for Windows 8/10)
C++
509
star
5

uWindowCapture

This allows you to use Windows Graphics Capture / PrintWindow / BitBlt in Windows to capture multiple windows individually and easily use them as Texture2D in Unity.
C++
469
star
6

UnityScreenSpaceBoolean

Screen Space Boolean Implementation for Unity.
ShaderLab
244
star
7

UnityWaterSurface

Water Surface Simulation using CutomRenderTexture in Unity 2017.1
ShaderLab
236
star
8

UnityVolumeRendering

A simple example of Volume Rendering for Unity.
GLSL
159
star
9

uRaymarchingExamples

Examples using uRaymarching (https://github.com/hecomi/uRaymarching)
C#
141
star
10

MMD4Mecanim-LipSync-Plugin

LipSync and TTS Plugin for MMD4Mecanim
C#
140
star
11

uShaderTemplate

This is an Unity editor extension for generating shader code from template files.
C#
133
star
12

UnityPseudoInstancedGPUParticles

GPU Particles w/ Screen Space Collision Example.
GLSL
109
star
13

UnityECSBoidsSimulation

Simple Boids simulation example using Unity ECS.
C#
108
star
14

uWintab

Wintab API plugin for Unity
C++
99
star
15

node-mecab-async

Asynchronous japanese morphological analyser using MeCab.
JavaScript
94
star
16

uChromaKey

Chroma key shader asset for Unity
ShaderLab
91
star
17

uDllExporter

Tool to build DLLs in Unity.
C#
80
star
18

VrGrabber

VR Grabber components for Unity
C#
79
star
19

node-julius

Node.js module for voice recognition using Julius
C
72
star
20

UnityRemoteDesktopDuplication

An Unity example to send a desktop image to a remote PC using Desktop Duplication API and NVENC/NVDEC.
C#
64
star
21

uHomography

Homography Transformation Image Effect for Unity.
C#
64
star
22

HLSLToolsForVisualStudioConfigGenerator

Create shadertoolsconfig.json for Unity project
C#
60
star
23

uCurvedScreen

Curved-sreen shader aseet for Unity.
ShaderLab
57
star
24

UnityRaymarchingCollision

Raymarching x Rigidbody interaction example.
GLSL
54
star
25

NodejsUnity

Run Node.js automatically when Unity application starts, and they communicate with each other.
C#
51
star
26

UWO

Unity WebGL x WebSocket MMO demo
C#
49
star
27

node-openjtalk

Node.js TTS module using OpenJTalk
C++
49
star
28

Unity-WebGL-3D-Chat

Unity5 WebGL x Socket.IO = Online 3D Chat on the web browser!
JavaScript
44
star
29

UnityScreenSpaceReflection

Simple Screen Space Reflection Implementation.
GLSL
43
star
30

UnityDICOMVolumeRendering

This is a simple project for volume rendering of DICOM data in Unity.
C#
43
star
31

UnityRaymarchingSample

Unity3D Raymarching Simple Implementation.
GLSL
41
star
32

uNvEncoder

A simple wrapper of NVENC (NVIDIA's HW-accelerated encoder) for Unity.
C
41
star
33

uTouchInjection

Windows Multitouch Injection Plugin for Unity.
C#
38
star
34

UnityNativeTextureLoader

This is an example to load textures from StreaminAssets directory or web in a native plugin.
C++
36
star
35

tsubakumi2

Home Automation System for @hecomi room
JavaScript
36
star
36

HoloLensPlayground

My playground for HoloLens.
C#
31
star
37

uNvPipe

A simple NvPipe wrapper for Unity
C
25
star
38

UnityTimelineExample

A simple example of creating custom Track and Clip
C#
22
star
39

StereoAR-for-Unity

Oculus Rift にて 2 つの Playstation Eye を通じて現実世界を立体視し、更にそこへ AR を重畳するプロジェクトです。
C#
20
star
40

LITTAI

Interaction recognizer for LITTAI project.
C++
20
star
41

LITTAI-game

Unity project for the table-top projection game, LITTAI.
C#
20
star
42

unity-android-aruco-sample

Unity x Android x OpenCV x ArUco sample project.
C++
20
star
43

UnityCustomTextureUpdate

An example to load texture using CustomTextureUpdate().
C
19
star
44

node-wemo

Belkin 社の WeMo を操作する Node モジュールです
JavaScript
19
star
45

UnityRaymarchingForward

Example of raymarching in forward rendering for Unity
HLSL
17
star
46

UnityKinectV2DeferredRendering

Unity sample project to draw KinectV2 depth sensor values into G-Buffer directly.
C#
17
star
47

QwertyVR

An Unity sample project for my Mixed Reality-based concept, Visual IO.
C#
17
star
48

uNetVoice

Tiny voice chat implementation for Unity.
C#
16
star
49

CyalumeLive

Unity でシェーダを使って 20,000 人がサイリウムを音楽に合わせて振るデモです。
C
14
star
50

UnityWebGLAudioStream

A simple sample that uses the Web Audio API to play float[] sent from Unity in a WebGL build
C#
10
star
51

node-oll

Node.js module for oll (Online-Learning Library)
Shell
10
star
52

create-upm-branch-action

A Github Actions to create release branches for Unity Package Manager
Shell
10
star
53

UnityCustomFunctionNodeExample

Custom Function Node Example in Unity Shader Graph
HLSL
9
star
54

T265_Playground

Simple implementation of positional tracking for Oculus Go
C#
8
star
55

HmdInputModule

Unity VR cursor module
C#
7
star
56

dotfiles

...
Vim Script
7
star
57

node-iRemocon

iRemocon module for Node.js
JavaScript
7
star
58

node-execSync

node.js で shell コマンドを同期的に実行するアドオンです
C++
7
star
59

UnityCustomTextureUpdateForDXT1

CustomTextureUpdate for DXT1
C
7
star
60

Julius2iRemocon

Home electronics operation by speech recognition is enabled using Julius and iRemocon.
C++
6
star
61

MyoUnity-with-RawData

Emg raw data and RSSI are available
C#
5
star
62

uPacketDivision

A native plugin for Unity that provides simple packet division and restoration.
C++
5
star
63

qml-osc

QML custom element that can handle OSC.
C++
5
star
64

NativePluginExample

Unity Advent Calendar 2017 - 21 日目の記事のサンプルプロジェクトです
C#
4
star
65

node-kaden

node.js-based home electronics control system.
C
3
star
66

Outlinus

VR Effect Tool for Oculus Rift x Ovrvision
C++
3
star
67

node-openjtalk-waf

C
3
star
68

node-kana2voca

Katakana to Julius voca format converter
C++
3
star
69

TangoPlayground

my Google Tango application playground (Unity).
C#
3
star
70

node-tw2email

Twitter の特定のリストの TL をメールで送信してくれるヤツです
JavaScript
3
star
71

HAS-Project

Home Automation System (HAS) Project that makes our life lazier.
C++
2
star
72

Xml2dfa

[Julius] コマンドを記述した XML ファイルから dfa ファイルと dict ファイルを生成する
C++
2
star
73

node-voca

ICU を利用してカタカナをローマ字や Julius の voca 形式に変換する Node.js アドオンです。
C++
2
star
74

unite-fhc

Unite.vim source for Future Home Controller
Vim Script
1
star
75

JuliuslibTest

C
1
star
76

LegoAnalyzer

Maker Faire Tokyo に出展予定の LEGO の凹凸を検出する Qt ベースのアプリです
C++
1
star
77

Hellospc

HelloSPCafe
C
1
star
78

node-mecab

node.js から MeCab を利用したり文字列をカタカナに変換するアドオンです
C++
1
star
79

tsubakumi

音声認識家電コントロールシステム@hecomi 家
JavaScript
1
star
80

Blender

Blender 作品のアーカイブです。
1
star
81

tsubakumi-android-wear

凹家のホームオートメーションの Android Wear 連携用プロジェクト
Java
1
star
82

uMicrophoneWebGL

Enable microphone input buffer access in Unity WebGL builds
C#
1
star