depthoffieldrenderpass.java
来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 475 行 · 第 1/2 页
JAVA
475 行
/* * Copyright (c) 2003-2009 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of 'jMonkeyEngine' nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package com.jmex.effects.glsl;import java.net.URL;import com.jme.image.Texture;import com.jme.image.Texture2D;import com.jme.renderer.Camera;import com.jme.renderer.ColorRGBA;import com.jme.renderer.Renderer;import com.jme.renderer.TextureRenderer;import com.jme.renderer.pass.Pass;import com.jme.scene.Node;import com.jme.scene.Spatial;import com.jme.scene.shape.Quad;import com.jme.scene.state.BlendState;import com.jme.scene.state.GLSLShaderObjectsState;import com.jme.scene.state.RenderState;import com.jme.scene.state.TextureState;import com.jme.system.DisplaySystem;/** * GLSL Depth of Field effect pass. - Creating a depth texture with a root * Spatial and its subspatials - Use it on full screen texture downsampled to * blur it with stronger opacity and blurring on far away parts - render result * (unblended) on the screen overwriting with the blurred parts. * * @author Paul Illes - initial implementation of DepthOfFieldRenderPass for jME * 1.0 based on partially MrCorder's shaders plus : about original Ogre * DoF demo: "Depth of Field" demo for Ogre Copyright (C) 2006 Christian * Lindequist Larsen This code is in the public domain. You may do * whatever you want with it. - Used from that part the depth shader * with some modifications. * * @author (MrCoder) - initial implementation of BloomRenderPass (original pass) * @author Joshua Slack - Enhancements and reworking to use a single * texrenderer, ability to reuse existing back buffer, faster blur, * throttling speed-up, etc. */public class DepthOfFieldRenderPass extends Pass { private static final long serialVersionUID = 1L; /** The time between texture updates */ private float throttle = 0.001f; /** The amount of time since the last update */ private float sinceLast = 1; /** The renderer used to produce the DoF texture */ private TextureRenderer tRenderer; /** The final resulting texture to be splatted across the screen */ private Texture2D resultTexture; /** The texture storing the depth values to work out how much to blur by */ private Texture2D depthTexture; /** The copy of the screen */ private Texture2D screenTexture; /** * The full scren quad used to display the generated texture across the * screen */ private Quad fullScreenQuad; /** Shader to render the final texture */ private GLSLShaderObjectsState finalShader; /** Shader to record depth in to a texture */ private GLSLShaderObjectsState depthShader; /** Shader to provide bluring based on depth */ private GLSLShaderObjectsState dofShader; /** The size of the blur kernel */ private float blurSize; /** The depth at which blur starts */ public float nearBlurDepth = 10f; /** The depth at which the viewer is focussing */ public float focalPlaneDepth = 25f; /** The depth at which we're at full blur */ public float farBlurDepth = 50f; /** The cut off point where we stop blurring */ public float blurrinessCutoff = 1f; /** True if this pass is supported */ private boolean supported = true; /** The location where we'll find our shaders - relative to this class */ private static String shaderDirectory = "data/"; /** * A place to internally save previous enforced states setup before * rendering this pass */ private RenderState[] preStates = new RenderState[RenderState.StateType.values().length]; /** A utility to render the spatials added in one texture render */ private final SpatialsRenderNode spatialsRenderNode = new SpatialsRenderNode(); /** * Creates a new DOG renderpass * * @param cam * Camera used for rendering the bloomsource * @param renderScale * Scale of bloom texture */ public DepthOfFieldRenderPass(Camera cam, int renderScale) { DisplaySystem display = DisplaySystem.getDisplaySystem(); resetParameters(); // Create texture renderers and rendertextures(alternating between two // not to overwrite pbuffers) tRenderer = display.createTextureRenderer(display.getWidth() / renderScale, display.getHeight() / renderScale, TextureRenderer.Target.Texture2D); if (!tRenderer.isSupported()) { supported = false; return; } tRenderer.setBackgroundColor(new ColorRGBA(0.0f, 0.0f, 0.0f, 1.0f)); tRenderer.setCamera(cam); screenTexture = new Texture2D(); screenTexture.setWrap(Texture.WrapMode.Clamp); tRenderer.setupTexture(screenTexture); resultTexture = new Texture2D(); resultTexture.setWrap(Texture.WrapMode.Clamp); resultTexture .setMagnificationFilter(Texture.MagnificationFilter.Bilinear); tRenderer.setupTexture(resultTexture); depthTexture = new Texture2D(); depthTexture.setWrap(Texture.WrapMode.Clamp); tRenderer.setupTexture(depthTexture); if (!GLSLShaderObjectsState.isSupported()) { supported = false; return; } // Create final shader(basic texturing) finalShader = display.getRenderer().createGLSLShaderObjectsState(); finalShader.load(getResource(shaderDirectory + "dof_fullscreen.vert"), getResource(shaderDirectory + "dof_fullscreen.frag")); finalShader.setEnabled(true); // DOF depthShader = display.getRenderer().createGLSLShaderObjectsState(); depthShader.load(getResource(shaderDirectory + "dof_1_depth.vert"), getResource(shaderDirectory + "dof_1_depth.frag")); depthShader.setEnabled(true); // Create dof shader dofShader = display.getRenderer().createGLSLShaderObjectsState(); dofShader.load(getResource(shaderDirectory + "dof_simple.vert"), getResource(shaderDirectory + "dof_3_dof_2.frag")); dofShader.setEnabled(true); // Create fullscreen quad fullScreenQuad = new Quad("FullScreenQuad", display.getWidth() / 4, display.getHeight() / 4); fullScreenQuad.getLocalRotation().set(0, 0, 0, 1); fullScreenQuad.getLocalTranslation().set(display.getWidth() / 2, display.getHeight() / 2, 0); fullScreenQuad.getLocalScale().set(1, 1, 1); fullScreenQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO); fullScreenQuad.setCullHint(Spatial.CullHint.Never); fullScreenQuad .setTextureCombineMode(Spatial.TextureCombineMode.Replace); fullScreenQuad.setLightCombineMode(Spatial.LightCombineMode.Off); TextureState ts = display.getRenderer().createTextureState(); ts.setEnabled(true); fullScreenQuad.setRenderState(ts); BlendState as = display.getRenderer().createBlendState(); // no blending, result texture has to overwrite screen - not blend! as.setTestEnabled(true); as.setTestFunction(BlendState.TestFunction.GreaterThan); as.setEnabled(true); fullScreenQuad.setRenderState(as); fullScreenQuad.updateRenderState(); fullScreenQuad.updateGeometricState(0.0f, true); } /** * Reset bloom parameters to default */ public void resetParameters() { nearBlurDepth = 10f; focalPlaneDepth = 25f; farBlurDepth = 50f; blurrinessCutoff = 50f; blurSize = 0.005f; } /** * Release pbuffers in TextureRenderer's. Preferably called from user * cleanup method. */ public void cleanup() { super.cleanUp(); if (tRenderer != null) { tRenderer.cleanup(); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?