zhouenguang 3 年之前
父節點
當前提交
e41c408a54
共有 3 個文件被更改,包括 126 次插入127 次删除
  1. 6 6
      src/XAvatar.js
  2. 27 39
      src/XAvatarManager.js
  3. 93 82
      src/XMaterialComponent.js

+ 6 - 6
src/XAvatar.js

@@ -35,7 +35,7 @@ export default class XAvatar {
         this.hide = ()=>{
             this.isHide = !0
             this._hide()
-            !this.isRender
+            return !this.isRender
         },
 
         this._show = ()=>{
@@ -59,11 +59,11 @@ export default class XAvatar {
             )
         },
 
-        this.show = ()=>(
-            this.isHide = !1,
-            this._show(),
-            !!this.isRender
-        ),
+        this.show = ()=>{
+            this.isHide = !1
+            this._show()
+            return !!this.isRender
+        },
 
         this.setAnimations = _=>{
             this.controller.animations = _

+ 27 - 39
src/XAvatarManager.js

@@ -164,47 +164,35 @@ export default class XAvatarManager {
         this.characterMap.set(4, new Map),
         this.characterMap.set(5, new Map)
     }
-    loadAvatar({id: e, avatarType: t, priority: r, avatarManager: n, assets: o, status: a}) {
-        return new Promise((s,l)=>{
-            if (this.getAvatarById(e))
-                return l(new DuplicateAvatarIDError(`[Engine] cannot init avatar with the same id = ${e}`));
+    loadAvatar({id, avatarType, priority, avatarManager, assets, status}) {
+        return new Promise((resolve, reject)=>{
+            if (this.getAvatarById(id))
+                return reject(new DuplicateAvatarIDError(`[Engine] cannot init avatar with the same id = ${e}`));
             if (this.getAvatarNums() > this.maxAvatarNum)
-                return l(new ExceedMaxAvatarNumError(`[Engine] \u8D85\u51FA\u6700\u5927\u89D2\u8272\u9650\u5236 ${this.maxAvatarNum}`));
-            const u = new XAvatar({
-                id: e,
-                avatarType: t,
-                priority: r,
-                avatarManager: n,
-                assets: o,
-                status: a
-            });
-            this.registerAvatar(u);
-            if (r == 0)
-                this.setMainAvatar(u.id),
-                this.addAvatarToScene(u, 0).then(c=>(logger.debug(`[Engine] avatar ${u.id} has been added to scene`),
-                c ? (this._updateBillboardStatus(c, BillboardStatus.SHOW),
-                setTimeout(()=>{
-                    this.launchProcessLoadingLoop()
-                }
-                , this._delayTime),
-                s(c)) : (u.removeAvatarFromScene(),
-                l(new AvatarAssetLoadingError)))).catch(c=>(u.removeAvatarFromScene(),
-                l(new AvatarAssetLoadingError(c))));
-            else
-                return s(u)
+                return reject(new ExceedMaxAvatarNumError(`[Engine] \u8D85\u51FA\u6700\u5927\u89D2\u8272\u9650\u5236 ${this.maxAvatarNum}`));
 
-            // this.setMainAvatar(u.id),
-            // this.addAvatarToScene(u, 0).then(c=>(logger.debug(`[Engine] avatar ${u.id} has been added to scene`),
-            // c ? (this._updateBillboardStatus(c, BillboardStatus.SHOW),
-            // setTimeout(()=>{
-            //     this.launchProcessLoadingLoop()
-            // }
-            // , this._delayTime),
-            // s(c)) : (u.removeAvatarFromScene(),
-            // l(new AvatarAssetLoadingError)))).catch(c=>(u.removeAvatarFromScene(),
-            // l(new AvatarAssetLoadingError(c))));
-        }
-        )
+            const avatar = new XAvatar({ id, avatarType, priority, avatarManager, assets, status });
+            this.registerAvatar(avatar);
+
+            if (priority == 0)
+                this.setMainAvatar(avatar.id),
+                this.addAvatarToScene(avatar, 0).then(c=>(
+                    logger.debug(`[Engine] avatar ${avatar.id} has been added to scene`),
+                    c ? (
+                        this._updateBillboardStatus(c, BillboardStatus.SHOW),
+                        setTimeout(()=>{ this.launchProcessLoadingLoop() }, this._delayTime),
+                        resolve(c)
+                    ) : (
+                        avatar.removeAvatarFromScene(),
+                        reject(new AvatarAssetLoadingError)
+                    )
+                )).catch(c=>(
+                    avatar.removeAvatarFromScene(),
+                    reject(new AvatarAssetLoadingError(c))
+                ));
+            else
+                return resolve(avatar)
+        })
     }
     deleteAvatar(e) {
         return e.isRender ? (e.removeAvatarFromScene(),

+ 93 - 82
src/XMaterialComponent.js

@@ -79,19 +79,14 @@ export default class XMaterialComponent {
             const height = e.height || 1280
             const focus = width / (2 * Math.tan(Math.PI * fov / 360));
             (lowModelShader = this._lowModelShader) == null || lowModelShader.setVector3("focal_width_height", new BABYLON.Vector3(focus,width,height))
-        }
-        );
+        });
 
-        E(this, "updateRawYUVData", (stream,width,height,fov=-1)=>{
-            var o, a;
+        E(this, "updateRawYUVData", (stream, width, height, fov=-1)=>{
             fov == -1 && (fov = this.yuvInfo.fov);
+
             if (this._isUpdateYUV == !0) {
                 console.log('执行:updateRawYUVData')
-                const s = {
-                    width: width,
-                    height: height,
-                    fov: fov
-                }
+                const yuvInfo = { width, height, fov }
                 const videosResOriArrayIndex = this._videoRawYUVTexArray.findId(width, height)
                 const currentVideoId = this._videoRawYUVTexArray.getCurrentVideoTexId();
 
@@ -100,48 +95,51 @@ export default class XMaterialComponent {
                     this.yuvInfo.width = width;
                     this.yuvInfo.height = height;
                     this.yuvInfo.fov = fov;
+
                     this._videoRawYUVTexArray.setCurrentVideoTexId(videosResOriArrayIndex);
-                    this._changeVideoRes(videosResOriArrayIndex);
-                    this._changePureVideoLowModelShaderCanvasSize(s);
-                    this._scenemanager.cameraComponent.cameraFovChange(s);
-                    this._scenemanager.yuvInfo = s;
+                    this._changeVideoRes(videosResOriArrayIndex);   // 设置texture_video
+                    this._changePureVideoLowModelShaderCanvasSize(yuvInfo); // 设置focal_width_height
+                    this._scenemanager.cameraComponent.cameraFovChange(yuvInfo);
+                    this._scenemanager.yuvInfo = yuvInfo;
                 }
 
                 let VideoTexture = this._videoRawYUVTexArray.getVideoYUVTex(videosResOriArrayIndex)
                 if(VideoTexture != null){
+                    // 更新视频流
                     VideoTexture.update(stream)
                     VideoTexture.updateSamplingMode(BABYLON.Texture.BILINEAR_SAMPLINGMODE)
-                
                 }
+
+                //var o, a
                 //(o = this._videoRawYUVTexArray.getVideoYUVTex(videosResOriArrayIndex)) == null || o.update(stream),
                 //(a = this._videoRawYUVTexArray.getVideoYUVTex(videosResOriArrayIndex)) == null || a.updateSamplingMode(BABYLON.Texture.BILINEAR_SAMPLINGMODE)
             }
-        }
-        );
+        });
+
         E(this, "_changeVideoRes", e=>{
             this._lowModelShader.setTexture("texture_video", this._videoRawYUVTexArray.getVideoYUVTex(e))
-        }
-        );
+        });
 
-        E(this, "initDynamicData", (dynamicRange,width,height)=>new Promise((resolve,reject)=>{
-            this.setDynamicSize(dynamicRange).then(a=>{
-                if (a) {
-                    for (let s = 0; s < dynamicRange; ++s)
-                        (l=>{
-                            this.initDynamicTexture(l, width, height),
-                            this.initDynamicShaders(l).then(()=>{
-                                this._updatePanoShaderInput(l)
+        E(this, "initDynamicData", (dynamicRange,width,height)=>{
+            return new Promise((resolve,reject)=>{
+                this.setDynamicSize(dynamicRange).then(a=>{
+                    if (a) {
+                        for (let s = 0; s < dynamicRange; ++s)
+                            (l=>{
+                                this.initDynamicTexture(l, width, height),
+                                this.initDynamicShaders(l).then(()=>{
+                                    this._updatePanoShaderInput(l)
+                                }
+                                )
                             }
-                            )
-                        }
-                        )(s);
-                    resolve(!0)
-                } else
-                    reject(new XMaterialError(`[Engine] DynamicRoomSize (${dynamicRange}) is too small`))
-            }
-            )
-        }
-        ).catch(n=>logger.error(`[Engine] ${n}`)));
+                            )(s);
+                        resolve(!0)
+                    } else
+                        reject(new XMaterialError(`[Engine] DynamicRoomSize (${dynamicRange}) is too small`))
+                }
+                )
+            }).catch(n=>logger.error(`[Engine] ${n}`))
+        });
         
         E(this, "_initDefaultShader", ()=>{
             this._defaultShader == null && (this._defaultShader = new BABYLON.GridMaterial("GridShader",this.scene),
@@ -150,8 +148,8 @@ export default class XMaterialComponent {
             this._defaultShader.majorUnitFrequency = 1,
             this._defaultShader.mainColor = new BABYLON.Color3(.6,.6,.6),
             this._defaultShader.backFaceCulling = !1)
-        }
-        );
+        });
+
         E(this, "_initPureVideoShader", ()=>{
             if (this._lowModelShader == null) {
                 const e = new BABYLON.ShaderMaterial("PureVideoShader",this.scene,{
@@ -173,46 +171,49 @@ export default class XMaterialComponent {
                 e.backFaceCulling = !1,
                 this._lowModelShader = e
             }
-        }
-        );
+        });
+
         E(this, "setDynamicSize", e=>new Promise((t,r)=>{
             e >= 1 && e <= 100 ? (this._dynamic_size = e,
             t(!0)) : (this._dynamic_size = 1,
             t(!1))
-        }
-        ));
+        }));
+
         E(this, "_isInDynamicRange", e=>e < this._dynamic_size && e >= 0);
+
         E(this, "initDynamicTexture", (e,t,r)=>{
             this._isInDynamicRange(e) && (this._dynamic_textures[e] != null && (this._dynamic_textures[e].dispose(),
             this._dynamic_textures[e] = null),
             this._dynamic_textures[e] = new BABYLON.RawTexture(null,t,r * 1.5,BABYLON.Engine.TEXTUREFORMAT_LUMINANCE,this.scene,!1,!0,BABYLON.Texture.NEAREST_SAMPLINGMODE,BABYLON.Engine.TEXTURETYPE_UNSIGNED_BYTE),
             this._dynamic_textures[e].name = "Pano_Dynamic_" + e + "_" + Date.now())
-        }
-        );
-        E(this, "initDynamicShaders", e=>(logger.info("[Engine] Material init dynamic shader."),
-        new Promise((t,r)=>{
-            this._dynamic_shaders[e] != null && this._dynamic_shaders[e].dispose();
-            const n = new BABYLON.ShaderMaterial("Pano_Shader_" + e,this.scene,{
-                vertexSource: panoVertex,
-                fragmentSource: panoFragment
-            },{
-                attributes: ["uv", "position", "world0", "world1", "world2", "world3"],
-                uniforms: ["view", "projection", "worldViewProjection", "world"],
-                defines: ["#define SHADOWFULLFLOAT"]
-            });
-            n.setTexture("texture_pano", null),
-            n.setVector3("centre_pose", new BABYLON.Vector3(0,0,0)),
-            n.setFloat("isYUV", this._inputPanoYUV420 ? 1 : 0),
-            n.setTexture("shadowSampler", null),
-            n.setMatrix("lightSpaceMatrix", null),
-            n.setFloat("haveShadowLight", 0),
-            n.setFloat("fireworkLight", 0),
-            n.setVector3("fireworkLightPosition", new BABYLON.Vector3(0,0,0)),
-            n.backFaceCulling = !1,
-            this._dynamic_shaders[e] = n,
-            t(!0)
-        }
+        });
+
+        E(this, "initDynamicShaders", e=>(
+            logger.info("[Engine] Material init dynamic shader."),
+            new Promise((t,r)=>{
+                this._dynamic_shaders[e] != null && this._dynamic_shaders[e].dispose();
+                const n = new BABYLON.ShaderMaterial("Pano_Shader_" + e,this.scene,{
+                    vertexSource: panoVertex,
+                    fragmentSource: panoFragment
+                },{
+                    attributes: ["uv", "position", "world0", "world1", "world2", "world3"],
+                    uniforms: ["view", "projection", "worldViewProjection", "world"],
+                    defines: ["#define SHADOWFULLFLOAT"]
+                });
+                n.setTexture("texture_pano", null),
+                n.setVector3("centre_pose", new BABYLON.Vector3(0,0,0)),
+                n.setFloat("isYUV", this._inputPanoYUV420 ? 1 : 0),
+                n.setTexture("shadowSampler", null),
+                n.setMatrix("lightSpaceMatrix", null),
+                n.setFloat("haveShadowLight", 0),
+                n.setFloat("fireworkLight", 0),
+                n.setVector3("fireworkLightPosition", new BABYLON.Vector3(0,0,0)),
+                n.backFaceCulling = !1,
+                this._dynamic_shaders[e] = n,
+                t(!0)
+            }
         )));
+
         this._scenemanager = e,
         this.scene = e.Scene,
         this.engine = this.scene.getEngine(),
@@ -410,24 +411,34 @@ export default class XMaterialComponent {
             pointLight.intensity = 0
         }
     }
+
     _updatePanoShaderInput(e) {
-        var t, r, n, o, a, s, l, u, c, h;
-        if (this._isInDynamicRange(e))
-            if (this.scene.getLightByName("AvatarLight") ? ((t = this._dynamic_shaders[e]) == null || t.setFloat("haveShadowLight", 1),
-            (o = this._dynamic_shaders[e]) == null || o.setTexture("shadowSampler", (n = (r = this.scene.getLightByName("AvatarLight")) == null ? void 0 : r.getShadowGenerator()) == null ? void 0 : n.getShadowMapForRendering()),
-            (l = this._dynamic_shaders[e]) == null || l.setMatrix("lightSpaceMatrix", (s = (a = this.scene.getLightByName("AvatarLight")) == null ? void 0 : a.getShadowGenerator()) == null ? void 0 : s.getTransformMatrix())) : ((u = this._dynamic_shaders[e]) == null || u.setTexture("shadowSampler", null),
-            (c = this._dynamic_shaders[e]) == null || c.setMatrix("lightSpaceMatrix", new Matrix),
-            (h = this._dynamic_shaders[e]) == null || h.setFloat("haveShadowLight", 0)),
-            this.scene.getLightByName("fireworkLight"))
+        var n,s;
+        if (this._isInDynamicRange(e)) 
+        {
+            let shader = this._dynamic_shaders[e]
+            let avatarLight = this.scene.getLightByName("AvatarLight")
+
+            shader == null || (avatarLight ? (
+                shader.setFloat("haveShadowLight", 1),
+                shader.setTexture("shadowSampler", (n = avatarLight == null ? void 0 : avatarLight.getShadowGenerator()) == null ? void 0 : n.getShadowMapForRendering()),
+                shader.setMatrix("lightSpaceMatrix", (s = avatarLight == null ? void 0 : avatarLight.getShadowGenerator()) == null ? void 0 : s.getTransformMatrix())
+            ) : (
+                shader.setTexture("shadowSampler", null),
+                shader.setMatrix("lightSpaceMatrix", new Matrix),
+                shader.setFloat("haveShadowLight", 0)
+            ))
+
+            let fireworkLight = this.scene.getLightByName("fireworkLight")
+            if (fireworkLight) {
                 this.scene.registerBeforeRender(()=>{
-                    var f;
-                    this._dynamic_shaders[e].setFloat("fireworkLight", this.scene.getLightByName("fireworkLight").getScaledIntensity()),
-                    this._dynamic_shaders[e].setVector3("fireworkLightPosition", (f = this.scene.getLightByName("fireworkLight")) == null ? void 0 : f.position)
-                }
-                );
-            else {
-                const f = new BABYLON.PointLight("fireworkLight",new BABYLON.Vector3(0,0,0),this.scene);
+                    shader.setFloat("fireworkLight", fireworkLight.getScaledIntensity()),
+                    shader.setVector3("fireworkLightPosition", fireworkLight == null ? void 0 : fireworkLight.position)
+                });
+            } else {
+                const f = new BABYLON.PointLight("fireworkLight", new BABYLON.Vector3(0,0,0), this.scene);
                 f.intensity = 0
             }
+        }
     }
 }