1+ import { Object , ObjectManager } from "./Object.js" ;
2+ import { canvas , device , shaderModule } from "./Core.js" ;
3+
4+ export class Model extends Object {
5+ constructor ( src , spriteName = "null" ) {
6+ super ( spriteName ) ;
7+
8+ this . image = null ;
9+ this . texture = null ;
10+ this . width = null ;
11+ this . height = null ;
12+
13+ this . src = src ;
14+
15+ this . SetResource ( src ) ;
16+ }
17+
18+ Update ( deltaTime ) {
19+ super . Update ( deltaTime ) ;
20+
21+ let uniformValues = new Float32Array ( this . uniformBufferSize / 4 ) ;
22+
23+ uniformValues . set ( [ this . transform . position . x , this . transform . position . y ] , 0 ) ;
24+ uniformValues . set ( [ this . transform . scale . x , this . transform . scale . y ] , 2 ) ;
25+ uniformValues . set ( [ 1 , 1 , 1 , 1 ] , 4 ) ;
26+ device . getDevice ( ) . queue . writeBuffer ( this . uniformBuffer , 0 , uniformValues ) ;
27+ }
28+
29+ Render ( deltaTime ) {
30+ super . Render ( deltaTime ) ;
31+
32+ this . renderPassDescriptor . colorAttachments [ 0 ] . view = device . getContext ( ) . getCurrentTexture ( ) . createView ( ) ;
33+
34+ let encoder = device . getDevice ( ) . createCommandEncoder ( { label : "sprite encoder" } ) ;
35+ let pass = encoder . beginRenderPass ( this . renderPassDescriptor ) ;
36+ pass . setPipeline ( this . pipeline ) ;
37+ pass . setBindGroup ( 0 , this . group [ 0 ] ) ;
38+ pass . draw ( 6 , 1 , 0 , 0 ) ;
39+ pass . end ( ) ;
40+
41+ let commandBuffer = encoder . finish ( ) ;
42+ device . getDevice ( ) . queue . submit ( [ commandBuffer ] ) ;
43+ }
44+
45+ SetRender ( ) {
46+ super . SetRender ( ) ;
47+
48+ this . module = shaderModule . UseModule ( "texture" ) ;
49+ this . pipeline = device . getDevice ( ) . createRenderPipeline ( {
50+ label : "sprite" ,
51+ layout : "auto" ,
52+ vertex : {
53+ module : this . module ,
54+ } ,
55+ fragment : {
56+ module : this . module ,
57+ targets : [ { format : device . presentationFormat } ] ,
58+ } ,
59+ } ) ;
60+
61+ this . uniformBufferSize =
62+ 2 * 4 + //translate
63+ 2 * 4 + //scale
64+ 4 * 4 ; //color
65+ this . uniformBuffer = device . getDevice ( ) . createBuffer ( {
66+ label : "texture" ,
67+ size : this . uniformBufferSize ,
68+ usage : GPUBufferUsage . UNIFORM | GPUBufferUsage . COPY_DST ,
69+ } ) ;
70+
71+ for ( let i = 0 ; i < 8 ; ++ i ) {
72+ this . sampler = device . getDevice ( ) . createSampler ( {
73+ addressModeU : ( i & 1 ) ? "repeat" : "clamp-to-edge" ,
74+ addressModeV : ( i & 2 ) ? "repeat" : "clamp-to-edge" ,
75+ magFilter : ( i & 4 ) ? "linear" : "nearest" ,
76+ } ) ;
77+
78+ let binding = device . getDevice ( ) . createBindGroup ( {
79+ layout : this . pipeline . getBindGroupLayout ( 0 ) ,
80+ entries : [
81+ { binding : 0 , resource : this . sampler } ,
82+ { binding : 1 , resource : this . texture . createView ( ) } ,
83+ { binding : 2 , resource : { buffer : this . uniformBuffer } }
84+ ] ,
85+ } ) ;
86+ this . group . push ( binding ) ;
87+ }
88+
89+ this . renderPassDescriptor = {
90+ label : "texture" ,
91+ colorAttachments : [
92+ {
93+ clearValue : [ 0.3 , 0.3 , 0.3 , 1 ] ,
94+ loadOp : "clear" ,
95+ storeOp : "store" ,
96+ } ,
97+ ] ,
98+ } ;
99+ }
100+
101+ async SetResource ( src ) {
102+ super . SetResource ( ) ;
103+
104+ this . image = await spriteManager . LoadImageToSrc ( src ) ;
105+ this . width = this . image . width ;
106+ this . height = this . image . height ;
107+
108+ this . texture = device . getDevice ( ) . createTexture ( {
109+ label : src ,
110+ format : "rgba8unorm" ,
111+ size : [ this . width , this . height ] ,
112+ usage : GPUTextureUsage . TEXTURE_BINDING |
113+ GPUTextureUsage . COPY_DST |
114+ GPUTextureUsage . RENDER_ATTACHMENT ,
115+ } ) ;
116+ device . getDevice ( ) . queue . copyExternalImageToTexture (
117+ { source : this . image , flipY : true } ,
118+ { texture : this . texture } ,
119+ { width : this . width , height : this . height } ,
120+ ) ;
121+
122+ this . SetRender ( ) ;
123+ }
124+
125+ getSize ( ) {
126+ return [ this . width , this . height ] ;
127+ }
128+
129+ setWidth ( width ) {
130+ this . width = width ;
131+ }
132+
133+ setHeight ( height ) {
134+ this . height = height ;
135+ }
136+ }
0 commit comments