public override void Run(RenderControl render)
        {
            // Create material
            var mMaterial1 = MeshPhongMaterial.Create("phong.texture");

            mMaterial1.SetFaceSide(EnumFaceSide.DoubleSide);
            var texture = ImageTexture2D.Create(GetResourcePath("textures/bricks2.jpg"));

            mMaterial1.SetColorMap(texture);

            var plane     = GeometryBuilder.CreatePlane(500, 500);
            var planeNode = new PrimitiveSceneNode(plane, mMaterial1);

            planeNode.SetTransform(Matrix4.makeTranslation(new Vector3(0, 0, -100)));
            render.ShowSceneNode(planeNode);

            var box     = GeometryBuilder.CreateBox(100, 100, 200);
            var boxNode = new PrimitiveSceneNode(box, mMaterial1);

            render.ShowSceneNode(boxNode);

            var sphere     = GeometryBuilder.CreateSphere(50, 32, 32);
            var sphereNode = new PrimitiveSceneNode(sphere, mMaterial1);

            sphereNode.SetTransform(Matrix4.makeTranslation(new Vector3(0, 0, 150)));

            render.ShowSceneNode(sphereNode);
        }
        public override void Run(RenderControl render)
        {
            RedMaterial = MeshPhongMaterial.Create("phong.color");
            RedMaterial.SetColor(Vector3.Red);

            GrayMaterial = MeshPhongMaterial.Create("phong.color");
            GrayMaterial.SetColor(Vector3.LightGray);
            GrayMaterial.SetTransparent(true);
            GrayMaterial.SetOpacity(0.5f);

            var tube = ShapeBuilder.MakeTube(new GPnt(0, 0, 5), GP.DZ(), 10, 2, 50);

            TubeNode = BrepSceneNode.Create(tube, RedMaterial, null);

            var cylinder = ShapeBuilder.MakeCylinder(GP.XOY(), 10, 60, 0);

            CylinderNode = BrepSceneNode.Create(cylinder, GrayMaterial, null);

            render.ShowSceneNode(TubeNode);
            render.ShowSceneNode(CylinderNode);


            var cone = ShapeBuilder.MakeCone(GP.YOZ(), 5, 0, 10, 0);
            var bs   = new BufferShape(cone, RedMaterial, null, 0.1);

            bs.Build();
            ConeNode1 = new BrepSceneNode(bs);
            ConeNode2 = new BrepSceneNode(bs);

            render.ShowSceneNode(ConeNode1);
            render.ShowSceneNode(ConeNode2);
        }
Example #3
0
        public override void Run(RenderControl render)
        {
            if (!ReadData())
            {
                return;
            }

            var material = MeshPhongMaterial.Create("cae-material");

            material.GetTemplate().SetVertexColors(true);
            material.SetFaceSide(EnumFaceSide.DoubleSide);


            var position = BufferAttribute.Create(EnumAttributeSemantic.Position, EnumAttributeComponents.Three, mPositions);
            var color    = BufferAttribute.Create(EnumAttributeSemantic.Color, EnumAttributeComponents.Three, mColors);

            BufferGeometry geometry = new BufferGeometry();

            geometry.AddAttribute(position);
            geometry.AddAttribute(color);

            NormalCalculator.ComputeVertexNormals(geometry);

            var node = new PrimitiveSceneNode(geometry, EnumPrimitiveType.TRIANGLES, material);

            node.SetPickable(false);

            PaletteWidget pw = new PaletteWidget();

            pw.Update(mColorTable);

            render.ShowSceneNode(pw);

            render.ShowSceneNode(node);
        }
Example #4
0
        private void NewForm3D_Load(object sender, EventArgs e)
        {
            var box      = GeometryBuilder.CreateBox(100, 200, 300);
            var material = MeshPhongMaterial.Create("simple");

            material.SetColor(ColorTable.Red);
            var node = new PrimitiveSceneNode(box, material);

            mRenderView.ShowSceneNode(node);

            mRenderView.SetStandardView(EnumStandardView.DefaultView);
            mRenderView.ZoomAll();

            // 转发给事件处理
            mRenderView.SetAfterRenderingCallback(() => {
                if (UpdateTagEvent != null)
                {
                    UpdateTagEvent();
                }
            });

            // 创建两个自定义标注
            var mTagCtl = new MyTagControl(mRenderView, new Vector3(200, 300, 400), Vector3.Zero);

            UpdateTagEvent += mTagCtl.UpdateLayout;

            var mTagCtl2 = new MyTagControl(mRenderView, new Vector3(-100, -200, 100), Vector3.Zero);

            UpdateTagEvent += mTagCtl2.UpdateLayout;
        }
        public override void Run(RenderControl render)
        {
            // add a plane
            var mMaterial1 = MeshPhongMaterial.Create("phong.texture");

            mMaterial1.SetFaceSide(EnumFaceSide.DoubleSide);
            var texture = ImageTexture2D.Create(GetResourcePath("textures/bricks2.jpg"));

            mMaterial1.SetColorMap(texture);

            var plane     = GeometryBuilder.CreatePlane(500, 500);
            var planeNode = new PrimitiveSceneNode(plane, mMaterial1);

            planeNode.SetTransform(Matrix4.makeTranslation(new Vector3(0, 0, -2.5f)));
            planeNode.SetPickable(false);

            render.ShowSceneNode(planeNode);



            mDevice = new PrimitiveSceneNode(GeometryBuilder.CreateSphere(5), null);
            render.ShowSceneNode(mDevice);

            mCome = new RigidAnimation();
            mCome.Add(new MoveAnimationClip(mDevice, mWorkingPosition, 0, 5));

            mGo = new RigidAnimation();
            mGo.Add(new MoveAnimationClip(mDevice, new Vector3(-200, -200, 0), 0, 5));
        }
        public override void Run(RenderControl renderer)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "IGES (*.igs;*.iges)|*.igs;*.iges|STEP (*.stp;*.step)|*.stp;*.step|Brep (*.brep)|*.brep";
            if (dialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            var shape = ShapeIO.Open(dialog.FileName);

            if (shape == null)
            {
                return;
            }

            var bs = new BufferShape(shape, null, null, 0);

            bs.Build();

            // 1. Compute
            var holeExp = new HoleExplor();

            if (!holeExp.Initialize(bs))
            {
                return;
            }
            var dir = holeExp.ComputeDirection();

            holeExp.Compute(dir);

            var defaultMaterial = MeshPhongMaterial.Create("face-x");

            defaultMaterial.SetFaceSide(EnumFaceSide.DoubleSide);
            bs.SetFaceMaterial(defaultMaterial);


            // 2. Set hole faces with red color
            var material = BasicMaterial.Create("hole-face");

            material.SetFaceSide(EnumFaceSide.DoubleSide);
            material.SetColor(new Vector3(0.5f, 0, 0));
            var holeNumber = holeExp.GetHoleCount();

            for (uint ii = 0; ii < holeNumber; ++ii)
            {
                var faceIDs = holeExp.GetHoleFaces(ii);
                foreach (var faceIdx in faceIDs)
                {
                    bs.SetFaceMaterial(faceIdx, material);
                }
            }

            // 2. Show the faces
            var shapeNode = new BrepSceneNode(bs);

            renderer.ShowSceneNode(shapeNode);
        }
Example #7
0
        public override void Run(RenderControl render)
        {
            var material = MeshPhongMaterial.Create("phong.bspline");

            material.SetUniform("diffuse", Uniform.Create(new Vector3(1, 0, 1)));

            var shape = CreateShape();
            var node  = BrepSceneNode.Create(shape, material, null);

            render.ShowSceneNode(node);
        }
Example #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="control"></param>
        public override void Load(Control control)
        {
            base.Load(control);

            camera = new PerspectiveCamera(70, control.Width / (float)control.Height, 1, 1000);
            this.camera.Position.Z = 400;

            scene     = new Scene();
            scene.Fog = new Fog(Color.Black, 1, 1000);

            object3D = new Object3D();
            scene.Add(object3D);

            var geometry = new SphereGeometry(1, 4, 4);
            var material = new MeshPhongMaterial()
            {
                Color = Color.White, Shading = ThreeCs.Three.FlatShading
            };

            for (var i = 0; i < 100; i++)
            {
                var mesh = new Mesh(geometry, material);
                mesh.Position.set(Mat.Random() - 0.5f, Mat.Random() - 0.5f, Mat.Random() - 0.5f).Normalize();
                mesh.Position.MultiplyScalar(Mat.Random() * 400);
                mesh.Rotation.set(Mat.Random() * 2, Mat.Random() * 2, Mat.Random() * 2);
                mesh.Scale.X = mesh.Scale.Y = mesh.Scale.Z = Mat.Random() * 50;
                object3D.Add(mesh);
            }

            scene.Add(new AmbientLight((Color)colorConvertor.ConvertFromString("#222222")));

            light = new DirectionalLight(Color.White);
            light.Position.set(1, 1, 1);
            scene.Add(light);

            // postprocessing

            composer = new EffectComposer(renderer, control);
            composer.AddPass(new RenderPass(scene, camera));

            var effect1 = new ShaderPass(new DotScreenShader());

            effect1.Uniforms["scale"]["value"] = 4;
            composer.AddPass(effect1);

            var effect2 = new ShaderPass(new RGBShiftShader());

            effect2.Uniforms["amount"]["value"] = 0.0015f;
            effect2.RenderToScreen = true;
            composer.AddPass(effect2);
        }
Example #9
0
        public override void Run(RenderControl render)
        {
            if (!ReadData())
            {
                return;
            }

            var matplot = Matplot.Create("MyMatlab 2020");

            var xRange = new PlotRange(mMinX, mMaxX - 1, 1);
            var yRange = new PlotRange(mMinY, mMaxY - 1, 1);

            matplot.AddSurface(xRange, yRange, (idxU, idxV, u, v) =>
            {
                double x = u;
                double y = v;
                double z = mData[idxU + 1][idxV + 1];

                return(new GPnt(x, y, z));
            });

            var node = matplot.Build(ColorMapKeyword.Create(EnumSystemColorMap.Cooltowarm));

            node.SetPickable(false);

            var pw = new PaletteWidget();

            pw.Update(matplot.GetColorTable());

            render.ShowSceneNode(pw);
            render.ShowSceneNode(node);

            var material = MeshPhongMaterial.Create("font-x");

            material.SetColor(Vector3.Red);
            var shape = FontManager.Instance().CreateMesh("Create a better world!");

            shape.SetMaterial(material);
            var text = new TextSceneNode(shape, 24, true);
            //var text = new TextSceneNode("Wow", 24, new Vector3(1, 1, 0), Vector3.Red, false);
            var tag = TagNode2D.Create(text, new Vector3(15), new Vector3(0));

            render.ShowSceneNode(tag);
        }
Example #10
0
        public override void Run(RenderControl render)
        {
            var arrowMaterial = MeshPhongMaterial.Create("arrow");

            arrowMaterial.SetColor(ColorTable.Red);
            mArrow = ArrowWidget.Create(2, 10, arrowMaterial);
            mArrow.SetPickable(false);
            render.ShowSceneNode(mArrow);


            var shape = ShapeBuilder.MakeBox(GP.XOY(), 10, 20, 30);
            var mat   = MeshStandardMaterial.Create("ss");

            mat.SetColor(Vector3.LightGray);

            var node = BrepSceneNode.Create(shape, mat, null);

            render.ShowSceneNode(node);
        }
        /// <summary>
        ///     Converts the material node to <see cref="MeshPhongMaterial"/> instance.
        /// </summary>
        /// <param name="material">The material.</param>
        /// <returns></returns>
        public static MeshPhongMaterial ToMeshPhong(this MaterialNode material)
        {
            if (material is null)
            {
                throw new System.ArgumentNullException(nameof(material));
            }

            var materialColor = material.Color.ToInt();
            var materialUuid  = $@"MaterialNode_{materialColor}_{material.Transparency * 100:0.##}";
            var meshPhong     = new MeshPhongMaterial(materialUuid)
            {
                Color       = materialColor,
                Ambient     = materialColor,
                Emissive    = 0,
                Specular    = materialColor,
                Shininess   = 1,
                Opacity     = (100D - material.Transparency) / 100,
                Transparent = material.Transparency > 0,
                Wireframe   = false,
            };

            return(meshPhong);
        }
Example #12
0
        public override void Run(RenderControl render)
        {
            mMaterial1 = MeshPhongMaterial.Create("phong.texture");
            mMaterial1.SetColor(new Vector3(1, 0, 1));

            var texture = ImageTexture2D.Create(GetResourcePath("textures/bricks2.jpg"));

            texture.SetRepeat(new Vector2(2.0f, 2.0f));
            texture.UpdateTransform();

            mMaterial1.SetColorMap(texture);

            var shape = ShapeBuilder.MakeBox(GP.XOY(), 4, 4, 8);

            mNode = BrepSceneNode.Create(shape, mMaterial1, null);

            mMaterial2 = MeshPhongMaterial.Create("phong.texture");
            var texture2 = ImageTexture2D.Create(GetResourcePath("textures/water.png"));

            mMaterial2.SetColorMap(texture2);


            render.ShowSceneNode(mNode);
        }
Example #13
0
        private static void Init()
        {
            camera = new PerspectiveCamera(renderer, 30, 1, 10000)
            {
                Position = new Vector3(0, 0, 100)
            };

            cameraRTT = new OrthographicCamera(renderer, -10000, 10000)
            {
                Position = new Vector3(0, 0, 100)
            };

            scene       = new Scene();
            sceneRTT    = new Scene();
            sceneScreen = new Scene();

            var light = new DirectionalLight(Color.White)
            {
                Position = Vector3.UnitZ.Normalized()
            };

            sceneRTT.Add(light);

            light = new DirectionalLight(new Color(0xffaaaa))
            {
                Position  = Vector3.UnitNegativeZ.Normalized(),
                Intensity = 1.5f
            };
            sceneRTT.Add(light);

            rtTexture = new RenderTarget(renderer.Width, renderer.Height)
            {
                MinFilter = TextureMinFilter.Linear,
                MagFilter = TextureMagFilter.Nearest,
                Format    = Three.Net.Renderers.PixelFormat.RGB
            };

            var vertexShaderSource = @"
varying vec2 vUv;
void main() 
{
	vUv = uv;
	gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}";

            var fragment_shader_screenSource = @"
varying vec2 vUv;
uniform sampler2D tDiffuse;
void main() 
{
	gl_FragColor = texture2D( tDiffuse, vUv );
}";
            var fragment_shader_pass_1Source = @"
varying vec2 vUv;
uniform float time;
void main() 
{
	float r = vUv.x;
	if( vUv.y < 0.5 ) r = 0.0;
	float g = vUv.y;
	if( vUv.x < 0.5 ) g = 0.0;

	gl_FragColor = vec4( r, g, time, 1.0 );
}";

            material = new CustomShaderMaterial(renderer, vertexShaderSource, fragment_shader_pass_1Source, m => { });

            var materialScreen = new CustomShaderMaterial(renderer, vertexShaderSource, fragment_shader_screenSource, m => {})
            {
                ShouldDepthWrite = false
            };

            var plane = new PlaneGeometry(renderer.Width, renderer.Height);
            var quad  = new Mesh(plane, material)
            {
                Position = new Vector3(0, 0, -100)
            };

            sceneRTT.Add(quad);

            var geometry = new TorusGeometry(100, 25, 15, 30);

            var mat1 = new MeshPhongMaterial(renderer)
            {
                Diffuse   = new Color(0x555555),
                Specular  = new Color(0xffaa00),
                Shininess = 5
            };
            var mat2 = new MeshPhongMaterial(renderer)
            {
                Diffuse   = new Color(0x550000),
                Specular  = new Color(0xff2200),
                Shininess = 5
            };

            zmesh1 = new Mesh(geometry, mat1)
            {
                Position = new Vector3(0, 0, 100),
                Scale    = new Vector3(1.5f, 1.5f, 1.5f)
            };
            sceneRTT.Add(zmesh1);

            zmesh2 = new Mesh(geometry, mat2)
            {
                Position = new Vector3(0, 150, 100),
                Scale    = new Vector3(0.75f, 0.75f, 0.75f)
            };
            sceneRTT.Add(zmesh2);

            quad = new Mesh(plane, materialScreen)
            {
                Position = new Vector3(0, 0, -100)
            };
            sceneScreen.Add(quad);

            var n = 5;
            var sphereGeometry = new SphereGeometry(10, 64, 32);
            var material2      = new MeshBasicMaterial(renderer)
            {
                Diffuse    = Color.White,
                DiffuseMap = rtTexture
            };

            for (var j = 0; j < n; j++)
            {
                for (var i = 0; i < n; i++)
                {
                    var mesh = new Mesh(sphereGeometry, material2)
                    {
                        Position = new Vector3(
                            (i - (n - 1) / 2) * 20,
                            (j - (n - 1) / 2) * 20,
                            0),
                        Rotation = new Euler(0, -Mathf.Pi / 2, 0)
                    };
                    scene.Add(mesh);
                }
            }

            renderer.ShouldAutoClear = false;
        }
Example #14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="control"></param>
        public override void Load(Control control)
        {
            base.Load(control);

            camera = new PerspectiveCamera(60, control.Width / (float)control.Height, 1, 10000);
            this.camera.Position.Z = 100;

            this.cameraRtt            = new OrthographicCamera(control.Width / -2, control.Width / 2, control.Height / 2, control.Height / -2, -10000, 10000);
            this.cameraRtt.Position.Z = 100;

            scene         = new Scene();
            this.sceneRtt = new Scene();
            sceneScreen   = new Scene();

            var light = new DirectionalLight(Color.White);

            light.Position.set(0, 0, 1).Normalize();
            this.sceneRtt.Add(light);

            light = new DirectionalLight((Color)colorConvertor.ConvertFromString("#ffaaaa"), 1.5f);
            light.Position.set(0, 0, -1).Normalize();
            this.sceneRtt.Add(light);

            rtTexture = new WebGLRenderTarget(control.Width, control.Height)
            {
                MinFilter = ThreeCs.Three.LinearFilter, MagFilter = ThreeCs.Three.NearestFilter, Format = ThreeCs.Three.RGBFormat
            };

            material = new ShaderMaterial()
            {
                Uniforms = new Uniforms {
                    { "time", new Uniform()
                      {
                          { "type", "f" }, { "value", 0.0f }
                      } }
                },
                VertexShader   = VertexShader,
                FragmentShader = FragmentShaderPass1,
            };

            materialScreen = new ShaderMaterial()
            {
                Uniforms = new Uniforms {
                    { "tDiffuse", new Uniform()
                      {
                          { "type", "t" }, { "value", rtTexture }
                      } }
                },
                VertexShader   = VertexShader,
                FragmentShader = FragmentShaderScreen,
            };

            var plane = new PlaneBufferGeometry(control.Width, control.Height);

            quad            = new Mesh(plane, material);
            quad.Position.Z = -100;
            this.sceneRtt.Add(quad);

            var geometry = new TorusGeometry(100, 25, 15, 30);

            var mat1 = new MeshPhongMaterial()
            {
                Color = (Color)colorConvertor.ConvertFromString("#555555"), Specular = (Color)colorConvertor.ConvertFromString("#ffaa00"), Shininess = 5
            };
            var mat2 = new MeshPhongMaterial()
            {
                Color = (Color)colorConvertor.ConvertFromString("#550000"), Specular = (Color)colorConvertor.ConvertFromString("#ff2200"), Shininess = 5
            };

            zmesh1 = new Mesh(geometry, mat1);
            zmesh1.Position.set(0, 0, 100);
            zmesh1.Scale.set(1.5f, 1.5f, 1.5f);
            this.sceneRtt.Add(zmesh1);

            zmesh2 = new Mesh(geometry, mat2);
            zmesh2.Position.set(0, 150, 100);
            zmesh2.Scale.set(0.75f, 0.75f, 0.75f);
            this.sceneRtt.Add(zmesh2);

            quad            = new Mesh(plane, materialScreen);
            quad.Position.Z = -100;
            sceneScreen.Add(quad);

            //
            var geometry3 = new SphereGeometry(10, 64, 32);
            var material2 = new MeshBasicMaterial()
            {
                Color = Color.White                                                  /*, Map = rtTexture*/
            };

            int n = 5;

            for (var j = 0; j < n; j++)
            {
                for (var i = 0; i < n; i++)
                {
                    var mesh = new Mesh(geometry3, material2);

                    mesh.Position.X = (i - (n - 1) / 2) * 20;
                    mesh.Position.Y = (j - (n - 1) / 2) * 20;
                    mesh.Position.Z = 0;

                    mesh.Rotation.Y = (float)-Math.PI / 2.0f;

                    scene.Add(mesh);
                }
            }

            renderer.AutoClear = false;
        }
Example #15
0
        static void Main(string[] args)
        {
            var mediaPath    = Path.GetFullPath("../../../../../js/r68/examples/");
            var texturesPath = Path.Combine(mediaPath, "textures");

            var renderer = new Renderer();

            var scene = new Scene()
            {
                //Fog = new FogExp2(Color.Blue, 0.24f)
            };

            var camera = new PerspectiveCamera(renderer, 75)
            {
                Position = new Vector3(0, 0, 2)
            };

            //// create a point light
            scene.Add(new PointLight(Color.Gold)
            {
                Position  = new Vector3(3, 2, 2),
                Intensity = 1
            });

            scene.Add(new DirectionalLight(Color.Blue)
            {
                Target    = new Vector3(1, 0, 0),
                Intensity = 2
            });

            SpotLight s = new SpotLight(Color.Red)
            {
                Intensity = 5,
                Distance  = 10,
                Angle     = 106.7f,
                Exponent  = 200,
                Position  = new Vector3(-2, 0, 4),
                Target    = new Vector3(0, 0, 0),
            };

            scene.Add(s);

            //var loader = new JsonLoader();
            mediaPath = Path.Combine(mediaPath, "../../tests/");
            //var path = Path.Combine(mediaPath, "frankie.obj");
            //var fullpath = Path.GetFullPath(path);
            //Debug.Assert(File.Exists(fullpath));
            //var frankie = ObjMtlLoader.Parse(renderer, fullpath);
            //frankie.Scale.Multiply(0.4f);
            //scene.Add(frankie);
            //var json = JObject.Parse(File.ReadAllText(fullpath));
            //var info = loader.Parse(renderer, json, mediaPath);
            //var monkey = new Mesh(info.Geometry, new MeshFaceMaterial(renderer, info.Materials));
            //scene.Add(monkey);

            var materialMap = new MeshPhongMaterial(renderer)
                              //var materialMap = new MeshBasicMaterial(renderer)
                              //var materialMap = new MeshLambertMaterial(renderer)
            {
                Hardness = 1,

                Shininess = 10,

                Specular = new Color(.001f, .05f, .1f),

                //DiffuseColor = Color.Red,
                //Ambient = Color.Red,
                ShouldWrapAround = true,

                NormalMap   = new Texture(Path.Combine(texturesPath, "planets\\earth_normal_2048.jpg")),
                SpecularMap = new Texture(Path.Combine(texturesPath, "planets\\earth_specular_2048.jpg")),
                DiffuseMap  = new Texture(Path.Combine(texturesPath, "planets\\earth_atmos_2048.jpg")),

                //DiffuseColor = Color.Red,
            };

            var materialNoMap = new MeshPhongMaterial(renderer)
                                //var materialNoMap = new MeshBasicMaterial(renderer)
                                //var materialNoMap = new MeshLambertMaterial(renderer)
            {
                Hardness = 1,

                Shininess = 10,

                Specular = new Color(.001f, .05f, .1f),

                ShouldWrapAround = true,

                NormalMap   = new Texture(Path.Combine(texturesPath, "planets\\earth_normal_2048.jpg")),
                SpecularMap = new Texture(Path.Combine(texturesPath, "planets\\earth_specular_2048.jpg")),
                DiffuseMap  = new Texture(Path.Combine(texturesPath, "planets\\earth_atmos_2048.jpg")),

                //DiffuseColor = Color.Blue,
            };

            var cube = CreateCube(renderer, scene, texturesPath, materialMap);
            var cube2
                = CreateCube(renderer, scene, texturesPath, materialNoMap);
            var cube3 = CreateCube(renderer, scene, texturesPath, materialMap);

            var p = cube.Position;

            //p.x -= 2;

            cube.Position = p;

            p    = cube2.Position;
            p.x += 2;

            cube2.Position = p;

            scene.Add(cube);
            //scene.Add(cube2);
            //scene.Add(cube3);
            //frankie.Add(cube);
            //cube.Position = new Vector3(0, 1, 1);

            var lines = CreateHilbertCube(renderer, scene);

            lines.Scale = new Vector3(4, 4, 4);
            //scene.Add(lines);
            //lines.Position = new Vector3(0, 1, -1);

            var arrowHelper = ArrowHelper.Create(renderer, new Vector3(1, 1, 0), Vector3.Zero, 0.35f, Color.Red);

            arrowHelper.Position = new Vector3(0.25f, 0.25f, 0.25f);
            //scene.Add(arrowHelper);

            var axisHelper = AxisHelper.Create(renderer, 0.25f);

            axisHelper.Position = new Vector3(-0.25f, 0.25f, 0.25f);
            //scene.Add(axisHelper);

            //var boundingBoxHelper = BoundingBoxHelper.Create(renderer, frankie);
            //scene.Add(boundingBoxHelper);

            var previousTime = 0f;
            var stopwatch    = Stopwatch.StartNew();

            while (!renderer.Done)
            {
                var now       = (float)stopwatch.Elapsed.TotalSeconds;
                var deltaTime = now - previousTime;
                previousTime = now;

                //var offset = material.DiffuseMap.Offset;
                //offset.x += deltaTime * 0.75f;
                //offset.y -= deltaTime * 0.5f;
                //material.DiffuseMap.Offset = offset;

                //var r = frankie.Rotation;
                //r.y += deltaTime;
                //frankie.Rotation = r;

                //r = axisHelper.Rotation;
                //r.x = Mathf.Pi / 4;
                //r.y -= deltaTime;
                //axisHelper.Rotation = r;

                var r = cube.Rotation;
                r.y          -= deltaTime / 2;
                r.x           = Mathf.Sin(now / 4);
                cube.Rotation = r;

                r              = cube2.Rotation;
                r.y           -= deltaTime / 2;
                cube2.Rotation = r;
                //boundingBoxHelper.Update();

                renderer.RenderFrame(scene, camera);
            }
        }
Example #16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="control"></param>
        public override void Load(Control control)
        {
            base.Load(control);

            camera            = new PerspectiveCamera(27, control.Width / (float)control.Height, 1, 3500);
            camera.Position.Z = 2750;

            scene     = new Scene();
            scene.Fog = new Fog((Color)colorConvertor.ConvertFromString("#050505"), 2000, 3500);

            scene.Add(new AmbientLight((Color)colorConvertor.ConvertFromString("#444444")));

            var light1 = new DirectionalLight(Color.White, 0.5f);

            light1.Position = new Vector3(1, 1, 1);
            scene.Add(light1);

            var light2 = new DirectionalLight(Color.White, 1.5f);

            light2.Position = new Vector3(0, -1, 0);
            scene.Add(light2);

            //

            const int Triangles = 5000;

            var geometry = new BufferGeometry();

            var positions = new float[Triangles * 3 * 3];
            var normals   = new float[Triangles * 3 * 3];
            var colors    = new float[Triangles * 3 * 3];

            var color = new Color();

            const int n = 800; const int n2 = n / 2; // triangles spread in the cube
            const int d = 120; const int d2 = d / 2; // individual triangle size

            for (var i = 0; i < positions.Length; i += 9)
            {
                // positions

                var r = 0.5f;

                var x = getRandom() * n - n2;
                var y = getRandom() * n - n2;
                var z = getRandom() * n - n2;

                var ax = x + getRandom() * d - d2;
                var ay = y + getRandom() * d - d2;
                var az = z + getRandom() * d - d2;

                var bx = x + getRandom() * d - d2;
                var by = y + getRandom() * d - d2;
                var bz = z + getRandom() * d - d2;

                var cx = x + getRandom() * d - d2;
                var cy = y + getRandom() * d - d2;
                var cz = z + getRandom() * d - d2;

                positions[i]     = ax;
                positions[i + 1] = ay;
                positions[i + 2] = az;

                positions[i + 3] = bx;
                positions[i + 4] = by;
                positions[i + 5] = bz;

                positions[i + 6] = cx;
                positions[i + 7] = cy;
                positions[i + 8] = cz;

                // flat face normals

                var pA = new Vector3(ax, ay, az);
                var pB = new Vector3(bx, by, bz);
                var pC = new Vector3(cx, cy, cz);

                var cb = pC - pB;
                var ab = pA - pB;
                cb.Cross(ab).Normalize();

                var nx = cb.X;
                var ny = cb.Y;
                var nz = cb.Z;

                normals[i]     = nx;
                normals[i + 1] = ny;
                normals[i + 2] = nz;

                normals[i + 3] = nx;
                normals[i + 4] = ny;
                normals[i + 5] = nz;

                normals[i + 6] = nx;
                normals[i + 7] = ny;
                normals[i + 8] = nz;

                // colors

                var vx = (x / n) + 0.5;
                var vy = (y / n) + 0.5;
                var vz = (z / n) + 0.5;

                color = Color.FromArgb(255, (int)(vx * 255), (int)(vy * 255), (int)(vz * 255));

                colors[i]     = color.R / 255.0f;
                colors[i + 1] = color.G / 255.0f;
                colors[i + 2] = color.B / 255.0f;

                colors[i + 3] = color.R / 255.0f;
                colors[i + 4] = color.G / 255.0f;
                colors[i + 5] = color.B / 255.0f;

                colors[i + 6] = color.R / 255.0f;
                colors[i + 7] = color.G / 255.0f;
                colors[i + 8] = color.B / 255.0f;
            }

            geometry.AddAttribute("position", new BufferAttribute <float>(positions, 3));
            geometry.AddAttribute("normal", new BufferAttribute <float>(normals, 3));
            geometry.AddAttribute("color", new BufferAttribute <float>(colors, 3));

            geometry.ComputeBoundingSphere();

            var material = new MeshPhongMaterial()
            {
                Color     = (Color)colorConvertor.ConvertFromString("#aaaaaa"),
                Ambient   = (Color)colorConvertor.ConvertFromString("#aaaaaa"),
                Specular  = Color.White,
                Shininess = 250,
                Side      = Three.DoubleSide, VertexColors = Three.VertexColors,
            };

            mesh = new Mesh(geometry, material);
            scene.Add(mesh);

            //

            projector = new Projector();
            raycaster = new Raycaster();

            mouse = new Vector2();

            var geometry2 = new BufferGeometry();

            geometry2.AddAttribute("position", new BufferAttribute <float>(new float[4 * 3], 3));

            var material2 = new LineBasicMaterial()
            {
                Color = Color.White, Linewidth = 2, Transparent = true
            };

            line = new Line(geometry2, material2);
            scene.Add(line);

            renderer.SetClearColor(scene.Fog.Color);
        }
Example #17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="control"></param>
        public override void Load(Control control)
        {
            base.Load(control);

            camera = new PerspectiveCamera(27, control.Width / (float)control.Height, 1, 3500);
            this.camera.Position.Z = 2750;

            scene     = new Scene();
            scene.Fog = new Fog((Color)colorConvertor.ConvertFromString("#050505"), 2000, 3500);

            scene.Add(new AmbientLight((Color)colorConvertor.ConvertFromString("#444444")));

            var light1 = new DirectionalLight(Color.White, 0.5f);

            light1.Position = new Vector3(1, 1, 1);
            scene.Add(light1);

            var light2 = new DirectionalLight(Color.White, 1.5f);

            light2.Position = new Vector3(0, -1, 0);
            scene.Add(light2);

            const int triangles = 160000;

            var geometry = new BufferGeometry();

            var indices = new uint[triangles * 3];

            for (uint i = 0; i < indices.Length; i++)
            {
                indices[i] = i;
            }

            var positions = new float[triangles * 3 * 3];
            var normals   = new float[triangles * 3 * 3];
            var colors    = new float[triangles * 3 * 3];

            var color = Color.White;

            const int n = 800; var n2 = n / 2;  // triangles spread in the cube
            const int d = 12; var d2 = d / 2;   // individual triangle size

            for (var i = 0; i < positions.Length; i += 9)
            {
                // positions

                var x = Mat.Random() * n - n2;
                var y = Mat.Random() * n - n2;
                var z = Mat.Random() * n - n2;

                var ax = x + Mat.Random() * d - d2;
                var ay = y + Mat.Random() * d - d2;
                var az = z + Mat.Random() * d - d2;

                var bx = x + Mat.Random() * d - d2;
                var by = y + Mat.Random() * d - d2;
                var bz = z + Mat.Random() * d - d2;

                var cx = x + Mat.Random() * d - d2;
                var cy = y + Mat.Random() * d - d2;
                var cz = z + Mat.Random() * d - d2;

                positions[i + 0] = ax;
                positions[i + 1] = ay;
                positions[i + 2] = az;

                positions[i + 3] = bx;
                positions[i + 4] = by;
                positions[i + 5] = bz;

                positions[i + 6] = cx;
                positions[i + 7] = cy;
                positions[i + 8] = cz;

                // flat face normals

                var pA = new Vector3(ax, ay, az);
                var pB = new Vector3(bx, by, bz);
                var pC = new Vector3(cx, cy, cz);

                var cb = new Vector3().SubtractVectors(pC, pB);
                var ab = new Vector3().SubtractVectors(pA, pB);
                cb.Cross(ab).Normalize();

                var nx = cb.X;
                var ny = cb.Y;
                var nz = cb.Z;

                normals[i + 0] = nx;
                normals[i + 1] = ny;
                normals[i + 2] = nz;

                normals[i + 3] = nx;
                normals[i + 4] = ny;
                normals[i + 5] = nz;

                normals[i + 6] = nx;
                normals[i + 7] = ny;
                normals[i + 8] = nz;

                // colors

                var vx = (x / n) + 0.5;
                var vy = (y / n) + 0.5;
                var vz = (z / n) + 0.5;

                color = Color.FromArgb(255, (int)(vx * 255), (int)(vy * 255), (int)(vz * 255));

                colors[i + 0] = color.R / 255.0f;
                colors[i + 1] = color.G / 255.0f;
                colors[i + 2] = color.B / 255.0f;

                colors[i + 3] = color.R / 255.0f;
                colors[i + 4] = color.G / 255.0f;
                colors[i + 5] = color.B / 255.0f;

                colors[i + 6] = color.R / 255.0f;
                colors[i + 7] = color.G / 255.0f;
                colors[i + 8] = color.B / 255.0f;
            }


            geometry.AddAttribute("index", new BufferAttribute <uint>(indices, 1));
            geometry.AddAttribute("position", new BufferAttribute <float>(positions, 3));
            geometry.AddAttribute("normal", new BufferAttribute <float>(normals, 3));
            geometry.AddAttribute("color", new BufferAttribute <float>(colors, 3));

            geometry.ComputeBoundingSphere();

            var material = new MeshPhongMaterial
            {
                Color        = (Color)colorConvertor.ConvertFromString("#aaaaaa"),
                Ambient      = (Color)colorConvertor.ConvertFromString("#aaaaaa"),
                Specular     = (Color)colorConvertor.ConvertFromString("#ffffff"),
                Shininess    = 250,
                Side         = Three.DoubleSide,
                VertexColors = Three.VertexColors,
            };

            this.mesh = new Mesh(geometry, material);
            scene.Add(mesh);

            renderer.SetClearColor(scene.Fog.Color);

            renderer.gammaInput  = true;
            renderer.gammaOutput = true;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="control"></param>
        public override void Load(Control control)
        {
            base.Load(control);

            camera            = new PerspectiveCamera(35, control.Width / (float)control.Height, 1, 25000);
            camera.Position.Z = 1500;

            scene1 = new Scene();
            scene2 = new Scene();

            scene1.Fog = new Fog((Color)colorConvertor.ConvertFromString("#f2f7ff"), 1, 25000);
            scene2.Fog = scene1.Fog;

            scene1.Add(new AmbientLight((Color)colorConvertor.ConvertFromString("#eef0ff")));
            scene2.Add(new AmbientLight((Color)colorConvertor.ConvertFromString("#eef0ff")));

            var light1 = new DirectionalLight(Color.White, 2);

            light1.Position = new Vector3(1, 1, 1);
            scene1.Add(light1);

            var light2 = new DirectionalLight(Color.White, 2);

            light2.Position = new Vector3(1, 1, 1);
            scene2.Add(light2);

            // GROUND

            var maxAnisotropy = renderer.MaxAnisotropy;

            var texture1  = ImageUtils.LoadTexture("examples/textures/crate.gif");
            var material1 = new MeshPhongMaterial()
            {
                Color = Color.White, Map = texture1
            };

            texture1.Anisotropy = maxAnisotropy;
            texture1.WrapS      = texture1.WrapT = ThreeCs.Three.RepeatWrapping;
            texture1.Repeat     = new Vector2(512, 512);

            var texture2  = ImageUtils.LoadTexture("examples/textures/crate.gif");
            var material2 = new MeshPhongMaterial()
            {
                Color = Color.White, Map = texture2
            };

            texture2.Anisotropy = 1;
            texture2.WrapS      = texture2.WrapT = ThreeCs.Three.RepeatWrapping;
            texture2.Repeat     = new Vector2(512, 512);

            //if (maxAnisotropy > 0)
            //{
            //    document.getElementById("val_left").innerHTML = texture1.anisotropy;
            //    document.getElementById("val_right").innerHTML = texture2.anisotropy;
            //}
            //else
            //{
            //    document.getElementById("val_left").innerHTML = "not supported";
            //    document.getElementById("val_right").innerHTML = "not supported";
            //}

            //

            var geometry = new PlaneGeometry(100, 100);

            var mesh1 = new Mesh(geometry, material1);

            mesh1.Rotation.X = (float)-Math.PI / 2;
            mesh1.Scale      = new Vector3(1000, 1000, 1000);

            var mesh2 = new Mesh(geometry, material2);

            mesh2.Rotation.X = (float)-Math.PI / 2;
            mesh2.Scale      = new Vector3(1000, 1000, 1000);

            scene1.Add(mesh1);
            scene2.Add(mesh2);

            // RENDERER

            renderer.SetClearColor(scene1.Fog.Color, 1);
            renderer.AutoClear = false;
        }
Example #19
0
        private static void ParseMaterials(string fileName)
        {
            MeshBasicMaterial currentMaterial = null;
            var path  = Path.GetDirectoryName(fileName);
            var lines = File.ReadAllLines(fileName);

            foreach (var line in lines)
            {
                if (line.Length == 0 || line.StartsWith("#"))
                {
                    continue;
                }

                var parts = line.Trim().Split(' ');

                if (parts[0] == "newmtl")
                {
                    currentMaterial = new MeshPhongMaterial(renderer)
                    {
                        Name = parts[1].Trim()
                    };
                    materials.Add(currentMaterial.Name, currentMaterial);
                }
                else if (parts[0] == "Ns")
                {
                    var phong = currentMaterial as MeshPhongMaterial;
                    if (phong != null)
                    {
                        var hardness = Convert.ToSingle(parts[1]);
                        phong.Hardness = hardness;
                    }
                }
                else if (parts[0] == "Ni")
                {
                }
                else if (parts[0] == "Ka")
                {
                    var lambert = currentMaterial as MeshLambertMaterial;
                    if (lambert != null)
                    {
                        var r = Convert.ToSingle(parts[1]);
                        var g = Convert.ToSingle(parts[2]);
                        var b = Convert.ToSingle(parts[3]);
                        lambert.Ambient = new Color(r, g, b);
                    }
                }
                else if (parts[0] == "Kd")
                {
                    var r = Convert.ToSingle(parts[1]);
                    var g = Convert.ToSingle(parts[2]);
                    var b = Convert.ToSingle(parts[3]);
                    currentMaterial.Diffuse = new Color(r, g, b);
                }
                else if (parts[0] == "map_Kd")
                {
                    var fullPath = Path.Combine(path, parts[1].Trim());
                    currentMaterial.DiffuseMap = new Texture(fullPath);
                }
                else if (parts[0] == "Ks")
                {
                    var phong = currentMaterial as MeshPhongMaterial;
                    if (phong != null)
                    {
                        var r = Convert.ToSingle(parts[1]);
                        var g = Convert.ToSingle(parts[2]);
                        var b = Convert.ToSingle(parts[3]);
                        phong.Specular = new Color(r, g, b);
                    }
                }
                else if (parts[0] == "d")
                {
                }
                else if (parts[0] == "illum")
                {
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }