【问题标题】:Texture splatting with Three.js使用 Three.js 进行纹理喷溅
【发布时间】:2013-09-23 17:07:34
【问题描述】:

纹理飞溅是否适用于 Three.js 或其他 Javascript 3D 渲染框架?如果是的话,我想看看例子,甚至可能是关于大地形的教程。如果它不起作用,还有其他方法可以绘制大地形吗? 谢谢。

【问题讨论】:

标签: html 3d three.js webgl


【解决方案1】:

接受挑战!

首先,您可以编写一个顶点着色器,它采用灰度图像并将其用作高度图,并包含一个可变的浮点数(下面称为vAmount)以传递给片段着色器以确定要显示的纹理(混合)在那个时候。

uniform sampler2D bumpTexture;
uniform float bumpScale;

varying float vAmount;
varying vec2 vUV;

void main() 
{
    vUV = uv;
    vec4 bumpData = texture2D( bumpTexture, uv );

    vAmount = bumpData.r; // assuming map is grayscale it doesn't matter if you use r, g, or b.

    // move the position along the normal
    vec3 newPosition = position + normal * bumpScale * vAmount;

    gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
}

接下来是片段着色器,它可以包含不同高度所需的任意数量的纹理,并且有一个名为smoothstep 的强大内置函数,可以更轻松地计算平滑过渡。

这种片段着色器的代码示例:

uniform sampler2D oceanTexture;
uniform sampler2D sandyTexture;
uniform sampler2D grassTexture;
uniform sampler2D rockyTexture;
uniform sampler2D snowyTexture;

varying vec2 vUV;

varying float vAmount;

void main() 
{
    vec4 water = (smoothstep(0.01, 0.25, vAmount) - smoothstep(0.24, 0.26, vAmount)) * texture2D( oceanTexture, vUV * 10.0 );
    vec4 sandy = (smoothstep(0.24, 0.27, vAmount) - smoothstep(0.28, 0.31, vAmount)) * texture2D( sandyTexture, vUV * 10.0 );
    vec4 grass = (smoothstep(0.28, 0.32, vAmount) - smoothstep(0.35, 0.40, vAmount)) * texture2D( grassTexture, vUV * 20.0 );
    vec4 rocky = (smoothstep(0.30, 0.50, vAmount) - smoothstep(0.40, 0.70, vAmount)) * texture2D( rockyTexture, vUV * 20.0 );
    vec4 snowy = (smoothstep(0.50, 0.65, vAmount))                                   * texture2D( snowyTexture, vUV * 10.0 );
    gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0) + water + sandy + grass + rocky + snowy;
}  

然后您可以使用THREE.ShaderMaterial 将其用于给定的网格。上面的代码在http://stemkoski.github.io/Three.js/Shader-Heightmap-Textures.html 实现,并产生如下结果:

希望这可以帮助您入门。编码愉快!

【讨论】:

  • 谢谢,我试试看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
相关资源
最近更新 更多