欧美日韩精品一区二区在线线,一级无码在线收看,精品国产高清91,久久精品欧美电影

# 渲染紋理

相機(jī)一章中提到了渲染目標(biāo)renderTarget可以指定為渲染紋理,紋理也提到了渲染紋理可以作為紋理使用??梢?,渲染紋理RenderTexture就是連接多個(gè)相機(jī)渲染之間的橋梁。

一個(gè)典型場景就是用渲染紋理來做鏡子之類的效果。

# 創(chuàng)建渲染紋理

創(chuàng)建渲染紋理有兩種方式,一般我們會(huì)優(yōu)先在wxml中進(jìn)行創(chuàng)建。

# 在wxml中創(chuàng)建

xml中創(chuàng)建需要用到xr-asset-render-texture標(biāo)簽:

<xr-asset-render-texture asset-id="rt" width="2048" height="1024" />

其中rt是資源id,widthheight是渲染紋理的寬高。

# 手動(dòng)代碼創(chuàng)建

手動(dòng)代碼創(chuàng)建直接使用scene.createRenderTexture即可:

const rt = scene.createRenderTexture({width: 2048, height: 2048});

// 當(dāng)然,也可以添加到資源系統(tǒng)中
scene.assets.addAsset('render-texture', 'rt', rt);

# 使用渲染紋理

創(chuàng)建完渲染紋理后便可以使用它,下面是一個(gè)在xml中的簡單使用實(shí)例,用代碼使用也是類似的:

<xr-node layer="1">
  <xr-mesh id="cube" node-id="mesh-cube" position="-1 0.5 1.5" scale="1 1 1" rotation="0 45 0" geometry="cube" uniforms="u_baseColorFactor:0.298 0.764 0.85 1" />
  <xr-mesh node-id="mesh-sphere" position="0 1.25 0" scale="1.25 1.25 1.25" geometry="sphere" uniforms="u_baseColorFactor:0.937 0.176 0.368 1" />
  <xr-mesh node-id="mesh-cylinder" position="1 0.7 1.5" scale="1 0.7 1" geometry="cylinder" uniforms="u_baseColorFactor:1 0.776 0.364 1" />
</xr-node>

<xr-node layer="2">
  <xr-mesh node-id="mesh-plane" position="0 0 -6" rotation="-90 0 0" scale="16 1 12" geometry="plane" uniforms="u_baseColorMap:render-rt" />
</xr-node>

<xr-node node-id="rt-camera-target" position="0 1 0"></xr-node>
<xr-camera
  position="0 1 4" clear-color="0.925 0.925 0.925 1" target="rt-camera-target" render-target="rt" cull-mask="0b011"
/>

<xr-camera
 position="0 4 6" clear-color="0.925 0.925 0.925 1" target="mesh-plane" cull-mask="0b111" camera-orbit-control=""
></xr-camera>

第一個(gè)相機(jī)的render-target設(shè)置為了rt,然后mesh-planeuniforms中的一張紋理也設(shè)置為了rt。在繪制中就會(huì)用第一個(gè)相機(jī)先繪制到渲染紋理上,然后第二個(gè)相機(jī)繪制的物體中的那個(gè)平面在使用渲染紋理渲染。

注意這里我們用到了layercullMask,這個(gè)在前面的章節(jié)提到過。因?yàn)槲覀儾幌胱尩谝粋€(gè)相機(jī)繪制那個(gè)平面,第二個(gè)相機(jī)則會(huì)繪制所有東西。