最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

threejs使用canvas更新紋理代碼實(shí)例

 更新時(shí)間:2024年12月05日 10:09:08   作者:gis分享者  
在使用three.js開發(fā)的過程中,我們常常使用canvas來作為紋理貼圖,下面這篇文章主要介紹了如何基于Three.js在三維場(chǎng)景中使用Canvas更新紋理,通過實(shí)現(xiàn)思路和代碼樣例,幫助讀者掌握這一技能,需要的朋友可以參考下

一、前言

本文詳細(xì)介紹如何基于threejs在三維場(chǎng)景中使用canvas更新紋理,親測(cè)可用。希望能幫助到您。一起學(xué)習(xí),加油!加油!

1.1 Texture 貼圖

創(chuàng)建一個(gè)紋理貼圖,將其應(yīng)用到一個(gè)表面,或者作為反射/折射貼圖。

構(gòu)造函數(shù):Texture( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding )

常用屬性:

方法:

二、使用canvas更新紋理

1. 實(shí)現(xiàn)思路

  • 1、初始化renderer渲染器
  • 2、初始化Scene三維場(chǎng)景scene,創(chuàng)建THREE.CubeTextureLoader立方體紋理加載器cubeTextureLoader,加載cubeTextureLoader的六個(gè)方位的圖片獲取紋理對(duì)象cubeTexture,scene背景background設(shè)置為cubeTexture。
  • 3、創(chuàng)建id為‘surface’的canvas頁面元素火焰然后動(dòng)畫,并執(zhí)行。具體實(shí)現(xiàn)參考下面代碼樣例。
  • 4、初始化camera相機(jī),定義相機(jī)位置 camera.position.set
  • 5、初始化THREE.AmbientLight環(huán)境光源,scene場(chǎng)景加入環(huán)境光源,初始化THREE.DirectionalLight平行光源,設(shè)置平行光源位置,設(shè)置平行光源投影,scene添加平行光源。
  • 6、加載幾何模型:創(chuàng)建THREE.AxesHelper坐標(biāo)輔助工具h(yuǎn)elper,scene場(chǎng)景中加入helper。創(chuàng)建THREE.BoxGeometry立方體幾何體geometry,創(chuàng)建THREE.MeshBasicMaterial基礎(chǔ)材質(zhì)material,material設(shè)置map貼圖(為步驟3canvas元素)、水平和垂直貼圖包裹,傳入geometry和material創(chuàng)建THREE.Mesh網(wǎng)格對(duì)象,scene中加入創(chuàng)建的網(wǎng)格對(duì)象。
  • 7、加入controls控制,加入stats監(jiān)控器,監(jiān)控幀數(shù)信息。

2. 代碼樣例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>learn60(使用CANVAS更新紋理)</title>
    <script src="lib/threejs/127/three.js-master/build/three.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="lib/threejs/127/three.js-master/examples/js/controls/OrbitControls.js"></script>
    <script src="lib/threejs/127/three.js-master/examples/js/libs/stats.min.js"></script>
    <script src="lib/threejs/127/three.js-master/examples/js/libs/dat.gui.min.js"></script>
    <script src="lib/js/Detector.js"></script>
</head>
<style type="text/css">
    html, body {
        margin: 0;
        height: 100%;
    }

    canvas {
        display: block;
    }

    #surface {
        position: fixed;
        left: 0;
        bottom: 0;
    }
</style>
<body onload="draw()">
<canvas id="surface"></canvas>
</body>
<script>
  var renderer, camera, scene, gui, light, stats, controls

  //引入一個(gè)canvas動(dòng)畫
  function initCanvas() {
    $(document).ready(function () {

      // Set canvas drawing surface
      var space = document.getElementById("surface")
      var surface = space.getContext("2d")
      surface.scale(1, 1)

      // Set Particles
      var particles = []
      var particle_count = 150
      for (var i = 0; i < particle_count; i++) {
        particles.push(new particle())
      }
      var time = 0
      // Set wrapper and canvas items size
      var canvasWidth = 480
      var canvasHeight = 480
      $(".wrapper").css({width: canvasWidth, height: canvasHeight})
      $("#surface").css({width: canvasWidth, height: canvasHeight})

      // shim layer with setTimeout fallback from Paul Irish
      window.requestAnimFrame = (function () {
        return window.requestAnimationFrame ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame ||
          function (callback) {
            window.setTimeout(callback, 6000 / 60)
          }
      })()

      function particle() {

        this.speed = {x: -1 + Math.random() * 2, y: -5 + Math.random() * 5}
        canvasWidth = (document.getElementById("surface").width)
        canvasHeight = (document.getElementById("surface").height)
        this.location = {x: canvasWidth / 2, y: (canvasHeight / 2) + 35}

        this.radius = .5 + Math.random() * 1

        this.life = 10 + Math.random() * 10
        this.death = this.life

        this.r = 255
        this.g = Math.round(Math.random() * 155)
        this.b = 0
      }

      function ParticleAnimation() {
        surface.globalCompositeOperation = "source-over"
        surface.fillStyle = "black"
        surface.fillRect(0, 0, canvasWidth, canvasHeight)
        surface.globalCompositeOperation = "lighter"

        for (var i = 0; i < particles.length; i++) {
          var p = particles[i]

          surface.beginPath()

          p.opacity = Math.round(p.death / p.life * 100) / 100
          var gradient = surface.createRadialGradient(p.location.x, p.location.y, 0, p.location.x, p.location.y, p.radius)
          gradient.addColorStop(0, "rgba(" + p.r + ", " + p.g + ", " + p.b + ", " + p.opacity + ")")
          gradient.addColorStop(0.5, "rgba(" + p.r + ", " + p.g + ", " + p.b + ", " + p.opacity + ")")
          gradient.addColorStop(1, "rgba(" + p.r + ", " + p.g + ", " + p.b + ", 0)")
          surface.fillStyle = gradient
          surface.arc(p.location.x, p.location.y, p.radius, Math.PI * 2, false)
          surface.fill()
          p.death--
          p.radius++
          p.location.x += (p.speed.x)
          p.location.y += (p.speed.y)

          //regenerate particles
          if (p.death < 0 || p.radius < 0) {
            //a brand new particle replacing the dead one
            particles[i] = new particle()
          }
        }

        requestAnimFrame(ParticleAnimation)

      }

      ParticleAnimation()

    })
  }

  var initRender = () => {
    renderer = new THREE.WebGLRenderer({antialias: true})
    renderer.setClearColor(0xeeeeee)
    renderer.setSize(window.innerWidth, window.innerHeight)
    renderer.shadowMap.enabled = true
    renderer.setPixelRatio(window.devicePixelRatio)
    document.body.appendChild(renderer.domElement)
  }

  var initCamera = () => {
    camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000)
    camera.position.set(0, 0, 15)
  }

  var initScene = () => {
    var cubeTextureLoader = new THREE.CubeTextureLoader()
    cubeTextureLoader.setPath('data/texture/skybox/space/')
    var cubeTexture = cubeTextureLoader.load([
      'right.jpg', 'left.jpg',
      'top.jpg', 'bottom.jpg',
      'front.jpg', 'back.jpg'
    ])
    scene = new THREE.Scene()
    scene.background = cubeTexture
  }

  var initLight = () => {
    scene.add(new THREE.AmbientLight(0x444444))

    light = new THREE.DirectionalLight(0xffffff)
    light.position.set(0, 20, 20)
    light.castShadow = true
    scene.add(light)
  }

  var initModel = () => {
    var helper = new THREE.AxesHelper(50)
    scene.add(helper)

    var geometry = new THREE.BoxBufferGeometry(5, 5, 5)
    var canvas = $('#surface')[0]
    var texture = new THREE.Texture(canvas)
    material = new THREE.MeshBasicMaterial({map: texture})
    scene.add(new THREE.Mesh(geometry, material))
  }

  var initStats = () => {
    stats = new Stats()
    document.body.appendChild(stats.dom)
  }

  var initControls = () => {
    controls = new THREE.OrbitControls(camera, renderer.domElement)
    controls.enableDamping = true
  }

  var render = () => {
    material.map.needsUpdate = true
    renderer.render(scene, camera)
  }

  var onWindowResize = () => {
    camera.aspect = window.innerWidth / window.innerHeight
    camera.updateProjectionMatrix()
    renderer.setSize(window.innerWidth, window.innerHeight)
  }

  var animate = () => {
    render()
    stats.update()
    controls.update()
    requestAnimationFrame(animate)
  }

  var draw = () => {
    if(!Detector.webgl)Detector.addGetWebGLMessage()
    initCanvas()
    initRender()
    initScene()
    initCamera()
    initLight()
    initModel()
    initStats()
    initControls()

    animate()

    window.onresize = onWindowResize
  }
</script>
</html>

效果如下:

總結(jié) 

到此這篇關(guān)于threejs使用canvas更新紋理的文章就介紹到這了,更多相關(guān)threejs canvas更新紋理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JavaScript函數(shù)增強(qiáng)以及額外知識(shí)

    JavaScript函數(shù)增強(qiáng)以及額外知識(shí)

    函數(shù)就是封裝了一段可以被重復(fù)執(zhí)行調(diào)用的代碼塊,下面這篇文章主要給大家介紹了關(guān)于JavaScript函數(shù)增強(qiáng)以及額外知識(shí)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • 將HTMLCollection/NodeList/偽數(shù)組轉(zhuǎn)換成數(shù)組的實(shí)現(xiàn)方法

    將HTMLCollection/NodeList/偽數(shù)組轉(zhuǎn)換成數(shù)組的實(shí)現(xiàn)方法

    將HTMLCollection/NodeList/偽數(shù)組轉(zhuǎn)換成數(shù)組的實(shí)現(xiàn)方法,需要的朋友可以參考下。
    2011-06-06
  • electron的webview和內(nèi)嵌網(wǎng)頁通信的方法

    electron的webview和內(nèi)嵌網(wǎng)頁通信的方法

    在 Electron 的世界里,webview 標(biāo)簽相當(dāng)于一個(gè)小盒子,里面可以裝一個(gè)完整的網(wǎng)頁,就像一個(gè)迷你瀏覽器,這篇文章主要介紹了electron的webview和內(nèi)嵌網(wǎng)頁如何通信,需要的朋友可以參考下
    2024-04-04
  • js實(shí)現(xiàn)多圖和單圖上傳顯示

    js實(shí)現(xiàn)多圖和單圖上傳顯示

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)多圖和單圖上傳顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 你必須了解的JavaScript中的屬性描述對(duì)象詳解(下)

    你必須了解的JavaScript中的屬性描述對(duì)象詳解(下)

    JavaScript提供了一個(gè)內(nèi)部數(shù)據(jù)結(jié)構(gòu),用來描述對(duì)象的屬性,控制它的行為,比如該屬性是否可寫、可遍歷等等。這個(gè)內(nèi)部數(shù)據(jù)結(jié)構(gòu)稱為“屬性描述對(duì)象”。本文主要帶大家了解一下JavaScript中你必須了解的屬性描述對(duì)象,需要的可以參考一下
    2022-12-12
  • es6 filter() 數(shù)組過濾方法總結(jié)

    es6 filter() 數(shù)組過濾方法總結(jié)

    這篇文章主要介紹了es6 filter() 數(shù)組過濾方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 自定義事件解決重復(fù)請(qǐng)求BUG的問題

    自定義事件解決重復(fù)請(qǐng)求BUG的問題

    下面小編就為大家?guī)硪黄远x事件解決重復(fù)請(qǐng)求BUG的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • 使用JS實(shí)現(xiàn)jQuery的addClass, removeClass, hasClass函數(shù)功能

    使用JS實(shí)現(xiàn)jQuery的addClass, removeClass, hasClass函數(shù)功能

    這篇文章主要介紹了使用JS實(shí)現(xiàn)jQuery的addClass, removeClass, hasClass函數(shù)功能,需要的朋友可以參考下
    2014-10-10
  • Google Map API更新實(shí)現(xiàn)用戶自定義標(biāo)注坐標(biāo)

    Google Map API更新實(shí)現(xiàn)用戶自定義標(biāo)注坐標(biāo)

    由于工作需要,又要開始看Google Map API 代碼,今天再把我之前的GoogleMap類,又更新了下,加了個(gè)簡(jiǎn)單的用戶自定義標(biāo)注坐標(biāo)的功能??纯窗?代碼沒怎么優(yōu)化,別見笑)
    2009-07-07
  • js如何讀取csv內(nèi)容拼接成json

    js如何讀取csv內(nèi)容拼接成json

    這篇文章主要介紹了js如何讀取csv內(nèi)容拼接成json,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評(píng)論

钟祥市| 罗平县| 行唐县| 民权县| 伊通| 鹤壁市| 东丽区| 晋中市| 运城市| 馆陶县| 鄂温| 宁化县| 岱山县| 石渠县| 神池县| 札达县| 陵水| 长春市| 辽中县| 青田县| 五台县| 吴堡县| 团风县| 会理县| 大余县| 尉氏县| 沂南县| 焦作市| 镇宁| 安乡县| 巴林左旗| 宜兴市| 邯郸县| 蒙山县| 田林县| 阜城县| 龙川县| 原阳县| 台东县| 合江县| 北辰区|