forked from Orillusion/orillusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample_GraphicMesh_6.ts
171 lines (134 loc) · 5.68 KB
/
Sample_GraphicMesh_6.ts
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
import { GUIHelp } from "@orillusion/debug/GUIHelp";
import { Object3D, Scene3D, Engine3D, AtmosphericComponent, CameraUtil, HoverCameraController, View3D, DirectLight, KelvinUtil, LitMaterial, MeshRenderer, BoxGeometry, SphereGeometry, VirtualTexture, GPUTextureFormat, UnLitMaterial, UnLitTexArrayMaterial, BitmapTexture2DArray, BitmapTexture2D, PlaneGeometry, Vector3, Graphic3DMesh, Matrix4, Time, BlendMode, Color } from "@orillusion/core";
import { GUIUtil } from "@samples/utils/GUIUtil";
import { Stats } from "@orillusion/stats";
export class Sample_GraphicMesh_6 {
lightObj3D: Object3D;
scene: Scene3D;
parts: Object3D[];
width: number;
height: number;
cafe: number = 47;
frame: number = 16;
view: View3D;
colors: Color[];
constructor() { }
async run() {
Matrix4.maxCount = 500000;
Matrix4.allocCount = 500000;
await Engine3D.init({ beforeRender: () => this.update() });
Engine3D.setting.render.debug = true;
Engine3D.setting.shadow.shadowBound = 5;
this.colors = [];
GUIHelp.init();
this.scene = new Scene3D();
this.scene.addComponent(Stats);
let sky = this.scene.addComponent(AtmosphericComponent);
sky.enable = false;
let camera = CameraUtil.createCamera3DObject(this.scene);
camera.perspective(60, Engine3D.aspect, 1, 5000.0);
camera.object3D.addComponent(HoverCameraController).setCamera(30, 0, 120);
this.view = new View3D();
this.view.scene = this.scene;
this.view.camera = camera;
Engine3D.startRenderView(this.view);
GUIUtil.renderDebug();
await this.initScene();
sky.relativeTransform = this.lightObj3D.transform;
}
async initScene() {
/******** light *******/
{
this.lightObj3D = new Object3D();
this.lightObj3D.rotationX = 21;
this.lightObj3D.rotationY = 108;
this.lightObj3D.rotationZ = 10;
let directLight = this.lightObj3D.addComponent(DirectLight);
directLight.lightColor = KelvinUtil.color_temperature_to_rgb(5355);
directLight.castShadow = false;
directLight.intensity = 10;
GUIUtil.renderDirLight(directLight);
this.scene.addChild(this.lightObj3D);
}
let texts = [];
texts.push(await Engine3D.res.loadTexture("textures/128/star_0008.png") as BitmapTexture2D);
let bitmapTexture2DArray = new BitmapTexture2DArray(texts[0].width, texts[0].height, texts.length);
bitmapTexture2DArray.setTextures(texts);
let mat = new UnLitTexArrayMaterial();
mat.baseMap = bitmapTexture2DArray;
mat.name = "LitMaterial";
GUIHelp.add(this, "cafe", 0.0, 100.0);
GUIHelp.add(this, "frame", 0.0, 100.0);
{
this.width = 15;
this.height = 15;
// let geometry = new BoxGeometry(1, 1, 1);
let geometry = new PlaneGeometry(1, 1, 1, 1, Vector3.Z_AXIS);
let mr = Graphic3DMesh.draw(this.scene, geometry, bitmapTexture2DArray, this.width * this.height);
this.parts = mr.object3Ds;
mr.material.blendMode = BlendMode.ADD;
// mr.material.doubleSide = true;
mr.material.transparent = true;
mr.material.depthWriteEnabled = false;
mr.material.useBillboard = true;
for (let i = 0; i < this.width * this.height; i++) {
const element = this.parts[i];
// mr.setTextureID(i, i % texts.length);
// mr.setTextureID(i, 52);
mr.setTextureID(i, 0);
// mr.setTextureID(i, 39);
// mr.setTextureID(i, 18);
element.transform.scaleX = 5.5;
element.transform.scaleY = 5.5;
element.transform.scaleZ = 5.5;
// let c = Color.random();
// c.a = 0.55;
// this.colors.push(c);
}
let c1 = new Color(0.65, 0.1, 0.2, 0.15);
let c2 = new Color(1.0, 1.1, 0.2, 0.65);
this.colors.push(c1);
this.colors.push(c2);
}
this.updateOnce(1000);
}
private tmpArray: any[] = [];
update() {
}
updateOnce(engineFrame: number) {
if (this.parts) {
let pos = new Vector3();
this.tmpArray.length = 0;
for (let i = 0; i < this.parts.length; i++) {
const element = this.parts[i];
let tmp = this.sphericalFibonacci(i, this.parts.length);
tmp.scaleBy(this.cafe);
element.transform.localPosition = tmp;
// if (sc > this.cafe * 0.95) {
this.tmpArray.push(element);
// }
}
for (let i = 0; i < this.tmpArray.length - 1; i++) {
this.view.graphic3D.Clear(i.toString());
this.view.graphic3D.drawLines(i.toString(), [
Vector3.ZERO,
this.tmpArray[i + 1].transform.worldPosition,
],
this.colors);
}
}
}
public madfrac(A: number, B: number): number {
return A * B - Math.floor(A * B);
}
public sphericalFibonacci(i: number, n: number): Vector3 {
const PHI = Math.sqrt(5.0) * 0.5 + 0.5;
let phi = 2.0 * Math.PI * this.madfrac(i, PHI - 1);
let cosTheta = 1.0 - (2.0 * i + 1.0) * (1.0 / n);
let sinTheta = Math.sqrt(Math.max(Math.min(1.0 - cosTheta * cosTheta, 1.0), 0.0));
return new Vector3(
Math.cos(phi) * sinTheta,
Math.sin(phi) * sinTheta,
cosTheta);
}
}