Skip to content

Commit c5d910b

Browse files
committed
chore: add Infinity Grid story
1 parent 5488d40 commit c5d910b

File tree

2 files changed

+161
-1
lines changed

2 files changed

+161
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"b:all": "pnpm run b:types && cross-env BUILD_TYPE=ALL rollup -c",
2323
"clean": "pnpm -r exec rm -rf dist && pnpm -r exec rm -rf types",
2424
"release": "bumpp -r",
25-
"storybook": "storybook dev -p 6006",
25+
"play": "storybook dev -p 6006",
2626
"build-storybook": "storybook build"
2727
},
2828
"publishConfig": {},

stories/CustomMaterial.stories.ts

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import {
2+
Camera,
3+
WebGLEngine,
4+
Vector3,
5+
MeshRenderer,
6+
PrimitiveMesh,
7+
PBRMaterial, Color,
8+
DirectLight, Script
9+
} from "@galacean/engine";
10+
import { OrbitControl } from "../packages/controls/src/OrbitControl";
11+
import { GridControl } from "../packages/custom-material/src/grid/GridControl";
12+
import { Meta } from "@storybook/html-vite";
13+
14+
export default {
15+
title: 'Toolkit/CustomMaterial',
16+
parameters: {
17+
canvas: {
18+
engineOptions: {
19+
alpha: true,
20+
antialias: true
21+
}
22+
}
23+
},
24+
argTypes: {
25+
is2DGrid: {
26+
control: { type: 'boolean' },
27+
defaultValue: false
28+
},
29+
primaryScale: {
30+
control: { type: 'range', min: 1, max: 20, step: 1 },
31+
defaultValue: 10
32+
},
33+
secondaryScale: {
34+
control: { type: 'range', min: 0.1, max: 5, step: 0.1 },
35+
defaultValue: 1
36+
},
37+
gridIntensity: {
38+
control: { type: 'range', min: 0, max: 1, step: 0.05 },
39+
defaultValue: 0.2
40+
},
41+
axisIntensity: {
42+
control: { type: 'range', min: 0, max: 1, step: 0.05 },
43+
defaultValue: 0.1
44+
},
45+
distance: {
46+
control: { type: 'range', min: 1, max: 50, step: 1 },
47+
defaultValue: 8
48+
}
49+
}
50+
} as Meta;
51+
52+
/**
53+
* Custom control script to update grid parameters from Storybook controls
54+
*/
55+
class GridParameterControl extends Script {
56+
gridControl: GridControl;
57+
58+
override onUpdate(): void {
59+
if (this.gridControl) {
60+
// Update grid material parameters from the gridControl
61+
const material = this.gridControl.material;
62+
if (material) {
63+
material.primaryScale = this.gridControl.material.primaryScale;
64+
material.secondaryScale = this.gridControl.material.secondaryScale;
65+
material.gridIntensity = this.gridControl.material.gridIntensity;
66+
material.axisIntensity = this.gridControl.material.axisIntensity;
67+
}
68+
}
69+
}
70+
}
71+
72+
export const InfinityGrid = {
73+
args: {
74+
is2DGrid: false,
75+
primaryScale: 10,
76+
secondaryScale: 1,
77+
gridIntensity: 0.2,
78+
axisIntensity: 0.1,
79+
distance: 8
80+
},
81+
render: async (args, context) => {
82+
// Get engine from context
83+
const engine: WebGLEngine = await context.getEngine();
84+
engine.canvas.resizeByClientSize();
85+
86+
const scene = engine.sceneManager.activeScene;
87+
const rootEntity = scene.createRootEntity();
88+
89+
// Create camera
90+
const cameraEntity = rootEntity.createChild("camera");
91+
cameraEntity.transform.setPosition(4, 4, 4);
92+
cameraEntity.transform.lookAt(new Vector3(0, 0, 0));
93+
const camera = cameraEntity.addComponent(Camera);
94+
camera.farClipPlane = 100;
95+
96+
// Add OrbitControl to camera
97+
const orbitControl = cameraEntity.addComponent(OrbitControl);
98+
orbitControl.target = new Vector3(0, 0, 0);
99+
orbitControl.minDistance = 2;
100+
orbitControl.maxDistance = 50;
101+
102+
// Add a light
103+
const lightEntity = rootEntity.createChild("light");
104+
lightEntity.transform.position = new Vector3(0, 5, 10);
105+
lightEntity.transform.lookAt(new Vector3(0, 0, 0));
106+
const directLight = lightEntity.addComponent(DirectLight);
107+
directLight.color = new Color(1, 1, 1, 1).toLinear(new Color());
108+
109+
// Add a sample cube to visualize the grid
110+
const cubeEntity = rootEntity.createChild("cube");
111+
cubeEntity.transform.position = new Vector3(0, 0.5, 0);
112+
const cubeRenderer = cubeEntity.addComponent(MeshRenderer);
113+
cubeRenderer.mesh = PrimitiveMesh.createCuboid(engine, 1, 1, 1);
114+
const material = new PBRMaterial(engine);
115+
material.metallic = 0.1;
116+
material.roughness = 0.5;
117+
material.baseColor = new Color(0.129, 0.8, 0.8, 1.0);
118+
cubeRenderer.setMaterial(material);
119+
120+
// Create grid entity
121+
const gridEntity = rootEntity.createChild("grid");
122+
123+
// Add GridControl component
124+
const grid = gridEntity.addComponent(GridControl);
125+
grid.camera = camera;
126+
grid.distance = args.distance;
127+
128+
// Set grid parameters from args
129+
grid.material.primaryScale = args.primaryScale;
130+
grid.material.secondaryScale = args.secondaryScale;
131+
grid.material.gridIntensity = args.gridIntensity;
132+
grid.material.axisIntensity = args.axisIntensity;
133+
134+
// Set 2D/3D mode
135+
grid.is2DGrid = args.is2DGrid;
136+
137+
// Add parameter control script to update grid when controls change
138+
const paramControl = gridEntity.addComponent(GridParameterControl);
139+
paramControl.gridControl = grid;
140+
141+
// Watch for changes in args and update grid parameters
142+
const updateGridFromArgs = () => {
143+
grid.material.primaryScale = args.primaryScale;
144+
grid.material.secondaryScale = args.secondaryScale;
145+
grid.material.gridIntensity = args.gridIntensity;
146+
grid.material.axisIntensity = args.axisIntensity;
147+
grid.distance = args.distance;
148+
grid.is2DGrid = args.is2DGrid;
149+
};
150+
151+
context.watch = (newArgs) => {
152+
args = newArgs;
153+
updateGridFromArgs();
154+
};
155+
156+
engine.run();
157+
158+
return document.createElement('div');
159+
}
160+
};

0 commit comments

Comments
 (0)