-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCraft.yaml
3179 lines (2703 loc) · 97.9 KB
/
Craft.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
id: craft
name: Craft
subtitle: Advanced 3D Rendering, Physics and Voxels
ordering:
- Overview
- Entities
- Rendering
- Physics
- Voxels
- AR
- Noise
functions:
#---------------------------------
# Craft Overview
#---------------------------------
- id: craftOverview
category: overview
description: >
Craft provides extended capabilities to Codea that make it much easier to create 3D scenes, physics and game logic.
Craft introduces a number of new classes. These include `craft.entity`, which is the basic building block of 3D scenes.
Several systems are introduced, including:
`craft.scene` - The core class in craft, draws and updates all other objects and systems
`craft.physics` - Physics settings and helper functions
`craft.voxels` - Streaming voxel terrain, block types and helper functions
`craft.ar` - Augmented Reality, world tracking and helper functions
Each system can be used to access built-in objects, change settings or trigger specialised features.
group: Overview
name: Craft
related:
- craft.scene
- craft.physics
- craft.voxels
- craft.ar
- craft.entity
#---------------------------------
#---------------------------------
# craft.scene
#---------------------------------
- category: type
description: >
In order to use craft you must make a scene. Scenes are used to create, update and draw all entities and components.
Scenes also control basic things, such as ambient lighting, fog, the sky and contain references to some built-in entities.
Built-in entities include:
a main `camera` that renders the scene by default
a 'sun' which is a directional light
a 'sky' which is a `renderer` with a `Materials:Skybox` material used to draw the background
examples:
- example: |
function setup()
-- Create the scene
scene = craft.scene()
-- Move the main camera
scene.camera.position = vec3(0, -10, 0)
-- Adjust the scene lighting
scene.sun.rotation = quat.eulerAngles(45,0,45)
scene.ambientColor = color(90,90,90)
-- Turn on fog and set distances and color
scene.fogEnabled = true
scene.fogNear = 10
scene.fogFar = 50
scene.fogColor = color(255,255,255)
-- Get the sky material and change the sky and horizon colors
local skyMaterial = scene.sky.material
skyMaterial.sky = color(255,100,150)
skyMaterial.horizon = color(0,0,0)
end
function update(dt)
-- Update the scene (including physics, voxels and other systems)
scene:update(dt)
end
function draw()
update(DeltaTime)
-- Draw the scene (must be done within the draw function)
scene:draw()
end
group: Entities
id: craft.scene
name: craft.scene
parameters:
- description: entity, the main camera entity (contains a camera component)
name: camera
- description: entity, the sun entity (contains a directional light component)
name: sun
- description: sky, the sky entity (contains a renderer with a Skybox material)
name: sky
- description: color, the ambient light color (for materials with lighting)
name: ambientColor
- description: bool, turns fog on and off
name: fogEnabled
- description: float, the distance where fog begins
name: fogNear
- description: float, the distance where fog reaches maximum density
name: fogFar
- description: color, the color of the fog
name: fogColor
- description: int, the current number of batches being rendered (the less the better performance you can expect)
name: renderBatchCount
- description: int, the current number of batches saved by frustum culling
name: renderBatchCullCount
syntax: |
scene = craft.scene()
related:
- craft.renderer
- craft.camera
#---------------------------------
#---------------------------------
# craft.entity
#---------------------------------
- category: type
description: >
Entities are flexible objects that can be customised to change their appearance and behaviour.
You can customise an entity by adding components. Each type of component serves a different purpose.
Renderer components change entity appearance, physics components give entities physical behaviour.
examples:
- example: |
scene = craft.scene()
-- Create a new entity
entity = scene:entity()
-- Attach a model and material for rendering
entity.model = craft.model.cube(vec3(1,1,1))
entity.material = craft.material(asset.builtin.Materials.Standard)
-- Rotate the entity
entity.eulerAngles = vec3(45, 0, 45)
-- Set the z position of the entity
entity.z = -10
-- Destroy the entity
entity:destroy()
group: Entities
id: craft.entity
name: craft.entity
parameters:
- description: model, the model used for drawing this entity (stored within the attached renderer component)
name: model
- description: material, the material used for drawing this entity (stored within the attached renderer component)
name: material
- description: vec3, the position of this entity in local coordinate space
name: position
- description: vec3, the position of this entity in world coordinate space
name: worldPosition
- description: quat, the rotation of this entity in local coordinate space
name: rotation
- description: quat, the rotation of this entity in world coordinate space
name: worldRotation
- description: vec3, the scale of this entity in local coordinate space
name: scale
- description: float, the x position of this entity in local coordinate space
name: x
- description: float, the y position of this entity in local coordinate space
name: y
- description: float, the z position of this entity in local coordinate space
name: z
- description: vec3, the rotation of this entity in local coordinate space expressed in euler angles
name: eulerAngles
- description: entity, the parent of this entity (in the transform hierarchy)
name: parent
- description: table [readonly], an array of children entities attached to this entity
name: children
- description: bool, turn on and off to enable and disable entity in the scene
name: active
syntax: |
myEntity = scene:entity()
related:
- craft.renderer
#---------------------------------
#---------------------------------
# entity.add
#---------------------------------
- category: method
description: >
Adds a component to this entity. There are several built-in components that can be added, such as `craft.renderer`, `craft.shape.box` and others.
Lua classes can also be added as a component. Any additional parameters beyond the type will be forwarded to the component itself.
For Lua classes the first parameter passed to the `init()` function will be the entity itself (followed by the rest of the arguments) allowing it to be stored for later use.
Some special callback methods can be implemented in Lua classes to provide extra functionality.
The `update()` method will be called once per frame (useful for animations and game logic).
The `fixedUpdate()` method will be called once per physics update (useful for physics related behaviour).
If the component is successfully added, the component will be returned. Only one of a given component type can be added at a time.
group: Entities
id: entity.add
name: entity.add( type, ... )
parameters:
- description: type, the type of component to add to this entity
name: type
returns: object, the component added to this entity
related:
- craft.entity
syntax: |
myEntity:add( craft.renderer, myModel )
myEntity:add( craft.shape.box, vec3(1,1,1) )
myEntity:add( LuaClass, p1, p2, p3, ... )
examples:
- example: |
-- Lua classes can be attached to entities to customise behaviour
Mover = class()
function Mover:init(entity, speed)
self.entity = entity
self.speed = speed
end
function Mover:update()
-- Move the entity to the right at speed meters per second
self.entity.x = self.entity.x + self.speed * DeltaTime
self.entity.eulerAngles = vec3(0, ElapsedTime * 90, 270)
end
function setup()
-- Create a new craft scene
scene = craft.scene()
-- Create an entity and attach a Mover to make it move automatically
local entity = scene:entity()
entity.model = craft.model("SpaceKit:spaceCraft6")
entity.scale = vec3(0.1, 0.1, 0.1)
entity.x = -10
entity:add(Mover, 1.5)
scene.camera.z = 20
scene.camera.eulerAngles = vec3(0,0,180)
end
function update(dt)
scene:update(dt)
end
function draw()
update(DeltaTime)
scene:draw()
end
#---------------------------------
#---------------------------------
# entity.get
#---------------------------------
- category: method
description: >
Gets a component of a particular type attached to this entity. If the component does not exist, nil is returned.
group: Entities
id: entity.get
name: entity.get( type )
parameters:
- description: type, the type of component to get from this entity
name: type
returns: object, the requested component (or nil if it does not exist)
related:
- craft.entity
- entity.add
- entity.remove
syntax: |
myEntity:get( craft.renderer )
myEntity:get( craft.shape.box )
myEntity:get( craft.rigidbody )
myEntity:get( LuaClass )
#---------------------------------
#---------------------------------
# entity.remove
#---------------------------------
- category: method
description: >
Removes a component of a particular type attached to this entity, if it exists.
group: Entities
id: entity.remove
name: entity.remove( componentType )
parameters:
- description: type, the type of component to remove from this entity
name: type
related:
- craft.entity
- entity.add
- entity.remove
syntax: |
myEntity:remove( craft.renderer )
myEntity:remove( craft.shape.box )
myEntity:get( craft.rigidbody )
myEntity:remove( LuaClass )
#---------------------------------
#---------------------------------
# entity.destroy
#---------------------------------
- category: method
description: Marks this entity for destruction in the next frame. Any children will also be marked for destruction.
group: Entities
id: entity.destroy
name: entity.destroy()
related:
- craft.entity
syntax: |
myEntity:destroy()
#---------------------------------
#---------------------------------
# entity.transformPoint
#---------------------------------
- category: method
description: Transforms a point from local space into world space using this entity's transform.
group: Entities
id: entity.transformPoint
name: entity.transformPoint()
parameters:
- description: vec3, the point to transform
name: point
returns: vec3, the transformed point
related:
- craft.entity
syntax: |
myEntity:transformPoint(point)
#---------------------------------
#---------------------------------
# entity.inverseTransformPoint
#---------------------------------
- category: method
description: Transforms a point from world space into local space using this entity's transform.
group: Entities
id: entity.inverseTransformPoint
name: entity.inverseTransformPoint()
parameters:
- description: vec3, the point to transform
name: point
returns: vec3, the transformed point
related:
- craft.entity
syntax: |
myEntity:inverseTransformPoint(point)
#---------------------------------
#---------------------------------
# entity.transformDirection
#---------------------------------
- category: method
description: Transforms a direction from local space into world space using this entity's transform.
group: Entities
id: entity.transformDirection
name: entity.transformDirection()
parameters:
- description: vec3, the direction to transform
name: direction
returns: vec3, the transformed direction
related:
- craft.entity
syntax: |
myEntity:transformDirection(point)
#---------------------------------
#---------------------------------
# entity.inverseTransformDirection
#---------------------------------
- category: method
description: Transforms a direction from world space into local space using this entity's transform.
group: Entities
id: entity.inverseTransformDirection
name: entity.inverseTransformDirection()
parameters:
- description: vec3, the direction to transform
name: direction
related:
- craft.entity
syntax: |
myEntity:inverseTransformDirection(direction)
#---------------------------------
#---------------------------------
# craft.camera
#---------------------------------
- category: type
description: A component used to draw the scene from its point of view.
group: Rendering
id: craft.camera
name: craft.camera
parameters:
- description: float, the field of view of the camera in degrees
name: fieldOfView
- description: bool, orthographic rendering mode for this camera
name: ortho
- description: float, the near plane for the camera (the closest thing that can be rendered)
name: nearPlane
- description: float, the far plane for the camera (the farthest thing that can be rendered)
name: farPlane
- description: bool, the depth clearing flag for the camera (when set to true the existing depth buffer will be cleared before rendering)
name: clearDepthEnabled
- description: bool, the color clearing flag for the camera (when set to true the existing color buffer will be cleared before rendering)
name: clearColorEnabled
- description: color, the color to use when clearing the color buffer before rendering with the camera
name: clearColor
- description: entity, the entity that the camera is attached to
name: entity
syntax: |
myEntity:add(craft.camera, fov, nearPlane, farPlane, false)
myEntity:add(craft.camera, orthoSize, nearPlane, farPlane, true)
examples:
- example: |
-- create a new entity and add a camera to it
camera = scene:entity():add(craft.camera, 45, 0.1, 1000, false)
related:
- craft.entity
#---------------------------------
#---------------------------------
# camera.screenToWorld
#---------------------------------
- category: method
description: Converts a position given in screen coordinates (x, y) and a depth value (z) to world space relative to this camera.
group: Rendering
id: camera.screenToWorld
name: camera.screenToWorld( position )
parameters:
- description: vec3, the position to convert to world space
name: position
related:
- craft.camera
syntax: |
myCamera:screenToWorld( position )
returns: vec3
#---------------------------------
#---------------------------------
# camera.screenToRay
#---------------------------------
- category: method
description: Converts a position in screen coordinates (x, y) to a ray in the form of an origin and direction relative to this camera.
group: Rendering
id: camera.screenToRay
name: camera.screenToRay( position )
parameters:
- description: vec2, the position to generate a ray with
name: position
related:
- craft.camera
syntax: |
origin, dir = myCamera:screenToRay( position )
returns: vec3, vec3
#---------------------------------
#---------------------------------
# camera.worldToScreen
#---------------------------------
- category: method
description: Converts a position given in world coordinates to screen coordinates relative to this camera.
group: Rendering
id: camera.worldToScreen
name: camera.worldToScreen( screenPos )
parameters:
- description: vec3, the position to convert to screen space
name: position
related:
- craft.camera
syntax: |
myCamera:worldToScreen( screenPos )
returns: vec2
#---------------------------------
#---------------------------------
# camera.viewport
#---------------------------------
- category: method
description: >
Sets or gets the current viewport using normalized coordinates.
Can be used for adjusting the rendered area of the camera relative to the screen.
For instance, calling `camera:viewport(0.0, 0.0, 0.5, 1.0)` will render the camera to only the left half of the screen.
group: Rendering
id: camera.viewport
name: camera.viewport( x, y, width, height )
parameters:
- description: float, the x position of the viewport
name: x
- description: float, the y position of the viewport
name: y
- description: float, the width viewport
name: width
- description: float, height of the viewport
name: height
related:
- craft.camera
syntax: |
myCamera:viewport(x, y, width, height)
x, y, w, h = myCamera:viewport()
returns: x, y, w, h
#---------------------------------
#---------------------------------
# craft.renderer
#---------------------------------
- category: type
description: >
A component used to draw a 3D model in the scene.
Renderers can be attached to entities by using `entity:add(craft.renderer)` or by setting `entity.model` and `entity.material` directly.
group: Rendering
id: craft.renderer
name: craft.renderer
parameters:
- description: material, the material to apply to this renderer
name: material
- description: model, the model to render
name: model
syntax: |
myEntity:add(craft.renderer, model)
examples:
- example: |
-- create a new entity
entity = scene.entity()
entity.model = craft.model.icosphere(1, 3, false)
entity.material = craft.material(asset.builtin.Materials.Standard)
related:
- craft.entity
#---------------------------------
#---------------------------------
# craft.light
#---------------------------------
- category: type
description: >
A component that casts light onto the scene.
Different types of lights can be used to achieve different lighting effects, these include: `DIRECTIONAL`, `SPOT` and `POINT`.
The position and rotation of the entity controls the position and rotation of the attached light source.
By default the scene contains a single directional light known as the `sun`.
The scene also contains an ambient light controlled by `scene.ambientColor`.
The ambient light term is added to the emission of all other lights and is generally used for simulating scattered environmental light.
group: Rendering
id: craft.light
name: craft.light
parameters:
- description: the type of light, can be DIRECTIONAL, SPOT, POINT
name: type
- description: color, the color of the light
name: color
- description: float, how intense the emitted light is (can be higher than one)
name: intensity
- description: float, how far the light can travel (only applies for spot and point lights)
name: distance
- description: float, the spread angle of the light (only applies to spot lights)
name: angle
- description: float, the angle where light intensity begins to fade (only applies to spot lights)
name: penumbra
- description: float, the rate at which the intensity of the light fades due to distance (only applies to spot and point lights)
name: decay
- description: bitmask, the mask for which renderers should be effected by this light source
name: mask
syntax: |
myEntity:add(craft.light, DIRECTIONAL)
myEntity:add(craft.light, SPOT)
myEntity:add(craft.light, POINT)
examples:
- example: |
-- Create a new entity
entity = scene.entity()
light = entity:add(craft.light, POINT)
light.distance = 10
light.intensity = 1
light.color = color(255,128,128)
entity.position = vec3(0,5,0)
related:
- craft.entity
- craft.renderer
- craft.camera
#---------------------------------
#---------------------------------
# DIRECTIONAL
#---------------------------------
- category: const
description: >
This constant specifies the directional light type.
Directional lights simulate an infinitely distant light source where all rays are parallel (similar to the sun).
The position of a directional light is ignored.
group: Rendering
id: DIRECTIONAL
name: DIRECTIONAL
related:
- craft.light
returns: int
syntax: DIRECTIONAL
#---------------------------------
#---------------------------------
# SPOT
#---------------------------------
- category: const
description: >
This constant specifies the spot light type.
Spotlights simulate a cone of light within a specified angle (see the `angle` property).
The `distance` and `decay` properties define how far the light travels and how quickly the intensity falls off over distance.
The `penumbra` property defines how hard the edge of the spotlight is.
The closer this value is to `angle` the harder the edge will appear.
Setting this to zero will give the appearance of very soft spotlight.
group: Rendering
id: SPOT
name: SPOT
related:
- craft.light
returns: int
syntax: SPOT
#---------------------------------
#---------------------------------
# POINT
#---------------------------------
- category: const
description: >
This constant specifies the point light type.
Point lights simulate a omnidirectional light source emitting from a single point.
The `distance` and `decay` properties define how far the light travels and how quickly the intensity falls off over distance.
group: Rendering
id: POINT
name: POINT
related:
- craft.light
returns: int
syntax: POINT
#---------------------------------
#---------------------------------
# craft.material
#---------------------------------
- category: type
description: >
This type represents a surface material. Materials are used to control the physical appearance of 3D objects, such as models, sky boxes and voxels.
Set the material property on a `craft.renderer` component to apply a given material to it.
Materials are built out of modular shaders with a set of dynamic properties.
Each named property has a different effect on the material's appearance.
For instance, the `map` property can be set with an image or asset string to change the surface appearance of the `Standard` material.
Other properties can change the normals, roughness, metalness and reflectiveness of materials.
examples:
- example: |
local e = scene:entity()
e.model = craft.cube(vec3(1,1,1))
-- Load the standard material (physically based rendering)
local m = craft.material(asset.builtin.Materials.Standard)
e.material = m
-- Surface color
m.diffuse = color(255, 255, 255)
-- Opacity (0.0 fully transparent, 1.0 fully opaque)
m.opacity = 1.0
-- Surface color texture map
m.map = "Surfaces:Basic Bricks Color"
-- Texture map offset and repeat (tiling)
m.offsetRepeat = vec4(0.0, 0.0, 3.0, 3.0)
-- Normal map for small scale surface details
m.normalMap = "Surfaces:Basic Bricks Normal"
-- How intense the normal map effect is in tangent space (also used for flipping)
m.normalScale = vec2(1, 1)
-- How rough the material is
m.roughness = 1.0
-- A texture that controls the roughness
m.roughnessMap = "Surfaces:Basic Bricks Roughness"
-- How metallic the material is
m.metalness = 1.0
-- A texture that controls how metallic the surface is
m.metalnessMap = nil
-- The environment map (a CubeTexture), specular illumination
m.envMap = nil
-- The ambient occlusion map, used for self-occluding shadows
m.aoMap = "Surfaces:Basic Bricks AO"
-- How intense the aoMap is
m.aoMapIntensity = 2.5
-- The displacement map which modifies vertex positions based on normals
m.displacementMap = nil
-- Base offset of the displacement map
m.displacementBias = 0
-- Scale of the displacement map
m.displacementScale = 1
group: Rendering
id: craft.material
name: craft.material
parameters:
- description: int, the blend mode to use for this material (can be NORMAL, ADDITIVE or MULTIPLY)
name: blendMode
- description: int, the render queue to use for this material (can be OPAQUE or TRANSPARENT)
name: renderQueue
syntax: |
myMaterial = craft.material(asset.builtin.Materials.Basic)
myMaterial = craft.material(asset.builtin.Materials.Standard)
myMaterial = craft.material(asset.builtin.Materials.Specular)
myMaterial = craft.material(asset.builtin.Materials.Skybox)
related:
- craft.entity
- craft.renderer
- craft.model
#---------------------------------
#---------------------------------
# craft.model
#---------------------------------
- category: type
description: >
This type represents a model. Used in conjunction with a `craft.renderer` component to draw 3D objects.
Existing model assets can be loaded by using `craft.model(asset)`, supported 3D model formats
are `.obj`, `.fbx`, `.stl` and `.blend`.
Models are made up of a number of triangles, which are in-turn made up of vertices. Each vertex has a
number of attributes that control their appearance, such as `position`, `color` and `normal`.
To make a triangle you must set the indices of the model using the `indices` property.
You can resize the number of indices and vertices by using the `resizeIndices()` and `resizeVertices()` functions.
examples:
- example: |
-- Create a blank model
m = craft.model()
-- Blank models include attributes for position, normal, color and uv
local positions = {vec3(-1.0,-1.0,0), vec3(-1.0,1.0,0), vec3(1.0,1.0,0), vec3(1.0,-1.0,0)}
local normals = {vec3(0,0,-1), vec3(0,0,-1), vec3(0,0,-1), vec3(0,0,-1)}
local uvs = {vec2(0,0), vec2(0,1), vec2(1,1), vec2(1,0)}
local c = color(245, 3, 3, 255)
local colors = {c, c, c, c}
-- Indices are used to create triangles using groups of 3 vertices
local indices = {3,2,1,4,3,1}
-- Update model vertices using tables
m.positions = positions
m.normals = normals
m.uvs = uvs
m.colors = colors
m.indices = indices
group: Rendering
id: craft.model
name: craft.model
parameters:
- description: int, the number of vertices in this model
name: vertexCount
- description: int, the number of indices in this model
name: indexCount
- description: array, the positions of the vertices of this model
name: positions
- description: array, the normals of the vertices of this model
name: normals
- description: array, the colors of the vertices of this model
name: colors
- description: array, the uvs of the vertices of this model
name: uvs
- description: array, the primitive indices of this model used to form triangles
name: indices
- description: material, the material for this model (overridden by renderer materials)
name: material
syntax: |
myModel = craft.model()
myModel = craft.model( asset )
related:
- craft.renderer
- craft.material
#---------------------------------
#---------------------------------
# craft.model.cube
#---------------------------------
- id: craft.model.cube
category: function
description: >
Creates a cube model with the specified size and offset.
By default the size parameter is set to (1,1,1), and the offset parameter is (0,0,0).
group: Rendering
name: craft.model.cube
parameters:
- description: vec3, the size of the cube
name: size
- description: vec3, the offset of the center of the cube
name: offset
syntax: |
myModel = craft.model.cube()
myModel = craft.model.cube(size)
myModel = craft.model.cube(size, offset)
#---------------------------------
#---------------------------------
# craft.model.icosphere
#---------------------------------
- id: craft.model.icosphere
category: function
description: >
Creates an icosphere model. An icosphere is a geodesic dome made from equally sized triangles.
The number of subdivisions specified will control how dense the sphere is (increasing the number of triangles).
Setting the icosphere to flat will created faceted faces, otherwise the icosphere will have a smooth appearance.
group: Rendering
name: craft.model.icosphere
parameters:
- description: float, the radius of the icosphere
name: radius
- description: int, the number of subdivisions to apply the base icosphere
name: subdivisions
syntax: |
myModel = craft.model.icosphere(radius)
myModel = craft.model.icosphere(radius, subdivisions)
myModel = craft.model.icosphere(radius, subdivisions, flat)
#---------------------------------
#---------------------------------
# craft.model.plane
#---------------------------------
- id: craft.model.plane
category: function
description: >
Creates a horizontal plane model with the specified size and offset.
By default the size parameter is set to (1, 1), and the offset parameter is (0, 0, 0).
group: Rendering
name: craft.model.plane
parameters:
- description: vec2, the size of the plane (x, z dimensions)
name: size
- description: vec3, the offset of the center of the plane
name: offset
syntax: |
myModel = craft.model.plane()
myModel = craft.model.plane(size)
myModel = craft.model.plane(size, offset)
#---------------------------------
#---------------------------------
# model.resizeVertices
#---------------------------------
- category: method
description: Sets the number of vertices in the model.
group: Rendering
id: model.resizeVertices
name: model.resizeVertices( size )
parameters:
- description: int, the number of vertices
name: size
related:
- craft.model
- model.resizeIndices
syntax: |
myModel:resizeVertices( size )
#---------------------------------
#---------------------------------
# model.resizeIndices
#---------------------------------
- category: method
description: Sets the number of indices in the model where each set of 3 indices forms a triangle.
group: Rendering
id: model.resizeIndices
name: model.resizeIndices( size )
parameters:
- description: int, the number of indices
name: size
related:
- craft.model
- model.resizeVertices
syntax: |
myModel:resizeIndices( size )
#---------------------------------
#---------------------------------
# model.position
#---------------------------------
- category: method
description: >
Set or get the position for a vertex within this model.
Use with only the `index` parameter to return the x, y, z values of a vertex position (as 3 separate values).
group: Rendering
id: model.position
name: model.position( index, ... )
parameters:
- description: int,
name: index
related:
- craft.model
- model.resizeVertices
syntax: |
myModel:position( index )
myModel:position( index, p )
myModel:position( index, x, y, z )
#---------------------------------
#---------------------------------
# model.normal
#---------------------------------
- category: method
description: >
Set or get the normal for a vertex within this model.
Use with only the `index` parameter to return the x, y, z values of a vertex normal (as 3 separate values).
group: Rendering
id: model.normal
name: model.normal( index, ... )
parameters:
- description: int,
name: index
related:
- craft.model
- model.resizeVertices
- model.position
syntax: |
x, y, z = myModel:normal( index )
myModel:normal( index, n )
myModel:normal( index, x, y, z )
#---------------------------------
#---------------------------------
# model.color
#---------------------------------
- category: method
description: >
Set or get the color for a vertex within this model.
Use with only the `index` parameter to return the r, g, b, a values of a vertex color (as 4 separate values).
group: Rendering
id: model.color
name: model.color( index, ... )
parameters:
- description: int,
name: index
related:
- craft.model
- model.resizeVertices
- model.position