Qt3D 代码模块

  1 ----------------------------------------------
  2 概述
  3 - 请阅读QtHelp: Qt 3D Overview 
  4     https://www.kdab.com/overview-qt3d-2-0-part-1/
  5     http://blog.csdn.net/iron_lzn/article/details/51363959
  6 - 上youtube 搜索 Qt 3D 视频
  7 - 上kdab公司的网站,Qt3D的主要代码都是kdab公司提供的
  8 - 建模示例:https://www.kdab.com/qt-3d-animation-easter-teaser/
  9 ----------------------------------------------
 10 功能
 11     2D and 3D rendering     : 2d/3d渲染
 12     Meshes and Geometry     : 面片和坐标
 13     Materials               : 材质
 14     Shaders                 : 着色器
 15     Shadow mapping          : 阴影贴图(一种实时阴影技术)
 16     Ambient occlusion       : 环境光遮蔽(一种全局光技术)
 17     High dynamic range      : 高动态范围(HDR)
 18     Deferred rendering      : 延迟渲染(一种多光源渲染技术)
 19     Multitexturing          : 多材质
 20     Instanced rendering     : 实例渲染
 21     Uniform Buffer Objects  : 统一缓冲对象(HBO)
 22     
 23 相关库
 24     QT += 3dcore 3drender 3dinput 3dlogic 3dextras
 25     #include <Qt3DCore>
 26     #include <Qt3DRender>
 27     #include <Qt3DInput>
 28     #include <Qt3DLogic>
 29     #include <Qt3DExtras>
 30     QT += 3dcore 3drender 3dinput 3dlogic 3dextras qml quick 3dquick
 31     import Qt3D.Core 2.0      // 节点实体模型,基础变化
 32     import Qt3D.Render 2.0    // 相机,缓冲,滤镜,图层,面片,光照,特效,材质,Shader,
 33     import Qt3D.Input 2.0     // 输入
 34     import Qt3D.Logic 2.0     // 每帧动画控制
 35     import Qt3D.Extras 2.0    // 第一人称相机,轨道相机,反射贴图,基础模型等
 36     
 37 继承关系
 38     Node                            // qt3d 所有类的基类
 39         Entity                      // 
 40             Camera                  // 
 41         Component3D                 // 
 42             Layer                   // 
 43             Transform               // 
 44             CameraLens              // 
 45             ComputeCommand          // 
 46             FrameAction             // 
 47             InputSettings           // 
 48             KeyboardHandler         // 
 49             GeometryRenderer        // 
 50                 Mesh                // 面片
 51                 ConeMesh            // 圆锥体 ConeMesh {bottomRadius: 0.05; topRadius: 0; length : 0.1; rings : 10; slices: 10},
 52                 CuboidMesh          // 长方体
 53                 CylinderMesh        // 圆柱体 CylinderMesh{length : size; radius : 0.05; rings : 10; slices: 10}
 54                 PlaneMesh           // 平面  PlaneMesh {width: 1.0; height: 1.0; meshResolution: Qt.size(2, 2)}
 55                 SphereMesh          // 球体  SphereMesh {radius: 1}
 56                 TorusMesh           // 三叶体
 57             Light                   // 光照基类
 58                 DirectionLight      // 方向光(如太阳)
 59                 PointLight          // 电光源(如灯泡)
 60                 SportLight          // 聚光
 61             Material                               // 
 62                 Texture2D                          // 
 63                 DiffuseMapMaterial                 // 漫反射贴图材质
 64                 DiffuseSpecularMapMaterial         // 漫反射高光贴图材质
 65                 GoochMaterial                      // Gooch shading model, popular in CAD and CAM applications
 66                 NormalDiffuseMapMaterial           // 
 67                 NormalDiffuseMapAlphaMaterial      // 
 68                 NormalDiffuseSpecularMapMaterial   // 
 69                 PhongMaterial                      // 
 70                 PhongAlphaMaterial                 // 
 71                 PerVertexColorMaterial             // 
 72         AbstractTextureImage        // 
 73         Effect                      // 
 74         FilterKey                   // 
 75         FrameGraphNode              // FrameGraph 配置基类,用于控制渲染
 76             LayerFilter             // 层过滤(只显示指定层的对象)
 77             CameraSelector          // 相机选择器 
 78             ClearBuffers            // enables clearing of the specific render target buffers with specific values
 79             DispatchCompute         // FrameGraph node to issue work for the compute shader on GPU
 80             FrustumCulling          // enables frustum culling of the drawable entities based on the camera view and Geometry bounds of the entities
 81             NoDraw                  // Prevents from drawing anything
 82             RenderCapture           // Used to request render capture. User can specify a captureId to identify the request.
 83             RenderSurfaceSelector   // be used to select the surface
 84             RenderTargetSelector    // s used to select active RenderTarget for the FrameGraph
 85             RenderPassFilter        // 
 86             RenderStateSet          // 
 87             SortPolicy              // 
 88             TechniqueFilter         // 
 89             Viewport                // 
 90         KeyboardDevice              // 
 91         RenderPass                  // 
 92         RenderState                 // 
 93         RenderTargetOutput          // 
 94         ShaderPrograme
 95         CameraController
 96             FirstPersonCameraController  // 第一人称相机控制器(类似cs)
 97             OrbitCameraController        // 轨道相机控制器(围绕物体旋转)
 98         Geometry                    // 
 99             ConeGeometry            // allows creation of a cone in 3D space
100             CuboidGeometry          // allows creation of a cuboid in 3D space.
101             CylinderGeometry        // 
102             PlaneGeometry           // 
103             SphereGeometry          // 
104             TorusGeometry           // 
105 
106 
107 eg
108     CuboidMesh {
109         id: mesh
110         property real multiplier: 0.0256
111         xExtent: 512 * multiplier
112         yExtent: 768 * multiplier
113         zExtent: 2.0
114         yzMeshResolution: Qt.size(20, 20)
115         xzMeshResolution: Qt.size(20, 20)
116         xyMeshResolution: Qt.size(20, 20)
117     }
118 
119 显示OpenGL版本
120     Rectangle {
121         color: "black"
122         Text {
123             color: "white"
124             anchors.centerIn: parent
125             text: "Open%4 %1.%2 %3"
126                 .arg(OpenGLInfo.majorVersion)
127                 .arg(OpenGLInfo.minorVersion)
128                 .arg({0: "NoProfile", 1: "CoreProfile", 2: "CompatibilityProfile"}[OpenGLInfo.profile])
129                 .arg({0: "Unspecified", 1: "GL", 2: "GLES"}[OpenGLInfo.renderableType])
130             styleColor: "#8b8b8b"
131             style: Text.Sunken
132             font.pointSize: 24
133             onTextChanged: {
134                 Resources.setGlInfo(OpenGLInfo);
135             }
136         }
137     }
138 
139 ----------------------------------------------
140 Scene3D
141 提供3d运行场景,嵌在普通qml中
142 ----------------------------------------------
143 属性
144     activeCamera : Camera3D
145     activeLight : Light3D
146     devicePixelRatio : float
147     graphPositionQuery : point
148     invalidSelectionPoint : point
149     primarySubViewport : rect
150     secondarySubViewport : rect
151     secondarySubviewOnTop : bool
152     selectionQueryPosition : point
153     slicingActive : bool
154     viewport : rect 
155     
156 示例
157     Scene3D {
158       id: scene3d
159       anchors.fill: parent
160       anchors.margins: 10
161       focus: true
162       aspects: ["input", "logic"]
163       cameraAspectRatioMode: Scene3D.AutomaticAspectRatio
164       AnimatedEntity {}
165     }
166           
167     
168 
169 ----------------------------------------------
170 Entity
171 Entity有个属性component,里面可以容纳 Mesh, Transformation, Audio, Material.....,
172 这是一种很灵活的方式,可简化3d对象类的层次,也便于扩展。
173 ----------------------------------------------
174 标准写法
175     Entity {
176         components: [
177             Mesh {source: "assets/obj/toyplane.obj"},
178             PhongMaterial {diffuse: "white"; shininess: 50}
179         ]
180     }
181     
182 从文件中加载场景
183     Entity {
184         components: [
185             SceneLoader {source: "qrc:/assets/test_scene.dae"}
186         ]
187     }
188 
189                       
190 ----------------------------------------------
191 Camera
192 import Qt3D.Core 2.0
193 import Qt3D.Render 2.0
194 ----------------------------------------------
195 第一人称三角透视相机
196     Camera {
197         id: mainCamera
198         objectName: "mainCamera"
199         projectionType: CameraLens.PerspectiveProjection
200         fieldOfView: 22.5
201         aspectRatio: _window.width / _window.height
202         onAspectRatioChanged: console.log( "aspectRatio = " + aspectRatio )
203         nearPlane:   0.01
204         farPlane:    1000.0
205         viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
206         upVector:   Qt.vector3d( 0.0, 1.0, 0.0 )
207     }
208     FirstPersonCameraController { camera: mainCamera }
209     
210 
211 轨道透视相机(右键按住拖动,可对准原点旋转相机)
212     components: [
213         RenderSettings {
214             activeFrameGraph: ForwardRenderer {
215                 clearColor: Qt.rgba(0, 0.5, 1, 1)
216                 camera: camera
217             }
218         },
219         InputSettings { }
220     ]
221     Camera {
222         id: camera
223         projectionType: CameraLens.PerspectiveProjection
224         fieldOfView: 45
225         aspectRatio: 16/9
226         nearPlane : 0.1
227         farPlane : 1000.0
228         position: Qt.vector3d( 0.0, 0.0, -40.0 )
229         upVector: Qt.vector3d( 0.0, 1.0, 0.0 )
230         viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 )
231     }
232     OrbitCameraController { camera: camera}
233 
234 多角度观测
235     Viewport {
236           id: mainViewport
237           normalizedRect: Qt.rect(0, 0, 1, 1)
238           ClearBuffers {
239               buffers: ClearBuffers.ColorDepthBuffer
240           }
241     
242           Viewport {
243               id: topLeftViewport
244               normalizedRect: Qt.rect(0, 0, 0.5, 0.5)
245               CameraSelector { id: cameraSelectorTopLeftViewport }
246           }
247           Viewport {
248               id: topRightViewport
249               normalizedRect: Qt.rect(0.5, 0, 0.5, 0.5)
250               CameraSelector { id: cameraSelectorTopRightViewport }
251           }
252           Viewport {
253               id: bottomLeftViewport
254               normalizedRect: Qt.rect(0, 0.5, 0.5, 0.5)
255               CameraSelector { id: cameraSelectorBottomLeftViewport }
256           }
257           Viewport {
258               id: bottomRightViewport
259               normalizedRect: Qt.rect(0.5, 0.5, 0.5, 0.5)
260               CameraSelector { id: cameraSelectorBottomRightViewport }
261           }
262       }
263 
264     
265 
266 
267 ----------------------------------------------
268 Light
269 ----------------------------------------------
270 方向光(平行光,如太阳)
271     Enity {
272         components: [
273             DirectionalLight {
274                 worldDirection: Qt.vector3d(0.3, -3.0, 0.0).normalized();
275                 color: "#fbf9ce"
276                 intensity: 0.3
277             }
278         ]
279     }
280 
281 点光源(如灯泡)
282     Entity {
283         components: [
284             PointLight {
285                 color: "red"
286                 intensity: 0.3
287                 constantAttenuation: 1.0
288                 linearAttenuation: 0.0
289                 quadraticAttenuation: 0.0025
290             }
291         ]
292     }
293 
294 聚光(如舞台聚光灯)
295     Entity {
296         components: [
297             SpotLight {
298                 localDirection: Qt.vector3d(0.0, -1.0, 0.0)
299                 color: "white"
300                 intensity: 0.6
301             }
302         ]
303     }        
304     
305 
306 
307 
308 
309 ----------------------------------------------
310 Material(材质)
311 贴图相关概念
312     https://zhidao.baidu.com/question/167602912.html
313     http://www.cnblogs.com/xiuj/p/5875461.html
314     -------------------------------------------
315     ambient                 : 环境色。物体自己发出的光。
316     diffuse map             : 漫反射贴图(色彩贴图), 它的作用是给模型上颜色和材质
317     specular map            : 高光贴图,不同材质,在光照下,它们的反射光的强弱都不同
318     normal map              : 法线贴图.用来表现凹凸
319     -------------------------------------------
320     gloss map               : 指示着色器对物体的表面的某些部分着色更亮一点,某些部分更暗一点,因为Gloss Maps只需要单通道, 可用alpha通道的图或灰度图
321     emboss bump map         : 做表面的浮雕效果的
322     opacity map             : 透明贴图。透明通道制作头发或者损坏的衣服布料等等可以用到
323     shininess               : 光泽(0-1324     lighting map 和 AO map  : 让材质制作更加逼真,更加方便
325 Qt3D中材质
326     用Material组件编写
327     提供了几种材质组件,通常是几种贴图方式的组合
328         PhongMaterial
329         DiffuseMapMaterial
330         NormalDiffuseMapMaterial
331         GoochMaterial
332 ----------------------------------------------
333 简单色彩材质
334     色彩材质
335         ColorMaterial{
336             diffuseColor: Qt.rgba(0.2, 0.5, 0.3, 1.0)
337             specularColor: Qt.rgba(0, 0, 0, 1.0)
338         }
339     Phong 基础光照模型材质
340         PhongMaterial {
341             ambient: Qt.rgba( 0.02, 0.02, 0.02, 1.0 )
342             diffuse: Qt.rgba( 0.8, 0.0, 0.0, 1.0 )
343             specular: Qt.rgba( 0.8, 0.0, 0.0, 1.0 )
344             shininess: 1.0
345         }
346     PhongAlphaMaterial(带透明通道的基础Phong光照模型材质)
347         PhongMaterial {
348             ambient: Qt.rgba( 0.02, 0.02, 0.02, 1.0 )
349             diffuse: Qt.rgba( 0.8, 0.0, 0.0, 1.0 )
350             specular: Qt.rgba( 0.8, 0.0, 0.0, 1.0 )
351             shininess: 1.0
352             alpha: 0.8
353         }
354     GoochMaterial
355         Gooch光照渲染模型。
356         常用于CAD和CAM程序,完全拟真不是该模型的目标,
357         该模型同时使用 color 和 brightness 来显示3d表面的弯曲部分;
358         该模型混合了漫反射光/冷光/暖光来产生渐变色带,冷光和暖光分别由alpha和beta参数影响。色带区间:[cool + alpha * diffuse, warm + beta * diffuse]
359         参数
360             shininess : real
361             diffuse : color
362             specular : color
363             coolColor : color
364             warmColor : color
365             alpha : real
366             beta : real
367     PerVertexColorMaterial
368         Default implementation for rendering the color properties set for each vertex
369         帮助上没有任何参数
370 
371 
372 
373 漫反射贴图材质(Phong光照模型)
374     漫反射贴图材质(漫反射由贴图提供)
375         DiffuseMapMaterial {
376             diffuse: "assets/chest/diffuse.webp"
377             specular: Qt.rgba( 0.2, 0.2, 0.2, 1.0 )
378             shininess: 2.0
379             ambient: color
380             textureScale: 1.0
381         }
382     漫反射高光贴图材质(漫反射和高光均有贴图提供)
383         DiffuseSpecularMapMaterial{
384             diffuse: "assets/chest/diffuse.webp"
385             specular: "assets/chest/specular.webp"
386             shininess: 2.0
387             ambient: color
388             textureScale: 1.0
389         }
390         
391     
392 凹凸贴图材质(用贴图来模拟细节,减小建模复杂度)
393     法线漫反射贴图材质(实现了phong光照和凹凸贴图模型)
394         NormalDiffuseMapMaterial {
395             ambient: Qt.rgba( 0.05, 0.05, 0.05, 1.0 )
396             shininess: 5.0
397             textureScale: 1.0
398             diffuse: "assets/houseplants/pot.webp"
399             normal: "assets/houseplants/pot_normal.webp"
400             specular: Qt.rgba( 0.75, 0.75, 0.75, 1.0 )
401             }
402     法线漫反射贴图质(和前一个的区别?)
403         NormalDiffuseMapAlphaMaterial {
404             ambient: Qt.rgba( 0.05, 0.05, 0.05, 1.0 )
405             shininess: 1.0
406             textureScale: 1.0
407             diffuse: "qrc:/images/songtitle.png"
408             normal: "qrc:/images/normalmap.png"
409             specular: color
410             }
411     法线漫反射高光贴图材质(高光可以指定贴图)
412         NormalDiffuseSpecularMapMaterial {
413             ambient: Qt.rgba( 0.05, 0.05, 0.05, 1.0 )
414             shininess: 10.0
415             textureScale: 10.0
416             diffuse:  "assets/textures/pattern_09/diffuse.webp"
417             normal:   "assets/textures/pattern_09/normal.webp"
418             specular: "assets/textures/pattern_09/specular.webp"
419             }
420 
421 
422 自定义材质(使用Shader技术实现)
423     示例1
424         Material {
425             id: instancedPhongMaterial
426             effect: Effect {
427                 techniques: Technique {
428                     graphicsApiFilter {
429                         api: GraphicsApiFilter.OpenGL
430                         profile: GraphicsApiFilter.CoreProfile
431                         minorVersion: 2
432                         majorVersion: 3
433                     }
434                     filterKeys: FilterKey { name: "renderingStyle"; value: "forward" }
435                     renderPasses: RenderPass {
436                         shaderProgram: ShaderProgram {
437                             vertexShaderCode: loadSource("qrc:/instanced.vert")
438                             fragmentShaderCode: loadSource("qrc:/instanced.frag")
439                         }
440                     }
441                 }
442             }
443         }
444     线框材质
445         请查看官方示例“Wireframe qml example”
446         WireframeMaterial {
447             id: wireframeMaterial
448             effect: WireframeEffect {}
449             ambient: Qt.rgba( 0.2, 0.0, 0.0, 1.0 )
450             diffuse: Qt.rgba( 0.8, 0.0, 0.0, 1.0 )
451         }
452     详见官方示例:Planets qml example
453     Texture2D {
454           id: diffuseTexture
455           minificationFilter: Texture.LinearMipMapLinear
456           magnificationFilter: Texture.Linear
457           wrapMode {
458               x: WrapMode.Repeat
459               y: WrapMode.Repeat
460           }
461           generateMipMaps: true
462           maximumAnisotropy: 16.0
463           TextureImage {source: "qrc:/texturePalette.png"}  
464      }    
465 
466 
467 ----------------------------------------------
468 Layer(QLayer>)
469 类似ps中的图层,可在程序中控制某些图层显示隐藏
470 ----------------------------------------------
471   import Qt3D.Core 2.0
472   import Qt3D.Render 2.0
473   Entity {
474       id: root
475       components: RenderSettings {
476           Viewport {
477               ClearBuffers {
478                   buffers: ClearBuffers.ColorDepthBuffer
479                   CameraSelector {
480                       camera: mainCamera
481                       LayerFilter {layers: [layer1]}
482                   }
483               }
484           }
485       }
486       Camera { id: mainCamera }
487 
488       // scene
489       Entity {
490           id: renderableEntity
491           components: [ mesh, layer1 ]
492       }
493       Layer { id: layer1 }
494       GeometryRenderer { id: mesh }
495   }
496   
497   
498   
499 
500 ----------------------------------------------
501 TechniqueFilter e
502 ----------------------------------------------
503 import Qt3D.Core 2.0
504 import Qt3D.Render 2.0
505 TechniqueFilter {
506     property alias camera: cameraSelector.camera
507     property alias window: surfaceSelector.surface
508 
509     id: root
510     objectName : "techniqueFilter"
511     matchAll: [ FilterKey { name: "renderingStyle"; value: "forward" } ]  // Select the forward rendering Technique of any used Effect
512 
513     RenderSurfaceSelector {
514         id: surfaceSelector
515         Viewport {
516             id: viewport
517             objectName : "viewport"
518             normalizedRect: Qt.rect(0.0, 0.0, 1.0, 1.0)
519             CameraSelector {
520                 id : cameraSelector
521                 objectName : "cameraSelector"
522                 ClearBuffers {
523                     buffers : ClearBuffers.ColorDepthBuffer
524                     clearColor: "black"
525                     SortPolicy {
526                         sortTypes: [
527                             SortPolicy.StateChangeCost,
528                             SortPolicy.Material
529                         ]
530                     }
531                 }
532             }
533         }
534     }
535 }
536 
537 
538 ----------------------------------------------
539 AspectEngine
540 ----------------------------------------------
541 3d模拟是AspectEngine来执行等
542 AbstractAspect
543     Behavior 处理组件数据
544     
545 RenderAspect
546 InputAspect
547 LoginAspect
548 
549 
550 
551 
552 ----------------------------------------------
553 FrameGraph & ViewPort & Layer
554 描述了后期处理逻辑,如:阴影/透明/后期处理/多视图/选择渲染
555 import Qt3D.Core 2.0
556 import Qt3D.Render 2.0
557 ----------------------------------------------
558 默认的RenderSetting
559     Entity {
560         Camera { id:camera ... }
561         FirstPersonCameraController {camera:camera}
562         components: [
563             RenderSetting{
564                 activeFrameGraph : ForwardRenderer {camera:camera, clearColor:"transparent"},
565                 InputSetting{}
566             }
567         ]
568         ...
569     }
570 
571 自定义RenderSetting(多视图)
572     RenderSettings {
573       id: quadViewportFrameGraph
574       property alias topLeftCamera: cameraSelectorTopLeftViewport.camera;
575       property alias topRightCamera: cameraSelectorTopRightViewport.camera;
576       property alias bottomLeftCamera: cameraSelectorBottomLeftViewport.camera;
577       property alias bottomRightCamera: cameraSelectorBottomRightViewport.camera;
578       property alias window: surfaceSelector.surface
579 
580       activeFrameGraph: RenderSurfaceSelector {
581           id: surfaceSelector
582           Viewport {
583               id: mainViewport
584               normalizedRect: Qt.rect(0, 0, 1, 1)
585 
586               ClearBuffers {
587                   buffers: ClearBuffers.ColorDepthBuffer
588               }
589 
590               Viewport {
591                   id: topLeftViewport
592                   normalizedRect: Qt.rect(0, 0, 0.5, 0.5)
593                   CameraSelector { id: cameraSelectorTopLeftViewport }
594               }
595 
596               Viewport {
597                   id: topRightViewport
598                   normalizedRect: Qt.rect(0.5, 0, 0.5, 0.5)
599                   CameraSelector { id: cameraSelectorTopRightViewport }
600               }
601 
602               Viewport {
603                   id: bottomLeftViewport
604                   normalizedRect: Qt.rect(0, 0.5, 0.5, 0.5)
605                   CameraSelector { id: cameraSelectorBottomLeftViewport }
606               }
607 
608               Viewport {
609                   id: bottomRightViewport
610                   normalizedRect: Qt.rect(0.5, 0.5, 0.5, 0.5)
611                   CameraSelector { id: cameraSelectorBottomRightViewport }
612               }
613           }
614       }
615   }
616 
617 选择图层渲染
618     RenderSurfaceSelector{
619         RenderTargetSelector{
620             target: sceneTarget
621             CameraSelector{
622                 camera: mainCamera
623                 ClearBuffer {
624                     buffers: ClearBuffers.ColorDepthBuffer
625                     clearColor: "white"
626                     NoDraw{}
627                 }
628                 LayerFilter{
629                     layers: [objectsLayer]
630                     RenderPassFilter{
631                         matchAny: FilterKey{name: "renderPass"; value: "allObjects"}
632                     }
633                 }
634                 LayerFilter{
635                     layers: [enviromentLayer]
636                 }
637             }
638         }
639     }
640 
641 
642 ----------------------------------------------
643 CustomMaterial & Effect
644 ----------------------------------------------
645 import Qt3D.Core 2.0
646 import Qt3D.Render 2.0
647 Material {
648     // 定义材质的参数
649     id: root
650     property color ambient: Qt.rgba( 0.05, 0.05, 0.05, 1.0 )
651     property color diffuse: Qt.rgba( 0.7, 0.7, 0.7, 1.0 )
652     property color specular: Qt.rgba( 0.95, 0.95, 0.95, 1.0 )
653     property real shininess: 150.0
654     property real lineWidth: 0.8
655     property color lineColor: Qt.rgba( 0.0, 0.0, 0.0, 1.0 )
656     parameters: [
657         Parameter { name: "ka"; value: Qt.vector3d(root.ambient.r, root.ambient.g, root.ambient.b) },
658         Parameter { name: "kd"; value: Qt.vector3d(root.diffuse.r, root.diffuse.g, root.diffuse.b) },
659         Parameter { name: "ks"; value: Qt.vector3d(root.specular.r, root.specular.g, root.specular.b) },
660         Parameter { name: "shininess"; value: root.shininess },
661         Parameter { name: "line.width"; value: root.lineWidth },
662         Parameter { name: "line.color"; value: root.lineColor }
663     ]
664 
665     // 具体的效果实现逻辑(使用Shader实现)
666     effect: Effect {
667         parameters: [
668             Parameter { name: "ka";  value: Qt.vector3d( 0.1, 0.1, 0.1 ) },
669             Parameter { name: "kd";  value: Qt.vector3d( 0.7, 0.7, 0.7 ) },
670             Parameter { name: "ks";  value: Qt.vector3d( 0.95, 0.95, 0.95 ) },
671             Parameter { name: "shininess"; value: 150.0 }
672         ]
673         techniques: [
674             Technique {
675                 graphicsApiFilter {
676                     api: GraphicsApiFilter.OpenGL
677                     profile: GraphicsApiFilter.CoreProfile
678                     majorVersion: 3
679                     minorVersion: 1
680                 }
681                 filterKeys: [ FilterKey { name: "renderingStyle"; value: "forward" } ]
682                 parameters: [
683                     Parameter { name: "light.position"; value: Qt.vector4d( 0.0, 0.0, 0.0, 1.0 ) },
684                     Parameter { name: "light.intensity"; value: Qt.vector3d( 1.0, 1.0, 1.0 ) },
685                     Parameter { name: "line.width"; value: 1.0 },
686                     Parameter { name: "line.color"; value: Qt.vector4d( 1.0, 1.0, 1.0, 1.0 ) }
687                 ]
688                 // 渲染通道
689                 renderPasses: [
690                     RenderPass {
691                         shaderProgram: ShaderProgram {
692                             vertexShaderCode:   loadSource("qrc:/Materials/Shaders/robustwireframe.vert")
693                             geometryShaderCode: loadSource("qrc:/Materials/Shaders/robustwireframe.geom")
694                             fragmentShaderCode: loadSource("qrc:/Materials/Shaders/robustwireframe.frag")
695                         }
696                     }
697                 ]
698             }
699         ]
700     }
701 }
702 shader 详见Qt3D.Shader章节
703 
704 
705 
706 
707 ----------------------------------------------
708 ObjectPicker & animation(5.9)
709 https://www.kdab.com/qt-3d-animation-easter-teaser/
710 ClipAnimator 5.9 提供。可加载设计好的动画
711 ----------------------------------------------
712 Entity {
713     id: cube 
714     components: [
715         Transform {
716             id: cubeTransform
717         },
718         Mesh {
719             source: "qrc:/assets/egg/egg.obj"
720         },
721         TexturedMetalRoughMaterial {
722             baseColor: TextureLoader {
723                 format: Texture.SRGB8_Alpha8
724                 source: "qrc:/assets/egg/basecolor.png"
725             }
726             metalness: TextureLoader { source: "qrc:/assets/egg/metalness.png" }
727             roughness: TextureLoader { source: "qrc:/assets/egg/roughness.png" }
728             normal: TextureLoader { source: "qrc:/assets/egg/normal.png" }
729             ambientOcclusion: TextureLoader { source: "qrc:/assets/egg/ambientocclusion.png" }
730         },
731         ObjectPicker {
732             onClicked: animator.running = true
733         },
734         ClipAnimator {
735             id: animator
736             loops: 3
737             clip: AnimationClipLoader { source: "qrc:/jumpinganimation.json" }
738             channelMapper: ChannelMapper {
739                 mappings: [
740                     ChannelMapping { channelName: "Location"; target: cubeTransform; property: "translation" },
741                     ChannelMapping { channelName: "Rotation"; target: cubeTransform; property: "rotation" },
742                     ChannelMapping { channelName: "Scale"; target: cubeTransform; property: "scale3D" }
743                 ]
744             }
745         }
746     ]
747 }
748 
749 
750 
751 
752 ----------------------------------------------
753 -- 批量生成对象
754 ----------------------------------------------
755    NodeInstantiator {
756       id: collection
757       property int maxCount: parent.numberOfBars
758       model: maxCount
759       delegate: BarEntity {
760           id: cubicEntity
761           entityMesh: CuboidMesh {xExtent: 0.1; yExtent: 0.1; zExtent: 0.1}
762           rotationTimeMs: sceneRoot.barRotationTimeMs
763           entityIndex: index
764           entityCount: sceneRoot.numberOfBars
765           entityAnimationsState: animationState
766           magnitude: 0
767       }
768     }
769 
770     NodeInstantiator {
771         id: grid
772         model: rows * columns
773         property int rows: 6
774         property int columns: 11
775 
776         Entity {
777             property int _row: index / grid.columns
778             property int _col: index % grid.columns
779 
780             components: [ sphereTransform, sphereMesh, sphereMaterial ]
781             SphereMesh {
782                 id: sphereMesh
783                 slices: 60
784                 rings: 60
785                 radius: 0.65
786             }
787             Transform {
788                 id: sphereTransform
789                 translation: Qt.vector3d(-7.5 + _col * 1.5, -4.0 + _row * 1.5, 0)
790             }
791             MetalRoughMaterial {
792                 id: sphereMaterial
793                 baseColor: scene.baseColor
794                 metalness: 0.2 * _row
795                 roughness: 0.1 * _col
796             }
797         }
798     }
799 
800 
801 ----------------------------------------------
802 --803 ----------------------------------------------  
804 KeyboardInput
805     可响应键盘消息
806     
807     
808 LogicComponent
809     可设置逻辑。如物体位置/精灵创建/离屏销毁等。
810     
811     
812 FrameGraph
813     如何渲染场景
814     
815     
816 CameraSelector NoDraw    
817     Viewport {
818       CameraSelector {
819           ClearBuffers {
820               buffers: ClearBuffers.ColorDepthBuffer
821               NoDraw { } // Prevents from drawing anything
822           }
823           RenderPassFilter {
824               ...
825           }
826           RenderPassFilter {
827               ...
828           }
829       }
830     }
831 
832 ClearBuffers
833     ClearBuffers {
834       buffers: ClearBuffers.DepthBuffer
835       CameraSelector {
836           id: lightCameraSelector
837       }
838     }
839     
840 
841 --------------------------------------------------    
842 三维旋转控制
843 -------------------------------------------------- 
844     // 旋转
845     Transform {
846         id: modelTransform
847         property real angle: 0
848         matrix: {
849             var m = cameraMatrix.inverted();
850             m.translate(Qt.vector3d(6,
851                                     -4 - (1.0 - showDetailsTransitionState) * 10,
852                                     -20 * showDetailsTransitionState));
853             m.rotate(modelTransform.angle, Qt.vector3d(0, 1, 0));  // 动画绕y轴旋转360度
854             m.rotate(15, Qt.vector3d(0, 0, 1)); // 沿z轴倾斜15度
855             return m;
856         }
857     }
858     
859     //
860     Transform {
861         id: cameraRotationTransform
862         matrix: {
863             var m = Qt.matrix4x4();
864             m.rotate(-19 * window.carouselOffset, Qt.vector3d(0.0, 1.0, 0.0));
865             m.rotate(5 * mainCamera.roll, Qt.vector3d(0.0, 0.0, 1.0));
866             m.lookAt(Qt.vector3d( 0.0, 0.0, 0.0 ),
867                      Qt.vector3d(-40 * Math.sin(mainCamera.yaw), mainCamera.roll * 3, -40 * Math.cos(mainCamera.yaw) ),
868                      Qt.vector3d( 0.0, 1.0, 0.0 ));
869             return m;
870         }
871     }
872     
873     Transform {
874         id: modelTransform
875         matrix: {
876             var m = cameraMatrix.inverted();
877             m.translate(Qt.vector3d(0.0, -1.5 + (1.5 * showDetailsTransitionState), -40));
878             m.rotate(40 * Math.min(1.0, showDetailsTransitionState * 1.5), Qt.vector3d(0, 1, 0));
879             m.translate(Qt.vector3d(-25 * Math.max(0, showDetailsTransitionState - 0.4), 0.0, 0.0));
880             return m;
881         }
882     }    
883 -------------------------------------------------- 
884 贴图位置的控制
885 -------------------------------------------------- 
886 import Qt3D.Core 2.0
887 import Qt3D.Render 2.0
888 Texture2D {
889     property alias source: image.source
890     minificationFilter: Texture.LinearMipMapLinear
891     magnificationFilter: Texture.Linear
892     generateMipMaps: true
893     wrapMode {
894         x: WrapMode.ClampToEdge
895         y: WrapMode.ClampToEdge
896     }
897     TextureImage {
898         id: image
899     }
900 }

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

posted @ 2020-09-11 13:53  missman  阅读(783)  评论(0)    收藏  举报