如何在Babylon.js中为形状着色?

时间:2017-07-22 18:53:04

标签: javascript babylonjs

解决"基本场景"例如,在babylonjs-playground.com here上,我试图对球体的颜色做一个简单的修改。

这是我的尝试,可以交互式运行:

https://www.babylonjs-playground.com/#95BNBS

以下是代码:

var createScene = function () {

    // The original example, without comments:
    var scene = new BABYLON.Scene(engine);
    var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
    camera.setTarget(BABYLON.Vector3.Zero());
    camera.attachControl(canvas, true);
    var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
    light.intensity = 0.7;
    var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);
    sphere.position.y = 1;
    var ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);

    // My attempt to color the sphere
    var material = new BABYLON.StandardMaterial(scene);
    material.alpha = 1;
    material.diffuseColor = new BABYLON.Color3(1,0,0);
    scene.material = material;

    return scene;

};

我尝试将彩色材质添加到球体中无效。

我还尝试在球体对象上寻找与颜色相关的属性:

Object.keys(sphere).filter((key) => return key.includes("Color") )
// => "outlineColor", "overlayColor", "_useVertexColors", "edgesColor"

除了_useVertexColors之外,所有这些似乎都是颜色对象,但更改它们没有任何效果:

sphere.overlayColor.g = 1;
sphere.outlineColor.g = 1;
sphere.edgesColor.g = 1;

1 个答案:

答案 0 :(得分:4)

你非常接近。您正在使用diffuseColor正确设置颜色,但实际上并未将其专门添加到您的球体中。

您的球体对象存储在sphere中,因此您需要设置materialsphere而不是scene上的// My attempt to color the sphere var material = new BABYLON.StandardMaterial(scene); material.alpha = 1; material.diffuseColor = new BABYLON.Color3(1.0, 0.2, 0.7); sphere.material = material; // <--

{{1}}

请参阅此tutorial