该类用来表示空间中的“射线”,主要用来进行碰撞检测。
THREE.Ray = function ( origin, direction ) { this.origin = ( origin !== undefined ) ? origin : new THREE.Vector3(); this.direction = ( direction !== undefined ) ? direction : new THREE.Vector3(); };
Ray类的构造函数颇为简单,只有两个参数origin和direction,顾名思义,也就是端点和方向。
Ray类的主要作用是进行碰撞检测。
THREE.Ray.prototype = { constructor: THREE.Ray, set: function ( origin, direction ) {...}, copy: function ( ray ) {...}, at: function( t, optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); return result.copy( this.direction ).multiplyScalar( t ).add( this.origin ); }, recast: function ( t ) { this.origin.copy( this.at( t, THREE.Ray.__v1 ) ); return this; }, closestPointToPoint: function ( point, optionalTarget ) { var result = optionalTarget || new THREE.Vector3(); result.subVectors( point, this.origin ); var directionDistance = result.dot( this.direction ); return result.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin ); }, distanceToPoint: function ( point ) { var directionDistance = THREE.Ray.__v1.subVectors( point, this.origin ).dot( this.direction ); THREE.Ray.__v1.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin ); return THREE.Ray.__v1.distanceTo( point ); }, isIntersectionSphere: function( sphere ) { return ( this.distanceToPoint( sphere.center ) <= sphere.radius ); }, isIntersectionPlane: function ( plane ) { // check if the line and plane are non-perpendicular, if they // eventually they will intersect. var denominator = plane.normal.dot( this.direction ); if ( denominator != 0 ) { return true; } // line is coplanar, return origin if( plane.distanceToPoint( this.origin ) == 0 ) { return true; } return false; }, distanceToPlane: function ( plane ) { var denominator = plane.normal.dot( this.direction ); if ( denominator == 0 ) { // line is coplanar, return origin if( plane.distanceToPoint( this.origin ) == 0 ) { return 0; } // Unsure if this is the correct method to handle this case. return undefined; } var t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator; return t; }, intersectPlane: function ( plane, optionalTarget ) { var t = this.distanceToPlane( plane ); if( t === undefined ) { return undefined; } return this.at( t, optionalTarget ); }, transform: function ( matrix4 ) {...}, equals: function ( ray ) {...}, clone: function () {...} };
Ray类有这样一些方法:
之前我们分析过Material类和LineBasicMaterial类,现在我们来看剩下的几个材质类。MeshBasicMaterial是一种“光照无关”的材质,也就是说,在没有光照的情况下,材质依然能够要显示出来。
THREE.MeshBasicMaterial = function ( parameters ) { THREE.Material.call( this ); this.color = new THREE.Color( 0xffffff ); // emissive this.map = null; this.lightMap = null; this.specularMap = null; this.envMap = null; this.combine = THREE.MultiplyOperation; this.reflectivity = 1; this.refractionRatio = 0.98; this.fog = true; this.shading = THREE.SmoothShading; this.wireframe = false; this.wireframeLinewidth = 1; this.wireframeLinecap = 'round'; this.wireframeLinejoin = 'round'; this.vertexColors = THREE.NoColors; this.skinning = false; this.morphTargets = false; this.setValues( parameters ); };
MeshBasicMaterial包括这样一些属性:
MeshLambertMaterial是一种朗伯面材质。朗伯面就是各向反射同性面,任何角度的光线照射上去,反射的亮度和反射角度无关。以下摘录了除掉与MeshBasicMaterial重复的属性剩下的若干个属性。
THREE.MeshLambertMaterial = function ( parameters ) { THREE.Material.call( this ); this.color = new THREE.Color( 0xffffff ); // diffuse this.ambient = new THREE.Color( 0xffffff ); this.emissive = new THREE.Color( 0x000000 ); this.wrapAround = false; this.wrapRGB = new THREE.Vector3( 1, 1, 1 ); this.combine = THREE.MultiplyOperation; this.reflectivity = 1; this.refractionRatio = 0.98; ... this.setValues( parameters ); };
除了MeshBasicMaterial中的color,vertexColors等属性,MeshLambertMaterial类还具有几个跟光照相关的属性:
MeshPhongMaterial,旁氏反射面,表示有光泽的物体,在极端情况下就是镜面。
THREE.MeshPhongMaterial = function ( parameters ) { THREE.Material.call( this ); this.color = new THREE.Color( 0xffffff ); // diffuse this.ambient = new THREE.Color( 0xffffff ); this.emissive = new THREE.Color( 0x000000 ); this.specular = new THREE.Color( 0x111111 ); this.shininess = 30; this.metal = false; this.perPixel = true;
this.bumpMap = null; this.bumpScale = 1; this.normalMap = null; this.normalScale = new THREE.Vector2( 1, 1 ); ...this.setValues( parameters ); };
暂时还没大弄明白。
允许为某个geometry的每个面单独指定材质,通常用于从三维模型中读取数据,然后构造mesh。
THREE.MeshFaceMaterial = function ( materials ) { this.materials = materials instanceof Array ? materials : []; };
只需要传入一个数组作为参数,其materials数组的每一个元素都是某种MeshXXXXMaterial,然后再geometry中创建表面时,为face指定materialIndex即可。