1+
2+ #include " threepp/materials/ShaderMaterial.hpp"
3+ #include " threepp/renderers/GLRenderTarget.hpp"
4+ #include " threepp/textures/DepthTexture.hpp"
5+ #include " threepp/threepp.hpp"
6+
7+ #include < threepp/geometries/TorusKnotGeometry.hpp>
8+
9+ #include < cmath>
10+
11+ using namespace threepp ;
12+
13+ namespace {
14+ void setupScene (Scene& scene) {
15+
16+ const auto geometry = TorusKnotGeometry::create (1 , 0.3 , 128 , 64 );
17+ const auto material = MeshBasicMaterial::create ({{" color" , Color::blue}});
18+
19+ const auto count = 50 ;
20+ const auto scale = 5 ;
21+
22+ for (auto i = 0 ; i < count; i++) {
23+
24+ const auto r = math::randFloat () * 2 .0f * math::PI;
25+ const auto z = (math::randFloat () * 2 .0f ) - 1 .0f ;
26+ const auto zScale = std::sqrt (1 .0f - z * z) * scale;
27+
28+ const auto mesh = Mesh::create (geometry, material);
29+ mesh->position .set (
30+ std::cos (r) * zScale,
31+ std::sin (r) * zScale,
32+ z * scale);
33+ mesh->rotation .set (math::randFloat (), math::randFloat (), math::randFloat ());
34+ scene.add (mesh);
35+ }
36+ }
37+ }// namespace
38+
39+ int main () {
40+
41+ Canvas canvas (" Depth texture" );
42+ GLRenderer renderer (canvas.size ());
43+ renderer.checkShaderErrors = true ;
44+
45+ PerspectiveCamera camera (70 , canvas.aspect (), 0 .01f , 50 .f );
46+ camera.position .set (0 , 0 , 4 );
47+
48+ Scene scene;
49+ setupScene (scene);
50+
51+ OrbitControls controls (camera, canvas);
52+ controls.enableDamping = true ;
53+
54+
55+ GLRenderTarget::Options options;
56+ options.format = Format::RGB;
57+ options.minFilter = Filter::Nearest;
58+ options.magFilter = Filter::Nearest;
59+ options.generateMipmaps = false ;
60+ options.stencilBuffer = false ;
61+ options.depthBuffer = true ;
62+
63+ options.depthTexture = DepthTexture::create ();
64+ options.depthTexture ->format = Format::Depth;
65+ options.depthTexture ->type = Type::Float;
66+
67+
68+ GLRenderTarget target (canvas.size ().width (), canvas.size ().height (), options);
69+
70+
71+ auto postMaterial = ShaderMaterial::create ();
72+ postMaterial->vertexShader = R"(
73+ varying vec2 vUv;
74+
75+ void main() {
76+ vUv = uv;
77+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
78+ }
79+ )" ;
80+ postMaterial->fragmentShader = R"(
81+ #include <packing>
82+
83+ varying vec2 vUv;
84+ uniform sampler2D tDiffuse;
85+ uniform sampler2D tDepth;
86+ uniform float cameraNear;
87+ uniform float cameraFar;
88+
89+
90+ float readDepth( sampler2D depthSampler, vec2 coord ) {
91+ float fragCoordZ = texture2D( depthSampler, coord ).x;
92+ float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );
93+ return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
94+ }
95+
96+ void main() {
97+ //vec3 diffuse = texture2D( tDiffuse, vUv ).rgb;
98+ float depth = readDepth( tDepth, vUv );
99+
100+ gl_FragColor.rgb = 1 - vec3( depth );
101+ gl_FragColor.a = 1.0;
102+ }
103+ )" ;
104+
105+ postMaterial->uniforms = {
106+ {" tDiffuse" , Uniform ()},
107+ {" tDepth" , Uniform ()},
108+ {" cameraNear" , Uniform (camera.nearPlane )},
109+ {" cameraFar" , Uniform (camera.farPlane )}};
110+
111+ OrthographicCamera postCamera (-1 , 1 , 1 , -1 , 0 , 1 );
112+ const auto postPlane = PlaneGeometry::create (2 , 2 );
113+ const auto postQuad = Mesh::create (postPlane, postMaterial);
114+ Scene postScene;
115+ postScene.add (postQuad);
116+
117+
118+ canvas.onWindowResize ([&](WindowSize size) {
119+ renderer.setSize (size);
120+ camera.aspect = canvas.aspect ();
121+ camera.updateProjectionMatrix ();
122+ });
123+
124+ canvas.animate ([&] {
125+ renderer.setRenderTarget (&target);
126+ renderer.render (scene, camera);
127+
128+ postMaterial->uniforms .at (" tDiffuse" ).setValue (target.texture .get ());
129+ postMaterial->uniforms .at (" tDepth" ).setValue (target.depthTexture .get ());
130+
131+ renderer.setRenderTarget (nullptr );
132+
133+ renderer.render (postScene, postCamera);
134+
135+ controls.update ();
136+ });
137+ }
0 commit comments