首页 > 其他分享 >three.js基础之clipping

three.js基础之clipping

时间:2024-07-15 10:32:36浏览次数:10  
标签:clipping const THREE three add new js

clipping

<canvas id="mainCanvas"></canvas>
<script type="importmap">
  {
    "imports": {
      "three": "./js/build/three.module.js",
      "three/addons/": "./js/jsm/"
    }
  }
</script>
<script type="module">
  import * as THREE from "three";
  import Stats from "three/addons/libs/stats.module.js";
  import { GUI } from "three/addons/libs/lil-gui.module.min.js";
  import { TrackballControls } from "three/addons/controls/TrackballControls.js";
  import { initRenderer, initPerspectiveCamera, initAmbientLight, initSpotLight } from "./init.js";

  function init() {
    const renderer = initRenderer("mainCanvas");

    const scene = new THREE.Scene();

    const camera = initPerspectiveCamera();
    camera.position.set(-10, 10, 10);
    scene.add(camera);

    const ambientLight = initAmbientLight();
    scene.add(ambientLight);

    const spotLight = initSpotLight();
    scene.add(spotLight);

    const trackballControls = new TrackballControls(camera, renderer.domElement);
    const clock = new THREE.Clock();

    const axesHelper = new THREE.AxesHelper(150);
    scene.add(axesHelper);

    const stats = new Stats();
    document.body.appendChild(stats.dom);

    const localPlane = new THREE.Plane(new THREE.Vector3(0, -1, 0), 0.8);
    const globalPlane = new THREE.Plane(new THREE.Vector3(-1, 0, 0), 0.1);

    const globalPlanes = [globalPlane],
      Empty = Object.freeze([]);
    renderer.clippingPlanes = Empty;
    renderer.localClippingEnabled = true;

    const ground = new THREE.Mesh(new THREE.PlaneGeometry(9, 9), new THREE.MeshPhongMaterial({ color: 0xa0adaf, shininess: 150 }));
    ground.rotation.x = -Math.PI / 2;
    ground.receiveShadow = true;
    scene.add(ground);

    const material = new THREE.MeshPhongMaterial({
      color: 0x80ee10,
      shininess: 100,
      side: THREE.DoubleSide,

      clippingPlanes: [localPlane],
      clipShadows: true,
      alphaToCoverage: true,
    });
    const geometry = new THREE.TorusKnotGeometry(0.4, 0.08, 95, 20);
    const object = new THREE.Mesh(geometry, material);
    object.castShadow = true;
    scene.add(object);

    const gui = new GUI(),
      props = {
        alphaToCoverage: true,
      },
      folderLocal = gui.addFolder("Local Clipping"),
      propsLocal = {
        get Enabled() {
          return renderer.localClippingEnabled;
        },
        set Enabled(v) {
          renderer.localClippingEnabled = v;
        },
        get Shadows() {
          return material.clipShadows;
        },
        set Shadows(v) {
          material.clipShadows = v;
        },
        get Plane() {
          return localPlane.constant;
        },
        set Plane(v) {
          localPlane.constant = v;
        },
      },
      folderGlobal = gui.addFolder("Global Clipping"),
      propsGlobal = {
        get Enabled() {
          return renderer.clippingPlanes !== Empty;
        },
        set Enabled(v) {
          renderer.clippingPlanes = v ? globalPlanes : Empty;
        },
        get Plane() {
          return globalPlane.constant;
        },
        set Plane(v) {
          globalPlane.constant = v;
        },
      };

    gui.add(props, "alphaToCoverage").onChange(function (value) {
      ground.material.alphaToCoverage = value;
      ground.material.needsUpdate = true;

      material.alphaToCoverage = value;
      material.needsUpdate = true;
    });
    folderLocal.add(propsLocal, "Enabled");
    folderLocal.add(propsLocal, "Shadows");
    folderLocal.add(propsLocal, "Plane", 0.3, 1.25);

    folderGlobal.add(propsGlobal, "Enabled");
    folderGlobal.add(propsGlobal, "Plane", -0.4, 3);

    const startTime = Date.now();

    render();

    function render() {
      trackballControls.update(clock.getDelta());

      const currentTime = Date.now();
      const time = (currentTime - startTime) / 1000;

      object.position.y = 0.8;
      object.rotation.x = time * 0.5;
      object.rotation.y = time * 0.2;
      object.scale.setScalar(Math.cos(time) * 0.125 + 0.875);

      requestAnimationFrame(render);
      stats.begin();
      renderer.render(scene, camera);
      stats.end();
    }
  }

  init();
</script>

 

 

 

标签:clipping,const,THREE,three,add,new,js
From: https://www.cnblogs.com/caroline2016/p/18302612

相关文章

  • three.js之BufferGeometry
    BufferGeometry缓冲几何体<canvasid="mainCanvas"></canvas><scripttype="importmap">{"imports":{"three":"./js/build/three.module.js","three/addons/":"./......
  • nuxtjs2.x项目PC移动互相跳转
    1、在plugins目录下新建terminalToggle.js,写入以下代码(function(){letsUserAgent=navigator.userAgent.toLowerCase();letisIpad=sUserAgent.match(/ipad/i)=="ipad";letisIphoneOs=sUserAgent.match(/iphoneos/i)=="iphoneos";letis......
  • 前端面试题(JS篇三)
    一、||和&&操作符的返回值?||和&&首先会对第一个操作数执行条件判断,如果其不是布尔值就先进行ToBoolean强制类型转换,然后再执行条件判断。对于||来说,如果条件判断结果为true就返回第一个操作数的值,如果为false就返回第二个操作数的值。&&则相反,如果条件判断......
  • 面试题之一文搞定JS的事件循环机制
    面试又被问到了js事件循环机制,这不狠狠总结一波。事件循环机制是一个非常重要的概念,不仅仅是面试,在笔试以及平时开发过程所遇到的一些问题,都可以通过事件循环机制来理解问题的本质再来解决。在面试的时候,面对这样一道常规的面试题,就一定不能仅仅是将事件循环讲清楚就结束了,一定......
  • CAD、GIS与Three.js如何完美结合,实现2D与3D数据可视化的无限可能
    现状AutoCAD是全球最流行的CAD软件之一,其生成的DWG文件格式已经成为二维绘图的事实标准格式。然而,由于AutoCAD使用的是私有文件格式,这些文件只能在支持该格式的CAD软件中编辑和查看。这为希望将AutoCAD图纸发布到Web上,并在Web平台上进行应用开发(如数据展示和GIS分析)的用户带来了......
  • js常用条件语句
    ifelse结构:if结构先判断一个表达式的布尔值,然后根据布尔值的真伪,执行不同的语句。所谓布尔值,指的是JavaScript的两个特殊值,true表示“真”,false表示“伪”。if(m!==1){if(n===2){console.log('hello');}}else{console.log('world');}//worldswitch结构:s......
  • threejs可以做游戏吗
    Three.js非常适合用于开发Web端的3D游戏。‌实际上,‌Three.js已经被广泛用于开发各种类型的游戏,‌包括但不限于塔防游戏、‌RPG游戏等。‌在塔防游戏中,‌Three.js可以用来创建3D的游戏场景、‌角色和道具,‌以及处理游戏逻辑和动画。‌通过使用requestAnimationFrame方法,‌Three.......
  • 基于javaweb jsp ssm图书管理系统的设计与实现+vue录像(源码+lw+部署文档+讲解等)
    前言......
  • 基于javaweb jsp ssm图书管理借阅系统录像(源码+lw+部署文档+讲解等)
    前言......
  • 基于javaweb jsp ssm图书馆智能选座系统设计与实现+jsp演示录像(源码+lw+部署文档+讲
    前言......