SVGRenderer 是 three.js 中的一个渲染器,用于将 3D 场景渲染到 SVG(可缩放矢量图形)元素中。

demo案例

SVGRenderer 是 three.js 中的一个渲染器,用于将 3D 场景渲染到 SVG(可缩放矢量图形)元素中。虽然 SVG 本身不支持 3D 渲染,但 SVGRenderer 提供了一种将 three.js 的 3D 场景以 2D 形式投影到 SVG 平面的方法。这种渲染方式通常用于创建一些特定的视觉效果或用于支持 SVG 的环境。

属性

SVGRenderer 的一些主要属性可能包括:

  • domElement: 返回一个 SVG DOM 元素,这是渲染器渲染其输出的地方。
  • context: 访问底层的 SVG 渲染上下文。
  • size: 获取或设置渲染器输出的宽度和高度。
  • autoClear: 控制是否在每次渲染前自动清除画布。
  • gammaInput: 控制是否使用 gamma 输入。
  • gammaOutput: 控制是否使用 gamma 输出。

方法

SVGRenderer 的一些主要方法可能包括:

  • render(scene, camera): 渲染场景和相机到 SVG 元素。
    • scene: 要渲染的 three.js 场景对象。
    • camera: 用于渲染场景的相机对象。
  • setSize(width, height): 设置渲染器输出的宽度和高度。
    • width: 新的宽度值。
    • height: 新的高度值。
  • setClearColor(color, opacity): 设置清除画布时使用的颜色和透明度。
    • color: 清除颜色,可以是十六进制值或颜色对象。
    • opacity: 颜色的透明度。
  • clear(): 清除渲染器的输出。

入参与出参

入参(输入参数)和出参(输出参数)通常是针对方法的。例如,在 render 方法中:

  • 入参:scene(要渲染的场景)和 camera(用于渲染的相机)。
  • 出参:此方法通常没有直接的返回值,但它会更新 domElement 中的内容以反映渲染结果。

在 setSize 方法中:

  • 入参:width(新的宽度值)和 height(新的高度值)。
  • 出参:此方法通常没有直接的返回值,但会更新 domElement 的尺寸。

SVGRenderer 提供了一种将 three.js 的 3D 场景渲染到 SVG 的方式,尽管其功能和性能可能不如 WebGLRenderer(用于将 3D 场景渲染到 WebGL 上下文中)那么强大和高效。

demo 源码

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <title>three.js svg - lines</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link type="text/css" rel="stylesheet" href="main.css">
    <style>
        svg {
            display: block;
        }
    </style>
</head>
<body>

<div id="info">
    <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> svg - lines
</div>

<script type="importmap">
    {
        "imports": {
            "three": "../build/three.module.js",
            "three/addons/": "./jsm/"
        }
    }
</script>

<script type="module">

    import * as THREE from 'three';

    import { SVGRenderer } from 'three/addons/renderers/SVGRenderer.js';

    // 禁用颜色管理
    THREE.ColorManagement.enabled = false;

    let camera, scene, renderer;

    init();
    animate();

    function init() {

        // 初始化相机
        camera = new THREE.PerspectiveCamera( 33, window.innerWidth / window.innerHeight, 0.1, 100 );
        camera.position.z = 10;

        // 初始化场景
        scene = new THREE.Scene();
        scene.background = new THREE.Color( 0, 0, 0 );

        // 创建SVG渲染器
        renderer = new SVGRenderer();
        renderer.setSize( window.innerWidth, window.innerHeight );
        document.body.appendChild( renderer.domElement );

        //

        // 创建顶点数组
        const vertices = [];
        const divisions = 50;

        // 生成顶点坐标
        for ( let i = 0; i <= divisions; i ++ ) {

            const v = ( i / divisions ) * ( Math.PI * 2 );

            const x = Math.sin( v );
            const z = Math.cos( v );

            vertices.push( x, 0, z );

        }

        // 创建几何体
        const geometry = new THREE.BufferGeometry();
        geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );

        //

        // 创建并添加线条对象
        for ( let i = 1; i <= 3; i ++ ) {

            const material = new THREE.LineBasicMaterial( {
                color: Math.random() * 0xffffff, // 随机颜色
                linewidth: 10
            } );
            const line = new THREE.Line( geometry, material );
            line.scale.setScalar( i / 3 );
            scene.add( line );

        }

        // 创建并添加虚线对象
        const material = new THREE.LineDashedMaterial( {
            color: 'blue', // 蓝色
            linewidth: 1,
            dashSize: 10,
            gapSize: 10
        } );
        const line = new THREE.Line( geometry, material );
        line.scale.setScalar( 2 );
        scene.add( line );

        //

        // 监听窗口大小变化事件
        window.addEventListener( 'resize', onWindowResize );

    }

    function onWindowResize() {

        // 更新相机和渲染器大小
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize( window.innerWidth, window.innerHeight );

    }

    function animate() {

        let count = 0;
        const time = performance.now() / 1000;

        // 循环遍历场景中的对象,设置旋转
        scene.traverse( function ( child ) {

            child.rotation.x = count + ( time / 3 );
            child.rotation.z = count + ( time / 4 );

            count ++;

        } );

        // 渲染场景
        renderer.render( scene, camera );
        requestAnimationFrame( animate );

    }

</script>
</body>
</html>

本内容来源于小豆包,想要更多内容请跳转小豆包 》

相关推荐
下一站春风里13 分钟前
位运算完整学习笔记:按位与(&)+ 异或(^)
开发语言·c++·算法
天外飞雨道沧桑1 小时前
我在浏览器里练了一个 792 参数的大模型,它学会了补全 JavaScript
开发语言·javascript·ai·ecmascript·ai编程
hhhjhl1 小时前
Yup 跨端三方库仓颉适配全流程
java·开发语言
运维行者_1 小时前
网络监控工具怎么选?从设备监控到智能运维的能力清单
运维·服务器·开发语言·网络·人工智能·支持向量机·php
鶴哥只手遮天2 小时前
jQuery 4.0 源码架构与工程实践-第 01 章 · 全景与工程结构:一个 20 岁库的自省
前端·javascript·jquery
“AI国潮设计-小江”2 小时前
【AIGC实战】Python+SDXL打造“潮汕英歌舞IP”万能立牌插件,批量生成“发财元宝蛋糕”视觉营销方案(附源码)
开发语言·人工智能·python·prompt·aigc
lhbweilai3 小时前
分析FreeRTOS内核对象API的源码实现(八)
c语言·开发语言·stm32·单片机
努力努力再努力wz3 小时前
【C++高阶系列】C++20 协程底层原理:函数为什么能暂停?从状态机到 Task,彻底串起挂起、恢复与协程调度
开发语言·c++·c++20·1024程序员节
inksci3 小时前
在线编辑表格,功能强大,json互转
前端·javascript·html
咩咩啃树皮3 小时前
JavaScript 中 this 指向为什么总在变?
开发语言·前端·javascript