r/twotriangles • u/scrygl • Jul 12 '26
Just Pixels: A Song About Shaders, Illustrated with a shader
r/twotriangles • u/scrygl • Jul 12 '26
r/twotriangles • u/chippylongstocking • Jan 06 '24
This is true on both my Linux with a GTX1070 and on my Macbook Pro (with an M2). I know what the bottlenecks of the shader are (it's raymarching through noise functions), but what I don't understand is how shadertoy seems to run them so much more efficiently. Are there optimization flags or something that I can try when I run this locally?
I'm running the same shader in both places, it's very strange. I guess I'm assuming that it's how the shader is getting compiled as I'm sure it's using the same hardware in both cases. Thoughts are appreciated.
EDIT: Here's the code. RaymarchTerrain and RaymarchClouds are expensive functions, but aside from commenting things out and watching the framerate change I don't know how to do any proper profiling.
r/twotriangles • u/LordStarbuilder • Mar 10 '21
r/twotriangles • u/bencoder • Sep 17 '20
r/twotriangles • u/IronTau • Sep 14 '20
r/twotriangles • u/chippylongstocking • Jun 19 '20
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/chippylongstocking • Jun 19 '20
for(int i = 0; i < 80; ++i) {
if(hit) { break; }
t += 8.0 + t / 300.0;
vec3 pos = camera_pos + t * ray;
if( pos.y < terrainMap(pos) ) {
hit = true;
}
}I found this segment on shadertoy. I'm pretty new to raymarching, but I understand the gist of it. What I don't get is why t is incremented by 8 + t/300. I understand the 8 and that the t/300 becomes more relevant as you get farther away, but why bother with such a small factor? Why not just to a few more iterations? It only covers about 15 percent more distance at 80 iterations. Is division cheaper than a few more iterations? Am I missing something more fundamental?
r/twotriangles • u/BittyTang • Mar 14 '19
https://www.shadertoy.com/view/3slSWs
I have no idea what's happening with the mix functions. Why do you need the t and t+1 for this to look smooth? If you slightly modify those values, you get discontinuities. I feel like there's some cool math thing happening here with the fract(sin(n)) but I don't think sine does anything special with integers?
r/twotriangles • u/II__dominus__II • Feb 06 '19
Guys i will post this too in case someone will help. thank you very much
this actually the original post i made so, if someone can help i would appreciate it thank you very much
r/twotriangles • u/metaquine • Dec 21 '18
r/twotriangles • u/andybak • Aug 20 '18
Anyone got a link to the developer's docs?
r/twotriangles • u/HellIsBurnin • Jul 01 '18
r/twotriangles • u/DanieleGamba • Feb 17 '18
Hello everyone, here is the output of the simple shader
'return tex2D( myTex , floor(i.uv*3)/3);'
https://answers.unity.com/storage/temp/102601-floor.png
How can I avoid the lines that appear? They appear in the middle, at points where i.uv.x/3=1, 2, 3 and i.uv.y/3=1, 2, 3
r/twotriangles • u/warvstar • Feb 09 '18
When you get up close to a raymarched (from texture)surface, it looks voxelized/pixelated. Going up in resolution makes it less noticeable obviously but increases resources. I'm wondering if there are any good ways to smooth out the surface, maybe by upsampling or dithering... Anyone know of good technique to smooth out the surfaces? Lighting and texturing still need to be considered.
r/twotriangles • u/andybak • Jan 31 '18
r/twotriangles • u/deftware • Jan 08 '18
I have an RX470, which is perfectly capable (however, perhaps a bit slower). I can do all kinds of awesome things when writing GLSL shaders for my own projects, but whenever I try to use ShaderToy to view or write shaders it complains about the most ridiculous things that seem to work for everybody, as in, I literally cannot find anybody else inquiring anywhere about why it has so many problems for them.
For example, I cannot make a for loop that doesn't:
* init with a variable definition
* initialize the variable with a constant
* perform conditional with a constant
* not modify the variable within the loop bodyWTH!?
I can't even browse most of the time because the little previews crash, and WebGL brings down the whole Chrome tab, obviously due to the shaders loading and erroring with ridiculous parse errors that I've never seen before. All the machines I try to use ShaderToy on all have the same problems, but I can run completely normal GLSL on these devices. It's as if ShaderToy is using some completely wrong version of GLSL. What gives ?
r/twotriangles • u/DanieleGamba • Sep 28 '17
Hi, I wrote a simple raymarcher. as you can see in the following image https://preview.ibb.co/nNvTvb/raymarch_slicing_artifacts.png the 3d data is visualized correctly, however I get annoying artifact due to slicing. I can't reduce the step size too much, so any other ways to get rid of them?
r/twotriangles • u/TamasaurusRex • Jul 17 '17
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/pouja • Jun 18 '17
r/twotriangles • u/DiegoAlonsoCortez • May 20 '17
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/Meebsie • Apr 18 '17
Hey all. This is a very subtle error I've noticed with my textures, I'm wondering if any of you can help.
I'm running a "simulation" where pixels move across the screen horizontally. So I've got a persistent buffer storing the last frame. The next frame looks up the pixel color to the left and makes that it's own color. I've made it so blue goes 3x speed, green goes 2x speed, red goes 1x speed. RENDERSIZE is my resolution uniform. It starts with a white bar on the first frame, the color dispersion separates it from there. Here's the code:
vec4 new = vec4(0.0);
new.r = texture(backbuffer, (gl_FragCoord.xy-vec2(1.0,0.0))/RENDERSIZE).r;
new.g = texture(backbuffer, (gl_FragCoord.xy-vec2(2.0,0.0))/RENDERSIZE).g;
new.b = texture(backbuffer, (gl_FragCoord.xy-vec2(3.0,0.0))/RENDERSIZE).b;
fragColor = new;Here's a link to videos of the shader in action: https://www.youtube.com/playlist?list=PLWelNgbUeR_EwlOsZ9wauxRSOVilyVheM
Very simple effect, but as you can see in the video, I run into this super strange error after the color block passes from one side of the texture back around to 0.0 as it should with GL_REPEAT. At regular intervals of the y coordinate, it seems to lag or lead, as if there was some "extra distance" to cover. Changing the texture size has an effect on the output. You can see 4 different texture sizes in those tests, and you'll notice slightly different behavior for each, mainly that as the texture size changes, the number and spacing of the "rows" that experience the offset is changed.
Any ideas? The same thing happens with both GL_NEAREST and GL_LINEAR filtering modes. The effect still exists with power of 2 textures. Here's another really important key it happens even if I use the build-in fract() function instead of relying on their successful GL_REPEAT operation. I checked that my RENDERSIZE Uniform was indeed the correct value as well.
So it seems like it may be an error in how I'm setting up my textures, rather than something I'm doing in GLSL.
Any ideas? Uninitialized memory ruining something? Something like frametear happening? Any help much appreciated.