Posted Refactor
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
寮曞瓙馃棧
鏈€杩戯紝蹇界劧鎯宠捣鏇惧湪 WebGL 鍩虹绯诲垪 鏂囩珷涓珛涓?flag锛氣€滃悗缁繕鎵撶畻鍑?銆奣hreeJS 婧愮爜鍓栨瀽銆?/span> 绯诲垪鈥濓紙鐗规剰缈诲嚭鍘熻瘽馃寶锛夛紝椤圭洰蹇欎簡涓€闃靛悗锛屼究鍐冲畾寮€濮嬪啓姝ょ郴鍒楋紝鏇存柊鍛ㄦ湡涓嶅浐瀹氾紝姣曠珶椤圭洰鎺掓湡鈥滃ぉ鏅撳緱鈥濄€傛绯诲垪涓庡叾璇存槸鍒嗕韩锛屽€掍笉濡傝鏄叡鍚屽涔犵舰浜嗐€備綔涓烘渶鍑哄悕鐨?WebGL 鐨勫紑婧愬簱锛?span class="mq-10">ThreeJS 宸插彈鍒颁簡骞挎硾璁ゅ彲鍜屽ソ璇勶紝鍚屾椂鍏惰嚜韬篃鍦ㄤ笉鏂洿鏂帮紝璁╁紑鍙戣€呯敤鐨勮垝蹇冩斁蹇冦€?/p>
璇村洖鑷繁锛屾湰鍒濆叆鍥惧舰瀛︿笘鐣岋紝浠呭嚟涓汉鍏磋叮鎾炵殑榧婚潚鑴歌偪锛屼絾涔熶箰鍦ㄥ叾涓€傝嚜璁や负鎶€鏈湰涓嶈鏈夎窛绂绘劅锛屼互鏈夎叮涓旈€氫織鏄撴噦鐨勬柟寮忎紶鎾妧鏈篃鏄竴浠惰叮浜嬨€備换鍑叾鍚圭殑澶╄姳涔卞潬锛屼綍绛夐珮澶т笂锛屼絾鍏舵湰璐ㄦ槸鏈嶅姟浜庢垜浠殑锛屽綊鏍圭粨搴曡繕鏄鎴戜滑鎺ュ彈鎵嶅彲銆傛晠涔嬪墠鐨勬枃绔犱篃灏介噺淇濇寔鐫€鍩虹銆佹湁瓒c€佹槗鎳傜殑椋庢牸锛堝伓灏斾細寮€寮€杞︷煔楋級锛屾湭鏉ヤ害浼氬皾璇曟洿澶氬厓鐨勬枃绔犻鏍笺€備絾鍏跺畻鏃ㄩ兘鏄负浜嗚鍚勪綅鏇村鏄撴帴鍙椼€?/p>
浠ヤ笂浠呬釜浜鸿鐐癸紝濡備笌鍚勪綅瑙傚康鏈夌浉鎮栦箣澶勶紝鍙浉浜掍氦娴侊紝浠ュ郊涔嬮暱琛ュ繁涔嬬煭銆?/p>
涓嬮潰姝e紡寮€濮?ThreeJS 婧愮爜鍓栨瀽 绯诲垪馃コ
鍓嶈█馃搾
褰撳涔犱竴绉嶆柊鎶€鏈椂锛屽ぇ閮ㄥ垎鎯呭喌閮戒細棣栭€夊叾瀹樻柟鏂囨。锛岃€屾枃妗d腑鑳芥渶蹇鎴戜滑涓婃墜鐨勭珷鑺備究鏄?Getting Started銆?span class="mq-21">ThreeJS 涔熶笉渚嬪锛屾垜浠繘鍏ュ埌鍏跺搴旂殑 Getting Started 椤甸潰鍚庯紝渚胯兘鐪嬪埌 Creating the scene 鐨勪唬鐮佷粎鏈変笅闈㈠瀵ュ嚑琛岋細
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
鍐嶆粴鍔ㄨ嚦鏈€涓嬫柟锛屽悇浣嶄細鐪嬪埌瀹屾暣鐨?Demo锛?/p>
// ...
var animate = function () {
requestAnimationFrame( animate );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render( scene, camera );
};
animate();
闃呰瀹岀畝鍗曠殑鍗佸嚑琛屼唬鐮佷箣鍚庯紝鍚勪綅鍙兘浼氬彂鐜拌繖涓?Demo 涓湪涓€鐩磋皟鐢?animate()
鏂规硶锛岃€屾鏂规硶涓垜浠慨鏀逛簡绔嬫柟浣撶殑鏃嬭浆瑙掑害锛屽苟鎵ц浜嗚繖涓€琛屼唬鐮侊細renderer.render( scene, camera )
銆傚亣濡備綘娌℃湁鎺ヨЕ杩囧浘褰㈠鎴?WebGL锛屼粎鍑涔変細缈昏瘧鎴愶細鈥滄覆鏌撳櫒娓叉煋鍦烘櫙鍜屾憚鍍忔満鈥濄€傛病閿欙紝杩欎竴琛岀殑浣滅敤姝f槸濡傛锛?/p>
鈥滄垜鐪嬩綘楠ㄩ鎯婂锛岀悊瑙e姏闈炲嚒锛屼竴瀹氭槸涓鍥惧舰瀛︾殑澶╂墠鈥濓紙杩欎篃浣撶幇浜嗗懡鍚嶈鑼冨悎鐞嗘槸澶氫箞閲嶈锛夝煒?/p>
鍋囧鎴戜滑灏嗕笂闈㈤偅鍙ヨ瘽鎷嗚В涓€涓嬶細鈥滄覆鏌撳櫒 - 娓叉煋 - 鍦烘櫙鍜屾憚鍍忔満鈥濓紝杩欏彞璇濇绗﹀悎浜嗘垜浠殑 鈥滀富 - 璋?- 瀹锯€濈殑缁撴瀯銆傞偅涔堝氨鍙涓昏锛堝嵆娓叉煋鍣級鏄釜寰堥噸瑕佺殑瀛樺湪锛佹墍浠ユ绯诲垪鐨勭涓€绡囨枃绔犳垜浠氨浠庢覆鏌撳櫒 鈥斺€?Renderer 璇磋捣銆?/p>
Renderer鈿欙笍
灏?ThreeJS 鏂囨。鐩綍婊氬姩鑷?Renderers 涓€鏍忥紝鍙互鐪嬪埌鏈?WebGLMultisampleRenderTarget 銆?span class="mq-68">WebGLRenderer 銆?span class="mq-69">WebGL1Renderer 銆?span class="mq-70">WebGLRenderTarget 鍜?WebGLCubeRenderTarget 浜旂被娓叉煋鍣紝鑰屾垜浠氨鑱氱劍浜?WebGLRenderer 杩欎釜娓叉煋鍣ㄥ嵆鍙紝绋嶅悗浼氱粰澶у浠嬬粛涓€涓嬩负浣曚細鏈?WebGL1Renderer锛岃嚦浜庡叾浠栦笁绉嶆覆鏌撳櫒锛屾垜浠氨涓嶅厛璁ㄨ锛堝叾瀹炴垜涔熸病鐢ㄨ繃锛屽悗缁簡瑙d簡鍜变滑鍐嶈亰锛夝煠?/p>
WebGL1Renderer
鍋囧浣犲 WebGL 绋嶆湁浜嗚В锛岄偅涔堝氨浼氱煡閬撳叾瀹?WebGL 鏈?1.0 鍜?2.0 涓や釜鐗堟湰锛岄偅涔堜袱涓増鏈垎鍒槸鍩轰簬 OpenGL ES 2.0 鍜?OpenGL ES 3.0 鐨勶紝鑷充簬涓嶅熀浜?OpenGL ES 1.0 鏄洜涓?1.0 鏄浐瀹氱绾跨殑锛?span class="mq-88">2.0 鍜?3.0 鏄彲缂栫▼绠$嚎銆傛墍璋撳浐瀹氱绾垮氨鏄垜浠粰绠$嚎閰嶇疆鐩稿簲鍙傛暟鍜屽紑鍏冲嵆鍙紝鍙紪绋嬬绾跨殑鍙紪绋嬮儴鍒嗗氨鏄垜浠墍鐔熺煡鐨?椤剁偣鐫€鑹插櫒 鍜?鐗囧厓鐫€鑹插櫒銆?/p>
鎵繙浜嗭紝鍐嶈鍥?WebGL锛岃繖淇╃増鏈湁浠€涔堝樊鍒憿锛熷ぇ瀹跺彲浠ョ湅鏈枃 WebGL What's New锛岀畝鍗曟鎷竴涓嬪氨鏄浜嗘洿澶氱汗鐞嗘牸寮忋€佸唴缃嚱鏁般€?D 绾圭悊璐村浘锛屽悓鏃惰繕鏀寔浜嗛潪2鐨勬暣鏁版鏂瑰ぇ灏忕殑鍥剧墖銆傚悓鏃讹紝WebGL 2.0 涓?WebGL 1.0 鍦ㄥ娴忚鍣ㄧ殑鍏煎鎬т笂鏈夊緢澶х殑宸紓锛屼互 Chrome 涓轰緥锛?span class="mq-97">WebGL 1.0 鍏煎 9 鍙婁互涓婄殑 Chrome 鐗堟湰锛岃€?WebGL 2.0 鍒欏吋瀹?56 鍙婁互涓婄殑 Chrome 鐗堟湰锛屽娴忚鍣ㄧ殑鍏煎鎬х殑宸ㄥぇ宸紓灏辨湁鏋佸ぇ鐨勫彲鑳借涓€浜涢檲鏃х殑 WebGL 1.0 鐨勭郴缁熷穿婧冿紝鏁?ThreeJS 鎻愪緵浜?WebGL1Renderer 鏉ヨ繘琛岄€傞厤鍏煎銆?span class="mq-106">ThreeJS 宸茬粡浠?r118 鐗堟湰灏卞叏闈㈠崌绾у埌 WebGL 2.0 浜嗭紝鎵€浠ュ鏋滀綘鐨勭郴缁熶娇鐢ㄧ殑 ThreeJS 鐗堟湰浣庝簬 r118锛屽苟涓斿噯澶囧崌绾у埌鏈€鏂扮殑 r120 鐗堟湰锛屼负浜嗛伩鍏嶇▼搴忔寕鎺変綘闇€瑕佸皢 Renderer 鏇挎崲鎴?WebGL1Renderer锛佸浣曟煡鐪嬫渶鏂?ThreeJS 鐗堟湰璇疯涓嬪浘锛?/p>
鑰屾煡鐪嬪綋鍓嶉」鐩娇鐢ㄧ殑 ThreeJS 鐗堟湰璇疯 package.json 涓殑 ThreeJS 渚濊禆鐗堟湰鐨?MINOR 鐗堟湰鍙峰嵆鍙紝姣斿锛?span class="mq-121">three: 0.120.0 灏辫〃鏄庢槸 r120 鐗堟湰锛?span class="mq-123">npm 璇箟鍖栫増鏈瑙佸畼鏂规枃妗c€?/p>
WebGLRenderer
璇村洖鎴戜滑鐨勪富瑙?WebGLRenderer锛岃繘鍏ュ埌婧愮爜锛?code class="mq-131">src/renderers/WebGLRenderer.js锛夊悗鍙互鐪嬪埌澶撮儴鏈夊緢澶氬弬鏁板垵濮嬪寲鐨勬搷浣滐紝鍦ㄦ灏变笉涓€涓€浠嬬粛锛屽叧浜庡弬鏁板惈涔夊ぇ瀹跺彲鍙傝€冩枃妗o細浜嗚В鍙傛暟鍚箟璇烽槄璇?Constructor 閮ㄥ垎銆傚叾涓渶瑕佹彁涓€涓嬬殑鏄紝澶у鍙兘浼氬彂鐜拌繖鏉¤鍙ワ細
const _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
杩欓噷浣跨敤鐨勬槸 createElementNS
骞堕潪 createElement
锛屼簩鑰呭尯鍒強鐢ㄩ€旇闃呰 Document.createElementNS: What's the difference and why we need it銆?/p>
getContext
褰撴垜浠?new 涓€涓?Renderer 鏃讹紝鍏跺唴閮ㄤ細鏈€鍏堣繘鍏ヨ繖涓嬮潰鐨勮鍙ワ細
if ( _gl === null ) {
const contextNames = [ 'webgl2', 'webgl', 'experimental-webgl' ];
if ( _this.isWebGL1Renderer === true ) {
contextNames.shift();
}
_gl = getContext( contextNames, contextAttributes );
if ( _gl === null ) {
if ( getContext( contextNames ) ) {
throw new Error( 'Error creating WebGL context with your selected attributes.' );
} else {
throw new Error( 'Error creating WebGL context.' );
}
}
}
棣栧厛褰?WebGL 涓婁笅鏂囦笉瀛樺湪鏃讹紝Renderer 鍐呴儴鍒楀嚭浜嗘祻瑙堝櫒鎵€鎻愪緵鐨勬墍鏈?WebGL 涓婁笅鏂囧悕绉帮紝濡傝嫢鏄?WebGL1Renderer锛屽垯鍒犲幓 WebGL 2.0 鐨勪笂涓嬫枃鍚嶇О锛岀劧鍚庤皟鐢?getContext
鏂规硶鑾峰彇涓婁笅鏂囥€?code class="mq-181">getContext 鏂规硶瀹炵幇涔熷崄鍒嗙畝鍗曪細
function getContext( contextNames, contextAttributes ) {
for ( let i = 0; i < contextNames.length; i ++ ) {
const contextName = contextNames[ i ];
const context = _canvas.getContext( contextName, contextAttributes );
if ( context !== null ) return context;
}
return null;
}
閬嶅巻涓婃柟鍒椾妇鐨勪笂涓嬫枃鍚嶇О锛屽鏋滆幏鍙栦笂涓嬫枃鍒欒繑鍥烇紝鍚﹀垯杩斿洖 null
锛岃繖涔堢畝鍗曠殑浠g爜鑱槑鐨勪綘涔熻兘鍐欏嚭鏉ヤ綘锛屽洓鑸嶄簲鍏ヤ綘鍜?ThreeJS 璐$尞鑰呯殑姘村钩灏辨槸涓€鏍风殑馃寶
initContext
褰撹幏鍙栧埌涓婁笅鏂囧悗锛岀揣鎺ョ潃璋冪敤鐨勫氨鏄?initGLContext
鏂规硶浜嗭細
function initGLContext() {
extensions = new WebGLExtensions( _gl );
capabilities = new WebGLCapabilities( _gl, extensions, parameters );
// 鑻ヤ笉鏄?nbsp;WebGL 2.0锛屽垯闇€棰濆鑾峰彇浠ヤ笅 extensions
if ( capabilities.isWebGL2 === false ) {
// ...
}
extensions.get( 'OES_texture_float_linear' );
utils = new WebGLUtils( _gl, extensions, capabilities );
state = new WebGLState( _gl, extensions, capabilities );
state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor() );
state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor() );
// ...
}
鐢ㄥ娓哥殑璇濇潵璁诧細鈥滈鍏堟槧鍏ユ垜浠溂甯樼殑鏄? 绱ч殢鍏跺悗鐨勫氨鏄? 璋冪敤瀹? WebXR 鏄竴缁勬敮鎸佸皢娓叉煋3D鍦烘櫙鐢ㄦ潵鍛堢幇铏氭嫙涓栫晫锛堣櫄鎷熺幇瀹烇紝涔熺О浣?VR锛夋垨灏嗗浘褰㈠浘鍍忔坊鍔犲埌鐜板疄涓栫晫锛堝寮虹幇瀹烇紝涔熺О浣?AR锛夌殑鏍囧噯锛岃鎯呰浜嗚ВMDN鏂囨。銆?/p>
璋冪敤瀹? 褰撹皟鐢? 鑰? 杩欎箞蹇氨缁撴潫璇簡锛熶綘鏄笉鏄粰鎴戝伔宸ュ噺鏂欎簡锛侊紵 鍏跺疄 WebGLRenderer 澶х害 2000 琛屼唬鐮侊紝浣嗗叾涓粷澶ч儴鍒嗘槸鍑芥暟澹版槑渚涙垜浠悗缁殑浣跨敤銆傚洖椤炬湰鏂囷紝澶у灏变細鍙戠幇褰撴垜浠?new 涓€涓?Renderer 鏃?ThreeJS 鍙笉杩囨槸甯垜浠垵濮嬪寲浜? 鏃㈢劧鍐呴儴鏈夎繖涔堝鐨勫嚱鏁拌鎴戜滑鍚庣画浣跨敤锛岄偅涔堜笅绡囨枃绔犲氨鏉ヨ璁叉垜浠父鐢ㄧ殑 Renderer 涓殑鏂规硶锛?/p>
鎰熻阿闃呰锛?/p>
new WebGLExtensions
鍜?new WebGLCapabilities
涓ゆ潯璇彞鈥濓紝WebGLExtensions
鐨勪綔鐢ㄦ槸鍒ゆ柇鏌愪釜 extension
鏄惁瀛樺湪锛屼互鍙婅幏鍙栨寚瀹氱殑 extension
骞?鍏ㄩ噺杩斿洖锛岃€?WebGLCapabilities
鍒欐槸鑾峰彇褰撳墠 WebGL 绯荤粺鐨勪竴浜涘睘鎬э紝濡傦細鏀寔鐨勬渶澶х簿搴︺€佹槸鍚︽槸 WebGL 2.0 浠ュ強鎵€鏀寔鐨勬渶澶ц创鍥惧ぇ灏忕瓑銆傝€屼笅闈㈢殑 WebGLUtils
涓彧鏈変竴涓?convert
鏂规硶锛岃鏂规硶涓昏鏄?灏嗚嚜瀹氫箟鐨勭被鍨嬭浆鎹㈡垚 WebGL 鍐呯疆鐨勭被鍨?/span>銆?/p>
WebGLState
锛屽埆鐪嬪畠鍚嶅瓧寰堢煭锛屼絾鏄畠鍗村緢閲嶈锛佺敤鎻忚堪钄℃€伙紙鎴戜滑椤圭洰缁勭殑缈樻锛夌殑涓€鍙ヨ瘽灏辨槸 鈥滅煭锛屼絾鏈夌敤锛佲€?/span> 閫氳繃杩欎釜 state锛屾垜浠彲浠ヨ缃鍙eぇ灏忋€佽缃贩鍚堛€佽缃潗璐ㄣ€佺粦瀹氱汗鐞嗙瓑銆傚叾浣欑殑鍐呭灏卞厛鐪佺暐锛屽悗缁湁蹇呰鍐嶈锛屽厛璁╁悇浣嶇煡閬?Renderer 鍐呴儴鐨勫伐浣滄祦绋嬨€?/p>
initGLContext
鏂规硶鍚庡氨鏈変竴鍙ワ細const xr = new WebXRManager( _this, _gl );
animation
initContext
鍚庯紝灏辨潵鍒颁簡 animation
鐜妭锛?/p>
const animation = new WebGLAnimation();
animation.setAnimationLoop( onAnimationFrame );
if ( typeof window !== 'undefined' ) animation.setContext( window );animation
鐨勮皟鐢ㄦ洿鏄彧鏈夎繖绠€鍗曠殑 3 琛岃鍙ワ紝WebGLAnimation
鐨勭被鍨嬪涓嬶細class WebGLAnimation {
start(): void;
stop(): void;
setAnimationLoop( callback: Function ): void;
setContext( value: Window ): void;
}setAnimationLoop
鏃讹紝鍏跺疄灏辨槸灏嗕紶鍏ョ殑 callback
璧嬬粰 WenGLAnimation
鍐呴儴鐨?animationLoop
鍙橀噺锛屽綋璋冪敤 start
鏂规硶鏃讹紝灏辫皟鐢?window
鐨?requestAnimationFrame
鏂规硶銆傛墍浠?ThreeJS 鍐呴儴鐨勫姩鐢讳篃鏄笉鏂皟鐢?requestAnimationFrame
鏉ュ疄鐜扮殑锛屽懙锛佸嚒浜猴紒setContext
鍒欐槸灏?context
璁句负 window
锛岃嚦浜?stop
鏂规硶澶у搴旇涔熻兘鐚滃埌閲岄潰濡備綍鍋滄鍔ㄧ敾鐨勪簡鍚э紒缁撴潫璇煄?/span>
context
锛屽苟璁剧疆浜嗕竴涓?animation
锛屽叾浣欑殑鍑芥暟閮芥槸鍚庣画鎵嶄細鐢ㄥ埌銆傛晠鍒濆鍖栦竴涓?Renderer 涔熸病浠€涔堟晠寮勭巹铏氱殑锛?/p>
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章