This was a very useful tutorial; however there are issues with the audio. On my cell phone it sounds 'ok' but on my computer it is very muffled and bassy - to the point where I cannot understand what you are saying. That said the tutorial was perfect for what I needed to do! Thanks :)
@jeremiahmcelroy272611 ай бұрын
How would I detect if the mouse is hovering over a given cell? I created this code to load from a dataTexture generated by a compute shader that generates the game of life, but I can't for the life of me figure out how to use GlobalMousePosition I'm passing to the shader to determine if the mouse is within a given cell shader_type canvas_item; uniform sampler2D unlitTexture; uniform sampler2D litTexture; uniform sampler2D binaryDataTexture; uniform int gridWidth; uniform int gridHeight; uniform vec2 globalMousePos; varying flat vec2 vertexPos[2]; varying vec2 screenUV; const int cellSize = 8; void vertex() { vertexPos[0] = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy; } void fragment() { //total size of the grid in pixels vec2 totalGridSize = vec2(float(gridWidth) * float(cellSize), float(gridHeight) * float(cellSize)); //scale UV to grid dimensions vec2 scaledUV = UV * vec2(float(gridWidth), float(gridHeight)); vec2 cellUV = fract(scaledUV); bool isInsideCell = all(greaterThanEqual(cellUV, vec2(0.0))) && all(lessThan(cellUV, vec2(1.0))); ivec2 cellIndex = ivec2(floor(scaledUV)); vec2 binaryDataUV = vec2( float(cellIndex.x) / float(gridWidth - 1), float(cellIndex.y) / float(gridHeight - 1) ); float binaryValue = texture(binaryDataTexture, binaryDataUV).r; bool isWhite = binaryValue > 0.5; vec4 color = texture(unlitTexture, cellUV); if(isWhite && isInsideCell) { color = texture(litTexture, cellUV); } COLOR = color; }
@pixezy896211 ай бұрын
I've never tried it before, it looks like a coordinate problem, it's something that's going on my list of tutorials to do.
@jeremiahmcelroy272611 ай бұрын
@@pixezy8962 I ended up getting it actually. Here's the working code. Only issue is my compute shader isn't exactly updating now, I need to write it back to the data texture. I might take a pause on this project and read through the big book of shaders. Here's my code properly highlighting cells at least shader_type canvas_item; uniform sampler2D unlitTexture; uniform sampler2D litTexture; uniform sampler2D binaryDataTexture; uniform int gridWidth; uniform int gridHeight; uniform float brushSize; uniform vec2 globalMousePos; varying vec2 vertexPos; // Declare vertexPos varying variable const int cellSize = 8; void vertex() { // Automatically provided VERTEX is used, no manual assignment needed vertexPos = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy; } void fragment() { //total size of the grid in pixels vec2 totalGridSize = vec2(float(gridWidth) * float(cellSize), float(gridHeight) * float(cellSize)); //scale UV to grid dimensions vec2 scaledUV = UV * vec2(float(gridWidth), float(gridHeight)); vec2 cellUV = fract(scaledUV); bool isInsideCell = all(greaterThanEqual(cellUV, vec2(0.0))) && all(lessThan(cellUV, vec2(1.0))); ivec2 cellIndex = ivec2(floor(scaledUV)); vec2 binaryDataUV = vec2( float(cellIndex.x) / float(gridWidth - 1), float(cellIndex.y) / float(gridHeight - 1) ); float binaryValue = texture(binaryDataTexture, binaryDataUV).r; bool isWhite = binaryValue > 0.5; vec4 color = texture(unlitTexture, cellUV); // Use vertex positions to determine if the mouse is within the vertices of a cell vec2 vertex1 = floor(vertexPos / vec2(brushSize)) * vec2(brushSize); vec2 vertex2 = vertex1 + vec2(brushSize); bool isMouseInsideVertices = all(greaterThanEqual(globalMousePos, vertex1)) && all(lessThan(globalMousePos, vertex2)); // && isMouseInsideVertices if(isWhite && isInsideCell) { color = texture(litTexture, cellUV); } if(isMouseInsideVertices) { if(color == texture(litTexture, cellUV)) { color = texture(unlitTexture, cellUV); } else if(color == texture(unlitTexture, cellUV)){ color = texture(litTexture, cellUV); } } COLOR = color; } //void light() { // Called for every pixel for every light affecting the CanvasItem. // Uncomment to replace the default light processing function with this one. //}