About Monkey 2 › Forums › Monkey 2 Development › Canvas image to Pbr texture
This topic contains 3 replies, has 2 voices, and was last updated by
Diffrenzy
1 year, 3 months ago.
Viewing 4 posts - 1 through 4 (of 4 total)
-
AuthorPosts
-
December 27, 2017 at 5:04 am #12456
I’ve created a canvas that draws to an image.
I now want that Image to be pasted onto a 3D rectangle.I’ve got it almost working, but it seems the 3D rectangle only displays the top left of the image…
Basically what I want is to draw a bunch of stuff to a canvas, then get that into a 3D space as a texture.
Here’s an example:
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Class MyWindow Extends WindowField _scene:SceneField _camera:CameraField _light:LightField _rectMaterial:PbrMaterialField _rect:ModelField _rectCanvas:CanvasField _rectImage:ImageMethod New( title:String="Simple mojo3d app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )_scene=Scene.GetCurrent()_camera=New Camera_camera.Near=.1_camera.Far=100_camera.Move( 0,0,-1.15 )_light=New Light_light.RotateX( 90 )_rectMaterial=New PbrMaterial( Color.White )_rect=New Model( Mesh.CreateRect( New Rectf( -1, -1, 1, 1 ) ), _rectMaterial )EndMethod OnRender( canvas:Canvas ) OverrideRequestRender()RenderTexture()_scene.Update()_scene.Render( canvas,_camera )EndMethod RenderTexture()If Not _rectImage Then_rectImage=New Image( 512, 512 )_rectCanvas=New Canvas( _rectImage )'Set our material to use our image texture_rectMaterial.ColorTexture=_rectImage.TextureEndif'This should be orange with white text on'But since I'm drawing something in the top left corner -'I'm just getting that top left pixel on the entire rectangle_rectCanvas.Clear( Color.Orange )_rectCanvas.DrawText( "Hello World", 8, 8 )_rectCanvas.DrawPoint( 0, 0 ) 'White in the top left_rectCanvas.Flush()EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndDecember 27, 2017 at 11:05 am #12458Sounds like your texture coordinates are off. Hard to tell without a little snippet of code
December 27, 2017 at 1:20 pm #12460Sorry! I made one last night, but forgot to post it heh.
First post updated.December 27, 2017 at 2:29 pm #12461Here’s one that works:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Class MyWindow Extends WindowField _scene:SceneField _camera:CameraField _light:LightField _rect:ModelField _rectCanvas:CanvasField _rectImage:ImageMethod New( title:String="Simple mojo3d app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )_scene=Scene.GetCurrent()_camera=New Camera_camera.Near=.1_camera.Far=100_camera.Move( 0,0,-2 )_light=New Light_rect=MakeModel()EndMethod OnRender( canvas:Canvas ) OverrideRequestRender()_rect.RotateY( 1 )RenderTexture()_scene.Update()_scene.Render( canvas,_camera )EndMethod RenderTexture()If Not _rectCanvas Then_rectCanvas=New Canvas( _rectImage )Endif'This should be orange with white text on'But since I'm drawing something in the top left corner -'I'm just getting that top left pixel on the entire rectangle_rectCanvas.Clear( Color.Blue )_rectCanvas.Color = Color.White_rectCanvas.DrawText( "Hello World", Rnd(8,12), 8 )_rectCanvas.Color = Color.Orange_rectCanvas.DrawRect( 50, 50 , 200 ,90) 'White in the top left_rectCanvas.Flush()EndMethod MakeModel:Model()Local vertices:=New Vertex3f[4]vertices[0].position=New Vec3f( -1, 1,0 )vertices[1].position=New Vec3f( 1, 1,0 )vertices[2].position=New Vec3f( 1,-1,0 )vertices[3].position=New Vec3f( -1,-1,0 )vertices[0].texCoord0 = New Vec2f(0,0)vertices[1].texCoord0 = New Vec2f(1,0)vertices[2].texCoord0 = New Vec2f(1,1)vertices[3].texCoord0 = New Vec2f(0,1)Local indices:=New UInt[6]indices[0]=0indices[1]=1indices[2]=2indices[3]=0indices[4]=2indices[5]=3Local mesh:=New Mesh( )mesh.AddVertices(vertices)mesh.AddTriangles(indices)mesh.UpdateNormals()' mesh.AddMaterials(1)'create model for the meshLocal model:=New Modelmodel.Mesh=meshmodel.Materials = model.Materials.Resize(mesh.NumMaterials)Local sm:= New PbrMaterial()_rectImage=New Image( 256, 256 )_rectCanvas=New Canvas( _rectImage )sm.ColorTexture = _rectImage.Texturesm.ColorTexture.Flags = TextureFlags.FilterMipmapsm.CullMode=CullMode.Nonemodel.Materials[mesh.NumMaterials - 1] = smReturn modelEndEndFunction Main()New AppInstanceNew MyWindowApp.Run()End -
AuthorPosts
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic.