用脚本创建Material全白,颜色贴图均无法生效,卡了好几天了

public createGrid(effectName: string) {

    var _maxTicks = 1;

    const node = new Node(effectName);

    node.parent = this.node;

    node.setWorldPosition(v3(0, 0, 0));

    const model = node.addComponent(ModelComponent);

    const positions = [

        0,0,0,

        0,0,10,

        10,0,10,

        10,0,0

    ];

    const uv = [

        0,0,

        0,1,

        1,1,

        1,0

    ]

    const colors = [];

    const indices = [

        0,1,2,2,3,0

    ];

    

    const primitiveMode = cc.gfx.PrimitiveMode.TRIANGLE_STRIP;

   
    const attributes = [

        {

            name: cc.gfx.AttributeName.ATTR_POSITION,

            format: cc.gfx.Format.RGB32F,

        },

        {   

            name: cc.GFXAttributeName.ATTR_TEX_COORD, 

            format: cc.GFXFormat.RG32F 

        },

    ];

    var db:boolean = false

    

    // const plane = primitives.box({ width: 10, length: 10, widthSegments: 50, lengthSegments: 50 });

    // const mesh = utils.createMesh(plane);

    

    const mesh = utils.createMesh({primitiveMode,positions,uv,indices, attributes,},null,{ calculateBounds: false });

            
    const mesh = utils.createMesh({

        primitiveMode: cc.GFXPrimitiveMode.TRIANGLE_LIST,

        positions: positions,

        uvs: uv,

        colors: colors,

        indices: indices,

        attributes: [

            { name: cc.GFXAttributeName.ATTR_POSITION, format: cc.GFXFormat.RGB32F },

            { name: cc.GFXAttributeName.ATTR_TEX_COORD, format: cc.GFXFormat.RG32F },

            { name: cc.GFXAttributeName.ATTR_COLOR, format: cc.GFXFormat.RGBA8UI, isNormalized: true },

        ],

    }, undefined, { calculateBounds: false });

*/

    model.mesh = mesh;

    var texture_:Texture2D = null

    let remoteUrl = "http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.png";

        assetManager.loadRemote<ImageAsset>(remoteUrl, function (err, imageAsset) {

            const texture = new Texture2D();

            texture.image = imageAsset;

            texture_ = texture

            const ma = new cc.Material();

            ma.initialize({ 

                effectName: 'builtin-standard',

                defines:{

                    USE_ALBEDO_MAP:true,

                    // USE_VERTEX_COLOR:true,

                }

            });

            const mat = new Material();

            var com = node.getComponent(MeshRenderer);

            com.material = ma

            var m = com.material

            m.setProperty('mainColor',new Color(111,1,0,1))

            console.dir(node)


        });


    return model;

}
1赞

能提供个demo吗?还有你用的是什么版本?

能不能把无用的东西拿掉,把你觉得有问题的代码整一个demo,我这都运行不了这个工程,一些文件都找不到,也不知道你有问题的代码在哪。。。。

我从新弄个demo给你,我这个用npm装了第三方的包

材质无效demo.rar (1.1 MB)

你这个没有设置法线:

        const normals = [
            0,1,0,
            0,1,0,
            0,1,0,
            0,1,0
        ]

如果你不想要光照,可以改effectName为’unlit’
image

unlit可以改变颜色了,但是我后期需要光照,所以还是要用standard,但是用standard后颜色又是纯白的。麻烦大佬看看怎么解决。
另外还有其他问题我顺便提了,大佬有空的话也帮我看看:1、怎么启用双面材质。2、贴图没用生效。

我这边试是有效果的啊。。把法线加上的话

谢谢。颜色可以了,是我自己写的有问题。。我先搞贴图,要是不行估计还得麻烦大佬。

你好,方便看一下这个问题吗

请教个问题,贴图似乎上成功了,但是看不到贴图的细节。不知道是不是和顶点数量有关,目前只有4个顶点

UV没设置上。。这里边要的参数是uvs

好家伙,我uv少写了个s,多谢了。