如何使用单选按钮改变对象的材质
How to change a object's material with radio buttons?
我想根据单选按钮选择更改立方体的材质。在我当前的例子中,颜色变化是有效的。不幸的是,它没有更新整个材料。我使用一个叫做tweenjs的渐变插件来制作平滑的动画。
我怎样才能捕捉整个MeshPhongMaterial而不仅仅是颜色?
单选按钮<div id="inputs">
<input id="black" type="radio" data-color="black" name="color" value="" checked>
<input id="white" type="radio" data-color="white" name="color" value="">
<input id="red" type="radio" data-color="red" name="color" value="">
</div>
jQuery用于单选按钮连接
$("#inputs input").change(function(event){
if ($(this.checked)) {
var targetColor = $(this).data("color");
colorTo("box", color[targetColor]);
}
});
THREE.JS
var colorMap = THREE.ImageUtils.loadTexture( 'textures/color.jpg' );
colorMap.anisotropy = 16;
colorMap.wrapS = colorMap.wrapT = THREE.RepeatWrapping;
colorMap.repeat.set( 5, 5 );
var color = {
"black": new THREE.MeshPhongMaterial({
color: 0x222222, map: colorMap, combine: THREE.MixOperation
}),
"white": new THREE.MeshPhongMaterial({
color: 0xffffff, map: colorMap, combine: THREE.MixOperation
}),
"red": new THREE.MeshPhongMaterial({
color: 0xf80000, emissive: 0x222222, specular: 0x222222, map: colorMap, combine: THREE.MixOperation
})
}
var renderer, scene, camera;
init();
animate();
function init() {
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
camera = new THREE.PerspectiveCamera( 60, WIDTH / HEIGHT, 1, 300 );
camera.position.z = 10;
scene = new THREE.Scene();
var geometry= new THREE.BoxGeometry(2,2,2);
var mesh = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({ color: 0x222222}));
mesh.name = "box";
scene.add(mesh );
renderer = new THREE.WebGLRenderer({ antialias: true }); //Create WebGL context
renderer.setClearColor( 0xffffff ); //Set background
renderer.setPixelRatio( window.devicePixelRatio ); //Set pixel aspect ratio
renderer.setSize( WIDTH, HEIGHT ); //Set viewport size
document.body.appendChild( renderer.domElement ); //Append renderer object to the DOM
}
function animate() { //Animation draw loop
requestAnimationFrame(animate); //Request animation frame (graphics performant setTimeout equivalent)
TWEEN.update(); //Update tweens in progress
renderer.render(scene, camera); //Render the updates
}
function colorTo (target, value){
var target = scene.getObjectByName(target);
var initial = new THREE.Color(target.material.color.getHex());
var value = new THREE.Color(value.color.getHex());
var tween = new TWEEN.Tween(initial).to(value, 1000)
.easing(TWEEN.Easing.Cubic.InOut)
.onUpdate(function(){
target.material.color = initial;
})
.start();
}
http://jsfiddle.net/489k6s3g/当你在一个表面上切换材质时,你需要告诉THREE相应地更新其内部数据。所以当你切换时,也要设置model.material.needsUpdate = true;
相关提示,请查看https://github.com/mrdoob/three.js/wiki/Updates
相关文章:
- 改变<svg>对象's路径
- 为什么日期对象的时间没有改变?我怎样才能改变它
- 为什么第二个对象的值没有改变
- 触发标准“;改变“;表单对象上的事件-由其上的任何侦听器拾取
- Javascript排序的数组会改变窗口对象,但在遍历它时不会改变
- 为什么函数中改变的对象会改变它们在内存中指向的内容,但有时会创建一个新的内存对象
- 如何将场景中的对象添加到网格中而不改变其在three.js中的位置
- 如何上传JSON文件在Javascript运行时创建对象,而不改变服务器端脚本
- 基于查看区域,使用javascript动态改变html对象中嵌入flash文件的大小
- 如何改变对象's html
- js -如何动态改变对象的不透明度
- 我可以动态地改变'likeable'对象用于Facebook的Like按钮
- 改变JavaScript对象定义样式会导致画布保持空白
- 为什么我的画布对象在setInterval()被调用后不改变颜色?
- 如何使D3js对象改变它的属性,鼠标移动到对象附近
- 为什么我的innerHTML对象没有改变时分配
- 对象的属性在改变时激活功能
- 如何用JSONLoader改变在js中加载的对象的颜色
- 如果我们创建新的原型属性,对象会改变它的隐藏类吗?
- 当引用对象改变时,react .js视图不更新