有没有一种方法可以使用Three.js获得Lithophane效果



目前,我已经尝试了具有透明性和不透明性的其他 Material ,但没有成功。

    <html lang="en">
<head>
    <title>Lith (Three.js)</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">

</head>
<body>

<script src="js/three.min.js"></script>
<script src="./js/dat.gui.min.js"></script>
<script src="./js/STLLoader.js"></script>
<script src="js/Detector.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/SkyShader.js"></script>
<script src="js/THREEx.WindowResize.js"></script>

<div id="ThreeJS" style="position: absolute; left:0px; top:0px"></div>
<script>
var container, scene, camera, renderer, controls, stats;
var clock = new THREE.Clock();
var cube;

init();
animate();

function init()
{
    // SCENE
    scene = new THREE.Scene();
    // CAMERA
    var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
    var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
    camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
    scene.add(camera);
    camera.position.set(0,150,400);
    camera.lookAt(scene.position);
    // RENDERER
    if ( Detector.webgl )
        renderer = new THREE.WebGLRenderer( {antialias:true} );
    else
        renderer = new THREE.CanvasRenderer();
    renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
    renderer.setClearColor( 0x999999 );
    container = document.getElementById( 'ThreeJS' );
    container.appendChild( renderer.domElement );
    // EVENTS
    THREEx.WindowResize(renderer, camera);

    controls = new THREE.OrbitControls( camera, renderer.domElement );

    // SKYBOX/FOG
    var skyBoxGeometry = new THREE.CubeGeometry( 10000, 10000, 10000 );
    var skyBoxMaterial = new THREE.MeshBasicMaterial( { color: 0x9999ff, side: THREE.BackSide } );
    var skyBox = new THREE.Mesh( skyBoxGeometry, skyBoxMaterial );
    // scene.add(skyBox);
    scene.fog = new THREE.FogExp2( 0x9999ff, 0.00025 );

    ////////////
    // CUSTOM //
    ////////////

    // must enable shadows on the renderer
    renderer.shadowMapEnabled = true;

    // "shadow cameras" show the light source and direction

    // spotlight #1 -- yellow, dark shadow
    var spotlight = new THREE.SpotLight(0xffff00);
    spotlight.position.set(0,150,-50);
    spotlight.shadowCameraVisible = true;
    spotlight.shadowDarkness = 0.8;
    spotlight.intensity = 2;
    // must enable shadow casting ability for the light
    spotlight.castShadow = true;
    scene.add(spotlight);

    var sphereSize = 10;
    var pointLightHelper = new THREE.SpotLightHelper( spotlight, sphereSize );
    scene.add( pointLightHelper );


    var light = new THREE.SpotLight(0x999999);
    light.intensity = 0.6;
    camera.add(light);

    var loader = new THREE.STLLoader();
    loader.load('./TestOriginal.stl', function(object) {
        meshObject = object;
        var color = new THREE.Color( 0xffffff );
        var material = new THREE.MeshPhongMaterial({
                    color: color,//'white',
                    side: THREE.DoubleSide,
                    //shading: THREE.SmoothShading,
                    opacity: 0.6,
                    transparent: true
                });
        this.mesh = new THREE.Mesh(object, material);

        mesh.position.set(0,0,0);
        scene.add(mesh);

        mesh.position.set(0,0,0);
        var newScale = 1;
        mesh.geometry.computeBoundingBox();
        boundingBox = mesh.geometry.boundingBox;
        mesh.translateX(-((boundingBox.max.x + boundingBox.min.x) * newScale) / 2);
        mesh.translateY(-((boundingBox.max.y + boundingBox.min.y) * newScale) / 2);
        mesh.translateZ(-((boundingBox.max.z + boundingBox.min.z) * newScale) / 2);
    });

    // floor: mesh to receive shadows
    var floorTexture = new THREE.ImageUtils.loadTexture( './checkerboard.jpg' );
    floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
    floorTexture.repeat.set( 10, 10 );
    // Note the change to Lambert material.
    var floorMaterial = new THREE.MeshLambertMaterial( { map: floorTexture, side: THREE.DoubleSide } );
    var floorGeometry = new THREE.PlaneGeometry(1000, 1000, 100, 100);
    var floor = new THREE.Mesh(floorGeometry, floorMaterial);
    floor.position.y = -80.5;
    floor.rotation.x = Math.PI / 2;
    floor.receiveShadow = true;
    scene.add(floor);
}

function animate()
{
    requestAnimationFrame( animate );
    render();
    update();
}

function update()
{
    controls.update();
}

function render()
{
    renderer.render( scene, camera );
}

</script>

</body>
</html>

和我的输出是这样的:

我也尝试过着色器 Material ,它给了我类似:javascript - 三个JS中的石烷效应-LMLPHP

我想要的是:应该从物体的背面发出光,并且物体的雕刻部分应该发光(相对于物体的深度)。

最佳答案

这绝不是答案,但是我真的认为最好的方法是编写(或查找)自定义着色器。首先,我要说的是,编写着色器是而不是的 child 的玩法:由于着色器编程语言的级别较低,并且严重依赖于深奥的几何/数学知识,因此着色器可能变得异常复杂。

经过一番窥探之后,您似乎需要一个可以实现“次表面散射”(Sub-Surface Scattering,俗称“SSS”)的着色器-本质上,这是光通过半透明对象的行为,基于其厚度(以及其他一些属性)我不会研究):

javascript - 三个JS中的石烷效应-LMLPHP

要实现Lithophane效果,您需要生成映射到对象网格的各种“厚度图”,然后自定义着色器将通过该图正确散射光以产生与所需效果相似的效果。

我是通过在DICE游戏中由首席渲染程序员执行的simple presentation中学到的所有内容的。这是演示文稿中的示例幻灯片:

javascript - 三个JS中的石烷效应-LMLPHP

该着色器基于厚度实时产生类似于此的效果:

javascript - 三个JS中的石烷效应-LMLPHP

如果您真的想达到这种效果,我强烈建议您阅读有关Cg或GLSL着色器编程的文章。我个人喜欢Cg,因为我被激励去学习它与Unity3D的兼容性。因此,我找到了学习Cg的绝佳资源,即the wikibook on the subject。它(某种程度上)特定于Unity,但是原理是合理的。

无论哪种方式,我都希望你在这个问题上运气好。希望这可以帮助!

10-07 20:12