简体   繁体   English

使用gui.dat更改three.js网格的颜色

[英]Change color of three.js mesh using gui.dat

I have a three.js mesh loaded from an STL file: 我有一个从STL文件加载的three.js网格:

    var loader = new THREE.STLLoader();         
    var materialmodel = new THREE.MeshPhongMaterial( 
        { 
            color: 0xFF0000, 
            specular: 0x222222, 
            shininess: 75 
        } 
        );

    function model()    
        {

            loader.load( 'models/stl/binary/model.stl', function ( geometry ) {
                var meshMaterial = materialmodel;
                var model = new THREE.Mesh( geometry, meshMaterial );
                model.scale.set( 0.02, 0.02, 0.02 );        
                model.castShadow = true;
                model.receiveShadow = true;
                model.geometry.center();                            
                scene.add(model);
                render();
            } );
        }
    model();

When I call the model function in my page, the model renders as expected. 当我在页面中调用模型函数时,模型将按预期方式呈现。

I want to use dat.gui to as a lightweight interface for on the fly changes. 我想使用dat.gui作为动态更改的轻量级界面。

My first experiment is changing the color of the model. 我的第一个实验是更改模型的颜色。

The code I'm using is this: 我正在使用的代码是这样的:

            var params = {
                modelcolor: 0xff0000,  //RED
            };

            var gui = new dat.GUI();
            var folder = gui.addFolder( 'Model Colour' );
            folder.addColor( params, 'modelcolor' )
                .name('Model Color')
                .listen()
                .onChange( function() { materialmodel.MeshPhongMaterial.color.set( params.modelcolor); } );                  
            folder.open();

DAT.GUIs color picker appears fine, and I can select a color from the picker and the new hex value will display. DAT.GUIs颜色选择器看起来不错,我可以从选择器中选择一种颜色,然后将显示新的十六进制值。

However, the model/mesh itself doesn't update with the newly selected colour. 但是,模型/网格物体本身不会使用新选择的颜色进行更新。

I'm wondering if it's something to do with how I'm changing the color materialmodel.MeshPhongMaterial.color.set( params.modelcolor); 我想知道是否与更改颜色materialmodel.MeshPhongMaterial.color.set( params.modelcolor); (I've tried different ways of doing this with no luck). (我尝试过用其他方法来做到这一点,但是没有运气)。

I've seen a post here (one of the answers) where they're doing this using model.material.color.set(params.color) in their example. 我在这里看到了一个帖子(答案之一),他们在示例中使用model.material.color.set(params.color)进行了此操作。 My owen material properties are defined in a variable using a THREE.MeshPhongMaterial..... 我的材料的属性是使用THREE.MeshPhongMaterial .....在一个变量中定义的

Assuming this is where I've gone wrong, how can I change the color dynamically of a nested prroperty buried in a variable like this? 假设这是我出问题的地方,如何动态更改嵌套在这样的变量中的嵌套prroperty的颜色?

I didn't get why did you use .listen() , possibly there's a certain reason. 我不明白为什么要使用.listen() ,可能有一定原因。

In .onUpdate function you're using materialmodel , which is a material itself, and then you're setting .MeshPhongMaterial property that doesn't exist. .onUpdate功能你使用materialmodel ,这是材料本身,然后你设置.MeshPhongMaterial不存在财产。 Looks like you simply overlooked it. 看起来您只是忽略了它。

Here is a working example: 这是一个工作示例:

 var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 1, 1000); camera.position.set(0, 0, 10); var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); var light = new THREE.DirectionalLight(0xffffff, 0.5); light.position.setScalar(10); scene.add(light); scene.add(new THREE.AmbientLight(0xffffff, 0.5)); var materialmodel = new THREE.MeshPhongMaterial({ color: 0xFF0000, specular: 0x222222, shininess: 75 }); var geometrymodel = new THREE.SphereBufferGeometry(5, 32, 16); var model = new THREE.Mesh(geometrymodel, materialmodel); scene.add(model); var params = { modelcolor: "#ff0000" }; var gui = new dat.GUI(); var folder = gui.addFolder('Model Colour'); folder.addColor(params, 'modelcolor') .name('Model Color') .onChange(function() { materialmodel.color.set(params.modelcolor); }); folder.open(); render(); function render() { requestAnimationFrame(render); renderer.render(scene, camera); } 
 body { overflow: hidden; margin: 0; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/96/three.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.2/dat.gui.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM