@@ -5,17 +5,22 @@ import {
55 stepPointerOnGround ,
66} from "./logic/camera" ;
77import { computeFinalPlacement } from "./logic/computeFinalPlacement" ;
8- import { stepCrowd , stepRunnerLogic } from "./logic/crowd" ;
8+ import { createPhysicStepper , stepRunnerLogic } from "./logic/crowd" ;
99import { stepProgress } from "./logic/progress" ;
1010import { deriveSprites } from "./logic/sprites" ;
11- import { createState , setInitialState } from "./logic/state" ;
11+ import { createState } from "./logic/state" ;
1212import { attachUserEvent } from "./logic/userEvent" ;
1313import { createSpriteRenderer } from "./renderer/spriteRenderer" ;
1414import { createSpriteAtlas } from "./sprites" ;
1515
16+ const spritePromise = createSpriteAtlas ( ) ;
17+
18+ const searchParams = new URL ( location . href ) . searchParams ;
19+ const message = searchParams . get ( "message" ) || "Hello" ;
20+ const runnerCount = parseInt ( searchParams . get ( "n" ) ) || 2000 ;
21+
1622const canvas = document . getElementById ( "canvas" ) as HTMLCanvasElement ;
1723const gl = canvas . getContext ( "webgl2" ) ! ;
18- const dpr = window . devicePixelRatio ?? 1 ;
1924
2025gl . disable ( gl . CULL_FACE ) ;
2126
@@ -27,50 +32,41 @@ gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
2732
2833const renderer = createSpriteRenderer ( gl ) ;
2934
30- const state = createState ( ) ;
31- const resize = ( ) => {
32- canvas . width = canvas . clientWidth * dpr ;
33- canvas . height = canvas . clientHeight * dpr ;
34-
35- gl . viewport ( 0 , 0 , canvas . width , canvas . height ) ;
35+ const set = renderer . createSet ( ) ;
3636
37- const aspect = canvas . width / canvas . height ;
38- mat4 . perspective (
39- state . projectionMatrix ,
40- Math . PI / 4 / aspect ,
41- aspect ,
42- 0.1 ,
43- 2000 ,
44- ) ;
45- } ;
46- resize ( ) ;
37+ //
38+ //
4739
48- window . addEventListener ( "resize" , resize ) ;
49- attachUserEvent ( state ) ;
40+ const state = createState ( runnerCount ) ;
41+ attachUserEvent ( state , canvas , gl ) ;
42+ window . dispatchEvent ( new Event ( "resize" ) ) ;
5043
51- const set = renderer . createSet ( ) ;
52- const sets = [ set ] ;
44+ computeFinalPlacement ( state , message ) ;
5345
54- createSpriteAtlas ( ) . then ( ( res ) => {
55- const searchParams = new URL ( location . href ) . searchParams ;
56- const message = searchParams . get ( "message" ) || "Hello" ;
57- const entityCount = parseInt ( searchParams . get ( "n" ) ) || 2000 ;
46+ const stepPhysic = createPhysicStepper ( runnerCount ) ;
5847
48+ spritePromise . then ( ( res ) => {
5949 renderer . updateSet ( set , { colorTexture : res . texture } ) ;
60- setInitialState ( state , res . animationIndex , entityCount ) ;
61- computeFinalPlacement ( state , res . animationIndex , message ) ;
50+
51+ gl . useProgram ( renderer . _internal . program ) ;
52+
53+ gl . uniform1i ( renderer . _internal . u_colorTexture , 0 ) ;
54+ gl . bindVertexArray ( set . vao ) ;
55+
56+ gl . activeTexture ( gl . TEXTURE0 ) ;
57+ gl . bindTexture ( gl . TEXTURE_2D , set . colorTexture ) ;
6258
6359 const loop = ( ) => {
6460 state . time ++ ;
6561
6662 stepPointerOnGround ( state ) ;
6763
68- stepCrowd ( state ) ;
69- stepRunnerLogic ( state , res . animationIndex ) ;
64+ stepPhysic ( state ) ;
65+ stepRunnerLogic ( state ) ;
7066 stepCameraWobble ( state ) ;
7167 stepProgress ( state ) ;
7268
73- deriveSprites ( state , res . animationIndex , res . coords ) ;
69+ deriveSprites ( state , res . coords ) ;
7470 deriveViewMatrix ( state ) ;
7571
7672 //
@@ -81,7 +77,21 @@ createSpriteAtlas().then((res) => {
8177
8278 gl . clear ( gl . COLOR_BUFFER_BIT | gl . DEPTH_BUFFER_BIT ) ;
8379
84- renderer . draw ( state . projectionMatrix , state . viewMatrix , sets ) ;
80+ //
81+ // usually the API is meant to call renderer.draw(state.projectionMatrix,state.viewMatrix,[set])
82+ // but since there is only oone program let's save some gl instructions
83+ gl . uniformMatrix4fv (
84+ renderer . _internal . u_viewMatrix ,
85+ false ,
86+ state . viewMatrix ,
87+ ) ;
88+ gl . uniformMatrix4fv (
89+ renderer . _internal . u_projectionMatrix ,
90+ false ,
91+ state . projectionMatrix ,
92+ ) ;
93+
94+ gl . drawArraysInstanced ( gl . TRIANGLE_STRIP , 0 , 4 , set . numInstances ) ;
8595
8696 //
8797
0 commit comments