| 1 | // main.js |
| 2 | window.onload = init; |
| 3 | |
| 4 | class Vector2 |
| 5 | { |
| 6 | constructor (x, y) |
| 7 | { |
| 8 | this.x = x; |
| 9 | this.y = y; |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | const canvas = document.getElementById('canvas'); |
| 14 | canvas.width = window.innerWidth; |
| 15 | canvas.height = window.innerHeight; |
| 16 | |
| 17 | // Create the renderer (it will load default shaders if not provided) |
| 18 | const renderer = new Renderer(canvas, "bumpmap_vert", "bumpmap_frag"); |
| 19 | |
| 20 | const spriteImage = new Image(); |
| 21 | spriteImage.src = 'sprite.png'; |
| 22 | |
| 23 | const normalImage = new Image(); |
| 24 | normalImage.src = 'normal.png'; |
| 25 | |
| 26 | const position = new Vector2(200, 200); |
| 27 | |
| 28 | const scale = new Vector2(0.4, 0.4); |
| 29 | var angle = 45; |
| 30 | var texture; |
| 31 | var normalTexture; |
| 32 | |
| 33 | // TODO: See if we can use the old Framework way of loading images, better than this |
| 34 | spriteImage.onload = () => |
| 35 | { |
| 36 | texture = renderer.loadTexture(spriteImage); |
| 37 | }; |
| 38 | |
| 39 | normalImage.onload = () => |
| 40 | { |
| 41 | normalTexture = renderer.loadTexture(normalImage); |
| 42 | }; |
| 43 | |
| 44 | function init () |
| 45 | { |
| 46 | setInterval(() => |
| 47 | { |
| 48 | drawLoop(); |
| 49 | }, 10); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | function drawLoop () |
| 54 | { |
| 55 | angle += 0.3; |
| 56 | |
| 57 | // Update the light direction |
| 58 | const angleInRadians = (angle % 360) * (Math.PI / 180); |
| 59 | const lightDirection = [ |
| 60 | Math.cos(angleInRadians), |
| 61 | Math.sin(angleInRadians), |
| 62 | 0.2 // A default z-component for the light direction, adjust as needed |
| 63 | ]; |
| 64 | |
| 65 | renderer.bindTexture(normalTexture, renderer.gl.TEXTURE1); |
| 66 | // Render your sprite |
| 67 | renderer.render( |
| 68 | texture, |
| 69 | position, |
| 70 | scale, |
| 71 | 0, |
| 72 | canvas.width, |
| 73 | canvas.height, |
| 74 | { |
| 75 | 'u_normalMap': { unit: 1 }, // Example uniform |
| 76 | 'u_lightDirection': [lightDirection[0], lightDirection[1], lightDirection[2]] |
| 77 | } |
| 78 | ); |
| 79 | } |