• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Kevin Cheng's Yard
电脑是我的老婆,编程是我的灵魂,代码是我的语言,按键是我在歌唱。
https://github.com/surfsky/
博客园    首页    新随笔    联系   管理    订阅  订阅

Qt3D Shader

Qt3D Shader
---------------------------------------------------
Qt3D ShaderPrograme
Qt3D GLSL 渲染器
Shader示例可参考:
    http://blog.csdn.net/candycat1992/article/details/44039077
    https://www.shadertoy.com/
http://blog.csdn.net/candycat1992/article/details/44039077
faq: shader的输入参数怎么各家都不一样,到底是如何定义的
---------------------------------------------------
概念
    渲染代码可由ShaderProgram对象封装
    参数的传递比ShaderEffect复杂得多,好麻烦
    要求输出的参数是一致的(gl_Position, gl_FragColor)
    

Qt3D默认提供的参数
    attribute highp vec4 vertexPosition;   // 顶点位置
    attribute highp vec3 vertexNormal;     // 顶点法线
    attribute highp vec2 vertexTexCoord;   // 顶点纹理坐标
    uniform highp mat4 mvp;                // ?
    uniform highp mat4 modelMatrix;        // ?
    uniform highp vec3 cameraPosition;     // 相机位置
    uniform mat4 modelView;                // ?
    uniform mat4 modelNormalMatrix;        // ?

常用的方法
   normalize
   dot
   min/mix/max/pow/
   textureCube
    highp vec4 surface = texture2D(surfaceTexture, texCoord);
    highp vec3 reflectedDirection = reflect(viewDirection, normalize(normal));
    textureCube(skyboxTexture, reflectedDirection).rgb

VertexShader
    // Qt 3D默认提供的参数  
    attribute vec3 vertexPosition;  
    attribute vec3 vertexNormal;  
    uniform mat4 modelView;  
    uniform mat4 modelNormalMatrix;  
    uniform mat4 mvp;  
    // 自己提供的参数  
    uniform vec3 lightPosition;  
    varying vec3 reflectVec;  
    varying vec3 viewVec;  
    varying float NdotL;  
    void main( void )  
    {  
        vec3 ecPos = ( modelView * vec4( vertexPosition, 1.0 ) ).xyz;  
        vec3 normal = normalize( modelNormalMatrix * vec4( vertexNormal, 1.0 ) ).xyz;  
        vec3 lightVec = normalize( lightPosition - ecPos );  
        reflectVec = normalize( reflect( -lightVec, normal ) );  
        viewVec = normalize( -ecPos );  
        NdotL = ( dot( lightVec, normal ) + 1.0 ) * 0.5;  
        gl_Position = mvp * vec4( vertexPosition, 1.0 );  
    }  


FragmentShader
    // 自己提供的参数  
    uniform vec3 surfaceColor;  
    uniform vec3 warmColor;  
    uniform vec3 coolColor;  
    uniform float diffuseWarm;  
    uniform float diffuseCool;  
    varying vec3 reflectVec;  
    varying vec3 viewVec;  
    varying float NdotL;  
    void main( void )  
    {  
        vec3 kcool    = min( coolColor + diffuseCool * surfaceColor, 1.0 );  
        vec3 kwarm    = min( warmColor + diffuseWarm * surfaceColor, 1.0 );  
        vec3 kfinal   = mix( kcool, kwarm, NdotL );  
        float spec = max( dot( reflectVec, viewVec ), 0.0 );  
        spec = pow( spec, 32.0 );  
        gl_FragColor = vec4( min( kfinal + spec, 1.0 ), 1.0 );  
    }
    


cinematic3d/BackgroundCubeMap.qml    
    ShaderProgram {
        id: gles2SkyboxShader
        vertexShaderCode: "
            attribute vec3 vertexPosition;
            varying vec3 texCoord0;
            uniform mat4 mvp;
            void main()
            {
                texCoord0 = vertexPosition.xyz;
                gl_Position = vec4(mvp * vec4(vertexPosition, 1.0)).xyww; // Fail depth test always against any rendered pixel
            }
            "
        fragmentShaderCode: "
            varying highp vec3 texCoord0;
            uniform samplerCube skyboxTexture;
            void main()
            {
                gl_FragColor = textureCube(skyboxTexture, texCoord0);
            }
            "
    }
    
// 二维贴图材质
Texture2D {
    property alias source: image.source
    minificationFilter: Texture.LinearMipMapLinear
    magnificationFilter: Texture.Linear
    generateMipMaps: true
    wrapMode {
        x: WrapMode.ClampToEdge
        y: WrapMode.ClampToEdge
    }
    TextureImage {id: image}
}

 

转载请注明出处:http://surfsky.cnblogs.com 

posted @ 2017-06-08 15:42  surfsky  阅读(1548)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3