gamedev

Unity 3D – Server Side Highscores – JS Programming

Unity 3D – Server Side Highscores – JS Programming

How to manage server side highscores with Unity3D, PHP and MySQL.

Create a Data Base

Blue Host users:

CPanel> Database Tools> MySQL databases>

– Create a Database:
name -> game_scores
rules-> Collation

– Create a User

– Assign User to Database

CREATE TABLE ‘scores’

Blue Host users:

CPanel> phpMyAdmin> Enter with username and password

LEFT COLUMN> you will see your new empty Database.

A Database is a data structure with tables, every table has rows and columns.

TOP LABELS> SQL, here we can write the next SQL Query:


CREATE TABLE 'scores' (
   'id' INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
   'name' VARCHAR(15) NOT NULL DEFAULT 'anonymous',
   'score' INT(10) UNSIGNED NOT NULL DEFAULT '0'
)
TYPE=MyISAM;

If you get an SQL Syntax error, replace TYPE=MyISAM; by ENGINE=MyISAM; as TYPE is deprecated.

If you get others syntax errors you can: phpMyAdmin> LEFT COLUMN> Crea Tabella +> Create the table

We will create:

id
– it is our index
– it is an integer number from 0 to 9999999999
– it is UNSIGNED, it means that can’t be negative (a SIGNED integer can hold both positive and negative numbers)
– it can’t be NULL (NULL value is different from zero, it means no value)
– it have an autoincrement of 1 unit
– it is our primary key, the primary key of a relational table uniquely identifies each record in the table.

name
– it is the nickname of our player
– it can be a string of 15 characters
– it can’t be NULL
– the default value will be ‘anonymous’, if the user does not type a nickname

score
– it is the score value
– it is an integer number from 0 to 9999999999
– it is UNSIGNED, it means that can’t be negative (a SIGNED integer can hold both positive and negative numbers)
– it can’t be NULL (NULL value is different from zero, it means no value)
– the default value is zero

addscore.php

Create into your server the script:


<?php 
        // Create connection
        // Statement: mysqli_connect(host,username,password,dbname)
        // NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost 
        $db = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); 
        mysql_select_db('my_dbname') or die('Could not select database');
  
        // Strings must be escaped to prevent SQL injection attack. 
        $name = mysql_real_escape_string($_GET['name'], $db); 
        $score = mysql_real_escape_string($_GET['score'], $db);  
  
        // Send variables for the MySQL database class. 
        $query = "INSERT INTO scores VALUES (NULL, '$name', '$score');"; 
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());     
?>

For italian people: come funziona?

1. Apro una connessione con il database fornendo i dati di accesso – ‘mysql_host’, ‘mysql_user’, ‘mysql_password’) –

2. Se la connessione è andata a buon fine non viene restituito alcun messaggio, se fallisce – die(‘Could not select database’) – viene restituito un messaggio di errore.

3. Con il comando – $_GET – ricevo i dati POST inviati dal gioco che vengono immagazzinati nelle variabili $name, $score

4. Per evitare l’hacking del database – mysql_real_escape_string() – non aggiunge le sequenze di escape a % ed a _.
In questo modo i malintenzionati non potranno operare ‘SQL injection’ inserendo codice maligno all’interno di una query SQ.

5. Inserisce nella tabella ‘scores’ i valori ‘$name’ e ‘$score’

6. Se fallisce restituisce il messaggio ‘Query failed’.

display.php

This script will take the top 5 scores from the MySQL Database, Unity3D will read the render of this script and put it into a GUIText.


<?php
 
    // To change the total number of row you need only change LIMIT 5
    // Example: to see the top ten set LIMIT 10
 
    // Connect to database
    // NOTICE: se lo script è installato nello stesso server del Data Base, mysql_host->localhost 
    $database = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error());
    mysql_select_db('my_dbname') or die('Could not select database');
  
    // Send a query, order the records in descending 
    $query = "SELECT * FROM scores ORDER BY score DESC LIMIT 5";
    // Store the result inside a variable or If fails send an error message
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  
    // How many rows there are inside the result
    $num_results = mysql_num_rows($result);  
   
    // Loop 5 times, remember some line above that LIMIT 5
    for($i = 0; $i < $num_results; $i++)
    {
         $row = mysql_fetch_array($result);
         // Render the result in text, questo viene letto poi da Unity3D come testo e renderizzato su un GUIText
         echo $row['name'] . "\t" . $row['score'] . "\n";
    }
?>

Now, we are going to populate the database to test our .php scripts.
1. phpMyAdmin> SQL label> write


/* Insert New Records in a table */ 
INSERT INTO scores (name, score)
VALUES ('Maria', '1260');

2. BOTTOM RIGHT> press ‘Esegui’ button
3. Insert 10 records and try display.php

XML – crossdomain.xml

Unity3D can send a WWW Request to a server only if it have a cross domain policy.
The crossdomain.xml file is a cross-domain policy file, it grants at the game the permission to talk to server, even if the game is not hosted inside it.

Upload the next file to the root of your web server, for BlueHost user put it into www. folder

crossdomain.xml:


<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*"/>
</cross-domain-policy>

Blue Host users: inside crossdomain.xml if you have secure=”true” only requests from HTTPS will be allowed, secure=”false” mean requests from both HTTP and HTTPS are allowed.


<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" secure="false"/>
</cross-domain-policy>

For italian people: come funziona?
Il server deve essere settato per poter dialogare con applicazioni esterne, intese come applicazioni non installate direttamente all’interno del server stesso.
Il caso più ecclatante è quello di un gioco installato su Smartphone che deve inviare dei dati ad un server.
Anche un webgame che funziona su Unity Player è di fatto installato in locale sul PC del giocatore, nel momento in cui invia i punteggi al server viene visto dal servizio di hosting come un’applicazione esterna.

Potremo vedere alcuni esempio di sintassi per capire meglio.
Nel file sotto garantiamo il dialogo dalle richieste provenienti solo da domini specifici.


<?xml version="1.0"?>
<cross-domain-policy>
    <allow-access-from domain="*.mycompany.com" />
    <allow-access-from domain="*.speedtest.net" />
</cross-domain-policy>

Unity – Display Scores – JS

Open Unity 3D and create:
– Main Camera -> name it ‘Main Camera’
– GUI Text -> name it ‘Scores-Text’
– Empty Object -> name it ‘GameController’
– JS Script -> name it ‘HSController.js’, attach it to ‘GameController’


#pragma strict

var scoreText : GUIText; // Assign into Inspector the GUI Text you have created

// CHANGE THIS VALUE WITH YOUR ADDRESS
var urlDisplay = "http://www.lucedigitale.com/testgames/display.php";
  
function Start() {
    getScores(); // get and display the scores into GUIText scoreText
}
// Get Score START ###################################################################  
// Get the scores from the MySQL DB to display in a GUIText.
function getScores() {
    // First a loading message
    scoreText.text = "Loading Scores";
    // Start a download of the given URL
    var wwwDisplay : WWW = new WWW (urlDisplay);
    // Wait for download to complete
    yield wwwDisplay;
    // if it can't load the URL
    if(wwwDisplay.error) {
        // Write in the console: There was an error getting the high score: Could not resolve host: xxx; No data record of requested type
        print("There was an error getting the high score: " + wwwDisplay.error);
        // Display an error message
        scoreText.text = "No Data Record";
    } else {
        // This is a GUIText that will display the scores in game
        scoreText.text = wwwDisplay.text; 
    }
}
// Get Score END ###################################################################  

The result will be:

Maria 2999
Giovanna 1787
Arianna 87
Antonio 24
Erica 12

Unity – Display and Write Scores – JS

Ok, now we are going to add functions to write scores


#pragma strict

var scoreText : GUIText; // Assign into Inspector the GUI Text you have created

// CHANGE THIS VALUE WITH YOUR ADDRESS TO GET BEST SCORES
var urlDisplay = "http://www.lucedigitale.com/testgames/display.php";
// CHANGE THIS VALUE WITH YOUR ADDRESS TO WRITE SCORES
var urlAddScores = "http://www.lucedigitale.com/testgames/addscore.php";

// CHANGE THIS VALUES WITH YOUR OWN
// this vars are private because we won't put data from Inspector, public will cause a refresh error 
private var playerName : String = "Andrea"; // name of the player
private var playerScore : int = 56565656; // the players' score
  
function Start() {
    getScores(); // get and display the scores into GUIText scoreText
}
// Get Score START ###################################################################  
// Get the scores from the MySQL DB to display in a GUIText.
function getScores() {
    // First a loading message
    scoreText.text = "Loading Scores";
    // Start a download of the given URL
    var wwwDisplay : WWW = new WWW (urlDisplay);
    // Wait for download to complete
    yield wwwDisplay;
    // if it can't load the URL
    if(wwwDisplay.error) {
        // Write in the console: There was an error getting the high score: Could not resolve host: xxx; No data record of requested type
        print("There was an error getting the high score: " + wwwDisplay.error);
        // Display an error message
        scoreText.text = "No Data Record";
    } else {
        // This is a GUIText that will display the scores in game
        scoreText.text = wwwDisplay.text; 
    }
} // END getScores()
// Get Score END ###################################################################  

// Write Score START ###############################################################

// Button to send scores START
function OnGUI () {
    if (GUI.Button (Rect (10,10,350,100), "Send player name and Scores:" + playerName + " " + playerScore)) {
        // send datas to function to POST scores
        postScore(playerName, playerScore);
    }
} // END OnGUI
// Button to send scores END 

function postScore(name, score) {
     // Debug code to verify datas on console
     Debug.Log("Name " + name + " Score " + score);
     
    //This connects to a server side php script that will add the name and score to a MySQL DB.
    // Supply it with a string representing the players name and the players score.
    // Once it will generate an error message of Implicit Downcast, no problem it will work well
    var postData : String = urlAddScores + "?name=" + WWW.EscapeURL(name) + "&score=" + score;
    // Debug Code
    Debug.Log(postData);
    // It sends: http://www.lucedigitale.com/testgames/addscore.php?name=Andrea&score=123321
    // notare che il nome delle variabili in POST devono essere uguali a quelli in GET di addscore.php
    var wwwPostScore : WWW = new WWW (postData);
    // Wait until the post is done
    yield wwwPostScore; 
    // If it can't give an errore message
    if(wwwPostScore.error) {
        // Debug code
        print("There was an error posting the high score: " + wwwPostScore.error);
        // Error message for the player
        scoreText.text = "I can't record data";
    }
}// END postScore()

// Write Score END ################################################################# 

addscore.php – update existing player

Now I can improve my .php script, updating existing players and closing db connection.


<?php 
        // Create connection
        // Statement: mysqli_connect(host,username,password,dbname)
        // NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost 
        $db = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); 
        mysql_select_db('mysql_dbname') or die('Could not select database');
  
        // Strings must be escaped to prevent SQL injection attack. 
        $name = mysql_real_escape_string($_GET['name'], $db); 
        $score = mysql_real_escape_string($_GET['score'], $db);  
  
        // Check if the name already exists
		$checkname = mysql_query("SELECT 1 FROM scores WHERE name='$name' LIMIT 1");
		// if exists
		if (mysql_fetch_row($checkname)) {
		        // Update the existing name with new score
			// AGGIORNA db_name SETTA il valore di score dove name è uguale a quello ottenuto con GET
			$queryupdate = "UPDATE scores SET score=$score WHERE name='$name'"; 	
			$resultupdate = mysql_query($queryupdate) or die('Query failed: ' . mysql_error()); 		
		// if not exists
		} else {
		        // Insert a new name and a new score 
			$query = "INSERT INTO scores VALUES (NULL, '$name', '$score');"; 
			$result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
		}
		
		// Close the connection with the database
		mysqli_close($db); 
        echo "<br>Great! Connection Closed!"; 
?>

addscore.php – update existing player – write only best score

I will write in the database only if new score is better than the older one.


<?php 
        // Create connection
        // Statement: mysqli_connect(host,username,password,dbname)
        // NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost 
        $db = mysql_connect('mysql_host', 'mysql_user', 'mysql_password') or die('Could not connect: ' . mysql_error()); 
        mysql_select_db('mysql_dbname') or die('Could not select database');
  
        // Strings must be escaped to prevent SQL injection attack. 
        $name = mysql_real_escape_string($_GET['name'], $db); 
        $score = mysql_real_escape_string($_GET['score'], $db);  
		
		// Check if the name already exists 
		$checkname = mysql_query("SELECT 1 FROM scores WHERE name='$name' LIMIT 1");
		
		// ------------------------------------------------
		// if exists --------------------------------------
		//-------------------------------------------------
		if (mysql_fetch_row($checkname)) {	
		echo "Vecchio giocatore";// Debug Code
		echo "<br>";// Debug Code
	        echo "Punteggio arrivato dal gioco: ".$score;// Debug Code
		echo "<br>";// Debug Code
                // Check score from database
		$checkscore = mysql_query("SELECT score FROM scores WHERE name='$name'");
		$checkscorerow = mysql_fetch_array($checkscore);
		echo "Punteggio ottenuto dal database: ".$checkscorerow['score'];// Debug Code
		
				// if the new score are better than old one
				if ($score > $checkscorerow['score']){
					echo "<br>Great! New personal record";
					
					// Update the existing name with new score
					// AGGIORNA db_name SETTA il valore di score dove name è uguale a quello ottenuto con GET
					$queryupdate = "UPDATE scores SET score=$score WHERE name='$name'";     
					$resultupdate = mysql_query($queryupdate) or die('Query failed: ' . mysql_error());
					
					mysqli_close($db); // Close the connection with the database
					echo "<br>Connection Closed!"; 
					break; // stop the execution of the script
				} else {
					echo "<br>Bad! Are you tired?";
					mysqli_close($db); // Close the connection with the database
					echo "<br>Connection Closed!"; 
					break; // stop the execution of the script
				}	
         
		// ------------------------------------------------
		// if not exists ----------------------------------
		// ------------------------------------------------
		} else {
			    echo "Nuovo giocatore";// Debug Code
		        // Insert a new name and a new score 
				$query = "INSERT INTO scores VALUES (NULL, '$name', '$score');"; 
				$result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
		}		
		
		mysqli_close($db); // Close the connection with the database
        echo "<br>Connection Closed!"; 
?>

Ok, now we can add an MD5 encryption.

MD5 Encryption – HSController.js


#pragma strict

var scoreText : GUIText; // Assign into Inspector the GUI Text you have created

// CHANGE THIS VALUE WITH YOUR ADDRESS TO GET BEST SCORES
var urlDisplay = "http://www.lucedigitale.com/testgames/display.php";
// CHANGE THIS VALUE WITH YOUR ADDRESS TO WRITE SCORES
var urlAddScores = "http://www.lucedigitale.com/testgames/addscore.php";

// CHANGE THIS VALUES WITH YOUR OWN
// this vars are private because we won't put data from Inspector, public will cause a refresh error 
private var playerName : String = "AndreaTonin"; // name of the player
private var playerScore : int = 800; // the players' score
private var secretKey : String = "MyKey"; // the secret key to improve encryption  

private var secretSum : String; // the sum of playerName+playerScore+secretKey
private var secretSumMd5 : String; // the MD5 sum of: playerName+playerScore+secretKey
  
function Start() {
    getScores(); // get and display the scores into GUIText scoreText
    
    // calculate MD5 Key
    secretSum = playerName + playerScore + secretKey;
    secretSumMd5 = Md5Sum(secretSum);
}
// Get Score START ###################################################################  
// Get the scores from the MySQL DB to display in a GUIText.
function getScores() {
    // First a loading message
    scoreText.text = "Loading Scores";
    // Start a download of the given URL
    var wwwDisplay : WWW = new WWW (urlDisplay);
    // Wait for download to complete
    yield wwwDisplay;
    // if it can't load the URL
    if(wwwDisplay.error) {
        // Write in the console: There was an error getting the high score: Could not resolve host: xxx; No data record of requested type
        print("There was an error getting the high score: " + wwwDisplay.error);
        // Display an error message
        scoreText.text = "No Data Record";
    } else {
        // This is a GUIText that will display the scores in game
        scoreText.text = wwwDisplay.text; 
    }
} // END getScores()
// Get Score END ###################################################################  

// Write Score START ###############################################################
// Button to send scores START
function OnGUI () {
    if (GUI.Button (Rect (10,10,650,100), "SEND name score md5:" + playerName + " " + playerScore + " " + secretSumMd5)) {
        // send datas to function to POST scores
        postScore(playerName, playerScore,secretSumMd5);
    }
} // END OnGUI
// Button to send scores END 

function postScore(name, score, md5key) {
     // Debug code to verify datas on console
     Debug.Log("Name " + name + " Score " + score + " MD5Key " + md5key);
     
    //This connects to a server side php script that will add the name and score to a MySQL DB.
    // Supply it with a string representing the players name and the players score.
    // Once it will generate an error message of Implicit Downcast, no problem it will work well
    var postData : String = urlAddScores + "?name=" + WWW.EscapeURL(name) + "&score=" + score + "&md5key=" + md5key;
    // Debug Code
    Debug.Log(postData);
    // It sends: http://www.lucedigitale.com/testgames/addscore.php?name=Andrea&score=123321
    // notare che il nome delle variabili in POST devono essere uguali a quelli in GET di addscore.php
    var wwwPostScore : WWW = new WWW (postData);
    // Wait until the post is done
    yield wwwPostScore; 
    // If it can't give an errore message
    if(wwwPostScore.error) {
        // Debug code
        print("There was an error posting the high score: " + wwwPostScore.error);
        // Error message for the player
        scoreText.text = "I can't record data";
    }
}// END postScore()
// Write Score END ################################################################# 

// MD5 Encrytpt START ############################################################### 
// Server side note: the output is the same of the PHP function - md5($myString) -
static function Md5Sum(strToEncrypt: String)
{
	var encoding = System.Text.UTF8Encoding();
	var bytes = encoding.GetBytes(strToEncrypt);
 
	// encrypt bytes
	var md5 = System.Security.Cryptography.MD5CryptoServiceProvider();
	var hashBytes:byte[] = md5.ComputeHash(bytes);
 
	// Convert the encrypted bytes back to a string (base 16)
	var hashString = "";
 
	for (var i = 0; i < hashBytes.Length; i++)
	{
		hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, "0"[0]);
	}
 
	return hashString.PadLeft(32, "0"[0]);
}// End Md5Sum
// MD5 Encrypt END #####################################################################

MD5 Encryption – addscore.php


<?php 
        // Create connection
        // Statement: mysqli_connect(host,username,password,dbname)
        // NOTICE: se lo script è installato nello stesso server del Data Base, host->localhost 
        $db = mysql_connect('localhost', 'lucedigi_testgam', '3dmaster1508%A') or die('Could not connect: ' . mysql_error()); 
        mysql_select_db('lucedigi_testgames') or die('Could not select database');
  
        // GET post data from Unity3D
		// Strings must be escaped to prevent SQL injection attack. 
        $name = mysql_real_escape_string($_GET['name'], $db); 
        $score = mysql_real_escape_string($_GET['score'], $db);  
		$md5key = mysql_real_escape_string($_GET['md5key'], $db); 
		
		$secretKey = "MyKey"; // It is the same Unity3D posts
		$secretSum = $name.$score.$secretKey;
 
        // ------------------------------------------------
		// if MD5 Key is right ----------------------------
		//-------------------------------------------------
	if (md5($secretSum) === $md5key) {
        echo "Yes! It is the right MD5, let's write on the database";
		
		// Check if the name already exists 
		$checkname = mysql_query("SELECT 1 FROM scores WHERE name='$name' LIMIT 1");
		
		// ------------------------------------------------
		// if exists --------------------------------------
		//-------------------------------------------------
		if (mysql_fetch_row($checkname)) {	
		echo "<br>Old Player";// Debug Code
		echo "<br>";// Debug Code
	    echo "Punteggio arrivato dal gioco: ".$score;// Debug Code
		echo "<br>";// Debug Code
		$checkscore = mysql_query("SELECT score FROM scores WHERE name='$name'");
		$checkscorerow = mysql_fetch_array($checkscore);
		echo "Punteggio ottenuto dal database: ".$checkscorerow['score'];// Debug Code
		
				// if the new score are better than old one
				if ($score > $checkscorerow['score']){
					echo "<br>Great! New personal record";
					
					// Update the existing name with new score
					// AGGIORNA db_name SETTA il valore di score dove name è uguale a quello ottenuto con GET
					$queryupdate = "UPDATE scores SET score=$score WHERE name='$name'";     
					$resultupdate = mysql_query($queryupdate) or die('Query failed: ' . mysql_error());
					
					mysqli_close($db); // Close the connection with the database
					echo "<br>Connection Closed!"; 
					break; // stop the execution of the script
				} else {
					echo "<br>Bad! Are you tired?";
					mysqli_close($db); // Close the connection with the database
					echo "<br>Connection Closed!"; 
					break; // stop the execution of the script
				}	
         
		// ------------------------------------------------
		// if not exists ----------------------------------
		// ------------------------------------------------
		} else {
			    echo "Nuovo giocatore";// Debug Code
		        // Insert a new name and a new score 
				$query = "INSERT INTO scores VALUES (NULL, '$name', '$score');"; 
				$result = mysql_query($query) or die('Query failed: ' . mysql_error()); 
		}		
		
		mysqli_close($db); // Close the connection with the database
        echo "<br>Connection Closed!"; 
		
	} else {
		// Debug Code
		echo "Bad MD5! Who are you?";
		echo "<br>Data received: ".$name." ".$score." ".$md5key;
		echo "<br>MD5 calcolato dal server: ".md5($secretSum);
		break;
	}
?>

My official website: http://www.lucedigitale.com
Original article: http://wiki.unity3d.com/index.php?title=Server_Side_Highscores

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Server Side Highscores – JS Programming

Faceshift Studio – Basic Tutorial with XBOX360 Kinect

Faceshift Studio – Basic Tutorial with XBOX360 Kinect

Faceshift Studio (http://www.faceshift.com/) is a face-mocap software.
It analyzes the face motions of an actor, and describes them as a mixture of basic expressions, plus head orientation and gaze. This description is then used to animate virtual characters for use in movie or game production.

Faceshift Studio is really fantastic and it is used by some of most creative companies in the world as Industrial Light and Magic, Dreamworks, Disney, Sega. It works well with Autodesk 3DS Max and Unity3D.

To use Faceshift Studio you need a RGBD cameras (Red-Green-Blue-Deph).

Officially it runs with:
– PrimeSense Carmine 1.09
– Asus Xtion Live Pro
– Microsoft Kinect (it is the Kinetic sensor for PC)

Unofficially it might work with XBOX360 Kinetic sensor.

Ok let’s start!

XBOX360 sensor installation

1. Plug your sensor

2. From Microsoft website download and install KinectSDK-v1.8-Setup.exe

3. and install KinectDeveloperToolkit-v1.8.0-Setup.exe

XBOX360 Sensor testing

1. Go to Programs/Microsoft SDKs/Kinect/Developer Toolkit 1.8/bin/

– KinectExplorer-D2D.exe to move the bar

– FaceTrackingBasics-WPF.exe

NOTICE: You have to stay 80 cm from the sensor for face tracking

Faceshift capture and export

1. Start Faceshift> select MS Kinect Sensor Plugin

2. LEFT COLUMN> Tracking

3. You have to stay 80 cm from the sensor for face tracking

4. REC button to record

5. REC button to stop

6. Tracking> select the clip (it turns to blue)> Refine (this operation will remove glitches)

7. File> Save> to save the project in .fsp format

8. File> Export> to export in mocap data or animated .fbx file

Import into 3DS MAX

File> Import> .fbx

First expand the timeline, we will find:

– Mesh
– Head Bones
– Morph Targets

By |3D Graphic, 3DS Max, Video Games Development|Commenti disabilitati su Faceshift Studio – Basic Tutorial with XBOX360 Kinect

Allegorithmic Substance Painter – Basic Tutorial

Allegorithmic Substance Painter – Basic Tutorial

Substance Painter is a mesh painter software, with this you can paint your substances over a 3D Mesh.
Let’s take it from the top!

Paint a flat color

1. Start Substance Painter

2. MAIN TOP MENU> File> New> Mesh ‘Select…’, load your .fbx file

3. LEFT COLUMN> Document Settings> setup Size, click the minus ‘-‘ on the right of the channels slots to remove unwanted channels. For now with need only ‘Base Color’.

4. LEFT COLUMN> BOTTOM LABELS ‘Viewer Settings’ and ‘Post Effects’, to setup the 3D View.

5. MAIN TOP TOOLBAR> activate ‘Symmetry’ if you need.

6. BOTTOM> Material label> select ‘plastic matte’.

7. BOTTOM> Brush label> select ‘default brush’

8. RIGHT COLUMN> Tool-Paint, as you can see we will use the brush on the left thumbnail to paint the material on the right
UNDER click on the base color slot to change color. Click over the green box under the brush thumbnail to setup the brush size, flow and spacing.

9. Go to in 3D View, paint over the mesh and remember my friend:
– LMB paint
– ALT + LMB camera rotate
– ALT + MMB camera translate
– ALT + RMB or Mouse Wheel camera zoom
– MAIUSC + LMB rotate panorama
– CTRL + RMB changes brush flow
– CTRL + LMB canges brush size

10. RIGHT COLUMN> Layers> click plus ‘+’ to add a new layer, this window works like Photoshop.
We will create Layer 1 and Layer 2 for ALL CHANNELS, if you want paint only one channel you have to disable the unwanted layers inside the channel, for example select Layers> Specular> LMB over Layer 1 – Ldge (ON THE RIGHT)> Disable

11. LEFT COLUMN> BOTTOM LABELS ‘Viewer Settings’> Mode> Solo view to see only the strokes in current layer.

Paint height

1. LEFT COLUMN> Channels + Height

2. LEFT COLUMN> Viewer Settings> Height force = 1

3. RIGHT COLUMN> Layers> Height> All Layer> rename it Height> Disable all aother layers

4. RIGHT COLUMN> Tool – Paint> Material – advanced> enable only ‘height’ button> Height roolout> slide to 0.6

5. Paint

use the same way to paint only specular, metallic, reflection etc…

Fill Layer

You you use a procedural substance as bricks or other seamless materials you need a fill layer

1. RIGHT COLUMN> Layers> Add a Fill Layer

2. BOTTOM> Material> selec ‘bricks 001’

Mask – paint

1. RMB over the layer> Add WHITE mask (in Photoshop sarebbe aggiungi maschera> mostra tutto) and paint the mask.

Mask – decal using geometry

1. RIGHT COLUMN> Layers> Select a BLACK mask layer (it turn to red)

2. MAIN TOP TOOLBAR> Geometry Decal

3. LEFT COLUMN> Viewer Settings> Wireframe opacity

4. RIGHT COLUMN> Tool – Geometry Decal> Color to white, select faces mode then select into 3D View faces

Mask – add effect (id_selector)

Apply the mask using material id

1. RIGHT COLUMN> Layers> Select a BLACK mask layer (it turn to red)

2. RIGHT COLUMN> Layers> TOOLBAR> Add effect> Substance effect

3. BOTTOM> LEFT> Shelf> Effects> DRAN ANG DROP ‘id_selector’ over RIGHT COLUMN> Layers> Filters parameters> ‘Effect No effect selected’ slot

You can use id material from 3DS Max of using a ID map.

Mask – add effect (MG Edge Dirt)

1. RIGHT COLUMN> Layers> Select a BLACK mask layer (it turn to red)

2. RIGHT COLUMN> Layers> TOOLBAR> Add effect> Substance effect

3. BOTTOM> LEFT> Shelf> Effects> DRAN ANG DROP ‘MG Edge Dirt’ over RIGHT COLUMN> Layers>

– Filters parameters> ‘Effect No effect selected’ slot

– Filters parameters> Image inputs> apply a ‘curvature map’

NOTE:
With curvature maps you can easily create masks or textures to work on chipped or worn edges.
Substance Designer creates ‘curvature maps’, based on the normal map.

Mask – Stencils

1. RIGHT COLUMN> Layers> create a layer

2. BOTTOM> Shelf> Stencils> DRAG AND DROP a stencil over RIGHT COLUMN> Tool – Paint> Stencil – basic

3. BOTTOM> Shelf> Material> select a material and paint your mesh

NOTE: LEFT COLUMN> Viewer Settings> check ‘Hide stencil when painting’

Paint Stencils

1. RIGHT COLUMN> Layers> select a layer

2. BOTTOM> Shelf> Stencils> DRAG AND DROP a stencil over RIGHT COLUMN> Tool – Paint> ‘Base color’ slot

3. Use a big brush to paint your stencil!

Paint with particles

1. BOTTOM> Particles label> liquid stream

Save and Export

MAIN TOP MENU> File> Save As…, select a folder and ‘Save’
It will create a complex folder structure with all the project files.

MAIN TOP MENU> File> Export all channels> select a folder and ‘Export’
It will create a bitmap texture for every channel.

Increase Texture resolution on the fly

Substance Painter is resolution indipendent!

RIGHT COLUMN> Document Settings> Size> Change the size, Substance Painter will use the stack informations to recompute the new texture!

By |3D Graphic|Commenti disabilitati su Allegorithmic Substance Painter – Basic Tutorial

Allegorithmic – Substance Designer – Basic Tutorial

Allegorithmic – Substance Designer – Basic Tutorial

With Substance Designer you can create great materials for your favourite 3D application.
A ‘Substance’ is a file with the .SBSAR extension that can be used into 3DS Max, Maya, Unity3D, Unreal Engine.
Let’s go on!

Where can you use Substance Designer in your pipeline?
You need create a model and the UV Map using for example 3DSMax or MudBox, then import the mesh inside Substance Designer and here create the maps, see the workflow below:

substance-designer-workflow

Initial setup

1. Open Substance Designer

2. 3D View> Materials> Default> Shader> physically_based
– LMB to rotate View
– SHIFT+LMB to move light

3. Tools> Switch Engine…> Direct3D or SSE2

Create new substance

1. MAIN TOP MENU> File> New Substance…

2. ‘New Substance’ Window> ‘Outputs’ label> BOTTOM RIGHT ‘Add Output’ to add a channel, expard the roolout of the channel to select the type of maps that can be:
– diffuse
– baseColor
– opacity
– emissive
– ambient
– ambientOcclusion
– mask
– normal
– bump
– height
– dispacement
– specular
– specularLevel
– specularColor
– glossiness
– roughness
– anisotropyLevel
– anisotropyAngle
– transmissive
– reflection
– refraction
– environment
– IOR
– scattering
– any
– metallic
– panorama

Not bad!

3. DRAG AND DROP a bitmap file over New_Graph Window> Import resource(s)

4. DRAG AND DROP from BOTTOM LEFT Library> Material Filters> 1-Click> ‘Bitmap to Material Light’ over New_Graph Window

5. Connect the Input/output values as you can see in the graph below

substance-designer-material-diagram-1

6. Click over Bitmap to Material Light Box, setup parameters on the right box

Add simple dirt

See the diagram below

substance-designer-diagram-2

Notice the blocks:

Library>Generators> Dirt ———————————> | Filters Add/Sub Foreground |
Library>Material Filters>Bitmap to Materia Light -> | Filters Add/Sub Background | -> Diffuse Output
Library>Noises> Dirt Gradient —————————> | Filters Add/Sub Mask |

Make tileable

See the diagram below

substance-designer-diagram-3

Notice the blocks:

Bitmap -> | Library> Filters> Tiling> Make It Tile Photo | -> etc…

Save the project

1. TOP LEFT> Explorer> RMB over ‘New_Graph’> Rename> Bricks

2. TOP LEFT> Explorer> RMB over ‘Unsaved Package’> Save As…> Bricks.sbs
This is our project file. In order to edit a substance, you must then have its .sbs file.
In your hard drive you will find Bricks.sbs + Bricks.resources Folder with bitmap files.

Export the substance

1. TOP LEFT> Explorer> RMB over Bricks.sbs> Publish…> Bricks.sbsar
SBSAR files are heavily optimized for performance and contain only the data required to render the Substances they contain, thus SBSAR nodes cannot be opened for editing. This file is created to be imported in your 3D Software (3DSMax, Unity3D etc…).
In your hard drive you will find ONLY Bricks.sbsar, all resouces and code are embedded in this file.

Import the substance in Unity3D

1. Open Unity3D> Project> RMB> Import New Asset> Bricks.sbsar Done!!!

2. Setup in Inspector if you want.

Import a mesh of 3DS MAX

1. Inside 3DS Max create:
– a mesh
– assign UV
– assign a material from using Material Editor

Export in fbx format.

1. Open Substance Designer> File> New Substance

2. DRAG AND DROP the .fbx file over Substance Designer> Unsaved Package
It will be created a Folder Resources> Your Mesh> RMB> Open

3. 3D View> Geometry> Display UVs in 2D View
3D View> Materials> Your Mesh Material

4. DRAG AND DROP the bitmap texture of the Mesh over New_Graph Window and create your substance

5. RMB over New_Graph> View outputs in 3D View

By |3D Graphic|Commenti disabilitati su Allegorithmic – Substance Designer – Basic Tutorial

Unity3D – Access GameObjects and Components – CSharp

Unity3D – Access GameObjects and Components – CSharp

In Unity3D GameObjects are all entities in Unity scenes.

MAIN TOP MENU> GameObject>
– Particle System
– Camera
– GUI Text
– GUI Texture
– 3D Text
– Directional Light
– Point Light
– Spot Light
– Area Light
– Meshes
– etc…

To manage GameObjects in code you have to use the ‘gameObject’ base class.

In Unity3D Components are all ‘modifiers’ you can attach to a Gameobject.

MAIN TOP MENU> Components>
– Mesh
– Effects
– Physics
– Physics 2D
– Navigation
– etc…

Accessing this GameObject

1. Create a Cube, go to Inspector and add the script ‘DestroyBasic.cs’ as component.

DestroyBasic.cs:


using UnityEngine;
using System.Collections;

public class DestroyBasic : MonoBehaviour
{
    void Update ()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            Destroy(gameObject); // notice the keyword gameObject
        }
    }
}

Play, press Space to destroy it.

Notice: ‘Destroy(gameObject)’, you can use simple the keyword ‘gameObject’ and Unity3D will destroy the game object attached to the script.

Accessing this Component

1. Create a Cube, go to Inspector and add the scripts:
– ‘OtherScript.cs’ as component
– ‘example.cs’ as component

example.cs:


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Update() {
        // first declare the variable otherScript of type OtherScrip and store the component 'GetComponent'
        // type variablename = get <scriptname>
        OtherScript otherScript = GetComponent<OtherScript>();
        otherScript.DoSomething(); // manipulate the component
    }
}

Accessing Other GameObjects – Inspector assignable references

If you set a varible as ‘public’ you can assign it in Inspector, even while you are playing!


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    // scope type variablename
    public Transform target; // Setup in Inspector
    void Update() {
        target.Translate(0, 1, 0);
    }
}

Below you can drag a Gameobject that contains the ‘OtherScript’ on the target slot in the inspector.


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    // scope type variablename
    public OtherScript target; // Drag and Drop in Inspector a Gameobject with the script 'OtherScript' 
    void Update() {
        target.foo = 2;              // using OtherScript variables
        target.DoSomething("Hello"); // usibg OtherScript functions
    }
}

Located through the object hierarchy

1. Create the hierarchy:

Forearm (parent)
– Hand (child)

Attach to Forearm example.cs:


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Example() {
        transform.Find("Hand").Translate(0, 1, 0); // find childs with name
    }
}

Once you have found the transform in the hierarchy, you can use GetComponent to get to other scripts.


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Example() {
        transform.Find("Hand").GetComponent<OtherScript>().foo = 2;
        transform.Find("Hand").GetComponent<OtherScript>().DoSomething("Hello");
        transform.Find("Hand").rigidbody.AddForce(0, 10, 0);
    }
}

You can loop over all children:


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Example() {
        foreach (Transform child in transform) {
            child.Translate(0, 10, 0);
        }
    }
}

Located by name


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Start() {
        // type varname = find the gameobject SomeGuy
        GameObject go = GameObject.Find("SomeGuy"); // find in Project window the object with name 'SomeGuy'
        // manipulate SomeGuy 
        go.transform.Translate(0, 1, 0);

        GameObject player = GameObject.FindWithTag("Player");
        player.transform.Translate(0, 1, 0);
    }
}

Located by tag

To assign a tag go to Inspector> Tag> Add Tag


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Start() {
        // type varname = find a gameobject with tag Player
        GameObject player = GameObject.FindWithTag("Player");
        // get from Player the component OtherScript
        player.GetComponent<OtherScript>().DoSomething();
    }
}

Passed as parameters


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    // se il trigger entra in contatto con altri oggetti
    void OnTriggerStay(Collider other) {
        // se gli altri oggetti hanno un rigidbody
        if (other.rigidbody)
            // aggiungi una forza
            other.rigidbody.AddForce(0, 2, 0);
        
    }
}


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    // se il trigger entra in contatto con altri oggetti
    void OnTriggerStay(Collider other) {
        // se gli altri componenti hanno lo script OtherScript
        if (other.GetComponent<OtherScript>())
            // ottieni OtherScript e fai qualcosa
            other.GetComponent<OtherScript>().DoSomething();
        
    }
}

All scripts of one Type

Find any object of one class or script name.

FindObjectOfType


// Search object of class GUITexture

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Start() {
        GUITexture texture = FindObjectOfType(typeof(GUITexture));
        if (texture)
            Debug.Log("GUITexture object found: " + texture.name);
        else
            Debug.Log("No GUITexture object could be found");
    }
}  

FindObjectsOfType

Il precedente era singolare ‘FindObjectOfType’, questo è plurale ‘FindObjectsOfType’


// When clicking on the object, it will disable all springs on all 
// hinges in the scene

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void OnMouseDown() {
        HingeJoint[] hinges = FindObjectsOfType(typeof(HingeJoint)) as HingeJoint[];
        foreach (HingeJoint hinge in hinges) {
            hinge.useSpring = false;
        }
    }
}

My website: http://www.lucedigitale.com

Ref: http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Access GameObjects and Components – CSharp