r/twotriangles • u/LordStarbuilder • Mar 10 '21
r/twotriangles • u/scrygl • Jul 12 '26
Just Pixels: A Song About Shaders, Illustrated with a shader
r/twotriangles • u/MrWoohoo • Aug 20 '15
My first functioning GPU code! The Logistic Map with animated initial population. Neat!
r/twotriangles • u/chippylongstocking • Jun 19 '20
Any advice on combining "two triangles" approaches with other approaches?
I'm working on a project now that has a bunch of objects that interact with physics. This happens on the CPU and I pass down the triangles to the vertex & fragment shaders. I'm really excited by the possibilities of rendering the world using only a fragment shader. I don't really know how to merge the two, though.
If there's no interaction between the objects and the world, I can imagine rendering my fragment shader background with depth test off and then rendering my objects on top of them (that was hand wavy, I trust myself to figure out the specifics when the time comes).
But what if the world and the objects do interact? Suppose that there's some CPU physics that dictates how objects floating on the surface of a pond move. The pond itself lives in the fragment shader and I want the ripples of the pond to change (not necessarily with real physics). Is a reasonable approach to pass down positions of the objects to the fragment shader and just make ripples based on those positions? What if the object is half underwater, how can I obstruct or change the color of the submerged part?
As an aside, I'm interested in what it would look like to move some of the physics onto the GPU. Perhaps if the results have to come back to the CPU, it isn't worth it? When people talk about doing physics on the GPU, does that mean in a shader? I'm talking about video games physics for rendering a scene, not high precision academic physics simulations if it makes a difference.
r/twotriangles • u/TamasaurusRex • Jul 17 '17
Why would a shader render upside down on iOS and right side up in Android?
Hey guys, I'm doing an implementation of this shader on both iOS and Android:
https://www.shadertoy.com/view/4sjfRD
For some reason it renders fine on Android, but on iOS it's upside down. I'm not really sure why, but it's not the first time I've seen this issue. This time, however, if I change
textureCoordinate.y
to
1-textureCoordinate.y
it breaks literally everything else about the shader. Does anyone have any idea why this would be happening?
Edit: I did some poking around and it turns out that the projection matrix in iOS is actually inverted! I've been trying to figure this stuff out for DAYS and I just got it!
r/twotriangles • u/Parzival_Watts • Apr 17 '16
How do I store and operate on colors in a raytracer?
I'm currently working on a raytracer as a personal project. My next step is probably to implement phong shading, but I've hit a roadblock. I'm not sure how you're supposed to add and multiply colors. I've been storing them as a three-vector of rgb values in range [0,1]. I understand the vector math behind reflections and shading, but I'm not quite sure how to implement it.
I don't want a code answer (because that kills the fun). I'd just love some pointers. Thanks!
r/twotriangles • u/[deleted] • Jul 04 '13
Welcome to r/twotriangles
After many years of reading code and trying to interpret articles that make use of math which is far beyond me, I finally was able to fully understand raymarching and procedural graphics in every detail. These incredible techniques allow the rendering of complex 3D fractals in under 100 lines of code.
This is such an interesting field for me, and I figured it must be for others as well. Unfortunately, information is scarce, and discussion is close to nonexistent (besides pouet, but it can get a bit disorganized at times).
This subreddit can be a place for constructive discussions and sharing of anything related to fragment shaders (in OpenGL, DirectX, or even on the CPU).
I will be posting a series of tutorials that aim to teach raymarching and procedural generation to anyone with a basic knowledge of programming and math. With these techniques, you will be able to render anything you could possibly think of just by writing a few lines of code.
If you are interested, please subscribe and tell all your friends about it!
r/twotriangles • u/stduhpf • Mar 14 '17
Someone asked me to share all my shaders for the ShaderEditor Android app as an example library. there are 272 shader examples. Sorry for the lack of comments and if the variables names are ambiguous sometimes.
r/twotriangles • u/HighRelevancy • Dec 13 '13
Signalmesh -I finally understand raymarching of distance fields! =D
r/twotriangles • u/procedural_love • Dec 13 '16
Together in the Void: A short game that uses raymarching
r/twotriangles • u/DiegoAlonsoCortez • May 20 '17
Can someone ELI5 what TwoTriangles is?
Can someone ELI5 what TwoTriangles is? How can I get started?
As per IQ's presentation, the basic idea is to render exactly 4 vertices (one for each corner of the viewport), and then ignore the vertex shader altogether. This seems incredibly counterintutive to me. Why would we ignore the vertex shader? Why don't OpenGL books discuss this? In particular, it's clear that this is not the only way to render geometry precedurally. So, why is this so awesome, and why are the demos at Shadertoy some of the most beautiful computer graphics I've ever seen?
What's the computer graphics / math theory behind these ideas? Where can I find comprehensive textbooks treating them?
How do these ideas relate to the "standard" computer graphics one finds in "standard" books such as Real-Time Rendering? Are they the same?
In particular, I'm trying to find a place to learn this stuff from scratch, because the demos at Shadertoy seem to me like black boxes.
r/twotriangles • u/Ghopper21 • Dec 11 '15
Question about the audio-as-texture input format in Shadertoy
Hi everyone, I'm learning shaders and have been looking at the many on Shadertoy that use audio as input. I haven't been able to find documentation on how to interpret the audio-as-texture format, but it seems from comments and code use that x is the frequency, y=0.0 row is the amplitude, and higher rows are the "wave." Is that correct and if so what does that exactly mean? Is there any documentation out there on how this all works? Thanks for any help!
r/twotriangles • u/joebaf • Jul 05 '13
Geeks3D - Building Worlds With Distance Functions in GLSL
r/twotriangles • u/fb39ca4 • Jul 13 '13
Aliasing in normals when using domain displacement?
I'm trying to use a noise texture to displace the floor in my subway tunnel, and the actual geometry is displaced fine. However, the surface normals get a moiré pattern as you can see if you run the shader in its current state. How can I fix this?
r/twotriangles • u/wongsta • Jul 07 '13
"a volcanic landscape": Iñigo Quilez (creator of the 'Elevated' demo) posted a rendering of a shader toy, complete with source.
r/twotriangles • u/fb39ca4 • Jul 23 '13
Incorrect results from my get material function
It's the same shader - https://www.shadertoy.com/view/ldsGRS
I made a function that figures out which primitives (or groups of primitives) are the closest, and returns a number to identify it so that I can have different materials. Right now, all it does is select a different color. It works fine for the most part, except where the ground meets the tunnel wall, or near the wire - you can see "slices" of their respective color on the wall where it shouldn't be. Is this a common problem?
EDIT: Also, this subreddit seems pretty dead. It's like my private support forum :P I'm going to try posting links to here on other subreddits that may find this relevant.
r/twotriangles • u/[deleted] • Jul 06 '13
Fragmentarium - A tool for exploring pixel based graphics on the GPU
r/twotriangles • u/[deleted] • Jul 09 '13
Tutorial #1: Writing a simple distance field raymarcher
Writing a simple distance field raymarcher
Introduction
Raymarching is a relatively simple solution to a daunting task: finding the point of intersection between a 3D ray and object. To do so, we will need to find the origin point of the ray, and the direction it points for every pixel on the screen. GLSL makes this rather simple for us by including a vast number of built in functions and vector types, which are documented here. If you come across any functions which you don't recognize, that page is the place to look them up. In order to follow this tutorial, I would recommend you start a new shader on either ShaderToy so you can get some actual results in the end. The code can be translated to work in GLSL Sandbox just by changing a few uniform variables, but that will be left as an exercise to the reader.
GLSL is a simplified C like language, and is fairly easy to learn. You may have to do some reading on your own, but these points should outline the basics and a few things to watch out for.
For loops must have a constant number of iterations (except for later versions).
While loops are not supported (except for later versions).
The important built in numeric types for this tutorial are int, float, vec2, vec3, vec4.
Floats must have a decimal point, 1.0 is good, 1 is not. Likewise, ints must not have a decimal place.
vecN is for vectors, and has a N float components, accessible through xyzw, rgba, or stpq.
Casting and object construction is done as if the type was a function, like vec2(0.1, 0.5).
Functions must be defined above where they are used. You can use C style function declarations to get around this if you like.
Inital Setup
You will need a main function to enclose the raymarching code in. This will be called every frame for every pixel on your screen. Define one like so:
void main()
{
// Code goes here
}If you get confused about where to put something, a full code listing is available at the end.
Camera Setup
To begin, you will want to define the origin, target and up direction of the camera.
The origin is the location of the camera.
vec3 cameraOrigin = vec3(2.0, 2.0, 2.0);The target is the point which the camera is looking towards. Our object will be at (0, 0, 0), so we set it to that.
vec3 cameraTarget = vec3(0.0, 0.0, 0.0);The up direction specifies which way is up. For example, setting it to (0, -1, 0) would flip the camera upside down.
vec3 upDirection = vec3(0.0, 1.0, 0.0);Finding the direction the camera is pointing is now as easy as subtracting the target from the origin and normalizing it. When you normalize a vector, it essentially rescales it to be of length 1, so that further operations can be done with it without having side effects on the length of other vectors.
vec3 cameraDir = normalize(cameraTarget - cameraOrigin);We can now calcaulate which directions are up and right from the camera's perspective using vector cross products.
vec3 cameraRight = normalize(cross(upDirection, cameraOrigin));
vec3 cameraUp = cross(cameraDir, cameraRight);The built in gl_FragCoord variable is set to the coordinate of the current pixel, but it's a lot easier to work with if we rescale it to be between -1 and 1 and correct the aspect ratio. We can rescale it as such by doing this:
vec2 screenPos = -1.0 + 2.0 * gl_FragCoord.xy / iResolution.xy; // screenPos can range from -1 to 1
screenPos.x *= iResolution.x / iResolution.y; // Correct aspect ratioGLSL includes a very nice feature called swizzling, which allows us to take the components of a vector and rearrange them to create a new vector, which can be seen above. If we have a vec3 named v and we want to make a new vec2 containing v's x and z components, it's as easy as writing v.xz. The components can be repeated, and can be in any order. You will see this pop up quite often as it is a very useful technique.
Now that we have the camera direction, calculating the ray direction is rather simple.
vec3 rayDir = normalize(cameraRight * screenPos.x + cameraUp * screenPos.y + cameraDir);Raymarching loop
The basic idea behind raymarching is you define a function which returns the shortest distance you can travel from a given point in any direction before you hit an object. We can then iterate a number of times, advancing the ray forwards by this distance each time, and we are gauranteed to never miss any objects. We check this distance every iteration, and if it is smaller than EPSILON then we have hit an object. If the total distance traveled is very large, we assume that we will never hit anything.
const int MAX_ITER = 100; // 100 is a safe number to use, it won't produce too many artifacts and still be quite fast
const float MAX_DIST = 20.0; // Make sure you change this if you have objects farther than 20 units away from the camera
const float EPSILON = 0.001; // At this distance we are close enough to the object that we have essentially hit it
float totalDist = 0.0;
vec3 pos = cameraOrigin;
float dist = EPSILON;
for (int i = 0; i < MAX_ITER; i++)
{
// Either we've hit the object or hit nothing at all, either way we should break out of the loop
if (dist < EPSILON || totalDist > MAX_DIST)
break; // If you use windows and the shader isn't working properly, change this to continue;
dist = distfunc(pos); // Evalulate the distance at the current point
totalDist += dist;
pos += dist * rayDir; // Advance the point forwards in the ray direction by the distance
}After this code runs, pos will be set to the point of intersection. Using this information, we can now do lighting calculations, but first we need to define our distance function.
Defining a distance function
Distance functions can be difficult to wrap your head around to begin with, so for now I'll just show you a few simple ones you can add together to create scenes. Future tutorials will cover deriving these functions on your own. Insert these two functions somewhere above the main function. They will be used by distfunc to create a scene.
float sphere(vec3 pos, float radius)
{
return length(pos) - radius;
}
float box(vec3 pos, vec3 size)
{
return length(max(abs(pos) - size, 0.0));
}You can combine two shapes by simply taking the minimum of the two. GLSL's min function does exactly this.
float combined = min(d1, d2);To create a new object that is the intersection of two others, take the maximum, using the max function.
float intersection = max(d1, d2);To subtract one object from another, or in other words make a hole in it, you can use the following:
float subtracted = max(-d1, d2);More distance functions and operations can be found on Inigo Quilez's Distance functions page.
To actually define our scene we can now simply call sphere or box. For example, to define our distance function as a simple sphere centered around the origin with radius 1 (insert this directly before main):
float distfunc(vec3 pos)
{
return sphere(pos, 1.0);
}Lighting
We want to make sure we only do lighting if we actually hit an object, which we can check by seeing if dist is less than the epsilon.
if (dist < EPSILON)
{
// Lighting code
}
else
{
gl_FragColor = vec4(0.0);
}In order to calculate lighting, you need to find the normal of the surface. This is a vector which points perpendicular to the surface. Using the distance function, we can calculate this rather easily by sampling a few points.
vec2 eps = vec2(0.0, EPSILON);
vec3 normal = normalize(vec3(
distfunc(pos + eps.yxx) - distfunc(pos - eps.yxx),
distfunc(pos + eps.xyx) - distfunc(pos - eps.xyx),
distfunc(pos + eps.xxy) - distfunc(pos - eps.xxy)));Note that by swizziling here, we are creating vec3's that have EPSILON in one component and zero in the other two. By sampling the distance function at pos +/- this point and subtracting the two, we can construct a vector which points perpindicular to the surface. Using the normal, we can now find the diffuse and specular lighting components.
Diffuse lighting is done by taking the dot product of the negative ray direction and the normal for the simple case where the light is at the location of the camera. We also make sure it doesn't go below zero to prevent artifacts.
float diffuse = max(0.0, dot(-rayDir, normal));Specular lighting is achieved by raising the diffuse lighting to a high power, using GLSL's built in pow function. The higher the power, the shinier the surface.
float specular = pow(diffuse, 32.0);Now we can calculate the color by combining the two lighting methods.
vec3 color = vec3(diffuse + specular);
gl_FragColor = vec4(color, 1.0);And we're done! If all goes well you should see a sphere on your screen.
Where to go from here
Try changing the object, adding in colors, or improving the lighting. Future tutorials will cover this in more detail if you get stuck.
If you have any questions or comments don't hesitate to ask!