three_js_controls 0.1.0
three_js_controls: ^0.1.0 copied to clipboard
Flutter controls package converted from threejs and three_dart package.
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:three_js_controls/three_js_controls.dart';
import 'package:three_js_core/three_js_core.dart' as three;
import 'package:three_js_geometry/three_js_geometry.dart';
import 'package:three_js_math/three_js_math.dart' as tmath;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MiscControlsOrbit(),
);
}
}
class MiscControlsOrbit extends StatefulWidget {
const MiscControlsOrbit({super.key});
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MiscControlsOrbit> {
late three.ThreeJS threeJs;
@override
void initState() {
threeJs = three.ThreeJS(
onSetupComplete: (){setState(() {});},
setup: setup,
settings: three.Settings(
renderOptions: {
"minFilter": tmath.LinearFilter,
"magFilter": tmath.LinearFilter,
"format": tmath.RGBAFormat,
"samples": 4
}
)
);
super.initState();
}
@override
void dispose() {
threeJs.dispose();
controls.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return threeJs.build();
}
late three.Mesh mesh;
late OrbitControls controls;
void setup() {
threeJs.scene = three.Scene();
threeJs.scene.background = tmath.Color.fromHex32(0xcccccc);
threeJs.scene.fog = three.FogExp2(0xcccccc, 0.002);
threeJs.camera = three.PerspectiveCamera(60, threeJs.width / threeJs.height, 1, 1000);
threeJs.camera.position.setValues(400, 200, 0);
// controls
controls = OrbitControls(threeJs.camera, threeJs.globalKey);
// controls.listenToKeyEvents( window );
//controls.addEventListener( 'change', render ); // call this only in static scenes (i.e., if there is no animation loop)
controls.enableDamping =
true; // an animation loop is required when either damping or auto-rotation are enabled
controls.dampingFactor = 0.05;
controls.screenSpacePanning = false;
controls.minDistance = 100;
controls.maxDistance = 500;
controls.maxPolarAngle = math.pi / 2;
// world
final geometry = CylinderGeometry(0, 10, 30, 4, 1);
final material = three.MeshPhongMaterial.fromMap({"color": 0xffffff, "flatShading": true});
for (int i = 0; i < 500; i++) {
final mesh = three.Mesh(geometry, material);
mesh.position.x = math.Random().nextDouble() * 1600 - 800;
mesh.position.y = 0;
mesh.position.z = math.Random().nextDouble() * 1600 - 800;
mesh.updateMatrix();
mesh.matrixAutoUpdate = false;
threeJs.scene.add(mesh);
}
final dirLight1 = three.DirectionalLight(0xffffff);
dirLight1.position.setValues(1, 1, 1);
threeJs.scene.add(dirLight1);
final dirLight2 = three.DirectionalLight(0x002288);
dirLight2.position.setValues(-1, -1, -1);
threeJs.scene.add(dirLight2);
final ambientLight = three.AmbientLight(0x222222);
threeJs.scene.add(ambientLight);
threeJs.addAnimationEvent((dt){
controls.update();
});
}
}