yum-archive/SoggyShaders
Old Unity shaders.
git clone https://git.yummers.dev/yum-archive/SoggyShaders
8427cb4
master
1#ifndef __HG_SDF__ 2#define __HG_SDF__ 3 4#include "pema99.cginc" 5 6//////////////////////////////////////////////////////////////// 7// 8// HG_SDF 9// 10// GLSL LIBRARY FOR BUILDING SIGNED DISTANCE BOUNDS 11// 12// version 2021-07-28 13// 14// Check https://mercury.sexy/hg_sdf for updates 15// and usage examples. Send feedback to spheretracing@mercury.sexy. 16// 17// Brought to you by MERCURY https://mercury.sexy/ 18// 19// 20// 21// Released dual-licensed under 22// Creative Commons Attribution-NonCommercial (CC BY-NC) 23// or 24// MIT License 25// at your choice. 26// 27// SPDX-License-Identifier: MIT OR CC-BY-NC-4.0 28// 29// ///// 30// 31// CC-BY-NC-4.0 32// https://creatifloatommons.org/licenses/by-nc/4.0/legalcode 33// https://creatifloatommons.org/licenses/by-nc/4.0/ 34// 35// ///// 36// 37// MIT License 38// 39// Copyright (c) 2011-2021 Mercury Demogroup 40// 41// Permission is hereby granted, free of charge, to any person obtaining a copy 42// of this software and associated documentation files (the "Software"), to deal 43// in the Software without restriction, including without limitation the rights 44// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 45// copies of the Software, and to permit persons to whom the Software is 46// furnished to do so, subject to the following conditions: 47// 48// The above copyright notice and this permission notice shall be included in all 49// copies or substantial portions of the Software. 50// 51// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 52// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 53// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 54// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 55// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 56// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 57// SOFTWARE. 58// 59// ///// 60// 61//////////////////////////////////////////////////////////////// 62// 63// How to use this: 64// 65// 1. Build some system to #include glsl files in each other. 66// Include this one at the very start. Or just paste everywhere. 67// 2. Build a sphere tracer. See those papers: 68// * "Sphere Tracing" https://link.springer.com/article/10.1007%2Fs003710050084 69// * "Enhanced Sphere Tracing" http://diglib.eg.org/handle/10.2312/stag.20141233.001-008 70// * "Improved Ray Casting of Procedural Distance Bounds" https://www.bibsonomy.org/bibtex/258e85442234c3ace18ba4d89de94e57d 71// The Raymnarching Toolbox Thread on pouet can be helpful as well 72// http://www.pouet.net/topic.php?which=7931&page=1 73// and contains links to many more resources. 74// 3. Use the tools in this library to build your distance bound f(). 75// 4. ??? 76// 5. Win a compo. 77// 78// (6. Buy us a beer or a good vodka or something, if you like.) 79// 80//////////////////////////////////////////////////////////////// 81// 82// Table of Contents: 83// 84// * Helper functions and macros 85// * Collection of some primitive objects 86// * Domain Manipulation operators 87// * Object combination operators 88// 89//////////////////////////////////////////////////////////////// 90// 91// Why use this? 92// 93// The point of this lib is that everything is structured according 94// to patterns that we ended up using when building geometry. 95// It makes it more easy to write code that is reusable and that somebody 96// else can actually understand. Especially code on Shadertoy (which seems 97// to be what everybody else is looking at for "inspiration") tends to be 98// really ugly. So we were forced to do something about the situation and 99// release this lib ;) 100// 101// Everything in here can probably be done in some better way. 102// Please experiment. We'd love some feedback, especially if you 103// use it in a scene production. 104// 105// The main patterns for building geometry this way are: 106// * Stay Lipschitz continuous. That means: don't have any distance 107// gradient larger than 1. Try to be as close to 1 as possible - 108// Distances are euclidean distances, don't fudge around. 109// Underestimating distances will happen. That's why calling 110// it a "distance bound" is more correct. Don't ever multiply 111// distances by some value to "fix" a Lipschitz continuity 112// violation. The invariant is: each fSomething() function returns 113// a correct distance bound. 114// * Use very few primitives and combine them as building blocks 115// using combine opertors that preserve the invariant. 116// * Multiply objects by repeating the domain (space). 117// If you are using a loop inside your distance function, you are 118// probably doing it wrong (or you are building boring fractals). 119// * At right-angle intersections between objects, build a new local 120// coordinate system from the two distances to combine them in 121// interesting ways. 122// * As usual, there are always times when it is best to not follow 123// specific patterns. 124// 125//////////////////////////////////////////////////////////////// 126// 127// FAQ 128// 129// Q: Why is there no sphere tracing code in this lib? 130// A: Because our system is way too complex and always changing. 131// This is the constant part. Also we'd like everyone to 132// explore for themselves. 133// 134// Q: This does not work when I paste it into Shadertoy!!!! 135// A: Yes. It is GLSL, not GLSL ES. We like real OpenGL 136// because it has way more features and is more likely 137// to work compared to browser-based WebGL. We recommend 138// you consider using OpenGL for your productions. Most 139// of this can be ported easily though. 140// 141// Q: How do I material? 142// A: We recommend something like this: 143// Write a material ID, the distance and the local coordinate 144// p into some global variables whenever an object's distance is 145// smaller than the stored distance. Then, at the end, evaluate 146// the material to get color, roughness, etc., and do the shading. 147// 148// Q: I found an error. Or I made some function that would fit in 149// in this lib. Or I have some suggestion. 150// A: Awesome! Drop us a mail at spheretracing@mercury.sexy. 151// 152// Q: Why is this not on github? 153// A: Because we were too lazy. If we get bugged about it enough, 154// we'll do it. 155// 156// Q: Your license sucks for me. 157// A: Oh. What should we change it to? 158// 159// Q: I have trouble understanding what is going on with my distances. 160// A: Some visualization of the distance field helps. Try drawing a 161// plane that you can sweep through your scene with some color 162// representation of the distance field at each point and/or iso 163// lines at regular intervals. Visualizing the length of the 164// gradient (or better: how much it deviates from being equal to 1) 165// is immensely helpful for understanding which parts of the 166// distance field are broken. 167// 168//////////////////////////////////////////////////////////////// 169 170#define PI 3.14159265 171 172// Repeat around the origin by a fixed angle. 173// For easier use, num of repetitions is use to specify the angle. 174float pModPolar(inout float2 p, float repetitions) { 175 float angle = 2*PI/repetitions; 176 float a = atan2(p.y, p.x) + angle/2.; 177 float r = length(p); 178 float c = floor(a/angle); 179 a = glsl_mod(a,angle) - angle/2.; 180 p = float2(cos(a), sin(a))*r; 181 // For an odd number of repetitions, fix cell index of the cell in -x direction 182 // (cell index would be e.g. -5 and 5 in the two halves of the cell): 183 if (abs(c) >= (repetitions/2)) c = abs(c); 184 return c; 185} 186 187#endif // __HG_SDF__