Skip to content

Instantly share code, notes, and snippets.

@jesterKing
Last active September 20, 2019 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesterKing/597646d207f7ceeb827b752f070a5305 to your computer and use it in GitHub Desktop.
Save jesterKing/597646d207f7ceeb827b752f070a5305 to your computer and use it in GitHub Desktop.
Cycles as Rhino Render plug-in code.
/**
Copyright 2014-2019 Robert McNeel and Associates
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**/
using System;
using System.Drawing;
using Rhino;
using Rhino.Commands;
using Rhino.Display;
using Rhino.PlugIns;
using Rhino.Render;
using RhinoCyclesCore.RenderEngines;
namespace CyclesForRhino.CyclesForRhino
{
public class Plugin : RenderPlugIn
{
public override PlugInLoadTime LoadTime => PlugInLoadTime.AtStartup;
protected override bool SupportsFeature(RenderFeature feature)
{
if (feature == RenderFeature.CustomDecalProperties)
return false;
return true;
}
protected override PreviewRenderTypes PreviewRenderType()
{
return PreviewRenderTypes.Progressive;
}
/// <summary>
/// Implement the render entry point.
///
/// Rhino data is prepared for further conversion in RenderEngine.
/// </summary>
/// <param name="doc">Rhino document for which the render command was given</param>
/// <param name="mode">mode</param>
/// <param name="fastPreview">True for fast preview.</param>
/// <returns></returns>
protected override Result Render(RhinoDoc doc, RunMode mode, bool fastPreview)
{
var rc = RenderPipeline.RenderReturnCode.InternalError;
using (var rsv = new RenderSourceView(doc))
{
ModalRenderEngine engine = null;
engine = new ModalRenderEngine(doc, Id, rsv.GetViewInfo());
var renderSize = RenderPipeline.RenderSize(doc, true);
// Set up pipe and get the RenderWindow from it
var pipe = new RhinoCycles.RenderPipeline(doc, mode, this, engine);
engine.RenderWindow = pipe.GetRenderWindow(true, true);
// add all channels we currently write out
bool rwrc = engine.RenderWindow.AddChannel(Rhino.Render.RenderWindow.StandardChannels.DistanceFromCamera);
System.Diagnostics.Debug.Assert(rwrc);
rwrc = engine.RenderWindow.AddChannel(Rhino.Render.RenderWindow.StandardChannels.NormalX);
System.Diagnostics.Debug.Assert(rwrc);
rwrc = engine.RenderWindow.AddChannel(Rhino.Render.RenderWindow.StandardChannels.NormalY);
System.Diagnostics.Debug.Assert(rwrc);
rwrc = engine.RenderWindow.AddChannel(Rhino.Render.RenderWindow.StandardChannels.NormalZ);
System.Diagnostics.Debug.Assert(rwrc);
engine.RenderDimension = renderSize;
engine.Database.RenderDimension = renderSize;
engine.CreateWorld(); // has to be done on main thread, so lets do this just before starting render session
/* since we're an asynchronous renderer plugin we start the render process
* here, but, apart from data conversion and pumping, we fall right through
* without a complete render result.
*/
rc = pipe.Render();
}
if (Rhino.Render.RenderPipeline.RenderReturnCode.Ok != rc)
{
RhinoApp.WriteLine("Render setup failed:" + rc.ToString());
return Result.Failure;
}
return Result.Success;
}
/// <summary>
/// Handler for rendering preview thumbnails.
///
/// The CreatePreviewEventArgs parameter contains a simple
/// scene description to be rendered. It contains a set of meshes
/// and lights. Meshes have RenderMaterials attached to them.
/// </summary>
/// <param name="scene">The scene description to render, along with the requested quality setting</param>
protected override void CreatePreview(CreatePreviewEventArgs scene)
{
// don't initialise the scene, we do things differently here
scene.SkipInitialisation();
// let the system show OpenGL thumbnail for the very
// first version
if (scene.Quality == PreviewSceneQuality.Low)
{
scene.PreviewImage = null;
return;
}
var engine = new PreviewRenderEngine(scene, Id);
engine.RenderDimension = scene.PreviewImageSize;
/* create a window-less, non-document controlled render window */
engine.RenderWindow = Rhino.Render.RenderWindow.Create(scene.PreviewImageSize);
engine.Database.RenderDimension = engine.RenderDimension;
engine.CreateWorld();
/* render the preview scene */
PreviewRenderEngine.Renderer(engine);
/* set final preview bitmap, or null if cancelled */
scene.PreviewImage = engine.RenderWindow.GetBitmap();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment