Code Vonc

Matière Dégradé 3D

v 1.0

R13+ OSX Win

TéléchargerDownload
Faire un donMake a donation
Comment installer ?How to install ?
CommentairesComments

Matière dégradé 3D, UVW ou XYZ.3D gradient shader, UVW or XYZ



En mode XYZ, le dégradé partira du centre du monde (point global (0 ; 0 ; 0)). N'oubliez pas d'augmenter l'échelle si besoin est.In XYZ mode, the gradient starts from the world center. Don't forget to change the scale if needed.







Codes sourcesSources code

res/c4d_symbols.h

enum
{
	IDS_VONCMATDEG,
// End of symbol definition
  _DUMMY_ELEMENT_
};

res/description/matdeg3d.h

#ifndef _matdeg3d_H_
#define _matdeg3d_H_

enum
{
	VONCMATDEG_DEGU	= 1000,
	VONCMATDEG_DEGV	= 1001,
	VONCMATDEG_DEGW	= 1002,
	VONCMATDEG_ECH = 1003,
	VONCMATDEG_MODE = 1004,
		VONCMATDEG_MODE_UV = 0,
		VONCMATDEG_MODE_GLO = 1,
	VONCMATDEG_REP = 1005,
	VONCMATDEG_TURB = 1006,
	VONCMATDEG_TURB_OCTAVES = 1007,
	VONCMATDEG_TURB_ECH = 1008,
	VONCMATDEG_TURB_FREQ = 1009,
	VONCMATDEG_TURB_ABSOLU = 1010,
	VONCMATDEG_DEC = 1011,
	VONCMATDEG_MEL = 1012,
		VONCMATDEG_MEL_PROD = 0,
		VONCMATDEG_MEL_ADD = 1,
		VONCMATDEG_MEL_MOY = 2
};

#endif

res/description/matdeg3d.res

CONTAINER matdeg3d
{
	INCLUDE Mpreview;
	INCLUDE Xbase;

	GROUP ID_SHADERPROPERTIES
	{
		GRADIENT VONCMATDEG_DEGU { }
		GRADIENT VONCMATDEG_DEGV { }
		GRADIENT VONCMATDEG_DEGW { }
		LONG VONCMATDEG_MEL
		{
			CYCLE
			{
				VONCMATDEG_MEL_PROD;
				VONCMATDEG_MEL_ADD;
				VONCMATDEG_MEL_MOY;
			}
			FIT_H;
		}
		SEPARATOR { LINE; }
		VECTOR VONCMATDEG_ECH { STEP 0.01; }
		VECTOR VONCMATDEG_DEC { STEP 0.01; }
		BOOL VONCMATDEG_REP { }
		LONG VONCMATDEG_MODE
		{
			CYCLE
			{
				VONCMATDEG_MODE_UV;
				VONCMATDEG_MODE_GLO;
			}
			FIT_H;
		}
		SEPARATOR { LINE; }
		REAL VONCMATDEG_TURB			 { UNIT PERCENT; MIN 0.0; MAX 100.0; }
		REAL VONCMATDEG_TURB_OCTAVES	 { MIN 0.0; MAX 10.0; STEP 0.1; }
		REAL VONCMATDEG_TURB_ECH		 { UNIT PERCENT; MIN 0.0; MAX 1000.0; }
		REAL VONCMATDEG_TURB_FREQ		 { MIN 0.0; MAX 1000.0; STEP 0.1; }
		BOOL VONCMATDEG_TURB_ABSOLU		 { }
	}
}

res/strings_fr/c4d_strings.str

STRINGTABLE
{
  IDS_VONCMATDEG "D\u00E9grad\u00E9 3D";
}

res/strings_fr/description/matdeg3d.str

STRINGTABLE matdeg3d
{
	VONCMATDEG_DEGU			"D\u00E9grad\u00E9 U";
	VONCMATDEG_DEGV			"D\u00E9grad\u00E9 V";
	VONCMATDEG_DEGW			"D\u00E9grad\u00E9 W";
	VONCMATDEG_ECH			"\u00C9chelle";
	VONCMATDEG_DEC			"D\u00E9calage";
	VONCMATDEG_MODE			"Espace";
	VONCMATDEG_MODE_UV		"UVW";
	VONCMATDEG_MODE_GLO		"XYZ";
	VONCMATDEG_REP			"R\u00E9p\u00E9titions";
	VONCMATDEG_TURB			"Turbulences";
	VONCMATDEG_TURB_OCTAVES "Octaves";
	VONCMATDEG_TURB_ECH		"\u00C9chelle";
	VONCMATDEG_TURB_FREQ	"Fr\u00E9quence";
	VONCMATDEG_TURB_ABSOLU	"Absolue";
	VONCMATDEG_MEL			"M\u00E9lange";
	VONCMATDEG_MEL_PROD		"Produit";
	VONCMATDEG_MEL_ADD		"Addition";
	VONCMATDEG_MEL_MOY		"Moyenne";
}

source/main.cpp

#include "c4d.h"

Bool InscrireMatDeg(void);

Bool PluginStart(void)
{
	if (!InscrireMatDeg()) return FALSE;
	return TRUE;
}

void PluginEnd(void)
{
}

Bool PluginMessage(LONG id, void *data)
{
	switch (id)
	{
		case C4DPL_INIT_SYS:
			if (!resource.Init())
                return FALSE; // Ne pas lancer le module sans ressource
			return TRUE;

	}

	return FALSE;
}

source/matdeg3d.cpp

// Vonc - Matière Dégradé 3D - 1.0

#include "c4d.h"
#include "c4d_symbols.h"
#include "matdeg3d.h"

struct GradientData
{
	Real			turbulence, octaves, echelle, freq;
	Bool			absolu;

	Gradient*	degrade[3];
};

class SDKGradientClass : public ShaderData
{
	private:
		static const char U = 0;
		static const char V = 1;
		static const char W = 2;
		GradientData ddeg;
		Vector		 echelle;
		Vector		 decalage;
		Bool		 repete;
		LONG		 mode;
		LONG		 melange;

	public:
		void InitDeg(BaseContainer *data, LONG id);

	public:
		virtual Bool Init(GeListNode *node);
		virtual	Vector Output(BaseShader *sh, ChannelData *cd);
		virtual	INITRENDERRESULT InitRender(BaseShader *sh, const InitRenderStruct &irs);
		virtual	void FreeRender(BaseShader *sh);

		static NodeData *Alloc(void) { return gNew SDKGradientClass; }
};

void SDKGradientClass::InitDeg(BaseContainer *donnees, LONG id)
{
    AutoAlloc<Gradient> degrade;
	if (!degrade) return;
    
	GradientKnot k1,k2;
	k1.col = Vector(1.0, 1.0, 1.0);
	k1.pos = 0.0;
    
	k2.col = Vector(0.0, 0.0, 0.0);
	k2.pos = 1.0;
    
	degrade->InsertKnot(k1);
	degrade->InsertKnot(k2);
    
	donnees->SetData(id, GeData(CUSTOMDATATYPE_GRADIENT, degrade));
}

Bool SDKGradientClass::Init(GeListNode *noeud)
{
	BaseContainer *donnees = ((BaseShader*)noeud)->GetDataInstance();

	this->InitDeg(donnees, VONCMATDEG_DEGU);
    this->InitDeg(donnees, VONCMATDEG_DEGV);
    this->InitDeg(donnees, VONCMATDEG_DEGW);

	donnees->SetVector(VONCMATDEG_ECH, Vector(1.0));
	donnees->SetVector(VONCMATDEG_DEC, Vector());
	donnees->SetLong(VONCMATDEG_MODE, 0);
	donnees->SetLong(VONCMATDEG_MEL, 0);
	donnees->SetBool(VONCMATDEG_REP, TRUE);
	
	donnees->SetReal(VONCMATDEG_TURB, 0.0);
	donnees->SetReal(VONCMATDEG_TURB_OCTAVES, 5.0);
	donnees->SetReal(VONCMATDEG_TURB_ECH, 1.0);
	donnees->SetReal(VONCMATDEG_TURB_FREQ, 1.0);
	donnees->SetBool(VONCMATDEG_TURB_ABSOLU, FALSE);

	return TRUE;
}

INITRENDERRESULT SDKGradientClass::InitRender(BaseShader *sh, const InitRenderStruct &irs)
{
	BaseContainer *donnees = sh->GetDataInstance();
	
	ddeg.turbulence = donnees->GetReal(VONCMATDEG_TURB);
	ddeg.octaves    = donnees->GetReal(VONCMATDEG_TURB_OCTAVES);
	ddeg.echelle      = donnees->GetReal(VONCMATDEG_TURB_ECH);
	ddeg.freq       = donnees->GetReal(VONCMATDEG_TURB_FREQ);
	ddeg.absolu   = donnees->GetBool(VONCMATDEG_TURB_ABSOLU);
	
	echelle = donnees->GetVector(VONCMATDEG_ECH);
	decalage = donnees->GetVector(VONCMATDEG_DEC);
	repete = donnees->GetBool(VONCMATDEG_REP);
	mode = donnees->GetLong(VONCMATDEG_MODE);
	melange = donnees->GetLong(VONCMATDEG_MEL);

	ddeg.degrade[U]	 = (Gradient*)donnees->GetCustomDataType(VONCMATDEG_DEGU, CUSTOMDATATYPE_GRADIENT); 
	if (!ddeg.degrade[U] || !ddeg.degrade[U]->InitRender(irs)) return INITRENDERRESULT_OUTOFMEMORY;

	ddeg.degrade[V]	 = (Gradient*)donnees->GetCustomDataType(VONCMATDEG_DEGV, CUSTOMDATATYPE_GRADIENT); 
	if (!ddeg.degrade[V] || !ddeg.degrade[V]->InitRender(irs)) return INITRENDERRESULT_OUTOFMEMORY;

	ddeg.degrade[W]	 = (Gradient*)donnees->GetCustomDataType(VONCMATDEG_DEGW, CUSTOMDATATYPE_GRADIENT); 
	if (!ddeg.degrade[W] || !ddeg.degrade[W]->InitRender(irs)) return INITRENDERRESULT_OUTOFMEMORY;

	return INITRENDERRESULT_OK;
}

void SDKGradientClass::FreeRender(BaseShader *sh)
{
	if (ddeg.degrade[U]) ddeg.degrade[U]->FreeRender();
	ddeg.degrade[U] = NULL;

	if (ddeg.degrade[V]) ddeg.degrade[V]->FreeRender();
	ddeg.degrade[V] = NULL;

	if (ddeg.degrade[W]) ddeg.degrade[W]->FreeRender();
	ddeg.degrade[W] = NULL;
}

Vector SDKGradientClass::Output(BaseShader *sh, ChannelData *sd)
{
	Vector secours = Vector(1.0, 0.0, 0.0);
	Vector p = sd->p;

	if (mode == 1 && sd->vd) p = sd->vd->p;

	
	if (echelle.x == 0.0) echelle.x = 1.0;
	if (echelle.y == 0.0) echelle.y = 1.0;
	if (echelle.z == 0.0) echelle.z = 1.0;

	p.x /= echelle.x;
	p.y /= echelle.y;
	p.z /= echelle.z;

	p += decalage;

	
	if (ddeg.turbulence>0.0)
	{
		Vector	res;
		Real		scl = 5.0*ddeg.echelle, tt = sd->t*ddeg.freq*0.3;

		res = Vector(Turbulence(p*scl,tt,ddeg.octaves,TRUE), Turbulence((p+Vector(0.34,13.0,2.43))*scl,tt,ddeg.octaves,TRUE), Turbulence((p+Vector(2.32,1.88,4.51))*scl,tt,ddeg.octaves,TRUE));

		if (ddeg.absolu)
		{
			p.x  = Mix(p.x,res.x,ddeg.turbulence);
			p.y  = Mix(p.y,res.y,ddeg.turbulence);
			p.z  = Mix(p.z,res.z,ddeg.turbulence);
		}
		else
		{
			p.x += (res.x-0.5)*ddeg.turbulence;
			p.y += (res.y-0.5)*ddeg.turbulence;
			p.z += (res.z-0.5)*ddeg.turbulence;
		}
	}

	if (repete) {
		p.x = FMod(p.x, 1);
		p.y = FMod(p.y, 1);
		p.z = FMod(p.z, 1);
		p.x = fabs(p.x);
		p.y = fabs(p.y);
		p.z = fabs(p.z);
	}

	Vector u = ddeg.degrade[U]->CalcGradientPixel(p.x);
	Vector v = ddeg.degrade[V]->CalcGradientPixel(p.y);
	Vector w = ddeg.degrade[W]->CalcGradientPixel(p.z);

	if (melange == 0) {
		p = u ^ v ^ w;
	}
	else if (melange == 1) {
		p = u + v + w;
	}
	else if (melange == 2) {
		p = (u + v + w) ^ Vector(0.333333);
	}
	
	return p;
}

Bool InscrireMatDeg(void)
{
	return RegisterShaderPlugin(1029051, GeLoadString(IDS_VONCMATDEG), 0, SDKGradientClass::Alloc, "matdeg3d", 0);
}