|
1 | 1 | # Raymarching-Engine-Unity |
2 | | -A raymarching engine for Unity that supports 25+ primitives and various operations.<br> |
| 2 | +A fast GPU-accelerated raymarching engine for Unity with support for over 28 primitives (including fractals, n-dimensional objects, volumetric clouds) and set operations (Union, Subtract, Intersect). Includes a custom interface for manipulating shader parameters through the editor.<br> |
3 | 3 |
|
4 | 4 | https://user-images.githubusercontent.com/58925008/155891674-fdb4e1e8-3e80-447b-9439-aec03d8f34eb.mp4 |
5 | 5 |
|
6 | | -## Rendering a shape |
| 6 | +## Rendering the shapes provided |
| 7 | +1. Using an Image Effect Shader |
| 8 | + * Attach `Raymarcher.cs` to the Main Camera and `RaymarchRenderer.cs` to an empty gameobject and set the properties and type of shape to render in the inspector. |
| 9 | + * Drag the `ImageEffectRaymarcher.shader` in Shader field of `Raymarcher.cs` in inspector and direction light to the Sun's transform field. |
| 10 | + |
| 11 | +2. Using an Unlit Shader |
| 12 | + * Append the following lines in `Raymarcher` class of `Raymarcher.cs` |
| 13 | + ``` |
| 14 | + private void Awake() |
| 15 | + { |
| 16 | + GetComponent<MeshRenderer>().material = _raymarchMaterial; |
| 17 | + } |
| 18 | + private void OnEnable() |
| 19 | + { |
| 20 | + EditorApplication.update += OnUpdate; |
| 21 | + } |
| 22 | + private void OnUpdate() |
| 23 | + { |
| 24 | + RaymarchRender(); |
| 25 | + EditorApplication.QueuePlayerLoopUpdate(); |
| 26 | + } |
| 27 | + private void Update() |
| 28 | + { |
| 29 | + RaymarchRender(); |
| 30 | + } |
| 31 | + private void OnDisable() |
| 32 | + { |
| 33 | + EditorApplication.update -= OnUpdate; |
| 34 | + foreach (var buffer in disposable) |
| 35 | + { |
| 36 | + buffer.Dispose(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Append this in RaymarchRender function |
| 41 | + void RaymarchRender() |
| 42 | + { |
| 43 | + for (int i = 0; i < renderers.Count; i++) |
| 44 | + { |
| 45 | + if (renderers[i] == GetComponent<RaymarchRenderer>()) |
| 46 | + _raymarchMaterial.SetInt("rank", i); |
| 47 | + } |
| 48 | + } |
| 49 | + ``` |
| 50 | + * Attach `RaymarchRenderer.cs` and `Raymarcher.cs` to a gameobject having a mesh renderer and set the properties and type of shape to render in the inspector. |
| 51 | + * Drag the `Raymarcher.shader` in Shader field of `Raymarcher.cs` in inspector and direction light to the Sun's transform field. |
7 | 52 |
|
8 | | -* Append the distance functions in `DFs.cginc` like |
9 | | -``` |
10 | | -float sdShape(float3 p, // dimension parameters) |
11 | | -{ |
12 | | - // distance function here |
13 | | -} |
14 | | -``` |
| 53 | +## Rendering a custom shape |
| 54 | +* Append the distance function of the shape in `DFs.cginc` like |
| 55 | + ``` |
| 56 | + float sdShape(float3 p, // dimension parameters) |
| 57 | + { |
| 58 | + // distance function here |
| 59 | + } |
| 60 | + ``` |
15 | 61 | * Append the distance function created above in `GetDist()` in `Raymarcher.shader` (if using the unlit shader) or in the `ImageEffectRaymarcher.shader` (if using the Image Effect shader). |
16 | | -``` |
17 | | -float GetDist(Shape shape, float3 p) { |
18 | | - switch (shape.shapeIndex) { |
19 | | - case n: |
20 | | - return sdShape(float3 p, // dimension parameters); |
| 62 | + ``` |
| 63 | + float GetDist(Shape shape, float3 p) { |
| 64 | + switch (shape.shapeIndex) { |
| 65 | + case n: |
| 66 | + return sdShape(float3 p, // dimension parameters); |
| 67 | + } |
21 | 68 | } |
22 | | -} |
23 | | -``` |
| 69 | + ``` |
24 | 70 | * Add the shape in Shape enum and create a struct for its default dimension parameters in `RaymarchRenderer.cs` |
25 | | -``` |
26 | | -public enum Shape { |
27 | | - // shape name |
28 | | -}; |
29 | | -
|
30 | | -public struct shapeDimensions |
31 | | -{ |
32 | | - // default shape dimensions; |
33 | | -}; |
34 | | -``` |
| 71 | + ``` |
| 72 | + public enum Shape { |
| 73 | + // shape name |
| 74 | + }; |
| 75 | + |
| 76 | + public struct shapeDimensions |
| 77 | + { |
| 78 | + // default shape dimensions; |
| 79 | + }; |
| 80 | + ``` |
35 | 81 | * Make the dimension array in `Helpers.cs` to be sent as a compute buffer to the Raymarching shader. |
36 | | -``` |
37 | | -public static vector12 GetDimensionVectors(int i) |
38 | | -{ |
39 | | - //dimension array |
40 | | -} |
41 | | -``` |
| 82 | + ``` |
| 83 | + public static vector12 GetDimensionVectors(int i) |
| 84 | + { |
| 85 | + //dimension array |
| 86 | + } |
| 87 | + ``` |
42 | 88 | * Finally make a custom editor for your shape in the `PropertiesEdior.cs` |
43 | | -``` |
44 | | -public override void OnInspectorGUI() |
45 | | -{ |
46 | | - base.OnInspectorGUI(); |
47 | | - EditorGUILayout.Space(); |
48 | | - EditorGUILayout.LabelField("Dimensions", EditorStyles.boldLabel); |
49 | | - RaymarchRenderer rr = (RaymarchRenderer)target; |
50 | | - switch ((int)rr.shape) |
| 89 | + ``` |
| 90 | + public override void OnInspectorGUI() |
51 | 91 | { |
52 | | - //property editor here |
| 92 | + base.OnInspectorGUI(); |
| 93 | + EditorGUILayout.Space(); |
| 94 | + EditorGUILayout.LabelField("Dimensions", EditorStyles.boldLabel); |
| 95 | + RaymarchRenderer rr = (RaymarchRenderer)target; |
| 96 | + switch ((int)rr.shape) |
| 97 | + { |
| 98 | + //property editor here |
| 99 | + } |
53 | 100 | } |
54 | | -} |
55 | | -``` |
56 | | -## Implementation for Image Effect Shader |
57 | | -* Attach `Raymarcher.cs` to the Main Camera and `RaymarchRenderer.cs` to an empty gameobject and set the properties and type of shape to render in the inspector. |
58 | | -* Drag the `ImageEffectRaymarcher.shader` in Shader field of `Raymarcher.cs` in inspector and direction light to the Sun's transform field. |
59 | | - |
60 | | -## Implementation for Unlit Shader |
61 | | -* Append the following lines in `Raymarcher` class of `Raymarcher.cs` |
62 | | -``` |
63 | | - private void Awake() |
64 | | - { |
65 | | - GetComponent<MeshRenderer>().material = _raymarchMaterial; |
66 | | - } |
67 | | - private void OnEnable() |
68 | | - { |
69 | | - EditorApplication.update += OnUpdate; |
70 | | - } |
71 | | - private void OnUpdate() |
72 | | - { |
73 | | - RaymarchRender(); |
74 | | - EditorApplication.QueuePlayerLoopUpdate(); |
75 | | - } |
76 | | - private void Update() |
77 | | - { |
78 | | - RaymarchRender(); |
79 | | - } |
80 | | - private void OnDisable() |
81 | | - { |
82 | | - EditorApplication.update -= OnUpdate; |
83 | | - foreach (var buffer in disposable) |
84 | | - { |
85 | | - buffer.Dispose(); |
86 | | - } |
87 | | - } |
88 | | - |
89 | | - // Append this in RaymarchRender function |
90 | | - void RaymarchRender() |
91 | | - { |
92 | | - for (int i = 0; i < renderers.Count; i++) |
93 | | - { |
94 | | - if (renderers[i] == GetComponent<RaymarchRenderer>()) |
95 | | - _raymarchMaterial.SetInt("rank", i); |
96 | | - } |
97 | | - } |
98 | | -``` |
99 | | -* Attach `RaymarchRenderer.cs` and `Raymarcher.cs` to a gameobject having a mesh renderer and set the properties and type of shape to render in the inspector. |
100 | | -* Drag the `Raymarcher.shader` in Shader field of `Raymarcher.cs` in inspector and direction light to the Sun's transform field. |
101 | | - |
102 | | - |
103 | | - |
104 | | - |
0 commit comments