gpt4 book ai didi

javascript - 在 babylon.js 中改变 Material 的颜色

转载 作者:行者123 更新时间:2023-11-29 21:03:59 26 4
gpt4 key购买 nike

我刚接触 babylon.js,但我似乎无法弄清楚,您将如何更改 Material 的颜色?

我目前的代码是:

/* eslint-disable */
import * as BABYLON from 'babylonjs';


// Get the canvas element from our HTML above
const canvas = document.getElementById("root");

// Load the BABYLON 3D engine
const engine = new BABYLON.Engine(canvas, true);
let fn;
let mainColor = new BABYLON.Color3(1.0, 0.2, 0.7);

setTimeout(() => {
mainColor = new BABYLON.Color3(0.3, 0.2, 0.2);
fn();
}, 2000);

// This begins the creation of a function that we will 'call' just after it's built
function createScene () {

// Now create a basic Babylon Scene object
const scene = new BABYLON.Scene(engine);

// Change the scene background color to green.
scene.clearColor = new BABYLON.Color4(0.5, 0.8, 0.6, 0.8);

// This creates and positions a free camera
const camera = new BABYLON.ArcRotateCamera("ArcRotateCamera", 1, 0.8, 30, new BABYLON.Vector3(0, 0, 0), scene);

// This targets the camera to scene origin
camera.setTarget(BABYLON.Vector3.Zero());

// This attaches the camera to the canvas
camera.attachControl(canvas, false);

// This creates a light, aiming 0,1,0 - to the sky.
const light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 1), scene);

// Dim the light a small amount
light.intensity = .5;

// Let's try our built-in 'sphere' shape. Params: name, subdivisions, size, scene
const sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);

const materialSphere1 = new BABYLON.StandardMaterial("texture1", scene);
materialSphere1.alpha = 1;
materialSphere1.diffuseColor = mainColor;

sphere.material = materialSphere1;
sphere.position.y = 1;
// Move the sphere upward 1/2 its height

// Let's try our built-in 'ground' shape. Params: name, width, depth, subdivisions, scene
const ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);
fn = () => {
materialSphere1.diffuseColor = mainColor;
}
// Leave this function
return scene;

}; // End of createScene function
const scene = createScene();
engine.runRenderLoop(function () {
scene.render();
});
window.addEventListener("resize", function () {
engine.resize();
});

我觉得它很厉害。如您所见,我在 createScene 中定义了 fn,然后允许我以这种方式修改它,但我相信应该有更好的方法来实现它。我尝试在 createScene() 之外创建一个函数,该函数将获取颜色,然后将其用于 materialSphere1.diffuseColor,但这没有用。所以我的问题是:在 babylon.js 中是否有任何其他(更好的)方法来改变 Material 的颜色

最佳答案

为什么不在回调之外声明您的 Material ?也可以使用scene.materials来浏览素材

关于javascript - 在 babylon.js 中改变 Material 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45020212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com