`
wjlgryx
  • 浏览: 310678 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

o3d—立方体贴图例子逐步解释

阅读更多
第1步:定义纹理坐标
      下面的步骤操作和代码指定纹理坐标,将其添加到一个数组,并添加到数组的缓冲区。该setVertexStream ( )函数指定如何读取缓冲区,并设置了一个顶点缓冲流。

      指定(u,v)纹理坐标为每个顶点,并将其添加到一个数组( texCoordsArray ):

view plaincopy to clipboardprint?
var texCoordsArray = [  
    0, 0,  
    1, 0,  
    1, 1,  
    0, 1,  
    0, 0,  
    1, 0,  
    1, 1,  
    0, 1,  
    1, 1,  
    0, 1,  
    0, 0,  
    1, 0,  
    0, 0,  
    1, 0,  
    1, 1,  
    0, 1,  
    0, 0,  
    1, 0,  
    1, 1,  
    0, 1,  
    0, 0,  
    1, 0,  
    1, 1,  
    0, 1  
  ]; 
创建一个顶点缓冲区保存纹理坐标( texCoordsBuffer ) 。并在这个缓冲区设置纹理坐标数组:

view plaincopy to clipboardprint?
var texCoordsBuffer = g_pack.createObject('VertexBuffer');  
var texCoordsField = texCoordsBuffer.createField('FloatField', 2);  
texCoordsBuffer.set(texCoordsArray); 

      调用setVertexStream ()函数,将纹理坐标缓冲区和基元数据流银行关联起来。调用这个函数设置语义纹理坐标顶点流( TEXCOORD )以及有关如何读取数据流中数据的其他信息:

view plaincopy to clipboardprint?
streamBank.setVertexStream(  
   g_o3d.Stream.TEXCOORD,  // 语意 
   0,                      // 语意索引  
   texCoordsField,         // 域 
   0);                     // 起始索引 

第2步 :创建纹理取样器
下面的步骤操作和代码创建纹理采样和确定其状态。

•调用Effect.createUniformParams ( )创建一个材料参数的每一个效果的统一的参数(即,参数,适用于原始作为一个整体) 。在这个例子中,有一个统一的参数,纹理取样器:

view plaincopy to clipboardprint?
cubeEffect.createUniformParameters(cubeMaterial); 

•获取材料的采样参数:

view plaincopy to clipboardprint?
var samplerParam = cubeMaterial.getParam('texSampler0'); 

•创建纹理采样器,并设定其状态。该例子的缩小过滤器( minFilter )设置为各向异性ANISOTROPIC。从某一角度看,此值可以提高纹理的质量:

view plaincopy to clipboardprint?
g_sampler = g_pack.createObject('Sampler');  
g_sampler.minFilter = g_o3d.Sampler.ANISOTROPIC;  
g_sampler.maxAnisotropy = 4; 

•将此纹理采样器赋值给材质采样器参数:

view plaincopy to clipboardprint?
samplerParam.value = g_sampler; 

第3步:获取纹理

      下面演示了初始化材质贴图文件的路径参数,并且当用户改变材质贴图文件时,这个路径参数会发生相应的改变。

• 初始化纹理网址(在 initStep2 ()函数中)

view plaincopy to clipboardprint?
var path = window.location.href;  
var index = path.lastIndexOf('/');  
path = path.substring(0, index+1) + 'assets/texture_b3.jpg';  
var url = document.getElementById("url").value = path; 
 

•每当用户指定一个新的纹理,在changeTexture ( )函数获取一个新的纹理文件。

view plaincopy to clipboardprint?
function changeTexture() {  
  var textureUrl = document.getElementById('url').value;  
  try {  
    o3djs.io.loadTexture(g_pack, textureUrl, function(texture) {  
      // Remove the currently used texture from the pack so that when it's not  
      // referenced anymore, it can get destroyed.  
      if (g_sampler.texture)  
        g_pack.removeObject(g_sampler.texture);  
   
      // Set the texture on the sampler object to the newly created texture  
      // object returned by the request.  
      g_sampler.texture = texture;  
  
      // We can now safely add the cube transform to the root of the  
      // transform graph since it now has a valid texture.  If the transform  
      // is already parented under the root, the call will have no effect.  
      g_cubeTransform.parent = g_client.root;  
      .  
      .  
      .//Error handling calls here.  


    这个函数使用util.loadTexture ( )从指定的地址加载纹理。此实用工具,反过来,调用createFileRequest ()函数。根据预设, createFileRequest ( )创建了一套mipmaps时加载纹理。如果您知道您不会使用mipmapping ,您可以明确要求加载纹理文件,并指定generateMipmaps属性FileRequest为FALSE 。

•在<body>的HTML网页,添加用户输入框和按钮,使用户可以指定一个新的纹理:

view plaincopy to clipboardprint?
<body>  
<h1>Hello Cube: Textures</h1>  
This example shows how to texture map a cube using an image fetched from a URL.  
<br/>  
<!-- Start of O3D plugin -->  
<div id="o3d" style="width: 600px; height: 600px;"></div>  
<!-- End of O3D plugin -->  
<br />  
Image URL: <input type="text" id="url" size="100">  
<input type="button" onclick="changeTexture();" value="Update Texture"><BR> 

第4步:创建着色器

       在这个例子中, <textarea>元素包含的代码顶点和像素着色器。输入属性的顶点着色器的位置,有语义的立场,有语义的TEXCOORD0 。顶点着色器转换成均匀顶点位置剪辑空间。在这里,立场载于positionsBuffer和纹理坐标载于texCoordsBuffer 。顶点着色器通过纹理坐标值( TEXCOORD0 )通过不变( output.tex = input.tex ; ) 。支持Pixel Shader然后在查询中每个像素的原始和效益的颜色相应位的纹理贴图。以下是代码的顶点和像素着色器:

view plaincopy to clipboardprint?
<textarea id="effect">  
  // World View Projection matrix that will transform the input vertices  
  // to screen space.  
  float4x4 worldViewProjection : WorldViewProjection;  
  
  // The texture sampler is used to access the texture bitmap in the fragment  
  // shader.  
  sampler texSampler0;  
  
  // input for our vertex shader  
  struct VertexShaderInput {  
    float4 position : POSITION;  
    float2 tex : TEXCOORD0;  // Texture coordinates  
  };  
  
  // input for our pixel shader  
  struct PixelShaderInput {  
    float4 position : POSITION;  
    float2 tex : TEXCOORD0;  // Texture coordinates  
  };  
  
  /** 
   * The vertex shader simply transforms the input vertices to screen space. 
   */  
  PixelShaderInput vertexShaderFunction(VertexShaderInput input) {  
    PixelShaderInput output;  
  
    // Multiply the vertex positions by the worldViewProjection matrix to  
    // transform them to screen space.  
    output.position = mul(input.position, worldViewProjection);  
  
    output.tex = input.tex;  
    return output;  
  }  
  
/** 
  * Given the texture coordinates, our pixel shader grabs the corresponding 
  * color from the texture. 
  */  
  float4 pixelShaderFunction(PixelShaderInput input): COLOR {  
    return tex2D(texSampler0, input.tex);  
  }  
  
  // Here we tell our effect file *which* functions are  
  // our vertex and pixel shaders.  
  
  // #o3d VertexShaderEntryPoint vertexShaderFunction  
  // #o3d PixelShaderEntryPoint pixelShaderFunction  
  // #o3d MatrixLoadOrder RowMajor
分享到:
评论

相关推荐

    O3D 物体 编辑器

    O3D库包含了一系列的类和方法,如几何体创建、材质应用、光照设置、相机控制等,支持开发者构建丰富的网络3D应用。 2. **3D物体编辑**:O3D 物体编辑器允许用户创建、修改和管理3D模型。用户可以导入现有的3D模型...

    google o3d 编程例子1

    google开发的web3d编程APT o3d.提供的各种3d效果例子,因文件太大,分三个包传上

    google o3d例子3

    o3d是Google开发的基于web的3d编程API,因文件太大分三次上传

    O3D学习笔记[一]素材准备*.3ds转成*.o3dtgz

    在本篇O3D学习笔记中,我们将探讨如何将3DS格式的三维模型转换为O3DTGZ格式,这是O3D系统所使用的专有格式。3DS是一种广泛使用的三维建模软件3ds Max导出的文件格式,而O3DTGZ是O3D(Open 3D Engine)框架下的压缩...

    google o3d 编程例子2

    o3d是Google开发的基于web的3d编程API,因文件太大分三次上传

    谷歌O3D的javascript源码

    6. **纹理和光照**:为了使3D模型看起来更真实,O3D支持纹理贴图和光照模型。理解如何应用纹理和设置光照参数对于创建逼真的3D场景至关重要。 7. **性能优化**:由于O3D依赖于硬件加速,因此了解如何有效地利用GPU...

    O3D 实例 研究中

    **O3D 实例研究详解** O3D 是一个开源的 JavaScript 库,它允许开发者在 Web 浏览器中创建高性能的三维图形应用。这个实例可能是为了展示 O3D 的基本用法和功能,帮助用户快速入门。下面我们将深入探讨 O3D 的核心...

    O3D文档说明(主要关键字的说明及翻译)

    在O3D文档中,我们关注的是3D图形渲染的关键概念和术语,这些内容对于理解O3D框架的架构和工作原理至关重要。以下是对这些关键术语的详细解释: 1. **Draw Context 绘制上下文**:绘制上下文是3D渲染的核心组件,它...

    3D-o3d.zip

    在3D-o3d.zip这个压缩包中,我们看到的是Objective-3D视频游戏引擎的相关资料,这是一个专门用于3D游戏开发的工具。 Objective-3D游戏引擎,以其强大的功能和灵活性,为开发者提供了构建逼真3D游戏环境的可能性。它...

    o3d lib1 graph javascript ajax

    o3d lib1 graph javascript ajax

    o3d goolge javascript

    o3d goolge javascript texture graph

    离心力场中Al2O3纤维多孔体中的过临界渗透现象

    离心力场中Al2O3纤维多孔体中的过临界渗透现象的研究涉及了多孔介质的渗透理论、液态金属在多孔介质中的流动行为、以及离心力场对流体渗透行为的影响等多个方面。本文将详细介绍这些知识点。 首先,多孔介质的渗透...

    野村综研O3W架构资料(内部)

    ### 野村综研O3W架构资料:深入解析与技术要点 #### 一、背景与挑战 在1980年代至1990年代初,日本许多企业开始进行核心业务系统的信息化改造,主要采用大型机为核心的专用系统。当时的终端设备主要是哑终端,仅能...

    o3dv_0.8.3.zip

    "o3dv_0.8.3.zip" 是一个压缩包文件,包含了 Online3DViewer 的 0.8.3 版本。Online3DViewer 是一个基于 Web 的三维模型查看器,它允许用户在浏览器中查看和交互三维模型,无需安装任何额外的软件。这个工具主要利用...

    CaO-Al2O3和CaO-SiO2-Al2O3渣系中组元活度的计算 (2013年)

    根据分子和离子共存理论,建立了CaO-Al2O3 和CaO-SiO2-Al2O3 渣系的活度计算模型,并利用模型对渣中各组元的活度进行了计算,分析了 w (CaO )对渣中组元活度的影响.结果表明,在 CaO-Al2O3 渣系中,当w(CaO)小于45%时,...

    python三角网格处理程序

    01_mesh_sphere_ball.py:球面和立方体表面三角化示例程序。 02_trimesh_slz.py:三棱柱模型可视化,并将trimesh.Trimesh对象转换为open3d的o3d.geometry.TriangleMesh对象。 03_trimesh_o3d.py:三棱柱模型可视化,...

Global site tag (gtag.js) - Google Analytics