【问题标题】:How to locate texture on sphere in jME3?如何在 jME3 中定位球体上的纹理?
【发布时间】:2014-07-06 12:27:43
【问题描述】:

我想在球体上放置 JPEG 纹理贴图。它对我有用,但我想将纹理旋转 180 度。即我希望图像不是从零 UV 坐标开始,而是更早。

更新

我尝试重新分配球体的纹理坐标。纹理坐标是浮动的,我希望它们不会被限制在 [0..1] 的范围内。否则它应该将我的图像放置在 [0..1 x 0..1] 的区域中。

它做了类似后者的事情,但并不精确:

即整个图像被放入一个球体的小区域。但是,它所在的这个确切区域对应于负值U,即在相同的经度上,图像边缘在之前的实验中(顶部球体)。

为什么?

图片在这里:https://en.wikipedia.org/wiki/File:Equirectangular_projection_SW.jpg

代码如下:

package tests.com.jme3;

import java.nio.FloatBuffer;

import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapText;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.scene.VertexBuffer.Usage;
import com.jme3.scene.shape.Sphere;
import com.jme3.util.BufferUtils;

public class Try_TextureTransform  extends SimpleApplication {

    public static void main(String[] args) {
        Try_TextureTransform app = new Try_TextureTransform();
        app.setShowSettings(false);
        app.start(); // start the game
    }

    final float speed = 0.01f;

    BitmapText hudText;
    Sphere sphere1Mesh, sphere2Mesh;
    Material sphere1Mat, sphere2Mat;
    Geometry sphere1Geo, sphere2Geo;
    Quaternion orientation;
    DirectionalLight sun;

    @Override
    public void simpleInitApp() {

        flyCam.setEnabled(false);
        setDisplayStatView(false); 
        setDisplayFps(false);


        hudText = new BitmapText(guiFont, false);          
        hudText.setSize(guiFont.getCharSet().getRenderedSize());      // font size
        hudText.setColor(ColorRGBA.Blue);                             // font color
        hudText.setText("");             // the text
        hudText.setLocalTranslation(300, hudText.getLineHeight()*2, 0); // position
        guiNode.attachChild(hudText);

        sphere1Mesh = new Sphere(50, 50, 2);
        sphere1Mesh.setTextureMode(Sphere.TextureMode.Projected); // matrc

        sphere1Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        sphere1Mat.setTexture("ColorMap", assetManager.loadTexture("textures/Equirectangular_projection_SW.jpg"));

        sphere1Geo = new Geometry("Sphere2", sphere1Mesh);
        sphere1Geo.setMaterial(sphere1Mat); 
        sphere1Geo.setLocalTranslation(0, 0, 2);

        sphere2Mesh = new Sphere(50, 50, 2);

        VertexBuffer vb = sphere2Mesh.getBuffer(Type.Position);
        FloatBuffer fb = (FloatBuffer) vb.getData();
        float[] vertexCoordinates = BufferUtils.getFloatArray(fb);

        VertexBuffer vb2 = sphere2Mesh.getBuffer(Type.TexCoord);
        FloatBuffer fb2 = (FloatBuffer) vb2.getData();
        float[] uvCoordinates = BufferUtils.getFloatArray(fb2);

        double rho;
        for (int i = 0; i < vertexCoordinates.length/3; ++i) {

            uvCoordinates[i*2] = (float) Math.atan2(vertexCoordinates[i*3+1], vertexCoordinates[i*3]);
            rho = Math.sqrt(Math.pow( vertexCoordinates[i*3], 2) + Math.pow( vertexCoordinates[i*3+1], 2));
            uvCoordinates[i*2+1] = (float) Math.atan2(vertexCoordinates[i*3+2], rho);
        }
      //apply new texture coordinates
        VertexBuffer uvCoordsBuffer = new VertexBuffer(Type.TexCoord);
        uvCoordsBuffer.setupData(Usage.Static, 2, com.jme3.scene.VertexBuffer.Format.Float, BufferUtils.createFloatBuffer(uvCoordinates));
        sphere2Mesh.clearBuffer(Type.TexCoord);
        sphere2Mesh.setBuffer(uvCoordsBuffer);


        //sphere2Mesh.setTextureMode(Sphere.TextureMode.Projected); // better quality on spheres

        sphere2Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        sphere2Mat.setTexture("ColorMap", assetManager.loadTexture("textures/Equirectangular_projection_SW.jpg"));

        sphere2Geo = new Geometry("Sphere2", sphere2Mesh);
        sphere2Geo.setMaterial(sphere2Mat); 
        sphere2Geo.setLocalTranslation(0, 0, -2);

        cam.setLocation(new Vector3f(-10, 0, 0));
        cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Z);

        rootNode.attachChild(sphere1Geo);
        rootNode.attachChild(sphere2Geo); 

    }

    @Override
    public void simpleUpdate(float tpf) {


        Vector2f cursorPosition = inputManager.getCursorPosition();
        Vector3f cursorPositionWorld = cam.getWorldCoordinates(cursorPosition, 1);

        orientation = new Quaternion().fromAngleAxis(cursorPositionWorld.z*speed, Vector3f.UNIT_Y);
        orientation.multLocal(new Quaternion().fromAngleAxis(-cursorPositionWorld.y*speed, Vector3f.UNIT_Z));

        rootNode.setLocalRotation(orientation);



    }

}

【问题讨论】:

  • 可能会更容易旋转球体本身或使纹理与您的 UV 坐标一致。 UV 坐标定义了纹理“固定”到形状的位置,所以如果你能做到这一点,那将是一个令人讨厌的 hack
  • 坐标是固定的,为什么图片也要固定呢?
  • O,所以你的意思是你想在 Java 中从旧图像创建一个新图像?如果你只做一次,当然可以,但在 Java 之外更容易做。在任何图像处理程序中,您都可以在 30 秒内完成
  • 我无法在图形编辑器中做到精确(我们正在谈论地图)。另外,我知道 Java 有可能,但想知道 JME 中是否有可能。
  • JME 是 Java 之上的一个库(我知道你知道,我在强调),复制核心功能会浪费他们的时间

标签: java 3d jmonkeyengine uv-mapping


【解决方案1】:

执行此操作的正确方法是按照您认为合适的方式旋转几何图形或编辑纹理(技术 1 和 2),但是因为您谈到自己修改纹理坐标,所以我包括技术 3 和 4,以防您使用这个例子是为了学习一个更大的技术,当它合适的时候。

技术 1 - 旋转几何体

旋转几何体,使其按照您想要的方式定向。这是迄今为止最简单、最合适和最容易理解的技术,也是我推荐的

    //Add this
    Quaternion quat=new Quaternion();
    quat.fromAngles(0 ,0 , FastMath.PI);
    sphere1Geo.setLocalRotation(quat);

完整程序

public class Main extends SimpleApplication {

    public static void main(String[] args) {
        Main app = new Main();
        app.setShowSettings(false);
        app.start(); // start the game
    }

    final float speed = 0.01f;

    BitmapText hudText;
    Quaternion orientation;
    DirectionalLight sun;

    @Override
    public void simpleInitApp() {

        flyCam.setEnabled(false);
        setDisplayStatView(false); 
        setDisplayFps(false);


        hudText = new BitmapText(guiFont, false);          
        hudText.setSize(guiFont.getCharSet().getRenderedSize());      // font size
        hudText.setColor(ColorRGBA.Blue);                             // font color
        hudText.setText("");             // the text
        hudText.setLocalTranslation(300, hudText.getLineHeight()*2, 0); // position
        guiNode.attachChild(hudText);

        cam.setLocation(new Vector3f(10, 0, 0));
        cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Z);

        addOriginalSphere();
        addRotatedSphere();

    }

    public void addOriginalSphere(){
        Sphere sphere1Mesh = new Sphere(50, 50, 2);
        sphere1Mesh.setTextureMode(Sphere.TextureMode.Projected); // matrc

        Material sphere1Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        sphere1Mat.setTexture("ColorMap", assetManager.loadTexture("Textures/world.png"));

        Geometry sphere1Geo = new Geometry("Original Sphere", sphere1Mesh);
        sphere1Geo.setMaterial(sphere1Mat); 
        sphere1Geo.setLocalTranslation(0, -2, 0);

        rootNode.attachChild(sphere1Geo);
    }
    public void addRotatedSphere(){
        Sphere sphere1Mesh = new Sphere(50, 50, 2);
        sphere1Mesh.setTextureMode(Sphere.TextureMode.Projected); // matrc

        Material sphere1Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        sphere1Mat.setTexture("ColorMap", assetManager.loadTexture("Textures/world.png"));

        Geometry sphere1Geo = new Geometry("Rotated Sphere", sphere1Mesh);
        sphere1Geo.setMaterial(sphere1Mat); 
        sphere1Geo.setLocalTranslation(0, 2, 0);

        //Add this
        Quaternion quat=new Quaternion();
        quat.fromAngles(0 ,0 , FastMath.PI);
        sphere1Geo.setLocalRotation(quat);

        rootNode.attachChild(sphere1Geo);
    }

    @Override
    public void simpleUpdate(float tpf) {



    }

}

技术 2 - 编辑纹理以符合您想要的方式

存在许多图像编辑程序,我使用的是 Paint.Net,并且(与大多数编辑软件一样)提供精确的像素鼠标坐标。只需剪切并粘贴图像,使格林威治位于最左侧。在您的情况下,您无论如何都需要编辑图像,因为它上面有可怕的白色边框。

技巧 3 - 破坏顶点纹理坐标

这太过分了,不是我推荐的。但如果这是学习创建自己的custom mesh 的练习,请继续阅读

public void addRotatedSphere_ByMessingWithMesh(){
    Sphere sphere1Mesh = new Sphere(50, 50, 2);
    sphere1Mesh.setTextureMode(Sphere.TextureMode.Projected); // matrc


    FloatBuffer textureBuffer=sphere1Mesh.getFloatBuffer(Type.TexCoord);

    float[] newTextureCoordinates=new float[textureBuffer.capacity()];


    for(int i=0;i<newTextureCoordinates.length;i++){
        //texture buffer goes x co-ordinate, y coordinate, x coordinate, y coordinate
        if (i%2!=1){
            newTextureCoordinates[i]=(float)((textureBuffer.get(i)+0.5)%1);
        }else{
            newTextureCoordinates[i]=textureBuffer.get(i);
        }
    }

    sphere1Mesh.setBuffer(Type.TexCoord, 2,newTextureCoordinates);

    Material sphere1Mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    sphere1Mat.setTexture("ColorMap", assetManager.loadTexture("Textures/world.png"));



    Geometry sphere1Geo = new Geometry("Rotated Sphere", sphere1Mesh);
    sphere1Geo.setMaterial(sphere1Mat); 
    sphere1Geo.setLocalTranslation(0, 2, 0);



    rootNode.attachChild(sphere1Geo);
}

这是有问题的,因为后面的接缝没有做好;因为真正的纹理坐标是 0,0.2,0.4,0.8,1。而新的则在远端进行环绕。在这个特定的示例中,您可以手动处理接缝,但您已经可以看到这很痛苦。

技巧 4 - 编写自己的着色器

这与 rediculus 接壤,但您可以编写一个custom shader,它将获取真实的纹理坐标并应用类似于技术 3 中执行的转换,但这将在显卡上完成,对调试。

不用说,那将是使用小型核武器杀死苍蝇,我不会明确解释所有步骤(但它很大程度上基于 unshaded.j3md 和 unshaded.vert

  • 创建以下文件来定义我们的新材料

材质定义

唯一的改变是提及我们的自定义顶点着色器,而不是使用自定义的

MaterialDef Unshaded {

    MaterialParameters {
        Texture2D ColorMap
        Texture2D LightMap
        Color Color (Color)
        Boolean VertexColor (UseVertexColor)
        Boolean SeparateTexCoord

        // Texture of the glowing parts of the material
        Texture2D GlowMap
        // The glow color of the object
        Color GlowColor

        // For hardware skinning
        Int NumberOfBones
        Matrix4Array BoneMatrices

        // Alpha threshold for fragment discarding
        Float AlphaDiscardThreshold (AlphaTestFallOff)

        //Shadows
        Int FilterMode
        Boolean HardwareShadows

        Texture2D ShadowMap0
        Texture2D ShadowMap1
        Texture2D ShadowMap2
        Texture2D ShadowMap3
        //pointLights
        Texture2D ShadowMap4
        Texture2D ShadowMap5

        Float ShadowIntensity
        Vector4 Splits
        Vector2 FadeInfo

        Matrix4 LightViewProjectionMatrix0
        Matrix4 LightViewProjectionMatrix1
        Matrix4 LightViewProjectionMatrix2
        Matrix4 LightViewProjectionMatrix3
        //pointLight
        Matrix4 LightViewProjectionMatrix4
        Matrix4 LightViewProjectionMatrix5
        Vector3 LightPos
        Vector3 LightDir

        Float PCFEdge

        Float ShadowMapSize
    }

    Technique {
        VertexShader GLSL100:   MatDefs/TextureSplitting.vert
        FragmentShader GLSL100: Common/MatDefs/Misc/Unshaded.frag

        WorldParameters {
            WorldViewProjectionMatrix
        }

        Defines {
            SEPARATE_TEXCOORD : SeparateTexCoord
            HAS_COLORMAP : ColorMap
            HAS_LIGHTMAP : LightMap
            HAS_VERTEXCOLOR : VertexColor
            HAS_COLOR : Color
            NUM_BONES : NumberOfBones
            DISCARD_ALPHA : AlphaDiscardThreshold
        }
    }

    Technique {
    }

    Technique PreNormalPass {

          VertexShader GLSL100 :   Common/MatDefs/SSAO/normal.vert
          FragmentShader GLSL100 : Common/MatDefs/SSAO/normal.frag

          WorldParameters {
              WorldViewProjectionMatrix
              WorldViewMatrix
              NormalMatrix
          }

          Defines {
              NUM_BONES : NumberOfBones
          }
   }

    Technique PreShadow {

        VertexShader GLSL100 :   Common/MatDefs/Shadow/PreShadow.vert
        FragmentShader GLSL100 : Common/MatDefs/Shadow/PreShadow.frag

        WorldParameters {
            WorldViewProjectionMatrix
            WorldViewMatrix
        }

        Defines {
            COLOR_MAP : ColorMap
            DISCARD_ALPHA : AlphaDiscardThreshold
            NUM_BONES : NumberOfBones
        }

        ForcedRenderState {
            FaceCull Off
            DepthTest On
            DepthWrite On
            PolyOffset 5 3
            ColorWrite Off
        }

    }


    Technique PostShadow15{
        VertexShader GLSL150:   Common/MatDefs/Shadow/PostShadow15.vert
        FragmentShader GLSL150: Common/MatDefs/Shadow/PostShadow15.frag

        WorldParameters {
            WorldViewProjectionMatrix
            WorldMatrix
        }

        Defines {
            HARDWARE_SHADOWS : HardwareShadows
            FILTER_MODE : FilterMode
            PCFEDGE : PCFEdge
            DISCARD_ALPHA : AlphaDiscardThreshold           
            COLOR_MAP : ColorMap
            SHADOWMAP_SIZE : ShadowMapSize
            FADE : FadeInfo
            PSSM : Splits
            POINTLIGHT : LightViewProjectionMatrix5
            NUM_BONES : NumberOfBones
        }

        ForcedRenderState {
            Blend Modulate
            DepthWrite Off                 
            PolyOffset -0.1 0
        }
    }

    Technique PostShadow{
        VertexShader GLSL100:   Common/MatDefs/Shadow/PostShadow.vert
        FragmentShader GLSL100: Common/MatDefs/Shadow/PostShadow.frag

        WorldParameters {
            WorldViewProjectionMatrix
            WorldMatrix
        }

        Defines {
            HARDWARE_SHADOWS : HardwareShadows
            FILTER_MODE : FilterMode
            PCFEDGE : PCFEdge
            DISCARD_ALPHA : AlphaDiscardThreshold           
            COLOR_MAP : ColorMap
            SHADOWMAP_SIZE : ShadowMapSize
            FADE : FadeInfo
            PSSM : Splits
            POINTLIGHT : LightViewProjectionMatrix5
            NUM_BONES : NumberOfBones
        }

        ForcedRenderState {
            Blend Modulate
            DepthWrite Off   
            PolyOffset -0.1 0  
        }
    }

    Technique Glow {

        VertexShader GLSL100:   Common/MatDefs/Misc/TextureSplitting.vert
        FragmentShader GLSL100: Common/MatDefs/Light/Glow.frag

        WorldParameters {
            WorldViewProjectionMatrix
        }

        Defines {
            NEED_TEXCOORD1
            HAS_GLOWMAP : GlowMap
            HAS_GLOWCOLOR : GlowColor
            NUM_BONES : NumberOfBones
        }
    }
}

顶点着色器

使用平移将真实纹理坐标映射到移动坐标。顺便说一句,如果您认为这不是 java;不是。它的 OpenGL 着色器语言。

#import "Common/ShaderLib/Skinning.glsllib"

uniform mat4 g_WorldViewProjectionMatrix;
attribute vec3 inPosition;

#if defined(HAS_COLORMAP) || (defined(HAS_LIGHTMAP) && !defined(SEPARATE_TEXCOORD))
    #define NEED_TEXCOORD1
#endif

attribute vec2 inTexCoord;
attribute vec2 inTexCoord2;
attribute vec4 inColor;

varying vec2 texCoord1;
varying vec2 texCoord2;

varying vec4 vertColor;

void main(){
    #ifdef NEED_TEXCOORD1
        texCoord1 = inTexCoord;
        texCoord1.x=texCoord1.x+0.5;
        if (texCoord1.x>1){
            texCoord1.x=texCoord1.x-1;
        }
    #endif

    #ifdef SEPARATE_TEXCOORD
        texCoord2 = inTexCoord2;
    #endif

    #ifdef HAS_VERTEXCOLOR
        vertColor = inColor;
    #endif

    vec4 modelSpacePos = vec4(inPosition, 1.0);
    #ifdef NUM_BONES
        Skinning_Compute(modelSpacePos);
    #endif
    gl_Position = g_WorldViewProjectionMatrix * modelSpacePos;
}

然后用这个作为材质而不是unshaded.j3md

Material sphere1Mat = new Material(assetManager, "Materials/TextureSplitting.j3md");

再一次在后面有一个令人讨厌的中断,真正的纹理角色在 0 和 1 之间,如果我们想要,我们可以明确处理 但是我们必须确保有 2 个顶点纹理坐标为 0 的分割点和纹理坐标为 1 的分割点。

结论

技巧 1 或 2 是您应该使用的技巧。我包含技术 3 和 4 只是为了表明您可以使用实际的纹理坐标执行此操作,但您不应该这样做。

【讨论】:

  • 感谢您的完整回答。但只有第 3 种方式对我来说是对原始问题的回答才有趣。还要特别感谢方式 4——现在不需要它,但将来会很有趣。我不能使用方法 1 和 2,因为问题只包含 SSCCE。在完整的任务中,我正在生成自定义网格,并且更喜欢最接近正确的坐标。
  • @SuzanCioc Cool,关于纹理从 1 到 0 “环绕”的讨厌接缝。您需要对此进行特殊处理;就目前而言,网格根本无法避免接缝,但是如果您可以控制网格,那么您需要将接缝点的顶点数加倍,并在坐标 0 处设置一个,在坐标 1 处设置一个。这消除了接缝。如果您对此有疑问,请再问一个问题,但我需要查看您如何形成网格的细节来回答这个问题
猜你喜欢
  • 2011-10-18
  • 2015-09-22
  • 2013-10-21
  • 1970-01-01
  • 2013-06-22
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
相关资源
最近更新 更多