Вопрос

How to set a color with dat.gui in one object the Three js

I wanna choose the color with the dialog some like Box 3 in this example

How to make this?

Это было полезно?

Решение

I make the following function to change the color with a chooser color

function cambiarColor( obj )
{
    var gui = new dat.GUI();


    var Configuracion=function(){
            this.color = "#ffae23";
    }
    var conf = new Configuracion();

    var controlador = gui.addColor( conf, 'color');
    controlador.onChange( function( colorValue  )
    {
      //the return value by the chooser is like as: #ffff so
      //remove the # and replace by 0x
      colorValue=colorValue.replace( '#','0x' );
      //create a Color
      var colorObject = new THREE.Color( colorValue ) ;
      //set the color in the object
      obj.material.color = colorObject;
    });
}

Другие советы

A simple way to implement Three.js colors or uniform colors to dat.gui :

dat.GUI.prototype.addThreeColor=function(obj,varName){
    // threejs & dat.gui have color incompatible formats so we use a dummy data as target :
    var dummy={};
    // set dummy initial value :
    dummy[varName]=obj[varName].getStyle(); 
    return this.addColor(dummy,varName)
        .onChange(function( colorValue  ){
            //set color from result :
            obj[varName].setStyle(colorValue);
        });
};
dat.GUI.prototype.addThreeUniformColor=function(material,uniformName,label){
    return this.addThreeColor(material.uniforms[uniformName],"value").name(label||uniformName);
};

A simplified version without extra objects:

var gui = new dat.GUI();

var conf = { color : '#ffae23' };    
gui.addColor(conf, 'color').onChange( function(colorValue) {
    obj.material.color.set(colorValue);
});

There's no need to make a "new Three.Color". I'm not sure if that's even a thing. Try the code below. It worked for me.

    function cambiarColor( obj )
{
    var gui = new dat.GUI();


var Configuracion=function(){
        this.color = "#ffae23";
}
    var conf = new Configuracion();

    var controlador = gui.addColor( conf, 'color');
    controlador.onChange( function( colorValue  )
    {
      //the return value by the chooser is like as: #ffff so
      //remove the # and replace by 0x
      colorValue=colorValue.replace( '#','0x' );
      //set the color in the object
      obj.material.color.setHex(colorValue);
    });
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top