speed up loading a lot?

This commit is contained in:
RandomityGuy 2022-07-27 19:29:45 +05:30
parent 5b9271c6f9
commit e966a96fb5
3 changed files with 132 additions and 21 deletions

View file

@ -132,9 +132,13 @@ class MarbleWorld extends Scheduler {
var _loadingLength:Int = 0;
var _resourcesLoaded:Int = 0;
var textureResources:Array<Resource<h3d.mat.Texture>> = [];
var soundResources:Array<Resource<Sound>> = [];
var lock:Bool = false;
public function new(scene:Scene, scene2d:h2d.Scene, mission:Mission) {
this.scene = scene;
this.scene2d = scene2d;
@ -747,10 +751,28 @@ class MarbleWorld extends Scheduler {
function asyncLoadResources() {
if (this.resourceLoadFuncs.length != 0) {
// if (lock)
// return;
var func = this.resourceLoadFuncs.pop();
lock = true;
#if hl
func();
lock = false;
this._resourcesLoaded++;
#end
#if js
var prom = new js.lib.Promise((resolve, reject) -> {
func();
lock = false;
resolve(true);
this.loadingGui.setProgress((1 - resourceLoadFuncs.length / _loadingLength));
this._resourcesLoaded++;
});
#end
} else {
if (this._resourcesLoaded < _loadingLength)
return;
if (!_ready)
postInit();
}

View file

@ -1,5 +1,6 @@
package src;
import hxd.Pixels;
import shaders.Skybox;
import h3d.shader.pbr.PropsValues;
import h3d.shader.AmbientLight;
@ -38,13 +39,13 @@ class Sky extends Object {
skyMesh.material.blendMode = None;
// var pbrprops = skyMesh.material.mainPass.getShader(PropsValues);
// pbrprops.emissiveValue = 1;
// pbrprops.roughnessValue = 0;
// pbrprops.roughnessValue = 0;`
// pbrprops.occlusionValue = 0;
// pbrprops.metalnessValue = 1;
skyMesh.scale(3500);
var env = new Environment(texture);
env.compute();
// var env = new Environment(texture);
// env.compute();
// var renderer = cast(level.scene.renderer, h3d.scene.pbr.Renderer);
var shad = new Skybox(texture);
skyMesh.material.mainPass.addShader(shad);
@ -75,10 +76,14 @@ class Sky extends Object {
var line = StringTools.trim(lines[i]);
var filenames = ResourceLoader.getFullNamesOf(dmlDirectory + '/' + line);
if (filenames.length == 0) {
skyboxImages.push(new BitmapData(128, 128));
var pixels = Texture.fromColor(0).capturePixels(0, 0);
skyboxImages.push(pixels);
// var tex = new h3d.mat.Texture();
// skyboxImages.push(new BitmapData(128, 128));
} else {
var image = ResourceLoader.getResource(filenames[0], ResourceLoader.getImage, this.imageResources).toBitmap();
skyboxImages.push(image);
var image = ResourceLoader.getResource(filenames[0], ResourceLoader.getImage, this.imageResources).toTexture();
var pixels = image.capturePixels(0, 0);
skyboxImages.push(pixels);
}
}
var maxwidth = 0;
@ -103,7 +108,7 @@ class Sky extends Object {
var cubemaptexture = new Texture(maxheight, maxwidth, [Cube]);
for (i in 0...6) {
cubemaptexture.uploadBitmap(skyboxImages[skyboxIndices[i]], 0, i);
cubemaptexture.uploadPixels(skyboxImages[skyboxIndices[i]], 0, i);
}
return cubemaptexture;
}

View file

@ -38,46 +38,130 @@ class Util {
return new Vector(lerp(v1.x, v2.x, t), lerp(v1.y, v2.y, t), lerp(v1.z, v2.z, t), lerp(v1.w, v2.w, t));
}
public static function rotateImage(bitmap:BitmapData, angle:Float) {
bitmap.lock();
var curpixels = bitmap.getPixels().clone();
public static function rotateImage(bitmap:hxd.Pixels, angle:Float) {
var curpixels = bitmap.clone();
if (angle == Math.PI / 2)
for (x in 0...curpixels.width) {
for (y in 0...curpixels.height) {
bitmap.setPixel(x, y, curpixels.getPixel(y, curpixels.height - x - 1));
var psrc = ((y + (curpixels.height - x - 1) * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
switch (curpixels.format) {
case R8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
case BGRA | RGBA | ARGB:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2));
bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3));
case RG8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
default:
null;
}
// bitmap.setPixel(x, y, curpixels.getPixel(y, curpixels.height - x - 1));
}
}
if (angle == -Math.PI / 2)
for (x in 0...curpixels.width) {
for (y in 0...curpixels.height) {
bitmap.setPixel(x, y, curpixels.getPixel(curpixels.width - y - 1, x));
var psrc = ((curpixels.width - y - 1) + x * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + curpixels.offset;
var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
switch (curpixels.format) {
case R8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
case BGRA | RGBA | ARGB:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2));
bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3));
case RG8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
default:
null;
}
}
}
if (angle == Math.PI)
for (x in 0...curpixels.width) {
for (y in 0...curpixels.height) {
bitmap.setPixel(x, y, curpixels.getPixel(curpixels.width - x - 1, curpixels.height - y - 1));
var psrc = ((curpixels.width - x - 1)
+ (curpixels.height - y - 1) * curpixels.width) * @:privateAccess curpixels.bytesPerPixel
+ curpixels.offset;
var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
switch (curpixels.format) {
case R8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
case BGRA | RGBA | ARGB:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2));
bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3));
case RG8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
default:
null;
}
}
}
bitmap.unlock();
}
public static function flipImage(bitmap:BitmapData, hflip:Bool, vflip:Bool) {
bitmap.lock();
var curpixels = bitmap.getPixels().clone();
public static function flipImage(bitmap:hxd.Pixels, hflip:Bool, vflip:Bool) {
var curpixels = bitmap.clone();
if (hflip)
for (x in 0...curpixels.width) {
for (y in 0...curpixels.height) {
bitmap.setPixel(x, y, curpixels.getPixel(curpixels.height - x - 1, y));
var psrc = ((curpixels.width - x - 1) + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + curpixels.offset;
var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
switch (curpixels.format) {
case R8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
case BGRA | RGBA | ARGB:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2));
bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3));
case RG8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
default:
null;
}
}
}
if (vflip)
for (x in 0...curpixels.width) {
for (y in 0...curpixels.height) {
bitmap.setPixel(x, y, curpixels.getPixel(x, curpixels.width - y - 1));
var psrc = (x + (curpixels.width - y - 1) * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + curpixels.offset;
var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
switch (curpixels.format) {
case R8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
case BGRA | RGBA | ARGB:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2));
bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3));
case RG8:
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
default:
null;
}
}
}
bitmap.unlock();
}
public static function splitIgnoreStringLiterals(str:String, splitter:String, strLiteralToken = '"') {