Three.js - 鱼眼效果

问题描述 投票:0回答:5

所以,我用了 Three.js ,效果很好。我唯一不明白的是如何制作具有真正鱼眼效果的相机。

这怎么可能?

camera.setLens()

camera three.js fisheye
5个回答
16
投票

可以使用 Giliam de Carpentier 的镜头畸变着色器来实现鱼眼效果。

着色器代码:

function getDistortionShaderDefinition()
{
    return {

        uniforms: {
            "tDiffuse":         { type: "t", value: null },
            "strength":         { type: "f", value: 0 },
            "height":           { type: "f", value: 1 },
            "aspectRatio":      { type: "f", value: 1 },
            "cylindricalRatio": { type: "f", value: 1 }
        },

        vertexShader: [
            "uniform float strength;",          // s: 0 = perspective, 1 = stereographic
            "uniform float height;",            // h: tan(verticalFOVInRadians / 2)
            "uniform float aspectRatio;",       // a: screenWidth / screenHeight
            "uniform float cylindricalRatio;",  // c: cylindrical distortion ratio. 1 = spherical

            "varying vec3 vUV;",                // output to interpolate over screen
            "varying vec2 vUVDot;",             // output to interpolate over screen

            "void main() {",
                "gl_Position = projectionMatrix * (modelViewMatrix * vec4(position, 1.0));",

                "float scaledHeight = strength * height;",
                "float cylAspectRatio = aspectRatio * cylindricalRatio;",
                "float aspectDiagSq = aspectRatio * aspectRatio + 1.0;",
                "float diagSq = scaledHeight * scaledHeight * aspectDiagSq;",
                "vec2 signedUV = (2.0 * uv + vec2(-1.0, -1.0));",

                "float z = 0.5 * sqrt(diagSq + 1.0) + 0.5;",
                "float ny = (z - 1.0) / (cylAspectRatio * cylAspectRatio + 1.0);",

                "vUVDot = sqrt(ny) * vec2(cylAspectRatio, 1.0) * signedUV;",
                "vUV = vec3(0.5, 0.5, 1.0) * z + vec3(-0.5, -0.5, 0.0);",
                "vUV.xy += uv;",
            "}"
        ].join("\n"),

        fragmentShader: [
            "uniform sampler2D tDiffuse;",      // sampler of rendered scene?s render target
            "varying vec3 vUV;",                // interpolated vertex output data
            "varying vec2 vUVDot;",             // interpolated vertex output data

            "void main() {",
                "vec3 uv = dot(vUVDot, vUVDot) * vec3(-0.5, -0.5, -1.0) + vUV;",
                "gl_FragColor = texture2DProj(tDiffuse, uv);",
            "}"
        ].join("\n")

    };
}

使用效果编辑器设置效果的一种方法(假设已创建

scene
renderer
):

// Create camera
camera = new THREE.PerspectiveCamera( 100, window.innerWidth / window.innerHeight, 1, 1000000 );
camera.position.z = 800;

// Create effect composer
composer = new THREE.EffectComposer( renderer );
composer.addPass( new THREE.RenderPass( scene, camera ) );

// Add distortion effect to effect composer
var effect = new THREE.ShaderPass( getDistortionShaderDefinition() );
composer.addPass( effect );
effect.renderToScreen = true;

// Setup distortion effect
var horizontalFOV = 140;
var strength = 0.5;
var cylindricalRatio = 2;
var height = Math.tan(THREE.Math.degToRad(horizontalFOV) / 2) / camera.aspect;

camera.fov = Math.atan(height) * 2 * 180 / 3.1415926535;
camera.updateProjectionMatrix();

effect.uniforms[ "strength" ].value = strength;
effect.uniforms[ "height" ].value = height;
effect.uniforms[ "aspectRatio" ].value = camera.aspect;
effect.uniforms[ "cylindricalRatio" ].value = cylindricalRatio;

需要以下脚本,例如可以从 third.js GitHub 页面找到它们:

<script src="examples/js/postprocessing/EffectComposer.js"></script>
<script src="examples/js/postprocessing/RenderPass.js"></script>
<script src="examples/js/postprocessing/MaskPass.js"></script>
<script src="examples/js/postprocessing/ShaderPass.js"></script>
<script src="examples/js/shaders/CopyShader.js"></script>

链接到 Giliam 的示例:http://www.decarpentier.nl/downloads/lens Distortion-webgl/lens Distortion-webgl.html

Giliam 关于镜头畸变的文章链接:http://www.decarpentier.nl/lens- Distortion

我使用镜头畸变效果的测试图像:


1
投票

将相机放入反射球内。确保球体是双面的。如果您想在场景中移动相机和球体,请将其设置为父级。就像魅力一样工作:

http://tileableart.com/code/NOCosmos/test.html

借自:

http://mrdoob.github.io/ Three.js/examples/webgl_materials_cubemap_dynamic2.html

cubeCamera = new THREE.CubeCamera( 1, 3000, 1024);
            cubeCamera.renderTarget.minFilter = THREE.LinearMipMapLinearFilter;
scene.add( cubeCamera );
camParent.add(cubeCamera);
var material = new THREE.MeshBasicMaterial( { envMap: cubeCamera.renderTarget } );
material.side = THREE.DoubleSide;
sphere = new THREE.Mesh( new THREE.SphereGeometry( 2, 60, 30 ), material );

0
投票

一种方法是在相机上设置大视野:

new THREE.PerspectiveCamera(140, ... )

从技术上讲,这不会是鱼眼效果,但它可能就是您正在寻找的效果。

在真正的相机镜头中,获得大视场而不失真可能会使镜头相当昂贵,但在计算机图形学中,这是最简单的方法。

真正的鱼眼镜头会扭曲图像,使直线变得弯曲,如下图所示:

Fisheye example, CC by Ilveon at Wikipedia

如果您想创建具有这种扭曲的实际鱼眼效果,则必须修改几何形状,如Three.js 的鱼眼示例所示。在该示例中,几何图形实际上是预先修改的,但对于更高级的场景,您需要使用顶点着色器来动态更新顶点。


0
投票

广角镜头通常焦距非常短。

为了实现超广角,我们需要缩短焦距。

请注意,鱼眼镜头是一种超广角镜头。

要缩短焦距(或实现超广角),只需增加 FOV(视野)即可,因为 FOV 与焦距成反比。

示例:

var camera_1 = new THREE.PerspectiveCamera( 45, width / height, 1, 1000 );

var camera_2 = new THREE.PerspectiveCamera( 80, width / height, 1, 1000 );

这里camera_2是广角设置。

注意

为了达到想要的效果,可能需要调整相机位置。


0
投票

可以获得高视野的鱼眼效果。

var fishCamera = new THREE.PerspectiveCamera( 110, window.innerWidth / window.innerHeight, 1, 1100 );
var normalCamera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1100 );

// or

camera.fov = 110 
camera.updateProjectionMatrix();

现场示例: http://mrdoob.github.com/ Three.js/examples/canvas_geometry_panorama_fisheye.html

© www.soinside.com 2019 - 2024. All rights reserved.