Fixed Parallax without normal map in lighting.j3md

experimental
Nehon 10 years ago
parent 485af7cf2a
commit 6d3377a2a8
  1. 9
      jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.frag
  2. 16
      jme3-core/src/main/resources/Common/MatDefs/Light/Lighting.vert
  3. 27
      jme3-examples/src/main/java/jme3test/material/TestParallax.java
  4. 5
      jme3-testdata/src/main/resources/Textures/Terrain/BrickWall/BrickWall.j3m
  5. 4
      jme3-testdata/src/main/resources/Textures/Terrain/BrickWall/BrickWall2.j3m

@ -37,6 +37,7 @@ varying vec3 SpecularSum;
#endif #endif
#if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING) #if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING)
uniform float m_ParallaxHeight; uniform float m_ParallaxHeight;
varying vec3 vViewDirPrlx;
#endif #endif
#ifdef LIGHTMAP #ifdef LIGHTMAP
@ -78,18 +79,18 @@ void main(){
#ifdef STEEP_PARALLAX #ifdef STEEP_PARALLAX
#ifdef NORMALMAP_PARALLAX #ifdef NORMALMAP_PARALLAX
//parallax map is stored in the alpha channel of the normal map //parallax map is stored in the alpha channel of the normal map
newTexCoord = steepParallaxOffset(m_NormalMap, vViewDir, texCoord, m_ParallaxHeight); newTexCoord = steepParallaxOffset(m_NormalMap, vViewDirPrlx, texCoord, m_ParallaxHeight);
#else #else
//parallax map is a texture //parallax map is a texture
newTexCoord = steepParallaxOffset(m_ParallaxMap, vViewDir, texCoord, m_ParallaxHeight); newTexCoord = steepParallaxOffset(m_ParallaxMap, vViewDirPrlx, texCoord, m_ParallaxHeight);
#endif #endif
#else #else
#ifdef NORMALMAP_PARALLAX #ifdef NORMALMAP_PARALLAX
//parallax map is stored in the alpha channel of the normal map //parallax map is stored in the alpha channel of the normal map
newTexCoord = classicParallaxOffset(m_NormalMap, vViewDir, texCoord, m_ParallaxHeight); newTexCoord = classicParallaxOffset(m_NormalMap, vViewDirPrlx, texCoord, m_ParallaxHeight);
#else #else
//parallax map is a texture //parallax map is a texture
newTexCoord = classicParallaxOffset(m_ParallaxMap, vViewDir, texCoord, m_ParallaxHeight); newTexCoord = classicParallaxOffset(m_ParallaxMap, vViewDirPrlx, texCoord, m_ParallaxHeight);
#endif #endif
#endif #endif
#else #else

@ -48,6 +48,10 @@ varying vec3 lightVec;
uniform vec4 g_LightDirection; uniform vec4 g_LightDirection;
#endif #endif
#if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP))) && !defined(VERTEX_LIGHTING)
varying vec3 vViewDirPrlx;
#endif
#ifdef USE_REFLECTION #ifdef USE_REFLECTION
uniform vec3 g_CameraPosition; uniform vec3 g_CameraPosition;
@ -107,17 +111,25 @@ void main(){
wvLightPos.w = g_LightPosition.w; wvLightPos.w = g_LightPosition.w;
vec4 lightColor = g_LightColor; vec4 lightColor = g_LightColor;
#if defined(NORMALMAP) && !defined(VERTEX_LIGHTING) #if (defined(NORMALMAP) || defined(PARALLAXMAP)) && !defined(VERTEX_LIGHTING)
vec3 wvTangent = normalize(TransformNormal(modelSpaceTan)); vec3 wvTangent = normalize(TransformNormal(modelSpaceTan));
vec3 wvBinormal = cross(wvNormal, wvTangent); vec3 wvBinormal = cross(wvNormal, wvTangent);
mat3 tbnMat = mat3(wvTangent, wvBinormal * inTangent.w,wvNormal); mat3 tbnMat = mat3(wvTangent, wvBinormal * inTangent.w,wvNormal);
#endif
vViewDir = -wvPosition * tbnMat; #if defined(NORMALMAP) && !defined(VERTEX_LIGHTING)
vViewDir = -wvPosition * tbnMat;
#if (defined(PARALLAXMAP) || (defined(NORMALMAP_PARALLAX) && defined(NORMALMAP)))
vViewDirPrlx = vViewDir;
#endif
lightComputeDir(wvPosition, lightColor.w, wvLightPos, vLightDir, lightVec); lightComputeDir(wvPosition, lightColor.w, wvLightPos, vLightDir, lightVec);
vLightDir.xyz = (vLightDir.xyz * tbnMat).xyz; vLightDir.xyz = (vLightDir.xyz * tbnMat).xyz;
#elif !defined(VERTEX_LIGHTING) #elif !defined(VERTEX_LIGHTING)
vNormal = wvNormal; vNormal = wvNormal;
vViewDir = viewDir; vViewDir = viewDir;
#if defined(PARALLAXMAP)
vViewDirPrlx = -wvPosition * tbnMat;
#endif
lightComputeDir(wvPosition, lightColor.w, wvLightPos, vLightDir, lightVec); lightComputeDir(wvPosition, lightColor.w, wvLightPos, vLightDir, lightVec);
#endif #endif

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009-2012 jMonkeyEngine * Copyright (c) 2009-2015 jMonkeyEngine
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -39,20 +39,17 @@ import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight; import com.jme3.light.DirectionalLight;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.math.*; import com.jme3.math.*;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.FXAAFilter;
import com.jme3.renderer.queue.RenderQueue.ShadowMode; import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry; import com.jme3.scene.Geometry;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Quad; import com.jme3.scene.shape.Quad;
import com.jme3.texture.Texture.WrapMode;
import com.jme3.util.SkyFactory; import com.jme3.util.SkyFactory;
import com.jme3.util.TangentBinormalGenerator; import com.jme3.util.TangentBinormalGenerator;
public class TestParallax extends SimpleApplication { public class TestParallax extends SimpleApplication {
private Vector3f lightDir = new Vector3f(-1, -1, .5f).normalizeLocal(); private final Vector3f lightDir = new Vector3f(-1, -1, .5f).normalizeLocal();
public static void main(String[] args) { public static void main(String[] args) {
TestParallax app = new TestParallax(); TestParallax app = new TestParallax();
@ -60,7 +57,7 @@ public class TestParallax extends SimpleApplication {
} }
public void setupSkyBox() { public void setupSkyBox() {
rootNode.attachChild(SkyFactory.createSky(assetManager, "Scenes/Beach/FullskiesSunset0068.dds", false)); rootNode.attachChild(SkyFactory.createSky(assetManager, "Scenes/Beach/FullskiesSunset0068.dds", SkyFactory.EnvMapType.CubeMap));
} }
DirectionalLight dl; DirectionalLight dl;
@ -75,12 +72,7 @@ public class TestParallax extends SimpleApplication {
public void setupFloor() { public void setupFloor() {
mat = assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall2.j3m"); mat = assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall2.j3m");
mat.getTextureParam("DiffuseMap").getTextureValue().setWrap(WrapMode.Repeat); //mat = assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m");
mat.getTextureParam("NormalMap").getTextureValue().setWrap(WrapMode.Repeat);
// Node floorGeom = (Node) assetManager.loadAsset("Models/WaterTest/WaterTest.mesh.xml");
//Geometry g = ((Geometry) floorGeom.getChild(0));
//g.getMesh().scaleTextureCoordinates(new Vector2f(10, 10));
Node floorGeom = new Node("floorGeom"); Node floorGeom = new Node("floorGeom");
Quad q = new Quad(100, 100); Quad q = new Quad(100, 100);
@ -100,9 +92,9 @@ public class TestParallax extends SimpleApplication {
public void setupSignpost() { public void setupSignpost() {
Spatial signpost = assetManager.loadModel("Models/Sign Post/Sign Post.mesh.xml"); Spatial signpost = assetManager.loadModel("Models/Sign Post/Sign Post.mesh.xml");
Material mat = assetManager.loadMaterial("Models/Sign Post/Sign Post.j3m"); Material matSp = assetManager.loadMaterial("Models/Sign Post/Sign Post.j3m");
TangentBinormalGenerator.generate(signpost); TangentBinormalGenerator.generate(signpost);
signpost.setMaterial(mat); signpost.setMaterial(matSp);
signpost.rotate(0, FastMath.HALF_PI, 0); signpost.rotate(0, FastMath.HALF_PI, 0);
signpost.setLocalTranslation(12, 23.5f, 30); signpost.setLocalTranslation(12, 23.5f, 30);
signpost.setLocalScale(4); signpost.setLocalScale(4);
@ -116,7 +108,6 @@ public class TestParallax extends SimpleApplication {
cam.setRotation(new Quaternion(0.05173137f, 0.92363626f, -0.13454558f, 0.35513034f)); cam.setRotation(new Quaternion(0.05173137f, 0.92363626f, -0.13454558f, 0.35513034f));
flyCam.setMoveSpeed(30); flyCam.setMoveSpeed(30);
setupLighting(); setupLighting();
setupSkyBox(); setupSkyBox();
setupFloor(); setupFloor();
@ -124,13 +115,14 @@ public class TestParallax extends SimpleApplication {
inputManager.addListener(new AnalogListener() { inputManager.addListener(new AnalogListener() {
@Override
public void onAnalog(String name, float value, float tpf) { public void onAnalog(String name, float value, float tpf) {
if ("heightUP".equals(name)) { if ("heightUP".equals(name)) {
parallaxHeigh += 0.0001; parallaxHeigh += 0.01;
mat.setFloat("ParallaxHeight", parallaxHeigh); mat.setFloat("ParallaxHeight", parallaxHeigh);
} }
if ("heightDown".equals(name)) { if ("heightDown".equals(name)) {
parallaxHeigh -= 0.0001; parallaxHeigh -= 0.01;
parallaxHeigh = Math.max(parallaxHeigh, 0); parallaxHeigh = Math.max(parallaxHeigh, 0);
mat.setFloat("ParallaxHeight", parallaxHeigh); mat.setFloat("ParallaxHeight", parallaxHeigh);
} }
@ -142,6 +134,7 @@ public class TestParallax extends SimpleApplication {
inputManager.addListener(new ActionListener() { inputManager.addListener(new ActionListener() {
@Override
public void onAction(String name, boolean isPressed, float tpf) { public void onAction(String name, boolean isPressed, float tpf) {
if (isPressed && "toggleSteep".equals(name)) { if (isPressed && "toggleSteep".equals(name)) {
steep = !steep; steep = !steep;

@ -1,8 +1,7 @@
Material Pong Rock : Common/MatDefs/Light/Lighting.j3md { Material Pong Rock : Common/MatDefs/Light/Lighting.j3md {
MaterialParameters { MaterialParameters {
Shininess: 2.0 Shininess: 2.0
DiffuseMap : Textures/Terrain/BrickWall/BrickWall.jpg DiffuseMap : Repeat Textures/Terrain/BrickWall/BrickWall.jpg
NormalMap : Textures/Terrain/BrickWall/BrickWall_normal.jpg ParallaxMap : Repeat Textures/Terrain/BrickWall/BrickWall_height.jpg
ParallaxMap : Textures/Terrain/BrickWall/BrickWall_height.jpg
} }
} }

@ -1,8 +1,8 @@
Material Pong Rock : Common/MatDefs/Light/Lighting.j3md { Material Pong Rock : Common/MatDefs/Light/Lighting.j3md {
MaterialParameters { MaterialParameters {
Shininess: 2.0 Shininess: 2.0
DiffuseMap : Textures/Terrain/BrickWall/BrickWall.jpg DiffuseMap : Repeat Textures/Terrain/BrickWall/BrickWall.jpg
NormalMap : Textures/Terrain/BrickWall/BrickWall_normal_parallax.dds NormalMap : Repeat Textures/Terrain/BrickWall/BrickWall_normal_parallax.dds
PackedNormalParallax: true PackedNormalParallax: true
} }
} }
Loading…
Cancel
Save