我对three.js很新,所以试着在这里忍受我。因此,我被分配了一个项目,涉及使用WebGL创建一个车身并在mousedown上凹陷。我试图将凹痕的颜色从车身颜色改为黄色。知道怎么做吗?注意:我只想改变凹痕的颜色,而不是整车。
这里是凹痕材料的代码。我的第一个想法是为着色器材质添加颜色,但这不起作用,所以我对它进行了评论。
var dentMaterial = new THREE.ShaderMaterial({
uniforms : {
size : { type: 'f', value : 20.0 },
near : { type: 'f', value : camera.near },
far : { type: 'f', value : camera.far }
//color: { type: 'v3', value: { x: 1, y: 1, z: 1 } }
// map : { type: "t", value : THREE.ImageUtils.loadTexture( 'textures/heatmap.jpg' ) } //here
},
attributes : {},
vertexColors: THREE.VertexColors,
vertexShader: vertShader,
fragmentShader: fragShader,
side: THREE.DoubleSide,
transparent: true
});
此代码是实际创建凹痕的地方。
THREE.DentModifier = function () {};
THREE.DentModifier.prototype = {
constructor: THREE.DentModifier,
set: function ( origin, direction, radius, depth, material ) {
this.origin = origin; // vec3
this.direction = direction; // vec3
this.radius = radius; // float
this.depth = depth; // float
//this.material = material // GLmaterial
return this;
},
magnitude: function(vector) {
return vector.x * vector.x + vector.y * vector.y + vector.z * vector.z;
},
linearFalloff: function (distance, radius) {
return this.clamp01(1 - distance / radius);
},
gaussFalloff:function (distance, radius) {
return this.clamp01(Math.pow(360, Math.pow(distance / radius, 2.5) - .01));
},
needleFalloff:function (distance, radius) {
return -(distance * distance / (radius * radius) + 1);
},
clamp01: function(val) {
if (val < 0) return 0;
if(val > 1) return 1;
return val;
},
modify: function ( mesh , material ) {
this.mesh = mesh;
var matrix = new THREE.Matrix4().getInverse( this.mesh.matrixWorld );
var origin = this.origin.applyMatrix4( matrix );
var normal = new THREE.Vector3();
normal.copy( this.direction );
normal.multiplyScalar( -this.radius * ( 1 - this.depth ) );
var centerSphere = new THREE.Vector3();
centerSphere.addVectors( origin, normal );
var sphere = new THREE.Sphere( centerSphere, this.radius );
this.mesh.geometry.elementsNeedUpdate = false;
var sqrRadius = 100; //this.radius*this.radius;
var sqrMagnitude; //float
for ( var i = 0, il = this.mesh.geometry.vertices.length; i < il; i++ ) {
if ( centerSphere.distanceTo( this.mesh.geometry.vertices[ i ] ) < this.radius ) {
// new - Limit depth of dent
sqrMagnitude = this.magnitude(this.mesh.geometry.vertices[i]) - this.magnitude(centerSphere);
if (sqrMagnitude > sqrRadius) {
console.log("We are too deep Scotty !!");
break;
} // end new
var ray = new THREE.Ray( this.mesh.geometry.vertices[ i ], this.direction );
var dent = ray.intersectSphere( sphere );
this.mesh.geometry.vertices[ i ] = dent;
}
};
答案 0 :(得分:2)
dentMaterial.uniforms.diffuse = { type: "c", value: { r:1, g:0, b:0 } };
或者当使用默认的 phongShader制服时,这可行:
dentMaterial.uniforms.diffuse.value.setHex ( 0xFF0000 );
Three.js r.71