@@ -2,7 +2,6 @@ import canvasSizeObserver from 'WebGPU/canvasSizeObserver'
22import getDevice from 'WebGPU/getDevice'
33import initPrograms from 'WebGPU/programs/initPrograms'
44import runCreator from 'run'
5- import { createTextureFromSource } from 'WebGPU/getTexture'
65import {
76 init_state ,
87 add_asset ,
@@ -17,6 +16,7 @@ import initMouseController from 'WebGPU/pointer'
1716import IconsPng from '../msdf/output/icons.png'
1817import IconsJson from '../msdf/output/icons.json'
1918import getDefaultPoints from 'utils/getDefaultPoints'
19+ import * as Textures from 'textures'
2020
2121export type SerializedInputAsset = {
2222 id ?: number // not needed while loading project but useful for undo/redo to maintain selection
@@ -39,11 +39,6 @@ export interface CreatorAPI {
3939 destroy : VoidFunction
4040}
4141
42- export interface TextureSource {
43- url : string
44- texture ?: GPUTexture
45- }
46-
4742export default async function initCreator (
4843 canvas : HTMLCanvasElement ,
4944 onAssetsUpdate : ( assets : SerializedOutputAsset [ ] ) => void ,
@@ -57,8 +52,11 @@ export default async function initCreator(
5752 onProcessingUpdate ( loadingTextures > 0 || isMouseEventProcessing )
5853 }
5954
60- /* setup WebGPU stuff */
6155 const device = await getDevice ( )
56+ Textures . init ( device , ( texLoadings ) => {
57+ loadingTextures = texLoadings
58+ updateProcessing ( )
59+ } )
6260
6361 init_state ( canvas . clientWidth , canvas . clientHeight )
6462 const context = canvas . getContext ( 'webgpu' )
@@ -83,37 +81,7 @@ export default async function initCreator(
8381 updateProcessing ( )
8482 } )
8583
86- const textures : TextureSource [ ] = [ ]
87-
88- function addTexture ( url : string , callback ?: ( width : number , height : number ) => void ) : number {
89- loadingTextures ++
90- updateProcessing ( )
91-
92- const textureId = textures . length
93- textures . push ( { url } )
94-
95- const img = new Image ( )
96- img . src = url
97-
98- img . onload = ( ) => {
99- textures [ textureId ] . texture = createTextureFromSource ( device , img , { flipY : true } )
100- callback ?.( img . width , img . height )
101-
102- loadingTextures --
103- updateProcessing ( )
104- }
105-
106- img . onerror = ( ) => {
107- console . error ( `Failed to load image from ${ url } ` )
108-
109- loadingTextures --
110- updateProcessing ( )
111- }
112-
113- return textureId
114- }
115-
116- connect_on_asset_update_callback ( ( serializedData : AssetZig [ ] ) => {
84+ connect_on_asset_update_callback ( ( serializedData : ZigAssetOutput [ ] ) => {
11785 const serializedAssetsTextureUrl = [ ...serializedData ] . map < SerializedOutputAsset > ( ( asset ) => ( {
11886 id : asset . id ,
11987 textureId : asset . texture_id ,
@@ -123,21 +91,21 @@ export default async function initCreator(
12391 u : point . u ,
12492 v : point . v ,
12593 } ) ) ,
126- url : textures [ asset . texture_id ] . url ,
94+ url : Textures . getUrl ( asset . texture_id ) ,
12795 } ) )
12896 onAssetsUpdate ( serializedAssetsTextureUrl )
12997 } )
13098
13199 connect_on_asset_selection_callback ( onAssetSelect )
132100
133101 const addImage : CreatorAPI [ 'addImage' ] = ( url ) => {
134- const textureId = addTexture ( url , ( width , height ) => {
102+ const textureId = Textures . add ( url , ( width , height ) => {
135103 const points = getDefaultPoints ( width , height , canvas . clientWidth , canvas . clientHeight )
136104 add_asset ( 0 /* no id yet, needs to be generated */ , points , textureId )
137105 } )
138106 }
139107
140- addTexture ( IconsPng , ( width , height ) => {
108+ Textures . add ( IconsPng , ( width , height ) => {
141109 import_icons (
142110 IconsJson . chars . flatMap ( ( char ) => [
143111 char . id ,
@@ -151,29 +119,29 @@ export default async function initCreator(
151119 )
152120 } )
153121
154- const stopCreator = runCreator ( canvas , context , device , presentationFormat , textures , ( ) => {
122+ const stopCreator = runCreator ( canvas , context , device , presentationFormat , ( ) => {
155123 isMouseEventProcessing = false
156124 updateProcessing ( )
157125 } )
158126
159127 const resetAssets : CreatorAPI [ 'resetAssets' ] = async ( assets , withSnapshot = false ) => {
160128 const results = await Promise . allSettled (
161- assets . map < Promise < AssetZig > > (
129+ assets . map < Promise < ZigAssetInput > > (
162130 ( asset ) =>
163131 new Promise ( ( resolve , reject ) => {
164132 if ( asset . points ) {
165133 return resolve ( {
166134 points : asset . points ,
167- texture_id : asset . textureId || addTexture ( asset . url ) ,
135+ texture_id : asset . textureId || Textures . add ( asset . url ) ,
168136 id : asset . id || 0 ,
169137 } )
170138 }
171139
172- const textureId = addTexture ( asset . url , ( width , height ) => {
173- // we wait to add image once points are known because otherwise
174- // if we add img first with "Default" point value and update
175- // it later once texture is loaded, we will get history snapshot with
176- // that "default" points
140+ const textureId = Textures . add ( asset . url , ( width , height ) => {
141+ // we wait to add image once points are known. Other option was to add image first
142+ // with "default" points and then update it once texture is loaded.
143+ // However, that would cause issues with undo/redo since we would have history
144+ // snapshot with "default" points and then update it to the real points.
177145 return resolve ( {
178146 points : getDefaultPoints ( width , height , canvas . clientWidth , canvas . clientHeight ) ,
179147 texture_id : textureId , // if there is no points, then for sure there is no asset.textureId
@@ -184,11 +152,11 @@ export default async function initCreator(
184152 )
185153 )
186154
187- const zigAssets = results
155+ const serializedAssets = results
188156 . filter ( ( result ) => result . status === 'fulfilled' )
189157 . map ( ( result ) => result . value )
190158
191- reset_assets ( zigAssets , withSnapshot )
159+ reset_assets ( serializedAssets , withSnapshot )
192160 }
193161
194162 return {
0 commit comments