簡體   English   中英

我如何重寫代碼來為我的球體分配隨機顏色?

[英]How do I rewrite the code to assign a random color to my sphere?

  function randomColor(){
    var randomcolor = new Color().setRGB(Math.random(), Math.random(), Math.random());
    return{ randomcolor }
  };
  randomColor();
  var geometrySphere = new SphereGeometry(10, 15);
  var materialSphere = new MeshStandardMaterial({
    color: randomColor,
    flatShading: true,
    wireframe: true,
  });
  var meshSphere = new Mesh(geometrySphere, materialSphere);
  scene.add(meshSphere);

如果我 controll.log 顏色 function 它會給我一種顏色,但不會將其分配給立方體。

您正在使用 function 本身作為顏色值,您應該使用返回值,並且Math.random()返回一個介於 0 和 1 之間的數字,因此顏色將始終為黑色。

下面的代碼應該有所幫助:

  function randomColor(){
    var randomcolor = new Color().setRGB(Math.random() * 255, Math.random() * 255, Math.random() * 255);
    return{ randomcolor }
  };
  var color = randomColor();
  var geometrySphere = new SphereGeometry(10, 15);
  var materialSphere = new MeshStandardMaterial({
    color: color,
    flatShading: true,
    wireframe: true,
  });
  var meshSphere = new Mesh(geometrySphere, materialSphere);
  scene.add(meshSphere);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM