You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have made a lighting shader using GPU.js. After taking the exact same code and modifying it to work with variables passed to a GPU.js kernel, it stops working.
This is a photo of it working...
The exact same formula put into GPU.js kernel (I am away that the kernel's height is inverted from normal canvas.js... this is not the problem
Where does it happen?
Firefox, chrome browsers.
How do we replicate the issue?
Copied and pasted no worky code
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport" content="width=device-width, initial-scale=1.0"><title>Directional Lighting Scene</title><style>
canvas { border: 1px solid #000; }
</style></head><body><scriptsrc = "matter.js"></script><scriptsrc = "gpu.js"></script><scriptsrc = "anvil.js"></script><canvasid="canvas" width="400" height="400"></canvas><script>constcanvas=document.getElementById('canvas');constctx=canvas.getContext('2d');// Define colored triangles in the sceneconsttriangles=[{vertices: [[50,50],[150,50],[100,150]],color: [255,0,0,255]},// Red triangle{vertices: [[200,50],[300,50],[250,150]],color: [0,255,0,255]},// Green triangle{vertices: [[100,200],[200,200],[150,300]],color: [0,0,255,255]}// Blue triangle];functiondrawScene(){ctx.fillStyle="white";ctx.fillRect(0,0,canvas.width,canvas.height);for(consttriangleoftriangles){constpath=newPath2D();const[startX,startY]=triangle.vertices[0];path.moveTo(startX,startY);for(leti=1;i<triangle.vertices.length;i++){const[x,y]=triangle.vertices[i];path.lineTo(x,y);}path.closePath();const[r,g,b,a]=triangle.color;ctx.fillStyle=`rgba(${r}, ${g}, ${b}, ${a/255})`;ctx.fill(path);}}constdirectionalLights=[[Math.PI/4,100,100,1,400,Math.PI/3],[Math.PI,300,300,0.8,500,Math.PI/4]];vargpu=newGPU()vardiffuseKernel=gpu.createKernel(function(pix,width,height,dlights,numdlights,ambient){varpixNum=((width*height-this.thread.y)+this.thread.x)*4;varred=pix[pixNum];vargreen=pix[pixNum+1];varblue=pix[pixNum+2];varbrightness=ambient;vardlight=dlights[0];varlightDir=dlights[0][0];varlightX=dlights[0][1];varlightY=dlights[0][2];varintensity=dlights[0][3];vardiffuse=dlights[0][4];varspread=dlights[0][5];constdirX=this.thread.x-lightX;constdirY=this.thread.y-lightY;constdistance=Math.sqrt(dirX*dirX+dirY*dirY);constangleToLight=Math.atan2(dirY,dirX);constangleDiff=Math.abs(angleToLight-lightDir);if(angleDiff<=spread/2){// Calculate diffuse lightingconstdiffuseFactor=Math.max(0,Math.cos(angleDiff)*(1-(distance/diffuse)));brightness+=intensity*diffuseFactor;}this.color((red/255)*brightness,(green/255)*brightness,(blue/255)*brightness,pix[pixNum+2]/255)},{output: [canvas.width,canvas.height],graphical: true})functionapplyDirectionalLights(x,y,currentColor){letambient=0.05;letfinalColor=[0,0,0,255];// Initialize with ambient colorletpixelLit=false;brightness=ambient;for(constlightofdirectionalLights){const[lightDir,lightX,lightY,intensity,diffuse,spread]=light;// Calculate direction from light to pixelconstdirX=x-lightX;constdirY=y-lightY;constdistance=Math.sqrt(dirX*dirX+dirY*dirY);// Check if pixel is within the light spread angleconstangleToLight=Math.atan2(dirY,dirX);constangleDiff=Math.abs(angleToLight-lightDir);if(angleDiff<=spread/2){// Calculate diffuse lightingconstdiffuseFactor=Math.max(0,Math.cos(angleDiff)*(1-(distance/diffuse)));brightness+=intensity*diffuseFactor;}}finalColor=currentColor.map((channel,index)=>Math.min(255,channel*brightness));returnfinalColor}// Draw the initial scenedrawScene();functiondiffuse(){constwidth=canvas.width;constheight=canvas.height;constpix=ctx.getImageData(0,0,width,height).data;diffuseKernel(pix,width,height,directionalLights,directionalLights.length,0.05);constpixels=this.diffuseKernel.getPixels();ctx.putImageData(newImageData(pixels,width,height),0,0);}varmouseX,mouseY=[-1,-1]document.addEventListener("mousemove",(e)=>{mouseX=event.clientX-canvas.getBoundingClientRect().left;mouseY=event.clientY-canvas.getBoundingClientRect().top;})// Call the diffuse function to apply directional lightingdiffuse();setInterval(()=>{ctx.clearRect(0,0,canvas.width,canvas.height);directionalLights[0][0]=Math.atan2(mouseY-directionalLights[0][1],mouseX-directionalLights[0][2]);drawScene();diffuse();},1000/0.5);</script></body></html>
Copied and pasted worky code
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport" content="width=device-width, initial-scale=1.0"><title>Directional Lighting Scene</title><style>
canvas { border: 1px solid #000; }
</style></head><body><scriptsrc = "matter.js"></script><scriptsrc = "gpu.js"></script><scriptsrc = "anvil.js"></script><canvasid="canvas" width="400" height="400"></canvas><script>constcanvas=document.getElementById('canvas');constctx=canvas.getContext('2d');// Define colored triangles in the sceneconsttriangles=[{vertices: [[50,50],[150,50],[100,150]],color: [255,0,0,255]},// Red triangle{vertices: [[200,50],[300,50],[250,150]],color: [0,255,0,255]},// Green triangle{vertices: [[100,200],[200,200],[150,300]],color: [0,0,255,255]}// Blue triangle];functiondrawScene(){ctx.fillStyle="white";ctx.fillRect(0,0,canvas.width,canvas.height);for(consttriangleoftriangles){constpath=newPath2D();const[startX,startY]=triangle.vertices[0];path.moveTo(startX,startY);for(leti=1;i<triangle.vertices.length;i++){const[x,y]=triangle.vertices[i];path.lineTo(x,y);}path.closePath();const[r,g,b,a]=triangle.color;ctx.fillStyle=`rgba(${r}, ${g}, ${b}, ${a/255})`;ctx.fill(path);}}constdirectionalLights=[[Math.PI/4,100,100,1,400,Math.PI/3],[Math.PI,300,300,0.8,500,Math.PI/4]];functionapplyDirectionalLights(x,y,currentColor){letambient=0.05;letfinalColor=[0,0,0,255];// Initialize with ambient colorletpixelLit=false;brightness=ambient;for(constlightofdirectionalLights){const[lightDir,lightX,lightY,intensity,diffuse,spread]=light;// Calculate direction from light to pixelconstdirX=x-lightX;constdirY=y-lightY;constdistance=Math.sqrt(dirX*dirX+dirY*dirY);// Check if pixel is within the light spread angleconstangleToLight=Math.atan2(dirY,dirX);constangleDiff=Math.abs(angleToLight-lightDir);if(angleDiff<=spread/2){// Calculate diffuse lightingconstdiffuseFactor=Math.max(0,Math.cos(angleDiff)*(1-(distance/diffuse)));brightness+=intensity*diffuseFactor;}}finalColor=currentColor.map((channel,index)=>Math.min(255,channel*brightness));returnfinalColor}// Draw the initial scenedrawScene();functiondiffuse(){constwidth=canvas.width;constheight=canvas.height;constpix=ctx.getImageData(0,0,width,height).data;// Loop through each pixelfor(varx=0;x<width;x++){for(vary=0;y<height;y++){// Calculate the color of the pixelconstpixelColor=[pix[(x+y*width)*4+0],pix[(x+y*width)*4+1],pix[(x+y*width)*4+2],pix[(x+y*width)*4+3]];var[r,g,b,a]=applyDirectionalLights(x,y,pixelColor);ctx.fillStyle=`rgba(${r},${g},${b},${a})`ctx.fillRect(x,y,1,1);}}}varmouseX,mouseY=[-1,-1]document.addEventListener("mousemove",(e)=>{mouseX=event.clientX-canvas.getBoundingClientRect().left;mouseY=event.clientY-canvas.getBoundingClientRect().top;})// Call the diffuse function to apply directional lightingdiffuse();setInterval(()=>{ctx.clearRect(0,0,canvas.width,canvas.height);directionalLights[0][0]=Math.atan2(mouseY-directionalLights[0][1],mouseX-directionalLights[0][2]);drawScene();diffuse();},1000/10);</script></body></html>
How important is this (1-5)?
I have to delay the directional lighting system in my game as long as this is not resolved
4 ( I can work on other things in the meantime, but still important)
Expected behavior (i.e. solution)
Provided image
Other Comments
If it is a bug in my own code.... I'm sorry but I can't find it, ChatGPT can't find it, Bard can't find it, and my math teacher can't find it.
The text was updated successfully, but these errors were encountered:
No meme sorry
What is wrong?
I have made a lighting shader using GPU.js. After taking the exact same code and modifying it to work with variables passed to a GPU.js kernel, it stops working.
This is a photo of it working...
The exact same formula put into GPU.js kernel (I am away that the kernel's height is inverted from normal canvas.js... this is not the problem
Where does it happen?
Firefox, chrome browsers.
How do we replicate the issue?
Copied and pasted no worky code
Copied and pasted worky code
How important is this (1-5)?
I have to delay the directional lighting system in my game as long as this is not resolved
4 ( I can work on other things in the meantime, but still important)
Expected behavior (i.e. solution)
Provided image
Other Comments
If it is a bug in my own code.... I'm sorry but I can't find it, ChatGPT can't find it, Bard can't find it, and my math teacher can't find it.
The text was updated successfully, but these errors were encountered: