[LayaAir3]Laya2.13.3使用ShareMesh的setColors方法,不变色

export default class GameApp extends Laya.Script { 
    constructor() {
        super();
        /** @prop {name:intType, tips:"整数类型示例", type:Int, default:1000}*/
        let intType = 1000;
        /** @prop {name:numType, tips:"数字类型示例", type:Number, default:1000}*/
        let numType = 1000;
        /** @prop {name:strType, tips:"字符串类型示例", type:String, default:"hello laya"}*/
        let strType = "hello laya";
        /** @prop {name:boolType, tips:"布尔类型示例", type:Bool, default:true}*/
        let boolType = true;
        // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
    }
   
    onEnable() {
        VertexColor.initShader();
        var scene = Laya.stage.addChild(new Laya.Scene3D());
        //添加照相机
        var camera = (scene.addChild(new Laya.Camera(0, 0.1, 100)));
        camera.transform.translate(new Laya.Vector3(0, 3, 5));
        camera.transform.rotate(new Laya.Vector3(-30, 0, 0), true, false); 
        //添加方向光
        this.directionLight = scene.addChild(new Laya.DirectionLight());
        this.directionLight.color = new Laya.Vector3(1, 1, 1);
        this.directionLight.transform.worldMatrix.setForward(new Laya.Vector3(1, -1, 0));
        this.scene = scene;
       
        //
        var plane = scene.addChild(new Laya.MeshSprite3D(this.createMesh()));
        plane.meshRenderer.material = new Laya.UnlitMaterial(); 
        var len = plane.meshFilter.sharedMesh.vertexCount;
        //plane.meshFilter.sharedMesh._vertexBuffer = ver;
        var colors = new Array();
        var color = new Laya.Color();
        for(var i = 0; i < len; i++)
        {
            /*
            var c = color.clone();
            if(i<100)
            {
                c.a = 0;                
            }
            else{
                c.a = 1;
            }*/
            colors.push(Laya.Color.RED);
        }
        //plane.visiable = false;
        //var mesh = this.createMesh();
        //mesh.setColors(colors);
        //scene.addChild(new Laya.MeshSprite3D(mesh))
        plane.meshFilter.sharedMesh.setColors(colors);
        //
        //Laya.Sprite3D.load("res/plane/LayaMonkey.lh", Laya.Handler.create(this, this.loadSprite3D));
    }
   
    loadSprite3D(sp){
        var mesh = this.scene.addChild(sp);
        var filter = mesh._children[0]._children[0]._meshFilter;
       
        var len = filter.sharedMesh.vertexCount;
        var colors = new Array();
        for (var i = 0; i < len; i++) {
            /*
            var c = color.clone();
            if(i<100)
            {
                c.a = 0;                
            }
            else{
                c.a = 1;
            }*/
            colors.push(Laya.Color.RED);
        }
        filter.sharedMesh.setColors(colors);
    }
   
    createMesh(){
        var vertexDeclaration = Laya.VertexMesh.getVertexDeclaration("POSITION,NORMAL,UV,COLOR");
        var halfLong = 5/ 2;
        var halfHeight = 5 / 2;
        var halfWidth = 5 / 2;
        var vertices = new Float32Array([
            -halfLong, halfHeight, -halfWidth, 0, 1, 0, 0, 0, halfLong, halfHeight, -halfWidth, 0, 1, 0, 1, 0, halfLong, halfHeight, halfWidth, 0, 1, 0, 1, 1, -halfLong, halfHeight, halfWidth, 0, 1, 0, 0, 1,
            -halfLong, -halfHeight, -halfWidth, 0, -1, 0, 0, 1, halfLong, -halfHeight, -halfWidth, 0, -1, 0, 1, 1, halfLong, -halfHeight, halfWidth, 0, -1, 0, 1, 0, -halfLong, -halfHeight, halfWidth, 0, -1, 0, 0, 0,
            -halfLong, halfHeight, -halfWidth, -1, 0, 0, 0, 0, -halfLong, halfHeight, halfWidth, -1, 0, 0, 1, 0, -halfLong, -halfHeight, halfWidth, -1, 0, 0, 1, 1, -halfLong, -halfHeight, -halfWidth, -1, 0, 0, 0, 1,
            halfLong, halfHeight, -halfWidth, 1, 0, 0, 1, 0, halfLong, halfHeight, halfWidth, 1, 0, 0, 0, 0, halfLong, -halfHeight, halfWidth, 1, 0, 0, 0, 1, halfLong, -halfHeight, -halfWidth, 1, 0, 0, 1, 1,
            -halfLong, halfHeight, halfWidth, 0, 0, 1, 0, 0, halfLong, halfHeight, halfWidth, 0, 0, 1, 1, 0, halfLong, -halfHeight, halfWidth, 0, 0, 1, 1, 1, -halfLong, -halfHeight, halfWidth, 0, 0, 1, 0, 1,
            -halfLong, halfHeight, -halfWidth, 0, 0, -1, 1, 0, halfLong, halfHeight, -halfWidth, 0, 0, -1, 0, 0, halfLong, -halfHeight, -halfWidth, 0, 0, -1, 0, 1, -halfLong, -halfHeight, -halfWidth, 0, 0, -1, 1, 1
        ]);
        var indices = new Uint16Array([
            0, 1, 2, 2, 3, 0,
            4, 7, 6, 6, 5, 4,
            8, 9, 10, 10, 11, 8,
            12, 15, 14, 14, 13, 12,
            16, 17, 18, 18, 19, 16,
            20, 23, 22, 22, 21, 20
        ]);
        return Laya.PrimitiveMesh._createMesh(vertexDeclaration, vertices, indices);
    } 
    onDisable() {
    }
}
企业微信截图_1709632857660.png
已邀请:

要回复问题请先

商务合作
商务合作