Start a new topic

DuoCode ThreeJS Plugin Available

My New Plugin Is Available.


ThreeJS Support For DuoCode. Example usage is a mirror of the the api. This is an simple view controller using ThreeJS to render a rotating cube. 


Nuget Package: https://www.nuget.org/packages/DuoCode.Plugins.Three


 

using System;
using System.Gateway;
using System.Threading.Tasks;

using DuoCode.Dom;
using DuoCode.Runtime;

namespace Tx.Duo
{

	public class ThreeView : ViewController
	{
		public THREE.Scene scene { get; protected set; }
		public THREE.Camera camera { get; protected set; }
		public THREE.Renderer renderer { get; protected set; }
		public THREE.AmbientLight light { get; protected set; }
		public THREE.Mesh box { get; protected set; }

		public ThreeView() { }
		protected override void OnInitialize(object parameter)
		{
			base.OnInitialize(parameter);
			this.element.style.backgroundColor = "Black";

			double width = this.element.clientWidth;
			double height = this.element.clientHeight;

			// Setup scene
			scene = new THREE.Scene();
			light = new THREE.AmbientLight(0xffffff);
			if (Root.features.supportsWebGL()) {
				Trace.Info("Using WebGL rendering...");
				renderer = new THREE.WebGLRenderer();
			} else {
				Trace.Info("Using Canvas rendering...");
				renderer = new THREE.CanvasRenderer();
			}
			renderer.setSize(width, height);
			this.element.appendChild(renderer.domElement);

			// Add lighting
			scene.add(light);

			// Setup camera
			camera = new THREE.PerspectiveCamera(
			35,
			width / height,
			1,
			1000
			);
			camera.position.z = 100;
			scene.add(camera);

			// Create box mesh
			box = new THREE.Mesh(
			new THREE.BoxGeometry(20, 20, 20),
				new THREE.MeshBasicMaterial(new THREE.MeshBasicMaterialParameters { color = 0x008000 })
        	);
			box.name = "box";
			scene.add(box);

			// Start render loop
			OnRender();
		}

		#pragma warning disable RECS0135
		public void OnRender()
		{
			box.rotation.y += 0.01;

			renderer.render(scene, camera);
			Global.window.requestAnimationFrame(OnRender);
		}
		#pragma warning restore RECS0135

		protected override void OnDispose()
		{
			base.OnDispose();
		}
	}
}

 


1 Comment

Login or Signup to post a comment