[0]texture2D的setPixel纹理rgba数据之后会随着窗口的缩放,渐变成黑色

export class Texture2dTest
{
private sprite3D:Laya.Sprite3D;
private readonly AllPng:string[] = ["pic-001.png","output-06.png"];
private texture:Laya.Texture2D;
private box;
constructor(){
Laya3D.init(0, 0);
Laya.stage.scaleMode = Laya.Stage.SCALE_FULL;
Laya.stage.screenMode = Laya.Stage.SCREEN_NONE;
Laya.Stat.show();

var scene = Laya.stage.addChild(new Laya.Scene3D()) as Laya.Scene3D;

var camera = scene.addChild(new Laya.Camera(0, 0.1, 0)) as Laya.Camera;
camera.transform.translate(new Laya.Vector3(0, 2, 20));
camera.transform.rotate(new Laya.Vector3(-15, 0, 0), true, false);
camera.clearColor = new Laya.Vector4(0.2, 0.2, 0.2, 1.0);

var directionLight = scene.addChild(new Laya.DirectionLight()) as Laya.DirectionLight;
//设置平行光的方向
var mat = directionLight.transform.worldMatrix as Laya.Matrix4x4;
mat.setForward(new Laya.Vector3(-1.0, -1.0, -1.0));
directionLight.transform.worldMatrix=mat;

this.sprite3D = scene.addChild(new Laya.Sprite3D()) as Laya.Sprite3D;

//正方体
var box = this.sprite3D.addChild(new Laya.MeshSprite3D(Laya.PrimitiveMesh.createPlane())) as Laya.MeshSprite3D;
this.box = box;
box.transform.position = new Laya.Vector3(0.0, 0.0, 0);
box.transform.rotate(new Laya.Vector3(90, 0, 0), false, false);
Laya.loader.load(this.AllPng,new Laya.Handler(this,this.onCallBack),null,Laya.Loader.BUFFER);

Laya.stage.on(Laya.Event.KEY_DOWN,this,this.onKey);
//########################################################################
}

private curStatus:boolean = false;

private refreshCamera(){
this.curStatus =!this.curStatus;
let data:ArrayBuffer = Laya.Loader.getRes(this.AllPng[this.curStatus ? 0: 1]);//pic-001
const x2 = new Uint8Array(data);
let p = new window['PNG'](x2);
let getData = p.decode();//rgba数组序列
this.texture.setPixels(getData);
}
private onKey(e){
if(e.keyCode == 49){
this.refreshCamera();
// this.updateUITexture(data);

}else if(e.keyCode == 50){
// let t = Laya.Loader.getRes(this.AllPng[0]);
// console.log(t);
}
}

private onCallBack(){
var mater = new Laya.BlinnPhongMaterial();
//new Laya.UnlitMaterial();
//Laya.BlinnPhongMaterial();

// const x2 = new Uint8Array(data);
// let p = new window['PNG'](x2);
// let getData = p.decode();

let texture:Laya.Texture2D = new Laya.Texture2D(544,960);//p.width,p.height
this.texture = texture;
this.refreshCamera();


//在U方向上使用WARPMODE_CLAMP
// texture.format = Laya.TextureFormat.ASTC10x10
texture.wrapModeU = Laya.BaseTexture.WARPMODE_CLAMP;
//在V方向使用WARPMODE_REPEAT
texture.wrapModeV = Laya.BaseTexture.WARPMODE_CLAMP;
//设置过滤方式
texture.filterMode = Laya.BaseTexture.FILTERMODE_BILINEAR;
//设置各向异性等级
texture.anisoLevel = 2;
// texture.setCompressData(data);

mater.albedoTexture = texture;
//修改材质贴图的平铺和偏移
// var tilingOffset:Laya.Vector4 = mater.tilingOffset;
// tilingOffset.setValue(2, 2, 0, 0);
// mater.tilingOffset = tilingOffset;

this.box.meshRenderer.material =mater;
}

}
已邀请:

Yan

赞同来自:

请问window['PNG']是您自己封装的吗?我们这里运行您给的文件报Uncaught TypeError: window.PNG is not a constructor

泥潭里的金鱼

赞同来自:

npm install png-js
解决了 ,必须将纹理对象设置成minmap格式的。

要回复问题请先

商务合作
商务合作