在处理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;
最佳答案
你很亲密您已使用diffuseColor
正确设置了颜色,但实际上并未将其专门添加到球体中。
您的球体对象存储在sphere
中,因此您需要在material
而不是sphere
上设置您创建的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; // <--
看到这个tutorial