標籤:three.js 圖形 webgl 三維 資料視覺效果
商域無疆 (http://blog.csdn.net/omni360/)
本文遵循“署名-非商業用途-保持一致”創作公用協議
轉載請保留此句:商域無疆 - 本部落格專註於 敏捷開發及移動和物聯裝置研究:資料視覺效果、GOLANG、Html5、WEBGL、THREE.JS,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。
俺也是剛開始學,好多地兒肯定不對還請見諒.
以下代碼是THREE.JS 源碼檔案中objects/MorphAnimMesh.js檔案的注釋.
更多更新在 : https://github.com/omni360/three.js.sourcecode
/** * @author alteredq / http://alteredqualia.com/ *//*///MorphAnimMesh對象,最終的網格對象,和Mesh對象不同的是,這個對象是專門針對變形動畫的,增加了許多角色變形動畫的內容.有高人把圖形學建模比作是製作燈籠,先用Geometry建立燈籠的架構,/// 然後將材質material貼在架構上,最後形成的總體燈籠,就是Mesh對象.下面看一下Mesh對象的用法和具體實現./// 用法:var geometry = new THREE.Geometry(1,1,1);//建立geometry對象(燈籠的架構),/////有一下可選對象BoxGeometry,CircleGeometry,CubeGeometry,CylinderGeometry,DodecahedronGeometry,ExtrudeGeometry,IcosahedronGeometry,/////LatheGeometry,OctahedronGeometry,ParametricGeometry,PlaneGeometry,PolyhedronGeometry,RingGeometry,ShapeGeometry,SphereGeometry,/////TetrahedronGeometry,TextGeometry,TorusGeometry,TorusKnotGeometry,TubeGeometry/// var material = new THREE.Material({color: 0xffff00});//建立材質對象(燈籠的表面)/////有以下可選對象LineBasicMaterial,LineDashedMaterial,Material,MeshBasicMaterial,MeshDepthMaterial,MeshFaceMaterial,/////MeshLambertMaterial,MeshNormalMaterial,MeshPhongMaterial,PointCloudMaterial,RawShaderMaterial,ShaderMaterial,/////SpriteCanvasMaterial,SpriteMaterial/// var mesh = new THREE.MorphAnimMesh(geometry, material);//建立mesh(燈籠)對象,並將geometry對象(燈籠的架構)和material對象(燈籠的表面)傳遞給mesh(燈籠)對象/// mesh.duration = 5000;/// scene.add(mesh); //將mesh(燈籠)添加到情境中./// var delta = clock.getDelta();/// function render (){///mesh.updateAnimation(1000 * delta);//在渲染器裡設定動畫跟新的頻率./// }*////<summary>MorphAnimMesh</summary>///<param name ="geometry" type="THREE.Geometry">Geometry對象(燈籠的架構)</param>///<param name ="material" type="THREE.Material">Material對象(材質對象)</param>///<returns type="MorphAnimMesh">返回MorphAnimMesh對象</returns>THREE.MorphAnimMesh = function ( geometry, material ) {THREE.Mesh.call( this, geometry, material );//調用Mesh對象的call方法,將原本屬於Mesh的方法交給當前對象MorphAniMesh來使用.// APIthis.duration = 1000; // milliseconds//周期,單位毫秒,預設初始化為1000毫秒,每幀間隔時間長度.this.mirroredLoop = false;//鏡像迴圈,看下面的演算法,應該是播放完後,回放動畫,this.time = 0;//動畫時間長度// internalsthis.lastKeyframe = 0;//最後主要畫面格this.currentKeyframe = 0;//當前主要畫面格this.direction = 1;//方向,應該指的是時間軸的方向this.directionBackwards = false;//時間軸是否是返方向,預設為falsethis.setFrameRange( 0, this.geometry.morphTargets.length - 1 );//建立主要畫面格動畫時間軸,從morphTargets數組建立.};/*****************************************************下面是MorphAnimMesh對象的方法屬性定義,繼承自Mesh**************************************************/THREE.MorphAnimMesh.prototype = Object.create( THREE.Mesh.prototype );/*///setFrameRange方法將建立主要畫面格動畫時間軸,從morphTargets數組建立*////<summary>setFrameRange</summary>///<param name ="start" type="int">morphTargets元素的索引,用來指定主要畫面格動畫的開始</param>///<param name ="end" type="int">morphTargets元素的索引,用來指定主要畫面格動畫的結束</param>///<returns type="MorphAnimMesh">包含主要畫面格動畫的MorphAnimMesh對象.</returns>THREE.MorphAnimMesh.prototype.setFrameRange = function ( start, end ) {this.startKeyframe = start;//開始幀等於參數startthis.endKeyframe = end;//結束幀等於參數endthis.length = this.endKeyframe - this.startKeyframe + 1;//動畫長度等於開始幀到結束幀};/*///setDirectionForward方法設定主要畫面格動畫正放.*////<summary>setDirectionForward</summary>THREE.MorphAnimMesh.prototype.setDirectionForward = function () {this.direction = 1;//this.directionBackwards = false;};/*///setDirectionForward方法設定主要畫面格動畫倒放.*////<summary>setDirectionForward</summary>THREE.MorphAnimMesh.prototype.setDirectionBackward = function () {this.direction = - 1;this.directionBackwards = true;};/*///setDirectionForward方法從morphTagets數組中解析主要畫面格動畫*////<summary>setDirectionForward</summary>THREE.MorphAnimMesh.prototype.parseAnimations = function () {var geometry = this.geometry;if ( ! geometry.animations ) geometry.animations = {};var firstAnimation, animations = geometry.animations;var pattern = /([a-z]+)_?(\d+)/;for ( var i = 0, il = geometry.morphTargets.length; i < il; i ++ ) {var morph = geometry.morphTargets[ i ];//獲得單個變形動畫主要畫面格var parts = morph.name.match( pattern );//if ( parts && parts.length > 1 ) {var label = parts[ 1 ];//var num = parts[ 2 ];if ( ! animations[ label ] ) animations[ label ] = { start: Infinity, end: - Infinity };var animation = animations[ label ];if ( i < animation.start ) animation.start = i;if ( i > animation.end ) animation.end = i;if ( ! firstAnimation ) firstAnimation = label;}}geometry.firstAnimation = firstAnimation;//第一個動畫.};/*///setAnimationLabel方法從morphTagets數組中設定主要畫面格動畫標籤,可以將morphTargets數組中,分成幾段動畫,分別存放.*////<summary>setAnimationLabel</summary>///<param name ="label" type="string">動畫標籤名</param>///<param name ="start" type="int">morphTargets元素的索引,用來指定主要畫面格動畫的開始</param>///<param name ="end" type="int">morphTargets元素的索引,用來指定主要畫面格動畫的結束</param>///<returns type="MorphAnimMesh">返回帶有動畫標籤的MorphAnimMesh對象.</returns>THREE.MorphAnimMesh.prototype.setAnimationLabel = function ( label, start, end ) {if ( ! this.geometry.animations ) this.geometry.animations = {};this.geometry.animations[ label ] = { start: start, end: end };};/*///playAnimation方法根據動畫的標籤名(參數lab)按照指定的速度(參數fps)播放動畫*////<summary>playAnimation</summary>///<param name ="label" type="string">動畫標籤名</param>///<param name ="fps" type="int">多少幀/秒</param>THREE.MorphAnimMesh.prototype.playAnimation = function ( label, fps ) {var animation = this.geometry.animations[ label ];if ( animation ) {this.setFrameRange( animation.start, animation.end );this.duration = 1000 * ( ( animation.end - animation.start ) / fps );this.time = 0;} else {console.warn( 'animation[' + label + '] undefined' );}};/*///updateAnimation方法根據當前時鐘頻率產生補間動畫.*////<summary>updateAnimation</summary>///<param name ="delta" type="string">時鐘頻率</param>THREE.MorphAnimMesh.prototype.updateAnimation = function ( delta ) {var frameTime = this.duration / this.length;this.time += this.direction * delta;if ( this.mirroredLoop ) {if ( this.time > this.duration || this.time < 0 ) {this.direction *= - 1;if ( this.time > this.duration ) {this.time = this.duration;this.directionBackwards = true;}if ( this.time < 0 ) {this.time = 0;this.directionBackwards = false;}}} else {this.time = this.time % this.duration;if ( this.time < 0 ) this.time += this.duration;}var keyframe = this.startKeyframe + THREE.Math.clamp( Math.floor( this.time / frameTime ), 0, this.length - 1 );if ( keyframe !== this.currentKeyframe ) {this.morphTargetInfluences[ this.lastKeyframe ] = 0;this.morphTargetInfluences[ this.currentKeyframe ] = 1;this.morphTargetInfluences[ keyframe ] = 0;this.lastKeyframe = this.currentKeyframe;this.currentKeyframe = keyframe;}var mix = ( this.time % frameTime ) / frameTime;if ( this.directionBackwards ) {mix = 1 - mix;}this.morphTargetInfluences[ this.currentKeyframe ] = mix;this.morphTargetInfluences[ this.lastKeyframe ] = 1 - mix;};/*///updateAnimation方法根據變形幅度t將morphTaInfluences[a]設定為1-t,morphTaInfluences[b]設定為5.*////<summary>updateAnimation</summary>///<param name ="a" type="int">節點a</param>///<param name ="b" type="int">節點b</param>///<param name ="t" type="float">變形幅度</param>THREE.MorphAnimMesh.prototype.interpolateTargets = function ( a, b, t ) {var influences = this.morphTargetInfluences;for ( var i = 0, l = influences.length; i < l; i ++ ) {influences[ i ] = 0;}if ( a > -1 ) influences[ a ] = 1 - t;if ( b > -1 ) influences[ b ] = t;};/*clone方法///clone方法複製一個MorphAnimMesh帶有變形動畫的網格對象.*////<summary>clone</summary>///<param name ="object" type="MorphAnimMesh">接收複製的MorphAnimMesh對象</param>///<param name ="recursive" type="boolean">是否對子物件一一進行複製</param>///<returns type="Ray">返回MorphAnimMesh帶有變形動畫的網格對象.</returns>THREE.MorphAnimMesh.prototype.clone = function ( object ) {if ( object === undefined ) object = new THREE.MorphAnimMesh( this.geometry, this.material );object.duration = this.duration;object.mirroredLoop = this.mirroredLoop;object.time = this.time;object.lastKeyframe = this.lastKeyframe;object.currentKeyframe = this.currentKeyframe;object.direction = this.direction;object.directionBackwards = this.directionBackwards;THREE.Mesh.prototype.clone.call( this, object );//繼承Mesh的clone方法return object;//返回MorphAnimMesh帶有變形動畫網格對象.};
商域無疆 (http://blog.csdn.net/omni360/)
本文遵循“署名-非商業用途-保持一致”創作公用協議
轉載請保留此句:商域無疆 - 本部落格專註於 敏捷開發及移動和物聯裝置研究:資料視覺效果、GOLANG、Html5、WEBGL、THREE.JS,否則,出自本部落格的文章拒絕轉載或再轉載,謝謝合作。
以下代碼是THREE.JS 源碼檔案中objects/MorphAnimMesh.js檔案的注釋.
更多更新在 : https://github.com/omni360/three.js.sourcecode
three.js 源碼注釋(六十五)objects/MorphAnimMesh.js