DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
HTML5 WebGL 實作展示:Geolocation Love 案例
編輯:HTML5詳解     

本文分享一個以 HTML5 WebGL 打造的 3D 展示案例「Geolocation Love」。它透過 three.js 這套底層函式庫,搭配 WebGL 著色器(Shader),渲染出一顆立體地球與大氣光暈的效果,並結合 Geolocation API 與互動介面,讓使用者能與全球分享訊息。以下附上原始碼與部分關鍵程式碼供參考。

原始碼結構

頁面首先在 head 中載入了 jQuery、three.js 以及一系列輔助模組,包含 Detector、Stats、Tween,以及後處理相關的 ShaderExtras、EffectComposer、ShaderPass、MaskPass、RenderPass、BloomPass、FilmPass,最後引入主程式 main.js:

<!DOCTYPE Html>
<Html>
<head>
    <title>Geolocation love</title>
    <meta charset="utf-8">
    <script src="js/jquery-1.8.2.min.JS"></script>
    <script src="js/three.JS"></script>
    <script src="js/Detector.JS"></script>
    <script src="js/Stats.JS"></script>
    <script src="js/Tween.JS"></script>
    <script src="js/ShaderExtras.JS"></script>
    <script src="js/EffectComposer.JS"></script>
    <script src="js/ShaderPass.JS"></script>
    <script src="js/MaskPass.JS"></script>
    <script src="js/RenderPass.JS"></script>
    <script src="js/BloomPass.JS"></script>
    <script src="js/FilmPass.JS"></script>
    <script type="text/Javascript" src="js/main.JS"></script>
    <link href="style.css" rel="stylesheet" type="text/CSS" />
</head>

頁面結構

body 部分包含一個作為渲染容器的 div,以及兩個資訊區塊:一個用來顯示訊息與分享入口,另一個則提供輸入訊息、產生分享連結的表單:

<body>
<div id="container"></div>
    <section class="box" id="messageBox">
        <div id="message">No matter where you are, always make the world brighter place</div>
        <div id="dist"></div>
        <div id="shareSwitcher"><a>Click to share your love with others!</a></div>
        <div id="likeIt"><a href="https://developer.mozilla.org/en-US/demos/detail/geolocation-love">Like this demo on DevDerby</a></div>
    </section>

    <section class="box" id="shareBox">
        <div id="form">
            <h1>Type your message to the world</h1>
            <textarea id="messageArea"> </textarea>
            <input id="submitShare" type="button" value="get share link" />
        </div>
    </section>
</div>

著色器(Shader)程式碼

地球的質感與大氣光暈,是透過兩組自訂的頂點與片段著色器完成的。首先是地球本體,片段著色器會依據法線計算光暈強度,疊加出大氣層的柔光:

<script type="x-shader/x-vertex" id="earthVertex">
varying vec3 vNormal;
varying vec2 vUv;
void main() {
    gl_Position = projectionMatrix * modelVIEwMatrix * vec4( position, 1.0 );
    vNormal = normalize( normalMatrix * normal );
    vUv = uv;
}
</script>
<script type="x-shader/x-fragment" id="earthFragment">
uniform sampler2D texture;
varying vec3 vNormal;
varying vec2 vUv;
void main() {
    vec3 diffuse = texture2D( texture, vUv ).xyz;
    float intensity = 1.15 - dot( vNormal, vec3( 0.0, 0.0, 1.0 ) );
    vec3 atmosphere = vec3( 1.0, 1.0, 1.0 ) * pow( intensity, 3.0 );
    gl_FragColor = vec4( diffuse + atmosphere, 1.0 );
}
</script>

接著是大氣層外殼,透過一層半透明的球體模擬星球邊緣的散射光暈:

<script type="x-shader/x-vertex" id="atmosphereVertex">
varying vec3 vNormal;
void main() {
    vNormal = normalize( normalMatrix * normal );
    gl_Position = projectionMatrix * modelVIEwMatrix * vec4( position, 1.0 );
}
</script>
<script type="x-shader/x-fragment" id="atmosphereFragment">
varying vec3 vNormal;
void main() {
    float intensity = pow( 0.8 - dot( vNormal, vec3( 0, 0, 1.0 ) ), 10.0 );
    gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 ) * intensity;
}
</script>
</body>
</Html>

從這個案例可以清楚看到,WebGL 結合 three.js 與自訂 Shader,能在瀏覽器中即時渲染出相當精緻的 3D 視覺效果,再加上 Bloom、Film 等後處理,畫面質感更加出色。這是 HTML5 在圖形與多媒體應用上的一大里程碑。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved